Skip to content

Provided uncached way to use (Reactive)OidcIdTokenDecoderFactory #16647

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

iigolovko
Copy link

@iigolovko iigolovko commented Feb 25, 2025

Hello.

Sorry for ignoring issue creating.

If ClientRegistration is managed by db, it's unreal to update jwsAlgorithm or jwksUri without restarting the app.

So I added possibility not to use ConcurrentHashMap on decoder creation to build new one everytime. It will be useful in some multi-integration cases.

Fixes gh-12816

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 25, 2025
@franticticktick
Copy link
Contributor

Hey @iigolovko thanks for your valuable suggestion, but it would be better to create a ticket for this feature. It is quite possible that as a result of discussion we will find another solution if necessary.

@mipo256
Copy link

mipo256 commented Apr 11, 2025

fixes #16655

@orchestr7
Copy link

Hey @iigolovko thanks for your valuable suggestion, but it would be better to create a ticket for this feature. It is quite possible that as a result of discussion we will find another solution if necessary.

Looks like #16655 affects a lot. Any chance that can be reviewed and handled somehow? @franticticktick

@@ -78,7 +79,7 @@ public final class OidcIdTokenDecoderFactory implements JwtDecoderFactory<Client

private static final ClaimTypeConverter DEFAULT_CLAIM_TYPE_CONVERTER = createDefaultClaimTypeConverter();

private final Map<String, JwtDecoder> jwtDecoders = new ConcurrentHashMap<>();
private final Map<String, JwtDecoder> jwtDecoders;
Copy link

Choose a reason for hiding this comment

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

My 2 cents.

I think that caching in general is a functionality that needs to be extracted from here. Maybe the core maintainers can consider introducing the CachingOidcIdTokenDecoderFactory that extend OidcIdTokenDecoderFactory and adds caching functionality. Or the alternative solution via decorator/caching delegate is better.

Copy link
Contributor

Choose a reason for hiding this comment

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

@mipo256 I agree that caching could be removed all together as the simplest solution and one that makes sense since caching is a separate concern that the application can implement using a delegation based strategy.

I'd like to hear feedback from others before we decide to remove the caching.

@unb @iigolovko @franticticktick @orchestr7

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @mipo256 , in this case it is better to extract caching to a separate implementation. I don't really like the idea of ​​moving this logic to a delegation based strategy. It can complicate the API and make it less obvious than an explicit class with caching.

Copy link
Author

Choose a reason for hiding this comment

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

I don't think caching is necessary at all. But if you decide to keep it, we need to choose a default factory implementation. For me, the non-caching option better meets end user expectations.

Copy link

Choose a reason for hiding this comment

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

For me, the non-caching option better meets end user expectations.

I can see the motivation here. However, I think in order to stay backward compatible at least, and at most in order to omit the construction of NimbusJwtDecoder (which, at least from the first rough touch, seems to be quite expensive), the default behavior, IMVHO, should remain the cache usage.

But there must be an option to configure it for certain use cases nevertheless

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks all for the feedback.

I'm leaning towards removing the caching behaviour. This would be a breaking change so it would go into 7.0.

Users who want the caching behaviour could easily restore it using a delegation-based strategy, for example:

public class CachingOidcIdTokenDecoderFactory implements JwtDecoderFactory<ClientRegistration> {
	private final Map<String, JwtDecoder> jwtDecoders = new ConcurrentHashMap<>();
	
	private final OidcIdTokenDecoderFactory delegate = new OidcIdTokenDecoderFactory();

	@Override
	public JwtDecoder createDecoder(ClientRegistration clientRegistration) {
		Assert.notNull(clientRegistration, "clientRegistration cannot be null");
		return this.jwtDecoders.computeIfAbsent(clientRegistration.getRegistrationId(), 
				(key) -> this.delegate.createDecoder(clientRegistration));
	}

}

Thoughts?

Copy link
Author

Choose a reason for hiding this comment

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

@jgrandja , I removed it. Please check my PR again.

@jgrandja jgrandja added type: enhancement A general enhancement in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 11, 2025
@jgrandja jgrandja self-assigned this Jun 19, 2025
Signed-off-by: iigolovko <iigolovko@ginc-it.ru>
…ecoderFactory and ReactiveOidcIdTokenDecoderFactory

Signed-off-by: iigolovko <iigolovko@ginc-it.ru>
@iigolovko iigolovko force-pushed the feature/dynamic-oidc-token-decoder-factory branch from 0e6b512 to 6973519 Compare August 12, 2025 16:39
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.

OidcIdTokenDecoderFactory caching of ClientRegistration fails to decode tokens if clientId changes
6 participants