| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"net/http" |
| 6 |
"time" |
| 7 |
|
| 8 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 9 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 10 |
) |
| 11 |
|
| 12 |
type JWTAuthorizationGrantExchanger interface { |
| 13 |
Exchanger |
| 14 |
JWTProfileVerifier(context.Context) *JWTProfileVerifier |
| 15 |
} |
| 16 |
|
| 17 |
// JWTProfile handles the OAuth 2.0 JWT Profile Authorization Grant https://tools.ietf.org/html/rfc7523#section-2.1 |
| 18 |
func JWTProfile(w http.ResponseWriter, r *http.Request, exchanger JWTAuthorizationGrantExchanger) { |
| 19 |
ctx, span := Tracer.Start(r.Context(), "JWTProfile") |
| 20 |
defer span.End() |
| 21 |
r = r.WithContext(ctx) |
| 22 |
|
| 23 |
profileRequest, err := ParseJWTProfileGrantRequest(r, exchanger.Decoder()) |
| 24 |
if err != nil { |
| 25 |
RequestError(w, r, err, nil) |
| 26 |
return |
| 27 |
} |
| 28 |
|
| 29 |
tokenRequest, err := VerifyJWTAssertion(r.Context(), profileRequest.Assertion, exchanger.JWTProfileVerifier(r.Context())) |
| 30 |
if err != nil { |
| 31 |
RequestError(w, r, err, nil) |
| 32 |
return |
| 33 |
} |
| 34 |
|
| 35 |
tokenRequest.Scopes, err = exchanger.Storage().ValidateJWTProfileScopes(r.Context(), tokenRequest.Issuer, profileRequest.Scope) |
| 36 |
if err != nil { |
| 37 |
RequestError(w, r, err, nil) |
| 38 |
return |
| 39 |
} |
| 40 |
resp, err := CreateJWTTokenResponse(r.Context(), tokenRequest, exchanger) |
| 41 |
if err != nil { |
| 42 |
RequestError(w, r, err, nil) |
| 43 |
return |
| 44 |
} |
| 45 |
httphelper.MarshalJSON(w, resp) |
| 46 |
} |
| 47 |
|
| 48 |
func ParseJWTProfileGrantRequest(r *http.Request, decoder httphelper.Decoder) (*oidc.JWTProfileGrantRequest, error) { |
| 49 |
err := r.ParseForm() |
| 50 |
if err != nil { |
| 51 |
return nil, oidc.ErrInvalidRequest().WithDescription("error parsing form").WithParent(err) |
| 52 |
} |
| 53 |
tokenReq := new(oidc.JWTProfileGrantRequest) |
| 54 |
err = decoder.Decode(tokenReq, r.Form) |
| 55 |
if err != nil { |
| 56 |
return nil, oidc.ErrInvalidRequest().WithDescription("error decoding form").WithParent(err) |
| 57 |
} |
| 58 |
return tokenReq, nil |
| 59 |
} |
| 60 |
|
| 61 |
// CreateJWTTokenResponse creates an access_token response for a JWT Profile Grant request |
| 62 |
// by default the access_token is an opaque string, but can be specified by implementing the JWTProfileTokenStorage interface |
| 63 |
func CreateJWTTokenResponse(ctx context.Context, tokenRequest TokenRequest, creator TokenCreator) (*oidc.AccessTokenResponse, error) { |
| 64 |
ctx, span := Tracer.Start(ctx, "CreateJWTTokenResponse") |
| 65 |
defer span.End() |
| 66 |
|
| 67 |
// return an opaque token as default to not break current implementations |
| 68 |
tokenType := AccessTokenTypeBearer |
| 69 |
|
| 70 |
// the current CreateAccessToken function, esp. CreateJWT requires an implementation of an AccessTokenClient |
| 71 |
client := &jwtProfileClient{ |
| 72 |
id: tokenRequest.GetSubject(), |
| 73 |
} |
| 74 |
|
| 75 |
// by implementing the JWTProfileTokenStorage the storage can specify the AccessTokenType to be returned |
| 76 |
tokenStorage, ok := creator.Storage().(JWTProfileTokenStorage) |
| 77 |
if ok { |
| 78 |
var err error |
| 79 |
tokenType, err = tokenStorage.JWTProfileTokenType(ctx, tokenRequest) |
| 80 |
if err != nil { |
| 81 |
return nil, err |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
accessToken, _, validity, err := CreateAccessToken(ctx, tokenRequest, tokenType, creator, client, "") |
| 86 |
if err != nil { |
| 87 |
return nil, err |
| 88 |
} |
| 89 |
return &oidc.AccessTokenResponse{ |
| 90 |
AccessToken: accessToken, |
| 91 |
TokenType: oidc.BearerToken, |
| 92 |
ExpiresIn: uint64(validity.Seconds()), |
| 93 |
Scope: tokenRequest.GetScopes(), |
| 94 |
}, nil |
| 95 |
} |
| 96 |
|
| 97 |
// ParseJWTProfileRequest has been renamed to ParseJWTProfileGrantRequest |
| 98 |
// |
| 99 |
// deprecated: use ParseJWTProfileGrantRequest |
| 100 |
func ParseJWTProfileRequest(r *http.Request, decoder httphelper.Decoder) (*oidc.JWTProfileGrantRequest, error) { |
| 101 |
return ParseJWTProfileGrantRequest(r, decoder) |
| 102 |
} |
| 103 |
|
| 104 |
type jwtProfileClient struct { |
| 105 |
id string |
| 106 |
} |
| 107 |
|
| 108 |
func (j *jwtProfileClient) GetID() string { |
| 109 |
return j.id |
| 110 |
} |
| 111 |
|
| 112 |
func (j *jwtProfileClient) ClockSkew() time.Duration { |
| 113 |
return 0 |
| 114 |
} |
| 115 |
|
| 116 |
func (j *jwtProfileClient) RestrictAdditionalAccessTokenScopes() func(scopes []string) []string { |
| 117 |
return func(scopes []string) []string { |
| 118 |
return scopes |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
func (j *jwtProfileClient) GrantTypes() []oidc.GrantType { |
| 123 |
return []oidc.GrantType{ |
| 124 |
oidc.GrantTypeBearer, |
| 125 |
} |
| 126 |
} |