2017-05-09 20:20:35 +02:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-27 05:00:30 +02:00
|
|
|
"errors"
|
2017-05-09 20:20:35 +02:00
|
|
|
"fmt"
|
2021-04-21 11:33:27 +02:00
|
|
|
"net/url"
|
2017-05-09 20:20:35 +02:00
|
|
|
"time"
|
|
|
|
|
2022-02-15 13:18:32 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
|
2020-09-29 18:44:42 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
|
2020-11-03 20:10:08 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
|
2020-11-27 05:00:30 +02:00
|
|
|
"golang.org/x/oauth2"
|
2017-05-09 20:20:35 +02:00
|
|
|
)
|
|
|
|
|
2018-12-20 12:37:59 +02:00
|
|
|
// OIDCProvider represents an OIDC based Identity Provider
|
2017-05-09 20:20:35 +02:00
|
|
|
type OIDCProvider struct {
|
|
|
|
*ProviderData
|
2021-04-21 11:33:27 +02:00
|
|
|
|
|
|
|
SkipNonce bool
|
2017-05-09 20:20:35 +02:00
|
|
|
}
|
|
|
|
|
2018-12-20 12:37:59 +02:00
|
|
|
// NewOIDCProvider initiates a new OIDCProvider
|
2022-02-15 13:18:32 +02:00
|
|
|
func NewOIDCProvider(p *ProviderData, opts options.OIDCOptions) *OIDCProvider {
|
2017-10-08 06:40:36 +02:00
|
|
|
p.ProviderName = "OpenID Connect"
|
2021-06-26 12:49:08 +02:00
|
|
|
p.getAuthorizationHeaderFunc = makeOIDCHeader
|
|
|
|
|
2021-04-21 11:33:27 +02:00
|
|
|
return &OIDCProvider{
|
|
|
|
ProviderData: p,
|
2022-02-15 13:18:32 +02:00
|
|
|
SkipNonce: opts.InsecureSkipNonce,
|
2021-04-21 11:33:27 +02:00
|
|
|
}
|
2017-05-09 20:20:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 17:53:33 +02:00
|
|
|
var _ Provider = (*OIDCProvider)(nil)
|
|
|
|
|
2021-04-21 11:33:27 +02:00
|
|
|
// GetLoginURL makes the LoginURL with optional nonce support
|
2022-02-16 18:18:51 +02:00
|
|
|
func (p *OIDCProvider) GetLoginURL(redirectURI, state, nonce string, extraParams url.Values) string {
|
2021-04-21 11:33:27 +02:00
|
|
|
if !p.SkipNonce {
|
|
|
|
extraParams.Add("nonce", nonce)
|
|
|
|
}
|
|
|
|
loginURL := makeLoginURL(p.Data(), redirectURI, state, extraParams)
|
|
|
|
return loginURL.String()
|
|
|
|
}
|
|
|
|
|
2018-12-20 12:37:59 +02:00
|
|
|
// Redeem exchanges the OAuth2 authentication token for an ID token
|
2022-03-13 12:08:33 +02:00
|
|
|
func (p *OIDCProvider) Redeem(ctx context.Context, redirectURL, code, codeVerifier string) (*sessions.SessionState, error) {
|
2020-02-15 15:44:39 +02:00
|
|
|
clientSecret, err := p.GetClientSecret()
|
|
|
|
if err != nil {
|
2020-11-27 05:00:30 +02:00
|
|
|
return nil, err
|
2020-02-15 15:44:39 +02:00
|
|
|
}
|
|
|
|
|
2022-03-13 12:08:33 +02:00
|
|
|
var opts []oauth2.AuthCodeOption
|
|
|
|
if codeVerifier != "" {
|
|
|
|
opts = append(opts, oauth2.SetAuthURLParam("code_verifier", codeVerifier))
|
|
|
|
}
|
|
|
|
|
2017-05-09 20:20:35 +02:00
|
|
|
c := oauth2.Config{
|
|
|
|
ClientID: p.ClientID,
|
2020-02-15 15:44:39 +02:00
|
|
|
ClientSecret: clientSecret,
|
2017-05-09 20:20:35 +02:00
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
TokenURL: p.RedeemURL.String(),
|
|
|
|
},
|
|
|
|
RedirectURL: redirectURL,
|
|
|
|
}
|
2022-03-13 12:08:33 +02:00
|
|
|
token, err := c.Exchange(ctx, code, opts...)
|
2017-05-09 20:20:35 +02:00
|
|
|
if err != nil {
|
2020-11-28 22:33:05 +02:00
|
|
|
return nil, fmt.Errorf("token exchange failed: %v", err)
|
2017-05-09 20:20:35 +02:00
|
|
|
}
|
2020-02-06 20:09:30 +02:00
|
|
|
|
2020-11-27 05:00:30 +02:00
|
|
|
return p.createSession(ctx, token, false)
|
|
|
|
}
|
|
|
|
|
2020-12-02 03:50:27 +02:00
|
|
|
// EnrichSession is called after Redeem to allow providers to enrich session fields
|
2020-11-27 05:00:30 +02:00
|
|
|
// such as User, Email, Groups with provider specific API calls.
|
|
|
|
func (p *OIDCProvider) EnrichSession(ctx context.Context, s *sessions.SessionState) error {
|
|
|
|
// If a mandatory email wasn't set, error at this point.
|
|
|
|
if s.Email == "" {
|
|
|
|
return errors.New("neither the id_token nor the profileURL set an email")
|
2020-02-06 20:09:30 +02:00
|
|
|
}
|
2020-11-27 05:00:30 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-02-06 20:09:30 +02:00
|
|
|
|
2020-12-02 03:50:27 +02:00
|
|
|
// ValidateSession checks that the session's IDToken is still valid
|
2020-11-27 05:00:30 +02:00
|
|
|
func (p *OIDCProvider) ValidateSession(ctx context.Context, s *sessions.SessionState) bool {
|
2021-06-26 12:49:08 +02:00
|
|
|
_, err := p.Verifier.Verify(ctx, s.IDToken)
|
2021-04-21 11:33:27 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("id_token verification failed: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.SkipNonce {
|
|
|
|
return true
|
|
|
|
}
|
2021-06-26 12:49:08 +02:00
|
|
|
err = p.checkNonce(s)
|
2021-04-21 11:33:27 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("nonce verification failed: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-06-20 12:42:06 +02:00
|
|
|
}
|
|
|
|
|
2021-03-07 01:33:13 +02:00
|
|
|
// RefreshSession uses the RefreshToken to fetch new Access and ID Tokens
|
|
|
|
func (p *OIDCProvider) RefreshSession(ctx context.Context, s *sessions.SessionState) (bool, error) {
|
|
|
|
if s == nil || s.RefreshToken == "" {
|
2018-06-20 12:42:06 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:53:33 +02:00
|
|
|
err := p.redeemRefreshToken(ctx, s)
|
2018-06-20 12:42:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("unable to redeem refresh token: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
2017-05-09 20:20:35 +02:00
|
|
|
|
2020-11-27 05:00:30 +02:00
|
|
|
// redeemRefreshToken uses a RefreshToken with the RedeemURL to refresh the
|
|
|
|
// Access Token and (probably) the ID Token.
|
|
|
|
func (p *OIDCProvider) redeemRefreshToken(ctx context.Context, s *sessions.SessionState) error {
|
2020-02-15 15:44:39 +02:00
|
|
|
clientSecret, err := p.GetClientSecret()
|
|
|
|
if err != nil {
|
2020-11-27 05:00:30 +02:00
|
|
|
return err
|
2020-02-15 15:44:39 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 12:42:06 +02:00
|
|
|
c := oauth2.Config{
|
|
|
|
ClientID: p.ClientID,
|
2020-02-15 15:44:39 +02:00
|
|
|
ClientSecret: clientSecret,
|
2018-06-20 12:42:06 +02:00
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
TokenURL: p.RedeemURL.String(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
t := &oauth2.Token{
|
|
|
|
RefreshToken: s.RefreshToken,
|
|
|
|
Expiry: time.Now().Add(-time.Hour),
|
|
|
|
}
|
|
|
|
token, err := c.TokenSource(ctx, t).Token()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get token: %v", err)
|
|
|
|
}
|
2020-02-06 20:09:30 +02:00
|
|
|
|
2020-11-27 05:00:30 +02:00
|
|
|
newSession, err := p.createSession(ctx, token, true)
|
2020-02-06 20:09:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable create new session state from response: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-11-27 05:00:30 +02:00
|
|
|
// It's possible that if the refresh token isn't in the token response the
|
|
|
|
// session will not contain an id token.
|
|
|
|
// If it doesn't it's probably better to retain the old one
|
2020-02-06 20:09:30 +02:00
|
|
|
if newSession.IDToken != "" {
|
|
|
|
s.IDToken = newSession.IDToken
|
|
|
|
s.Email = newSession.Email
|
|
|
|
s.User = newSession.User
|
2020-07-28 20:42:09 +02:00
|
|
|
s.Groups = newSession.Groups
|
2020-03-01 17:02:51 +02:00
|
|
|
s.PreferredUsername = newSession.PreferredUsername
|
2020-02-06 20:09:30 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 12:42:06 +02:00
|
|
|
s.AccessToken = newSession.AccessToken
|
|
|
|
s.RefreshToken = newSession.RefreshToken
|
2019-05-07 16:32:46 +02:00
|
|
|
s.CreatedAt = newSession.CreatedAt
|
2018-06-20 12:42:06 +02:00
|
|
|
s.ExpiresOn = newSession.ExpiresOn
|
2020-02-06 20:09:30 +02:00
|
|
|
|
2020-11-27 05:00:30 +02:00
|
|
|
return nil
|
2018-06-20 12:42:06 +02:00
|
|
|
}
|
|
|
|
|
2020-11-27 05:00:30 +02:00
|
|
|
// CreateSessionFromToken converts Bearer IDTokens into sessions
|
|
|
|
func (p *OIDCProvider) CreateSessionFromToken(ctx context.Context, token string) (*sessions.SessionState, error) {
|
|
|
|
idToken, err := p.Verifier.Verify(ctx, token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-06 20:09:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-26 12:49:08 +02:00
|
|
|
ss, err := p.buildSessionFromClaims(token, "")
|
2020-11-27 05:00:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-05-09 20:20:35 +02:00
|
|
|
}
|
2020-02-06 20:09:30 +02:00
|
|
|
|
2020-11-27 05:00:30 +02:00
|
|
|
// Allow empty Email in Bearer case since we can't hit the ProfileURL
|
|
|
|
if ss.Email == "" {
|
|
|
|
ss.Email = ss.User
|
2020-02-06 20:09:30 +02:00
|
|
|
}
|
|
|
|
|
2020-11-27 05:00:30 +02:00
|
|
|
ss.AccessToken = token
|
|
|
|
ss.IDToken = token
|
|
|
|
ss.RefreshToken = ""
|
2021-03-07 01:33:40 +02:00
|
|
|
|
|
|
|
ss.CreatedAtNow()
|
|
|
|
ss.SetExpiresOn(idToken.Expiry)
|
2020-11-27 05:00:30 +02:00
|
|
|
|
|
|
|
return ss, nil
|
2020-02-06 20:09:30 +02:00
|
|
|
}
|
|
|
|
|
2020-11-27 05:00:30 +02:00
|
|
|
// createSession takes an oauth2.Token and creates a SessionState from it.
|
|
|
|
// It alters behavior if called from Redeem vs Refresh
|
|
|
|
func (p *OIDCProvider) createSession(ctx context.Context, token *oauth2.Token, refresh bool) (*sessions.SessionState, error) {
|
2021-06-26 12:49:08 +02:00
|
|
|
_, err := p.verifyIDToken(ctx, token)
|
2020-10-24 08:34:06 +02:00
|
|
|
if err != nil {
|
2020-12-01 22:01:42 +02:00
|
|
|
switch err {
|
|
|
|
case ErrMissingIDToken:
|
|
|
|
// IDToken is mandatory in Redeem but optional in Refresh
|
|
|
|
if !refresh {
|
|
|
|
return nil, errors.New("token response did not contain an id_token")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("could not verify id_token: %v", err)
|
|
|
|
}
|
2020-10-24 08:34:06 +02:00
|
|
|
}
|
|
|
|
|
2021-06-26 12:49:08 +02:00
|
|
|
rawIDToken := getIDToken(token)
|
|
|
|
ss, err := p.buildSessionFromClaims(rawIDToken, token.AccessToken)
|
2020-04-28 08:46:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-27 05:00:30 +02:00
|
|
|
ss.AccessToken = token.AccessToken
|
|
|
|
ss.RefreshToken = token.RefreshToken
|
2021-06-26 12:49:08 +02:00
|
|
|
ss.IDToken = rawIDToken
|
2020-04-28 08:46:46 +02:00
|
|
|
|
2021-03-07 01:33:40 +02:00
|
|
|
ss.CreatedAtNow()
|
|
|
|
ss.SetExpiresOn(token.Expiry)
|
2020-11-27 05:00:30 +02:00
|
|
|
|
|
|
|
return ss, nil
|
2020-04-28 08:46:46 +02:00
|
|
|
}
|