Skip to content

Allow configurable JwtDecoder for ID Token verification #5751

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

Merged
merged 2 commits into from
Dec 21, 2018

Conversation

jgrandja
Copy link
Contributor

This PR resolves gh-5717

@rwinch There is an issue with the changes in this PR.

Given the following that allows for setting a custom JwtDecoder factory:

http
  .oauth2Login()
    .tokenEndpoint()
      .jwtDecoderFactory(Function<ClientRegistration, JwtDecoder> jwtDecoderFactory)
      ...         

This introduces the oauth2-jose dependency for oauth2Login which is not ideal.
I'm not sure what we can do at this point to not include oauth2-jose as a required dependency?
Maybe you have an idea?

@jgrandja jgrandja requested a review from rwinch August 28, 2018 17:09
Copy link
Member

@rwinch rwinch left a comment

Choose a reason for hiding this comment

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

I agree that it isn't ideal to have the jose dependency here. Perhaps the better approach is allowing the user to configure the AuthenticationProvider instead.

@Override
public JwtDecoder apply(ClientRegistration clientRegistration) {
JwtDecoder jwtDecoder = this.jwtDecoders.get(clientRegistration.getRegistrationId());
if (jwtDecoder == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you are using ConcurrentHashMap, is it better to use computeIfAbsent instead of get?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for that catch @jzheaux! Yes, we should use computeIfAbsent. I'll apply that update.

@jgrandja
Copy link
Contributor Author

jgrandja commented Sep 7, 2018

@rwinch

Perhaps the better approach is allowing the user to configure the AuthenticationProvider instead

Sure this is another option to consider. However, given where we are with the release, this won't make it into 5.1. We'll come back to this after 5.1 is out.

@jgrandja jgrandja added in: config An issue in spring-security-config type: enhancement A general enhancement in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) OIDC labels Dec 12, 2018
@jgrandja jgrandja added this to the 5.2.0.M1 milestone Dec 12, 2018
@jgrandja jgrandja force-pushed the gh-5717-decoder-config branch from 7aea988 to ef77930 Compare December 12, 2018 22:22
@jgrandja
Copy link
Contributor Author

@rwinch I decided to go with the @Bean approach to allow users to configure a custom OidcIdToken signature verifier. The @Bean is expected to be of type Function<ClientRegistration, JwtDecoder>, as per our previous discussions.

I'm wondering if we should consider declaring a new type for this? It's just that Function is quite generic with the apply and maybe something like this would be more descriptive for the user:

public interface IdTokenDecoders {
	JwtDecoder getDecoder(ClientRegistration clientRegistration);
}

@jgrandja jgrandja force-pushed the gh-5717-decoder-config branch from ef77930 to bf833cc Compare December 12, 2018 22:36
@rwinch
Copy link
Member

rwinch commented Dec 14, 2018

I think you are right we need a custom interface. However, I think we should consider a different name. Perhaps JwtDecoderFactory?

@jgrandja
Copy link
Contributor Author

I feel JwtDecoderFactory is too general given this factory is really meant for client only. The name JwtDecoderFactory can be applied to client and resource server whereas IdTokenDecoderFactory is meant for client as far as how a user may interpret the name.

The reason I'm thinking IdToken should be in the name is that it's only meant for ID Token verification. I'm not anticipating the use of a JwtDecoder for client in any other areas - at least at this moment.

I think IdTokenDecoderFactory is fine but I also like IdTokenDecoders as well.

@rwinch
Copy link
Member

rwinch commented Dec 17, 2018

I see your point about it being client specific, but I still don't like the name. The Id Token is not part of the input or the output of the only method on the interface. Do we anticipate needing something similar for the resource server? If so, perhaps we make the input generic and stick with something like JwtDecoderFactory?

@jgrandja
Copy link
Contributor Author

Do we anticipate needing something similar for the resource server?

Very likely, probably with the multi-tenancy feature we still need to get to

we make the input generic and stick with something like JwtDecoderFactory

I like this option and provides us the flexibility. I'll go with this

@jgrandja jgrandja force-pushed the gh-5717-decoder-config branch from bf833cc to a08c12b Compare December 17, 2018 18:41
@jgrandja
Copy link
Contributor Author

@rwinch I added the new JwtDecoderFactory and ReactiveJwtDecoderFactory.

@@ -32,6 +32,7 @@
return OAuth2AuthorizationRequest.authorizationCode()
.authorizationUri("https://example.com/login/oauth/authorize")
.clientId(clientId)
.scope("openid")
Copy link
Member

Choose a reason for hiding this comment

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

Did you verify this isn't going to break any existing test assumptions? We probably shouldn't modify this as we don't know what impact this is having on existing tests. It may invalidate a test that has different expectations of the scopes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did validate that this does not trigger any failed tests. However, it's probably safer to avoid this change and keep the changes isolated to the test that requires the openid scope. I'll make this 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.

@rwinch I pushed the update for this

@rwinch rwinch self-assigned this Dec 21, 2018
@rwinch rwinch merged commit 12f3208 into spring-projects:master Dec 21, 2018
@jgrandja jgrandja deleted the gh-5717-decoder-config branch December 21, 2018 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: config An issue in spring-security-config 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.

Allow configurable JwtDecoder for IdToken verification
3 participants