| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"net/http" |
| 6 |
|
| 7 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 8 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 9 |
) |
| 10 |
|
| 11 |
// CodeExchange handles the OAuth 2.0 authorization_code grant, including |
| 12 |
// parsing, validating, authorizing the client and finally exchanging the code for tokens |
| 13 |
func CodeExchange(w http.ResponseWriter, r *http.Request, exchanger Exchanger) { |
| 14 |
ctx, span := Tracer.Start(r.Context(), "CodeExchange") |
| 15 |
defer span.End() |
| 16 |
r = r.WithContext(ctx) |
| 17 |
|
| 18 |
tokenReq, err := ParseAccessTokenRequest(r, exchanger.Decoder()) |
| 19 |
if err != nil { |
| 20 |
RequestError(w, r, err, nil) |
| 21 |
return |
| 22 |
} |
| 23 |
if tokenReq.Code == "" { |
| 24 |
RequestError(w, r, oidc.ErrInvalidRequest().WithDescription("code missing"), nil) |
| 25 |
return |
| 26 |
} |
| 27 |
authReq, client, err := ValidateAccessTokenRequest(r.Context(), tokenReq, exchanger) |
| 28 |
if err != nil { |
| 29 |
RequestError(w, r, err, nil) |
| 30 |
return |
| 31 |
} |
| 32 |
resp, err := CreateTokenResponse(r.Context(), authReq, client, exchanger, true, tokenReq.Code, "") |
| 33 |
if err != nil { |
| 34 |
RequestError(w, r, err, nil) |
| 35 |
return |
| 36 |
} |
| 37 |
httphelper.MarshalJSON(w, resp) |
| 38 |
} |
| 39 |
|
| 40 |
// ParseAccessTokenRequest parsed the http request into an oidc.AccessTokenRequest |
| 41 |
func ParseAccessTokenRequest(r *http.Request, decoder httphelper.Decoder) (*oidc.AccessTokenRequest, error) { |
| 42 |
request := new(oidc.AccessTokenRequest) |
| 43 |
err := ParseAuthenticatedTokenRequest(r, decoder, request) |
| 44 |
if err != nil { |
| 45 |
return nil, err |
| 46 |
} |
| 47 |
return request, nil |
| 48 |
} |
| 49 |
|
| 50 |
// ValidateAccessTokenRequest validates the token request parameters including authorization check of the client |
| 51 |
// and returns the previous created auth request corresponding to the auth code |
| 52 |
func ValidateAccessTokenRequest(ctx context.Context, tokenReq *oidc.AccessTokenRequest, exchanger Exchanger) (AuthRequest, Client, error) { |
| 53 |
ctx, span := Tracer.Start(ctx, "ValidateAccessTokenRequest") |
| 54 |
defer span.End() |
| 55 |
|
| 56 |
authReq, client, err := AuthorizeCodeClient(ctx, tokenReq, exchanger) |
| 57 |
if err != nil { |
| 58 |
return nil, nil, err |
| 59 |
} |
| 60 |
if client.GetID() != authReq.GetClientID() { |
| 61 |
return nil, nil, oidc.ErrInvalidGrant() |
| 62 |
} |
| 63 |
if !ValidateGrantType(client, oidc.GrantTypeCode) { |
| 64 |
return nil, nil, oidc.ErrUnauthorizedClient().WithDescription("client missing grant type " + string(oidc.GrantTypeCode)) |
| 65 |
} |
| 66 |
if tokenReq.RedirectURI != authReq.GetRedirectURI() { |
| 67 |
return nil, nil, oidc.ErrInvalidGrant().WithDescription("redirect_uri does not correspond") |
| 68 |
} |
| 69 |
return authReq, client, nil |
| 70 |
} |
| 71 |
|
| 72 |
// AuthorizeCodeClient checks the authorization of the client and that the used method was the one previously registered. |
| 73 |
// It than returns the auth request corresponding to the auth code |
| 74 |
func AuthorizeCodeClient(ctx context.Context, tokenReq *oidc.AccessTokenRequest, exchanger Exchanger) (request AuthRequest, client Client, err error) { |
| 75 |
ctx, span := Tracer.Start(ctx, "AuthorizeCodeClient") |
| 76 |
defer span.End() |
| 77 |
|
| 78 |
request, err = AuthRequestByCode(ctx, exchanger.Storage(), tokenReq.Code) |
| 79 |
if err != nil { |
| 80 |
return nil, nil, err |
| 81 |
} |
| 82 |
|
| 83 |
codeChallenge := request.GetCodeChallenge() |
| 84 |
err = AuthorizeCodeChallenge(tokenReq.CodeVerifier, codeChallenge) |
| 85 |
if err != nil { |
| 86 |
return nil, nil, err |
| 87 |
} |
| 88 |
|
| 89 |
if tokenReq.ClientAssertionType == oidc.ClientAssertionTypeJWTAssertion { |
| 90 |
jwtExchanger, ok := exchanger.(JWTAuthorizationGrantExchanger) |
| 91 |
if !ok || !exchanger.AuthMethodPrivateKeyJWTSupported() { |
| 92 |
return nil, nil, oidc.ErrInvalidClient().WithDescription("auth_method private_key_jwt not supported") |
| 93 |
} |
| 94 |
client, err = AuthorizePrivateJWTKey(ctx, tokenReq.ClientAssertion, jwtExchanger) |
| 95 |
if err != nil { |
| 96 |
return nil, nil, err |
| 97 |
} |
| 98 |
return request, client, err |
| 99 |
} |
| 100 |
|
| 101 |
client, err = exchanger.Storage().GetClientByClientID(ctx, tokenReq.ClientID) |
| 102 |
if err != nil { |
| 103 |
return nil, nil, oidc.ErrInvalidClient().WithParent(err) |
| 104 |
} |
| 105 |
if client.AuthMethod() == oidc.AuthMethodPrivateKeyJWT { |
| 106 |
return nil, nil, oidc.ErrInvalidClient().WithDescription("private_key_jwt not allowed for this client") |
| 107 |
} |
| 108 |
if client.AuthMethod() == oidc.AuthMethodNone { |
| 109 |
if codeChallenge == nil { |
| 110 |
return nil, nil, oidc.ErrInvalidRequest().WithDescription("PKCE required") |
| 111 |
} |
| 112 |
return request, client, nil |
| 113 |
} |
| 114 |
if client.AuthMethod() == oidc.AuthMethodPost && !exchanger.AuthMethodPostSupported() { |
| 115 |
return nil, nil, oidc.ErrInvalidClient().WithDescription("auth_method post not supported") |
| 116 |
} |
| 117 |
err = AuthorizeClientIDSecret(ctx, tokenReq.ClientID, tokenReq.ClientSecret, exchanger.Storage()) |
| 118 |
if err != nil { |
| 119 |
return nil, nil, err |
| 120 |
} |
| 121 |
|
| 122 |
return request, client, err |
| 123 |
} |
| 124 |
|
| 125 |
// AuthRequestByCode returns the AuthRequest previously created from Storage corresponding to the auth code or an error |
| 126 |
func AuthRequestByCode(ctx context.Context, storage Storage, code string) (AuthRequest, error) { |
| 127 |
ctx, span := Tracer.Start(ctx, "AuthRequestByCode") |
| 128 |
defer span.End() |
| 129 |
|
| 130 |
authReq, err := storage.AuthRequestByCode(ctx, code) |
| 131 |
if err != nil { |
| 132 |
return nil, oidc.ErrInvalidGrant().WithDescription("invalid code").WithParent(err) |
| 133 |
} |
| 134 |
return authReq, nil |
| 135 |
} |