-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace *jwt.validateClaimsWithLeeway with custom validation func #176
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,12 +73,12 @@ func TestHandler(t *testing.T) { | |
t.Fatal(err) | ||
} | ||
|
||
token := buildJWTForTesting(t, jwk, testServer.URL, test.subject, []string{}) | ||
token := buildJWTForTesting(t, jwk, testServer.URL, test.subject, []string{"my-audience"}) | ||
req.Header.Set("Authorization", "Bearer "+token) | ||
|
||
rr := httptest.NewRecorder() | ||
|
||
mainHandler := setupHandler(testServer.URL, []string{}) | ||
mainHandler := setupHandler(testServer.URL, []string{"my-audience"}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was incorrectly set to validate no audience. |
||
mainHandler.ServeHTTP(rr, req) | ||
|
||
if want, got := test.wantStatusCode, rr.Code; want != got { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,62 +99,114 @@ func (v *Validator) ValidateToken(ctx context.Context, tokenString string) (inte | |
return nil, fmt.Errorf("could not parse the token: %w", err) | ||
} | ||
|
||
if string(v.signatureAlgorithm) != token.Headers[0].Algorithm { | ||
return nil, fmt.Errorf( | ||
"expected %q signing algorithm but token specified %q", | ||
v.signatureAlgorithm, | ||
token.Headers[0].Algorithm, | ||
) | ||
if err = validateSigningMethod(string(v.signatureAlgorithm), token.Headers[0].Algorithm); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trying to refactor this func so that the steps read better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👏🏼 |
||
return nil, fmt.Errorf("signing method is invalid: %w", err) | ||
} | ||
|
||
key, err := v.keyFunc(ctx) | ||
registeredClaims, customClaims, err := v.deserializeClaims(ctx, token) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting the keys from the key func: %w", err) | ||
} | ||
|
||
claimDest := []interface{}{&jwt.Claims{}} | ||
if v.customClaims != nil && v.customClaims() != nil { | ||
claimDest = append(claimDest, v.customClaims()) | ||
return nil, fmt.Errorf("failed to deserialize token claims: %w", err) | ||
} | ||
|
||
if err = token.Claims(key, claimDest...); err != nil { | ||
return nil, fmt.Errorf("could not get token claims: %w", err) | ||
if err = validateClaimsWithLeeway(registeredClaims, v.expectedClaims, v.allowedClockSkew); err != nil { | ||
return nil, fmt.Errorf("expected claims not validated: %w", err) | ||
} | ||
|
||
registeredClaims := *claimDest[0].(*jwt.Claims) | ||
expectedClaims := v.expectedClaims | ||
expectedClaims.Time = time.Now() | ||
if err = registeredClaims.ValidateWithLeeway(expectedClaims, v.allowedClockSkew); err != nil { | ||
return nil, fmt.Errorf("expected claims not validated: %w", err) | ||
if customClaims != nil { | ||
if err = customClaims.Validate(ctx); err != nil { | ||
return nil, fmt.Errorf("custom claims not validated: %w", err) | ||
} | ||
} | ||
|
||
validatedClaims := &ValidatedClaims{ | ||
RegisteredClaims: RegisteredClaims{ | ||
Issuer: registeredClaims.Issuer, | ||
Subject: registeredClaims.Subject, | ||
Audience: registeredClaims.Audience, | ||
ID: registeredClaims.ID, | ||
Issuer: registeredClaims.Issuer, | ||
Subject: registeredClaims.Subject, | ||
Audience: registeredClaims.Audience, | ||
ID: registeredClaims.ID, | ||
Expiry: numericDateToUnixTime(registeredClaims.Expiry), | ||
NotBefore: numericDateToUnixTime(registeredClaims.NotBefore), | ||
IssuedAt: numericDateToUnixTime(registeredClaims.IssuedAt), | ||
}, | ||
CustomClaims: customClaims, | ||
} | ||
|
||
if registeredClaims.Expiry != nil { | ||
validatedClaims.RegisteredClaims.Expiry = registeredClaims.Expiry.Time().Unix() | ||
return validatedClaims, nil | ||
} | ||
|
||
func validateClaimsWithLeeway(actualClaims jwt.Claims, expected jwt.Expected, leeway time.Duration) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this also include the sub and jti validation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although that original func does include them, we were never actually using them for the validation. We're only setting as expected the audience, issuer and the time based claims. Re: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that in other SDKs we do validate the sub claim: https://github.com/auth0/Auth0.swift/blob/master/Auth0/IDTokenValidator.swift#L56 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can add that validation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (if applicable, of course). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately we can't introduce that right now without a breaking change. The |
||
expectedClaims := expected | ||
expectedClaims.Time = time.Now() | ||
|
||
if actualClaims.Issuer != expectedClaims.Issuer { | ||
return jwt.ErrInvalidIssuer | ||
} | ||
|
||
if registeredClaims.NotBefore != nil { | ||
validatedClaims.RegisteredClaims.NotBefore = registeredClaims.NotBefore.Time().Unix() | ||
foundAudience := false | ||
for _, value := range expectedClaims.Audience { | ||
if actualClaims.Audience.Contains(value) { | ||
foundAudience = true | ||
break | ||
} | ||
} | ||
if !foundAudience { | ||
return jwt.ErrInvalidAudience | ||
Comment on lines
+145
to
+153
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is mainly the fix for the multiple audiences described in #148 |
||
} | ||
|
||
if registeredClaims.IssuedAt != nil { | ||
validatedClaims.RegisteredClaims.IssuedAt = registeredClaims.IssuedAt.Time().Unix() | ||
if actualClaims.NotBefore != nil && expectedClaims.Time.Add(leeway).Before(actualClaims.NotBefore.Time()) { | ||
return jwt.ErrNotValidYet | ||
} | ||
|
||
if v.customClaims != nil && v.customClaims() != nil { | ||
validatedClaims.CustomClaims = claimDest[1].(CustomClaims) | ||
if err = validatedClaims.CustomClaims.Validate(ctx); err != nil { | ||
return nil, fmt.Errorf("custom claims not validated: %w", err) | ||
} | ||
if actualClaims.Expiry != nil && expectedClaims.Time.Add(-leeway).After(actualClaims.Expiry.Time()) { | ||
return jwt.ErrExpired | ||
} | ||
|
||
return validatedClaims, nil | ||
if actualClaims.IssuedAt != nil && expectedClaims.Time.Add(leeway).Before(actualClaims.IssuedAt.Time()) { | ||
return jwt.ErrIssuedInTheFuture | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validateSigningMethod(validAlg, tokenAlg string) error { | ||
if validAlg != tokenAlg { | ||
return fmt.Errorf("expected %q signing algorithm but token specified %q", validAlg, tokenAlg) | ||
} | ||
return nil | ||
} | ||
|
||
func (v *Validator) customClaimsExist() bool { | ||
return v.customClaims != nil && v.customClaims() != nil | ||
} | ||
|
||
func (v *Validator) deserializeClaims(ctx context.Context, token *jwt.JSONWebToken) (jwt.Claims, CustomClaims, error) { | ||
key, err := v.keyFunc(ctx) | ||
if err != nil { | ||
return jwt.Claims{}, nil, fmt.Errorf("error getting the keys from the key func: %w", err) | ||
} | ||
|
||
claims := []interface{}{&jwt.Claims{}} | ||
if v.customClaimsExist() { | ||
claims = append(claims, v.customClaims()) | ||
} | ||
|
||
if err = token.Claims(key, claims...); err != nil { | ||
return jwt.Claims{}, nil, fmt.Errorf("could not get token claims: %w", err) | ||
} | ||
|
||
registeredClaims := *claims[0].(*jwt.Claims) | ||
|
||
var customClaims CustomClaims | ||
if len(claims) > 1 { | ||
customClaims = claims[1].(CustomClaims) | ||
} | ||
|
||
return registeredClaims, customClaims, nil | ||
} | ||
|
||
func numericDateToUnixTime(date *jwt.NumericDate) int64 { | ||
if date != nil { | ||
return date.Time().Unix() | ||
} | ||
return 0 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick toggle to not fail the CI build if we fail to upload codecov.