-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathhandler.go
More file actions
326 lines (272 loc) · 10.6 KB
/
handler.go
File metadata and controls
326 lines (272 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright © 2025 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package rfc7523
import (
"context"
"strings"
"time"
"github.com/ory/fosite/handler/oauth2"
"github.com/go-jose/go-jose/v4"
"github.com/go-jose/go-jose/v4/jwt"
"github.com/ory/fosite"
fositeJWT "github.com/ory/fosite/token/jwt"
"github.com/ory/x/errorsx"
)
// #nosec:gosec G101 - False Positive
const grantTypeJWTBearer = "urn:ietf:params:oauth:grant-type:jwt-bearer"
type Handler struct {
Storage RFC7523KeyStorage
Config interface {
fosite.AccessTokenLifespanProvider
fosite.TokenURLProvider
fosite.GrantTypeJWTBearerCanSkipClientAuthProvider
fosite.GrantTypeJWTBearerIDOptionalProvider
fosite.GrantTypeJWTBearerIssuedDateOptionalProvider
fosite.GetJWTMaxDurationProvider
fosite.AudienceStrategyProvider
fosite.ScopeStrategyProvider
}
*oauth2.HandleHelper
}
var _ fosite.TokenEndpointHandler = (*Handler)(nil)
// HandleTokenEndpointRequest implements https://tools.ietf.org/html/rfc6749#section-4.1.3 (everything) and
// https://tools.ietf.org/html/rfc7523#section-2.1 (everything)
func (c *Handler) HandleTokenEndpointRequest(ctx context.Context, request fosite.AccessRequester) error {
if err := c.CheckRequest(ctx, request); err != nil {
return err
}
assertion := request.GetRequestForm().Get("assertion")
if assertion == "" {
return errorsx.WithStack(fosite.ErrInvalidRequest.WithHintf("The assertion request parameter must be set when using grant_type of '%s'.", grantTypeJWTBearer))
}
token, err := jwt.ParseSigned(assertion, fositeJWT.SupportedSignatureAlgorithms)
if err != nil {
return errorsx.WithStack(fosite.ErrInvalidGrant.
WithHint("Unable to parse JSON Web Token passed in \"assertion\" request parameter.").
WithWrap(err).WithDebug(err.Error()),
)
}
// Check fo required claims in token, so we can later find public key based on them.
if err := c.validateTokenPreRequisites(token); err != nil {
return err
}
key, err := c.findPublicKeyForToken(ctx, token)
if err != nil {
return err
}
claims := jwt.Claims{}
if err := token.Claims(key, &claims); err != nil {
return errorsx.WithStack(fosite.ErrInvalidGrant.
WithHint("Unable to verify the integrity of the 'assertion' value.").
WithWrap(err).WithDebug(err.Error()),
)
}
if err := c.validateTokenClaims(ctx, claims, key); err != nil {
return err
}
scopes, err := c.Storage.GetPublicKeyScopes(ctx, claims.Issuer, claims.Subject, key.KeyID)
if err != nil {
return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
}
for _, scope := range request.GetRequestedScopes() {
if !c.Config.GetScopeStrategy(ctx)(scopes, scope) {
return errorsx.WithStack(fosite.ErrInvalidScope.WithHintf("The public key registered for issuer \"%s\" and subject \"%s\" is not allowed to request scope \"%s\".", claims.Issuer, claims.Subject, scope))
}
}
if claims.ID != "" {
if err := c.Storage.MarkJWTUsedForTime(ctx, claims.ID, claims.Expiry.Time()); err != nil {
return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
}
}
for _, scope := range request.GetRequestedScopes() {
request.GrantScope(scope)
}
for _, audience := range claims.Audience {
request.GrantAudience(audience)
}
session, err := c.getSessionFromRequest(request)
if err != nil {
return err
}
atLifespan := fosite.GetEffectiveLifespan(request.GetClient(), fosite.GrantTypeJWTBearer, fosite.AccessToken, c.HandleHelper.Config.GetAccessTokenLifespan(ctx))
session.SetExpiresAt(fosite.AccessToken, time.Now().UTC().Add(atLifespan).Round(time.Second))
session.SetSubject(claims.Subject)
return nil
}
func (c *Handler) PopulateTokenEndpointResponse(ctx context.Context, request fosite.AccessRequester, response fosite.AccessResponder) error {
if err := c.CheckRequest(ctx, request); err != nil {
return err
}
atLifespan := fosite.GetEffectiveLifespan(request.GetClient(), fosite.GrantTypeJWTBearer, fosite.AccessToken, c.Config.GetAccessTokenLifespan(ctx))
_, err := c.IssueAccessToken(ctx, atLifespan, request, response)
return err
}
func (c *Handler) CanSkipClientAuth(ctx context.Context, requester fosite.AccessRequester) bool {
return c.Config.GetGrantTypeJWTBearerCanSkipClientAuth(ctx)
}
func (c *Handler) CanHandleTokenEndpointRequest(ctx context.Context, requester fosite.AccessRequester) bool {
// grant_type REQUIRED.
// Value MUST be set to "urn:ietf:params:oauth:grant-type:jwt-bearer"
return requester.GetGrantTypes().ExactOne(grantTypeJWTBearer)
}
func (c *Handler) CheckRequest(ctx context.Context, request fosite.AccessRequester) error {
if !c.CanHandleTokenEndpointRequest(ctx, request) {
return errorsx.WithStack(fosite.ErrUnknownRequest)
}
// Client Authentication is optional:
//
// Authentication of the client is optional, as described in
// Section 3.2.1 of OAuth 2.0 [RFC6749] and consequently, the
// "client_id" is only needed when a form of client authentication that
// relies on the parameter is used.
// if client is authenticated, check grant types
if !c.CanSkipClientAuth(ctx, request) && !request.GetClient().GetGrantTypes().Has(grantTypeJWTBearer) {
return errorsx.WithStack(fosite.ErrUnauthorizedClient.WithHintf("The OAuth 2.0 Client is not allowed to use authorization grant \"%s\".", grantTypeJWTBearer))
}
return nil
}
func (c *Handler) validateTokenPreRequisites(token *jwt.JSONWebToken) error {
unverifiedClaims := jwt.Claims{}
if err := token.UnsafeClaimsWithoutVerification(&unverifiedClaims); err != nil {
return errorsx.WithStack(fosite.ErrInvalidGrant.
WithHint("Looks like there are no claims in JWT in \"assertion\" request parameter.").
WithWrap(err).WithDebug(err.Error()),
)
}
if unverifiedClaims.Issuer == "" {
return errorsx.WithStack(fosite.ErrInvalidGrant.
WithHint("The JWT in \"assertion\" request parameter MUST contain an \"iss\" (issuer) claim."),
)
}
if unverifiedClaims.Subject == "" {
return errorsx.WithStack(fosite.ErrInvalidGrant.
WithHint("The JWT in \"assertion\" request parameter MUST contain a \"sub\" (subject) claim."),
)
}
return nil
}
func (c *Handler) findPublicKeyForToken(ctx context.Context, token *jwt.JSONWebToken) (*jose.JSONWebKey, error) {
unverifiedClaims := jwt.Claims{}
if err := token.UnsafeClaimsWithoutVerification(&unverifiedClaims); err != nil {
return nil, errorsx.WithStack(fosite.ErrInvalidRequest.WithWrap(err).WithDebug(err.Error()))
}
var keyID string
for _, header := range token.Headers {
if header.KeyID != "" {
keyID = header.KeyID
break
}
}
keyNotFoundErr := fosite.ErrInvalidGrant.WithHintf(
"No public JWK was registered for issuer \"%s\" and subject \"%s\", and public key is required to check signature of JWT in \"assertion\" request parameter.",
unverifiedClaims.Issuer,
unverifiedClaims.Subject,
)
if keyID != "" {
key, err := c.Storage.GetPublicKey(ctx, unverifiedClaims.Issuer, unverifiedClaims.Subject, keyID)
if err != nil {
return nil, errorsx.WithStack(keyNotFoundErr.WithWrap(err).WithDebug(err.Error()))
}
return key, nil
}
keys, err := c.Storage.GetPublicKeys(ctx, unverifiedClaims.Issuer, unverifiedClaims.Subject)
if err != nil {
return nil, errorsx.WithStack(keyNotFoundErr.WithWrap(err).WithDebug(err.Error()))
}
claims := jwt.Claims{}
for _, key := range keys.Keys {
err := token.Claims(key, &claims)
if err == nil {
return &key, nil
}
}
return nil, errorsx.WithStack(keyNotFoundErr)
}
func (c *Handler) validateTokenClaims(ctx context.Context, claims jwt.Claims, key *jose.JSONWebKey) error {
if len(claims.Audience) == 0 {
return errorsx.WithStack(fosite.ErrInvalidGrant.
WithHint("The JWT in \"assertion\" request parameter MUST contain an \"aud\" (audience) claim."),
)
}
if !audienceMatchesTokenURLs(claims, c.Config.GetTokenURLs(ctx)) {
return errorsx.WithStack(fosite.ErrInvalidGrant.
WithHintf(
`The JWT in "assertion" request parameter MUST contain an "aud" (audience) claim containing a value "%s" that identifies the authorization server as an intended audience.`,
strings.Join(c.Config.GetTokenURLs(ctx), `" or "`)))
}
if claims.Expiry == nil {
return errorsx.WithStack(fosite.ErrInvalidGrant.
WithHint("The JWT in \"assertion\" request parameter MUST contain an \"exp\" (expiration time) claim."),
)
}
if claims.Expiry.Time().Before(time.Now()) {
return errorsx.WithStack(fosite.ErrInvalidGrant.
WithHint("The JWT in \"assertion\" request parameter expired."),
)
}
if claims.NotBefore != nil && !claims.NotBefore.Time().Before(time.Now()) {
return errorsx.WithStack(fosite.ErrInvalidGrant.
WithHintf(
"The JWT in \"assertion\" request parameter contains an \"nbf\" (not before) claim, that identifies the time '%s' before which the token MUST NOT be accepted.",
claims.NotBefore.Time().Format(time.RFC3339),
),
)
}
if !c.Config.GetGrantTypeJWTBearerIssuedDateOptional(ctx) && claims.IssuedAt == nil {
return errorsx.WithStack(fosite.ErrInvalidGrant.
WithHint("The JWT in \"assertion\" request parameter MUST contain an \"iat\" (issued at) claim."),
)
}
var issuedDate time.Time
if claims.IssuedAt != nil {
issuedDate = claims.IssuedAt.Time()
} else {
issuedDate = time.Now()
}
if claims.Expiry.Time().Sub(issuedDate) > c.Config.GetJWTMaxDuration(ctx) {
return errorsx.WithStack(fosite.ErrInvalidGrant.
WithHintf(
"The JWT in \"assertion\" request parameter contains an \"exp\" (expiration time) claim with value \"%s\" that is unreasonably far in the future, considering token issued at \"%s\".",
claims.Expiry.Time().Format(time.RFC3339),
issuedDate.Format(time.RFC3339),
),
)
}
if !c.Config.GetGrantTypeJWTBearerIDOptional(ctx) && claims.ID == "" {
return errorsx.WithStack(fosite.ErrInvalidGrant.
WithHint("The JWT in \"assertion\" request parameter MUST contain an \"jti\" (JWT ID) claim."),
)
}
if claims.ID != "" {
used, err := c.Storage.IsJWTUsed(ctx, claims.ID)
if err != nil {
return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
}
if used {
return errorsx.WithStack(fosite.ErrJTIKnown)
}
}
return nil
}
func audienceMatchesTokenURLs(claims jwt.Claims, tokenURLs []string) bool {
for _, tokenURL := range tokenURLs {
if claims.Audience.Contains(tokenURL) {
return true
}
}
return false
}
type extendedSession interface {
Session
fosite.Session
}
func (c *Handler) getSessionFromRequest(requester fosite.AccessRequester) (extendedSession, error) {
session := requester.GetSession()
if jwtSession, ok := session.(extendedSession); !ok {
return nil, errorsx.WithStack(
fosite.ErrServerError.WithHintf("Session must be of type *rfc7523.Session but got type: %T", session),
)
} else {
return jwtSession, nil
}
}