mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2024-12-12 11:15:02 +02:00
ce750e9b30
* Add the allowed_email_domains and the allowed_groups on the auth_request endpoint + support standard wildcard char for validation with sub-domain and email-domain. Signed-off-by: Valentin Pichard <github@w3st.fr> * Fix provider data initialisation * PKCE Support Adds Code Challenge PKCE support (RFC-7636) and partial Authorization Server Metadata (RFC-8414) for detecting PKCE support. - Introduces new option `--force-code-challenge-method` to force a specific code challenge method (either `S256` or `plain`) for instances when the server has not implemented RFC-8414 in order to detect PKCE support on the discovery document. - In all other cases, if the PKCE support can be determined during discovery then the `code_challenge_methods_supported` is used and S256 is always preferred. - The force command line argument is helpful with some providers like Azure who supports PKCE but does not list it in their discovery document yet. - Initial thought was given to just always attempt PKCE since according to spec additional URL parameters should be dropped by servers which implemented OAuth 2, however other projects found cases in the wild where this causes 500 errors by buggy implementations. See: https://github.com/spring-projects/spring-security/pull/7804#issuecomment-578323810 - Due to the fact that the `code_verifier` must be saved between the redirect and callback, sessions are now created when the redirect takes place with `Authenticated: false`. The session will be recreated and marked as `Authenticated` on callback. - Individual provider implementations can choose to include or ignore code_challenge and code_verifier function parameters passed to them Note: Technically speaking `plain` is not required to be implemented since oauth2-proxy will always be able to handle S256 and servers MUST implement S256 support. > If the client is capable of using "S256", it MUST use "S256", as "S256" > is Mandatory To Implement (MTI) on the server. Clients are permitted > to use "plain" only if they cannot support "S256" for some technical > reason and know via out-of-band configuration that the server supports > "plain". Ref: RFC-7636 Sec 4.2 oauth2-proxy will always use S256 unless the user explicitly forces `plain`. Fixes #1361 * Address PR comments by moving pkce generation * Make PKCE opt-in, move to using the Nonce generater for code verifier * Make PKCE opt-in, move to using the Nonce generater for code verifier * Encrypt CodeVerifier in CSRF Token instead of Session - Update Dex for PKCE support - Expose HTTPBin for further use cases * Correct the tests * Move code challenges into extra params * Correct typo in code challenge method Co-authored-by: Joel Speed <Joel.speed@hotmail.co.uk> * Correct the extra space in docs Co-authored-by: Joel Speed <Joel.speed@hotmail.co.uk> * Address changelog and new line nits * Add generated docs Co-authored-by: Valentin Pichard <github@w3st.fr> Co-authored-by: Joel Speed <joel.speed@hotmail.co.uk>
149 lines
4.7 KiB
Go
149 lines
4.7 KiB
Go
package providers
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/requests"
|
|
)
|
|
|
|
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")
|
|
|
|
// ErrMissingCode is returned when a Redeem method is called with an empty
|
|
// code
|
|
ErrMissingCode = errors.New("missing code")
|
|
|
|
// 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")
|
|
|
|
_ Provider = (*ProviderData)(nil)
|
|
)
|
|
|
|
// GetLoginURL with typical oauth parameters
|
|
// 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
|
|
func (p *ProviderData) GetLoginURL(redirectURI, state, _ string, extraParams url.Values) string {
|
|
loginURL := makeLoginURL(p, redirectURI, state, extraParams)
|
|
return loginURL.String()
|
|
}
|
|
|
|
// Redeem provides a default implementation of the OAuth2 token redemption process
|
|
// 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) {
|
|
if code == "" {
|
|
return nil, ErrMissingCode
|
|
}
|
|
clientSecret, err := p.GetClientSecret()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
params := url.Values{}
|
|
params.Add("redirect_uri", redirectURL)
|
|
params.Add("client_id", p.ClientID)
|
|
params.Add("client_secret", clientSecret)
|
|
params.Add("code", code)
|
|
params.Add("grant_type", "authorization_code")
|
|
if codeVerifier != "" {
|
|
params.Add("code_verifier", codeVerifier)
|
|
}
|
|
if p.ProtectedResource != nil && p.ProtectedResource.String() != "" {
|
|
params.Add("resource", p.ProtectedResource.String())
|
|
}
|
|
|
|
result := requests.New(p.RedeemURL.String()).
|
|
WithContext(ctx).
|
|
WithMethod("POST").
|
|
WithBody(bytes.NewBufferString(params.Encode())).
|
|
SetHeader("Content-Type", "application/x-www-form-urlencoded").
|
|
Do()
|
|
if result.Error() != nil {
|
|
return nil, result.Error()
|
|
}
|
|
|
|
// blindly try json and x-www-form-urlencoded
|
|
var jsonResponse struct {
|
|
AccessToken string `json:"access_token"`
|
|
}
|
|
err = result.UnmarshalInto(&jsonResponse)
|
|
if err == nil {
|
|
return &sessions.SessionState{
|
|
AccessToken: jsonResponse.AccessToken,
|
|
}, nil
|
|
}
|
|
|
|
values, err := url.ParseQuery(string(result.Body()))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// TODO (@NickMeves): Uses OAuth `expires_in` to set an expiration
|
|
if token := values.Get("access_token"); token != "" {
|
|
ss := &sessions.SessionState{
|
|
AccessToken: token,
|
|
}
|
|
ss.CreatedAtNow()
|
|
return ss, nil
|
|
}
|
|
|
|
return nil, fmt.Errorf("no access token found %s", result.Body())
|
|
}
|
|
|
|
// GetEmailAddress returns the Account email address
|
|
// Deprecated: Migrate to EnrichSession
|
|
func (p *ProviderData) GetEmailAddress(_ context.Context, _ *sessions.SessionState) (string, error) {
|
|
return "", ErrNotImplemented
|
|
}
|
|
|
|
// EnrichSession is called after Redeem to allow providers to enrich session fields
|
|
// such as User, Email, Groups with provider specific API calls.
|
|
func (p *ProviderData) EnrichSession(_ context.Context, _ *sessions.SessionState) error {
|
|
return nil
|
|
}
|
|
|
|
// Authorize performs global authorization on an authenticated session.
|
|
// This is not used for fine-grained per route authorization rules.
|
|
func (p *ProviderData) Authorize(_ context.Context, s *sessions.SessionState) (bool, error) {
|
|
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
|
|
}
|
|
|
|
// ValidateSession validates the AccessToken
|
|
func (p *ProviderData) ValidateSession(ctx context.Context, s *sessions.SessionState) bool {
|
|
return validateToken(ctx, p, s.AccessToken, nil)
|
|
}
|
|
|
|
// RefreshSession refreshes the user's session
|
|
func (p *ProviderData) RefreshSession(_ context.Context, _ *sessions.SessionState) (bool, error) {
|
|
return false, ErrNotImplemented
|
|
}
|
|
|
|
// CreateSessionFromToken converts Bearer IDTokens into sessions
|
|
func (p *ProviderData) CreateSessionFromToken(ctx context.Context, token string) (*sessions.SessionState, error) {
|
|
if p.Verifier != nil {
|
|
return middleware.CreateTokenToSessionFunc(p.Verifier.Verify)(ctx, token)
|
|
}
|
|
return nil, ErrNotImplemented
|
|
}
|