redirect to desired page after authenticating
This commit is contained in:
@@ -100,27 +100,28 @@ func (a *Authenticator) RunBackgroundCleanup(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// Exchange exchanges an auth code for an access token.
|
||||
func (a *Authenticator) Exchange(ctx context.Context, state, code string) (accessToken string, expiration time.Time, err error) {
|
||||
func (a *Authenticator) Exchange(ctx context.Context, state, code string) (accessToken, targetURI string, expiration time.Time, err error) {
|
||||
// validate state
|
||||
|
||||
if exp, err := a.GetStateExpiration(ctx, state); errors.Is(err, consts.ErrNotFound) {
|
||||
return "", time.Time{}, fmt.Errorf("invalid state: %w", consts.ErrNotFound)
|
||||
exp, targetURI, err := a.GetStateExpirationAndURL(ctx, state)
|
||||
if errors.Is(err, consts.ErrNotFound) {
|
||||
return "", "", time.Time{}, fmt.Errorf("invalid state: %w", consts.ErrNotFound)
|
||||
} else if err != nil {
|
||||
return "", time.Time{}, fmt.Errorf("failed to load state expiration: %w", err)
|
||||
return "", "", time.Time{}, fmt.Errorf("failed to load state expiration: %w", err)
|
||||
} else if exp.Before(time.Now()) {
|
||||
return "", time.Time{}, fmt.Errorf("invalid state: state expired")
|
||||
return "", "", time.Time{}, fmt.Errorf("invalid state: state expired")
|
||||
}
|
||||
|
||||
// obtain token and profile
|
||||
|
||||
tkn, err := a.Config.Exchange(ctx, code)
|
||||
if err != nil {
|
||||
return "", time.Time{}, fmt.Errorf("failed to exchange an authorization code for a token: %w", err)
|
||||
return "", "", time.Time{}, fmt.Errorf("failed to exchange an authorization code for a token: %w", err)
|
||||
}
|
||||
|
||||
idToken, claims, err := a.verifyIDTokenAndClaimsFromToken(ctx, tkn)
|
||||
if err != nil {
|
||||
return "", time.Time{}, err
|
||||
return "", "", time.Time{}, err
|
||||
}
|
||||
|
||||
claimsJSON, _ := json.Marshal(claims)
|
||||
@@ -200,10 +201,10 @@ func (a *Authenticator) Exchange(ctx context.Context, state, code string) (acces
|
||||
"claims": json.RawMessage(claimsJSON),
|
||||
},
|
||||
); err != nil {
|
||||
return "", time.Time{}, fmt.Errorf("failed to perform query to save tokens: %w", err)
|
||||
return "", "", time.Time{}, fmt.Errorf("failed to perform query to save tokens: %w", err)
|
||||
}
|
||||
|
||||
return tkn.AccessToken, tkn.Expiry.UTC(), nil
|
||||
return tkn.AccessToken, targetURI, tkn.Expiry.UTC(), nil
|
||||
}
|
||||
|
||||
// VerifyIDToken verifies that an *oauth2.Token is a valid *oidc.IDToken.
|
||||
|
||||
Reference in New Issue
Block a user