| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"net/http" |
| 7 |
"slices" |
| 8 |
"time" |
| 9 |
|
| 10 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 11 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 12 |
) |
| 13 |
|
| 14 |
type RefreshTokenRequest interface { |
| 15 |
GetAMR() []string |
| 16 |
GetAudience() []string |
| 17 |
GetAuthTime() time.Time |
| 18 |
GetClientID() string |
| 19 |
GetScopes() []string |
| 20 |
GetSubject() string |
| 21 |
SetCurrentScopes(scopes []string) |
| 22 |
} |
| 23 |
|
| 24 |
// RefreshTokenExchange handles the OAuth 2.0 refresh_token grant, including |
| 25 |
// parsing, validating, authorizing the client and finally exchanging the refresh_token for new tokens |
| 26 |
func RefreshTokenExchange(w http.ResponseWriter, r *http.Request, exchanger Exchanger) { |
| 27 |
ctx, span := Tracer.Start(r.Context(), "RefreshTokenExchange") |
| 28 |
defer span.End() |
| 29 |
r = r.WithContext(ctx) |
| 30 |
|
| 31 |
tokenReq, err := ParseRefreshTokenRequest(r, exchanger.Decoder()) |
| 32 |
if err != nil { |
| 33 |
RequestError(w, r, err, nil) |
| 34 |
return |
| 35 |
} |
| 36 |
validatedRequest, client, err := ValidateRefreshTokenRequest(r.Context(), tokenReq, exchanger) |
| 37 |
if err != nil { |
| 38 |
RequestError(w, r, err, nil) |
| 39 |
return |
| 40 |
} |
| 41 |
resp, err := CreateTokenResponse(r.Context(), validatedRequest, client, exchanger, true, "", tokenReq.RefreshToken) |
| 42 |
if err != nil { |
| 43 |
RequestError(w, r, err, nil) |
| 44 |
return |
| 45 |
} |
| 46 |
httphelper.MarshalJSON(w, resp) |
| 47 |
} |
| 48 |
|
| 49 |
// ParseRefreshTokenRequest parsed the http request into an oidc.RefreshTokenRequest |
| 50 |
func ParseRefreshTokenRequest(r *http.Request, decoder httphelper.Decoder) (*oidc.RefreshTokenRequest, error) { |
| 51 |
request := new(oidc.RefreshTokenRequest) |
| 52 |
err := ParseAuthenticatedTokenRequest(r, decoder, request) |
| 53 |
if err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
return request, nil |
| 57 |
} |
| 58 |
|
| 59 |
// ValidateRefreshTokenRequest validates the refresh_token request parameters including authorization check of the client |
| 60 |
// and returns the data representing the original auth request corresponding to the refresh_token |
| 61 |
func ValidateRefreshTokenRequest(ctx context.Context, tokenReq *oidc.RefreshTokenRequest, exchanger Exchanger) (RefreshTokenRequest, Client, error) { |
| 62 |
ctx, span := Tracer.Start(ctx, "ValidateRefreshTokenRequest") |
| 63 |
defer span.End() |
| 64 |
|
| 65 |
if tokenReq.RefreshToken == "" { |
| 66 |
return nil, nil, oidc.ErrInvalidRequest().WithDescription("refresh_token missing") |
| 67 |
} |
| 68 |
request, client, err := AuthorizeRefreshClient(ctx, tokenReq, exchanger) |
| 69 |
if err != nil { |
| 70 |
return nil, nil, err |
| 71 |
} |
| 72 |
if client.GetID() != request.GetClientID() { |
| 73 |
return nil, nil, oidc.ErrInvalidGrant() |
| 74 |
} |
| 75 |
if err = ValidateRefreshTokenScopes(tokenReq.Scopes, request); err != nil { |
| 76 |
return nil, nil, err |
| 77 |
} |
| 78 |
return request, client, nil |
| 79 |
} |
| 80 |
|
| 81 |
// ValidateRefreshTokenScopes validates that the requested scope is a subset of the original auth request scope |
| 82 |
// it will set the requested scopes as current scopes onto RefreshTokenRequest |
| 83 |
// if empty the original scopes will be used |
| 84 |
func ValidateRefreshTokenScopes(requestedScopes []string, authRequest RefreshTokenRequest) error { |
| 85 |
if len(requestedScopes) == 0 { |
| 86 |
return nil |
| 87 |
} |
| 88 |
for _, scope := range requestedScopes { |
| 89 |
if !slices.Contains(authRequest.GetScopes(), scope) { |
| 90 |
return oidc.ErrInvalidScope() |
| 91 |
} |
| 92 |
} |
| 93 |
authRequest.SetCurrentScopes(requestedScopes) |
| 94 |
return nil |
| 95 |
} |
| 96 |
|
| 97 |
// AuthorizeRefreshClient checks the authorization of the client and that the used method was the one previously registered. |
| 98 |
// It than returns the data representing the original auth request corresponding to the refresh_token |
| 99 |
func AuthorizeRefreshClient(ctx context.Context, tokenReq *oidc.RefreshTokenRequest, exchanger Exchanger) (request RefreshTokenRequest, client Client, err error) { |
| 100 |
ctx, span := Tracer.Start(ctx, "AuthorizeRefreshClient") |
| 101 |
defer span.End() |
| 102 |
|
| 103 |
if tokenReq.ClientAssertionType == oidc.ClientAssertionTypeJWTAssertion { |
| 104 |
jwtExchanger, ok := exchanger.(JWTAuthorizationGrantExchanger) |
| 105 |
if !ok || !exchanger.AuthMethodPrivateKeyJWTSupported() { |
| 106 |
return nil, nil, errors.New("auth_method private_key_jwt not supported") |
| 107 |
} |
| 108 |
client, err = AuthorizePrivateJWTKey(ctx, tokenReq.ClientAssertion, jwtExchanger) |
| 109 |
if err != nil { |
| 110 |
return nil, nil, err |
| 111 |
} |
| 112 |
if !ValidateGrantType(client, oidc.GrantTypeRefreshToken) { |
| 113 |
return nil, nil, oidc.ErrUnauthorizedClient() |
| 114 |
} |
| 115 |
request, err = RefreshTokenRequestByRefreshToken(ctx, exchanger.Storage(), tokenReq.RefreshToken) |
| 116 |
return request, client, err |
| 117 |
} |
| 118 |
client, err = exchanger.Storage().GetClientByClientID(ctx, tokenReq.ClientID) |
| 119 |
if err != nil { |
| 120 |
return nil, nil, err |
| 121 |
} |
| 122 |
if !ValidateGrantType(client, oidc.GrantTypeRefreshToken) { |
| 123 |
return nil, nil, oidc.ErrUnauthorizedClient() |
| 124 |
} |
| 125 |
if client.AuthMethod() == oidc.AuthMethodPrivateKeyJWT { |
| 126 |
return nil, nil, oidc.ErrInvalidClient() |
| 127 |
} |
| 128 |
if client.AuthMethod() == oidc.AuthMethodNone { |
| 129 |
request, err = RefreshTokenRequestByRefreshToken(ctx, exchanger.Storage(), tokenReq.RefreshToken) |
| 130 |
return request, client, err |
| 131 |
} |
| 132 |
if client.AuthMethod() == oidc.AuthMethodPost && !exchanger.AuthMethodPostSupported() { |
| 133 |
return nil, nil, oidc.ErrInvalidClient().WithDescription("auth_method post not supported") |
| 134 |
} |
| 135 |
if err = AuthorizeClientIDSecret(ctx, tokenReq.ClientID, tokenReq.ClientSecret, exchanger.Storage()); err != nil { |
| 136 |
return nil, nil, err |
| 137 |
} |
| 138 |
request, err = RefreshTokenRequestByRefreshToken(ctx, exchanger.Storage(), tokenReq.RefreshToken) |
| 139 |
return request, client, err |
| 140 |
} |
| 141 |
|
| 142 |
// RefreshTokenRequestByRefreshToken returns the RefreshTokenRequest (data representing the original auth request) |
| 143 |
// corresponding to the refresh_token from Storage or an error |
| 144 |
func RefreshTokenRequestByRefreshToken(ctx context.Context, storage Storage, refreshToken string) (RefreshTokenRequest, error) { |
| 145 |
ctx, span := Tracer.Start(ctx, "RefreshTokenRequestByRefreshToken") |
| 146 |
defer span.End() |
| 147 |
|
| 148 |
request, err := storage.TokenRequestByRefreshToken(ctx, refreshToken) |
| 149 |
if err != nil { |
| 150 |
return nil, oidc.ErrInvalidGrant().WithParent(err) |
| 151 |
} |
| 152 |
return request, nil |
| 153 |
} |