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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages in OidcIdTokenValidator #6349

Closed
wants to merge 1 commit into from

Conversation

raphaelDL
Copy link
Contributor

This commit ensures that error messages contain more specific
information regarding the reported error.

Fixes: gh-6323

@raphaelDL raphaelDL force-pushed the gh-6323 branch 2 times, most recently from 4ce2fea to 5f758ab Compare January 3, 2019 22:28
Copy link
Contributor

@jgrandja jgrandja left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @raphaelDL ! Please see my comments inline.

private final ClientRegistration clientRegistration;
private Collection<OAuth2Error> oAuth2Errors;
Copy link
Contributor

Choose a reason for hiding this comment

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

Having the errors as a member is not thread-safe. There may be 2 users authenticating at the same time that use the same client (client-id) and therefore would use the same JwtDecoder and this OidcIdTokenValidator for validation. See below comment for proposed change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I see, I missed that. I moved this inside the method

Instant issuedAt = idToken.getIssuedAt();
if (issuedAt == null) {
return invalidIdToken();
oAuth2Errors = validateRequiredClaims(idToken);
Copy link
Contributor

Choose a reason for hiding this comment

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

As per previous comment, instead of errors as a member, let's create a method variable Map<String, Object> invalidClaims. For all validation checks to follow, just populate the claim name and value.

Copy link
Contributor Author

@raphaelDL raphaelDL Jan 8, 2019

Choose a reason for hiding this comment

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

thank you Joe, just to be sure, in the first step, when verifying that all required claims are present, this map would be populated with, for example: invalidClaims.put(JwtClaimNames.SUB, subject) with the value being always null.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that is correct. So when OAuth2Error.description is built it would display as follows: sub (null), ...other invalid claims

return OAuth2TokenValidatorResult.success();
}

private static OAuth2TokenValidatorResult invalidIdToken() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's change the method signature to invalidIdToken(Map<String, Object> invalidClaims). This method would return one OAuth2Error with a generic message that lists the invalidClaims in comma-delimited format.

Copy link
Contributor Author

@raphaelDL raphaelDL Jan 8, 2019

Choose a reason for hiding this comment

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

Then, here I use the keys and join them with a comma to create an OAuth2Error like "Required claims were not found: issuer, iat".
What about the rest of the validations that are performed once we are sure that the required claims are present? Should we keep the message stating why the validation failed?

I submitted new changes... let me know what you think, as always I'm happy to make changes if needed.

Thank you

Copy link
Contributor

@jgrandja jgrandja Jan 8, 2019

Choose a reason for hiding this comment

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

Let's keep the message generic.

For example:

If one of the required claims is not present than fail-fast and return this message:

The ID Token contains invalid claims: sub (null), aud (null)

NOTE: Check all required claims before failing fast

If all required claims are present than proceed with the rest of the validation and if there is a failure than display the same generic message:

The ID Token contains invalid claims: aud (invalid-aud), azp (invalid-azp)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok Joe, I submitted new changes

@raphaelDL raphaelDL force-pushed the gh-6323 branch 2 times, most recently from 60a7384 to aa7d5dc Compare January 8, 2019 17:56
return OAuth2TokenValidatorResult.success();
}

private static OAuth2TokenValidatorResult invalidIdToken() {
return OAuth2TokenValidatorResult.failure(INVALID_ID_TOKEN_ERROR);
private static OAuth2Error invalidIdToken(Map<String, Object> invalidClaims) {
Copy link
Contributor

@jgrandja jgrandja Jan 8, 2019

Choose a reason for hiding this comment

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

Let's remove the methods requiredClaimError and invalidIdTokenError and build the OAuth2Error in this method. Please ensure the message text is same as mentioned in this comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, can you update the tests to assert on the OAuth2Error.description. And add a new that has at least 2 invalid claims present. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I've added the changes :)

@raphaelDL raphaelDL force-pushed the gh-6323 branch 2 times, most recently from cc1e323 to f4d408a Compare January 8, 2019 20:56
This commit ensures that error messages contain more specific
information regarding the reported error.

Fixes: spring-projectsgh-6323
@jgrandja jgrandja added type: enhancement A general enhancement in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) OIDC labels Jan 9, 2019
@jgrandja jgrandja added this to the 5.2.0.M1 milestone Jan 9, 2019
jgrandja added a commit that referenced this pull request Jan 9, 2019
@jgrandja
Copy link
Contributor

jgrandja commented Jan 9, 2019

Thanks for the PR @raphaelDL ! This is now in master.

I added a polish commit that fixed some extra spacing and formatting.
I also removed the test validateIdTokenWhenNoClaimsThenHasErrors since this is already being tested in JwtTests.constructorWhenClaimsIsEmptyThenThrowIllegalArgumentException.

Thank you so much for all your efforts as of late in contributing to Spring Security. Keep up the great work and we look forward to your next contribution! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants