Skip to content

Add possibility to customize JwkSource of NimbusJwtDecoder #17046

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 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ public static final class JwkSetUriJwtDecoderBuilder {

private Consumer<ConfigurableJWTProcessor<SecurityContext>> jwtProcessorCustomizer;

private Consumer<JWKSourceBuilder<SecurityContext>> jwkSourceBuilderCustomizer;

private JwkSetUriJwtDecoderBuilder(String jwkSetUri) {
Assert.hasText(jwkSetUri, "jwkSetUri cannot be empty");
this.jwkSetUri = (rest) -> jwkSetUri;
Expand Down Expand Up @@ -423,6 +425,20 @@ public JwkSetUriJwtDecoderBuilder jwtProcessorCustomizer(
return this;
}

/**
* Use the given {@link Consumer} to customize the {@link JWKSourceBuilder} before
* passing it to the build {@link NimbusJwtDecoder}.
* @param jwkSourceBuilderCustomizer the callback used to alter the builder
* @return a {@link JwkSetUriJwtDecoderBuilder} for further configurations
* @since 6.5
*/
public JwkSetUriJwtDecoderBuilder jwkSourceBuilderCustomizer(
Consumer<JWKSourceBuilder<SecurityContext>> jwkSourceBuilderCustomizer) {
Assert.notNull(jwkSourceBuilderCustomizer, "jwkSourceBuilderCustomizer cannot be null");
this.jwkSourceBuilderCustomizer = jwkSourceBuilderCustomizer;
return this;
}

JWSKeySelector<SecurityContext> jwsKeySelector(JWKSource<SecurityContext> jwkSource) {
if (this.signatureAlgorithms.isEmpty()) {
return new JWSVerificationKeySelector<>(this.defaultAlgorithms.apply(jwkSource), jwkSource);
Expand All @@ -437,11 +453,17 @@ JWSKeySelector<SecurityContext> jwsKeySelector(JWKSource<SecurityContext> jwkSou

JWKSource<SecurityContext> jwkSource() {
String jwkSetUri = this.jwkSetUri.apply(this.restOperations);
return JWKSourceBuilder.create(new SpringJWKSource<>(this.restOperations, this.cache, jwkSetUri))
JWKSourceBuilder<SecurityContext> jwkSourceBuilder = JWKSourceBuilder
.create(new SpringJWKSource<>(this.restOperations, this.cache, jwkSetUri))
.refreshAheadCache(false)
.rateLimited(false)
.cache(this.cache instanceof NoOpCache)
.build();
.cache(this.cache instanceof NoOpCache);

if (this.jwkSourceBuilderCustomizer != null) {
this.jwkSourceBuilderCustomizer.accept(jwkSourceBuilder);
}

return jwkSourceBuilder.build();
}

JWTProcessor<SecurityContext> processor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import javax.crypto.SecretKey;

Expand All @@ -43,6 +44,7 @@
import com.nimbusds.jose.crypto.MACSigner;
import com.nimbusds.jose.crypto.RSASSASigner;
import com.nimbusds.jose.jwk.source.JWKSource;
import com.nimbusds.jose.jwk.source.JWKSourceBuilder;
import com.nimbusds.jose.proc.BadJOSEException;
import com.nimbusds.jose.proc.DefaultJOSEObjectTypeVerifier;
import com.nimbusds.jose.proc.JWSKeySelector;
Expand Down Expand Up @@ -603,6 +605,13 @@ public void jwsKeySelectorWhenNoAlgorithmThenReturnsRS256Selector() {
assertThat(jwsVerificationKeySelector.isAllowed(JWSAlgorithm.RS256)).isTrue();
}

@Test
public void jwkSourceIsCustomizable() {
Consumer<JWKSourceBuilder<SecurityContext>> jwkSourceBuilderCustomizer = mock();
NimbusJwtDecoder.withJwkSetUri(JWK_SET_URI).jwkSourceBuilderCustomizer(jwkSourceBuilderCustomizer).build();
verify(jwkSourceBuilderCustomizer, times(1)).accept(any());
}

@Test
public void jwsKeySelectorWhenOneAlgorithmThenReturnsSingleSelector() {
JWKSource<SecurityContext> jwkSource = mock(JWKSource.class);
Expand Down
Loading