2015-05-20 23:23:48 -04:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-05-06 00:53:33 +09:00
|
|
|
"context"
|
2015-05-20 23:23:48 -04:00
|
|
|
"errors"
|
2015-06-06 14:15:43 -04:00
|
|
|
"fmt"
|
2015-05-20 23:23:48 -04:00
|
|
|
"net/url"
|
2015-06-23 07:23:39 -04:00
|
|
|
|
2020-10-23 23:34:06 -07:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
|
2020-09-30 01:44:42 +09:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/requests"
|
2015-05-20 23:23:48 -04:00
|
|
|
)
|
|
|
|
|
2020-09-26 14:06:52 -07:00
|
|
|
var (
|
|
|
|
// ErrNotImplemented is returned when a provider did not override a default
|
|
|
|
// implementation method that doesn't have sensible defaults
|
|
|
|
ErrNotImplemented = errors.New("not implemented")
|
|
|
|
|
2020-10-23 19:35:15 -07:00
|
|
|
// ErrMissingCode is returned when a Redeem method is called with an empty
|
|
|
|
// code
|
|
|
|
ErrMissingCode = errors.New("missing code")
|
|
|
|
|
2020-12-01 12:01:42 -08:00
|
|
|
// ErrMissingIDToken is returned when an oidc.Token does not contain the
|
|
|
|
// extra `id_token` field for an IDToken.
|
|
|
|
ErrMissingIDToken = errors.New("missing id_token")
|
|
|
|
|
|
|
|
// ErrMissingOIDCVerifier is returned when a provider didn't set `Verifier`
|
|
|
|
// but an attempt to call `Verifier.Verify` was about to be made.
|
|
|
|
ErrMissingOIDCVerifier = errors.New("oidc verifier is not configured")
|
|
|
|
|
2020-09-26 14:06:52 -07:00
|
|
|
_ Provider = (*ProviderData)(nil)
|
|
|
|
)
|
2020-05-06 00:53:33 +09:00
|
|
|
|
2021-04-21 02:33:27 -07:00
|
|
|
// GetLoginURL with typical oauth parameters
|
2022-03-13 06:08:33 -04:00
|
|
|
// codeChallenge and codeChallengeMethod are the PKCE challenge and method to append to the URL params.
|
|
|
|
// they will be empty strings if no code challenge should be presented
|
2022-02-16 16:18:51 +00:00
|
|
|
func (p *ProviderData) GetLoginURL(redirectURI, state, _ string, extraParams url.Values) string {
|
2021-04-21 02:33:27 -07:00
|
|
|
loginURL := makeLoginURL(p, redirectURI, state, extraParams)
|
|
|
|
return loginURL.String()
|
|
|
|
}
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// Redeem provides a default implementation of the OAuth2 token redemption process
|
2022-03-13 06:08:33 -04:00
|
|
|
// The codeVerifier is set if a code_verifier parameter should be sent for PKCE
|
|
|
|
func (p *ProviderData) Redeem(ctx context.Context, redirectURL, code, codeVerifier string) (*sessions.SessionState, error) {
|
2015-05-20 23:23:48 -04:00
|
|
|
if code == "" {
|
2020-10-23 19:35:15 -07:00
|
|
|
return nil, ErrMissingCode
|
2015-05-20 23:23:48 -04:00
|
|
|
}
|
2020-02-15 14:44:39 +01:00
|
|
|
clientSecret, err := p.GetClientSecret()
|
|
|
|
if err != nil {
|
2020-10-23 19:35:15 -07:00
|
|
|
return nil, err
|
2020-02-15 14:44:39 +01:00
|
|
|
}
|
2015-05-20 23:23:48 -04:00
|
|
|
|
|
|
|
params := url.Values{}
|
2015-11-09 00:47:44 +01:00
|
|
|
params.Add("redirect_uri", redirectURL)
|
2015-05-20 23:23:48 -04:00
|
|
|
params.Add("client_id", p.ClientID)
|
2020-02-15 14:44:39 +01:00
|
|
|
params.Add("client_secret", clientSecret)
|
2015-05-20 23:23:48 -04:00
|
|
|
params.Add("code", code)
|
|
|
|
params.Add("grant_type", "authorization_code")
|
2022-03-13 06:08:33 -04:00
|
|
|
if codeVerifier != "" {
|
|
|
|
params.Add("code_verifier", codeVerifier)
|
|
|
|
}
|
2015-11-09 09:28:34 +01:00
|
|
|
if p.ProtectedResource != nil && p.ProtectedResource.String() != "" {
|
|
|
|
params.Add("resource", p.ProtectedResource.String())
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:42:26 +01:00
|
|
|
result := requests.New(p.RedeemURL.String()).
|
2020-07-03 19:27:25 +01:00
|
|
|
WithContext(ctx).
|
|
|
|
WithMethod("POST").
|
|
|
|
WithBody(bytes.NewBufferString(params.Encode())).
|
|
|
|
SetHeader("Content-Type", "application/x-www-form-urlencoded").
|
|
|
|
Do()
|
2020-07-06 17:42:26 +01:00
|
|
|
if result.Error() != nil {
|
|
|
|
return nil, result.Error()
|
2015-06-06 14:15:43 -04:00
|
|
|
}
|
|
|
|
|
2015-05-20 23:23:48 -04:00
|
|
|
// blindly try json and x-www-form-urlencoded
|
|
|
|
var jsonResponse struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
}
|
2020-07-06 17:42:26 +01:00
|
|
|
err = result.UnmarshalInto(&jsonResponse)
|
2015-05-20 23:23:48 -04:00
|
|
|
if err == nil {
|
2020-10-23 19:35:15 -07:00
|
|
|
return &sessions.SessionState{
|
2015-06-23 07:23:39 -04:00
|
|
|
AccessToken: jsonResponse.AccessToken,
|
2020-10-23 19:35:15 -07:00
|
|
|
}, nil
|
2015-05-20 23:23:48 -04:00
|
|
|
}
|
|
|
|
|
2020-10-23 19:35:15 -07:00
|
|
|
values, err := url.ParseQuery(string(result.Body()))
|
2015-06-23 07:23:39 -04:00
|
|
|
if err != nil {
|
2020-10-23 19:35:15 -07:00
|
|
|
return nil, err
|
2015-06-23 07:23:39 -04:00
|
|
|
}
|
2021-03-06 15:33:40 -08:00
|
|
|
// TODO (@NickMeves): Uses OAuth `expires_in` to set an expiration
|
2020-10-23 19:35:15 -07:00
|
|
|
if token := values.Get("access_token"); token != "" {
|
2021-03-06 15:33:40 -08:00
|
|
|
ss := &sessions.SessionState{
|
|
|
|
AccessToken: token,
|
|
|
|
}
|
|
|
|
ss.CreatedAtNow()
|
|
|
|
return ss, nil
|
2015-06-23 07:23:39 -04:00
|
|
|
}
|
2020-10-23 19:35:15 -07:00
|
|
|
|
|
|
|
return nil, fmt.Errorf("no access token found %s", result.Body())
|
2015-05-20 23:23:48 -04:00
|
|
|
}
|
2015-06-06 14:15:43 -04:00
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// GetEmailAddress returns the Account email address
|
2021-02-17 20:15:45 +00:00
|
|
|
// Deprecated: Migrate to EnrichSession
|
2020-09-27 11:46:29 -07:00
|
|
|
func (p *ProviderData) GetEmailAddress(_ context.Context, _ *sessions.SessionState) (string, error) {
|
2020-09-26 14:06:52 -07:00
|
|
|
return "", ErrNotImplemented
|
2015-06-23 07:23:39 -04:00
|
|
|
}
|
|
|
|
|
2020-11-29 14:12:48 -08:00
|
|
|
// EnrichSession is called after Redeem to allow providers to enrich session fields
|
2020-09-27 11:46:29 -07:00
|
|
|
// such as User, Email, Groups with provider specific API calls.
|
2020-10-23 22:06:50 -07:00
|
|
|
func (p *ProviderData) EnrichSession(_ context.Context, _ *sessions.SessionState) error {
|
2020-09-27 11:46:29 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-26 17:29:34 -07:00
|
|
|
// Authorize performs global authorization on an authenticated session.
|
|
|
|
// This is not used for fine-grained per route authorization rules.
|
2020-10-23 19:35:15 -07:00
|
|
|
func (p *ProviderData) Authorize(_ context.Context, s *sessions.SessionState) (bool, error) {
|
2020-09-26 19:00:44 -07:00
|
|
|
if len(p.AllowedGroups) == 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, group := range s.Groups {
|
|
|
|
if _, ok := p.AllowedGroups[group]; ok {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
2020-09-26 17:29:34 -07:00
|
|
|
}
|
|
|
|
|
2020-11-29 14:12:48 -08:00
|
|
|
// ValidateSession validates the AccessToken
|
2020-10-23 22:06:50 -07:00
|
|
|
func (p *ProviderData) ValidateSession(ctx context.Context, s *sessions.SessionState) bool {
|
2020-05-06 00:53:33 +09:00
|
|
|
return validateToken(ctx, p, s.AccessToken, nil)
|
2015-06-23 07:23:39 -04:00
|
|
|
}
|
|
|
|
|
2021-03-06 15:33:13 -08:00
|
|
|
// RefreshSession refreshes the user's session
|
2021-06-12 11:41:03 -07:00
|
|
|
func (p *ProviderData) RefreshSession(_ context.Context, _ *sessions.SessionState) (bool, error) {
|
|
|
|
return false, ErrNotImplemented
|
2015-06-23 07:23:39 -04:00
|
|
|
}
|
2020-04-28 08:46:46 +02:00
|
|
|
|
2020-11-26 11:53:41 -08:00
|
|
|
// CreateSessionFromToken converts Bearer IDTokens into sessions
|
2020-11-15 18:57:48 -08:00
|
|
|
func (p *ProviderData) CreateSessionFromToken(ctx context.Context, token string) (*sessions.SessionState, error) {
|
|
|
|
if p.Verifier != nil {
|
|
|
|
return middleware.CreateTokenToSessionFunc(p.Verifier.Verify)(ctx, token)
|
|
|
|
}
|
2020-09-26 14:06:52 -07:00
|
|
|
return nil, ErrNotImplemented
|
2020-04-28 08:46:46 +02:00
|
|
|
}
|