Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace *jwt.validateClaimsWithLeeway with custom validation func #176

Merged
merged 3 commits into from
Nov 2, 2022

Conversation

sergiught
Copy link
Contributor

馃摑 Checklist

  • All new/changed/fixed functionality is covered by tests (or N/A)
  • I have added documentation for all new/changed functionality (or N/A)

馃敡 Changes

We are replacing the *jwt.validateClaimsWithLeeway func with our own logic to fix the issue with multiple audiences within the 3rd party pkg as it seems we are not getting a new release. This was preferred right now to swapping the jwt library used under the hood as that would be a bigger undertaking that we'll defer for later, perhaps a v3.

馃摎 References

馃敩 Testing

@sergiught sergiught force-pushed the pathc/issue#148-multiple-audience branch from 33c3d1d to 5cd381b Compare October 31, 2022 14:54
req.Header.Set("Authorization", "Bearer "+token)

rr := httptest.NewRecorder()

mainHandler := setupHandler(testServer.URL, []string{})
mainHandler := setupHandler(testServer.URL, []string{"my-audience"})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was incorrectly set to validate no audience.

@@ -26,5 +26,5 @@ jobs:
uses: codecov/codecov-action@v3
with:
files: coverage.out
fail_ci_if_error: true
fail_ci_if_error: false
Copy link
Contributor Author

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.

v.signatureAlgorithm,
token.Headers[0].Algorithm,
)
if err = validateSigningMethod(string(v.signatureAlgorithm), token.Headers[0].Algorithm); err != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to refactor this func so that the steps read better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

馃憦馃徏

Comment on lines +145 to +153
foundAudience := false
for _, value := range expectedClaims.Audience {
if actualClaims.Audience.Contains(value) {
foundAudience = true
break
}
}
if !foundAudience {
return jwt.ErrInvalidAudience
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mainly the fix for the multiple audiences described in #148

return validatedClaims, nil
}

func validateClaimsWithLeeway(actualClaims jwt.Claims, expected jwt.Expected, leeway time.Duration) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@Widcket Widcket Oct 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also include the sub and jti validation?

Copy link
Contributor Author

@sergiught sergiught Nov 1, 2022

Choose a reason for hiding this comment

The 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:

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add that validation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(if applicable, of course).

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 are a private internal property inside the validator and we set it with the passed in issuer and audience when constructing the validator. We'll have to change this func https://github.com/auth0/go-jwt-middleware/blob/master/validator/validator.go#L59 to allow for the subject as well. I'll note this down for a potential upcoming v3 perhaps, wdyt?

@sergiught sergiught marked this pull request as ready for review October 31, 2022 15:05
@sergiught sergiught requested a review from a team as a code owner October 31, 2022 15:05
Copy link
Contributor

@Widcket Widcket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just left a comment regarding the sub and jti claims.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants