Skip to content

Commit 25481ec

Browse files
committed
Add functionality to set custom web client in ReactiveOidcIdTokenDecoderFactory and that custom web client ultimately is used by NimbusReactiveJwtDecoder (spring-projectsgh-13274)
- resolve feedbacks - added a couple of unit tests
1 parent 382e701 commit 25481ec

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/oidc/authentication/ReactiveOidcIdTokenDecoderFactory.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,6 +59,7 @@
5959
* @author Joe Grandja
6060
* @author Rafael Dominguez
6161
* @author Mark Heckler
62+
* @author Ubaid ur Rehman
6263
* @since 5.2
6364
* @see ReactiveJwtDecoderFactory
6465
* @see ClientRegistration
@@ -90,7 +91,8 @@ public final class ReactiveOidcIdTokenDecoderFactory implements ReactiveJwtDecod
9091
private Function<ClientRegistration, Converter<Map<String, Object>, Map<String, Object>>> claimTypeConverterFactory = (
9192
clientRegistration) -> DEFAULT_CLAIM_TYPE_CONVERTER;
9293

93-
private WebClient webClient = WebClient.create();
94+
private Function<ClientRegistration, WebClient> webClientResolver = (clientRegistration) -> WebClient.create();
95+
9496
/**
9597
* Returns the default {@link Converter}'s used for type conversion of claim values
9698
* for an {@link OidcIdToken}.
@@ -166,8 +168,7 @@ private NimbusReactiveJwtDecoder buildDecoder(ClientRegistration clientRegistrat
166168
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
167169
}
168170
return NimbusReactiveJwtDecoder.withJwkSetUri(jwkSetUri).jwsAlgorithm((SignatureAlgorithm) jwsAlgorithm)
169-
.webClient(webClient)
170-
.build();
171+
.webClient(this.webClientResolver.apply(clientRegistration)).build();
171172
}
172173
if (jwsAlgorithm != null && MacAlgorithm.class.isAssignableFrom(jwsAlgorithm.getClass())) {
173174
// https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation
@@ -243,14 +244,16 @@ public void setClaimTypeConverterFactory(
243244
}
244245

245246
/**
246-
* Sets the custom web client that will be used in {@link NimbusReactiveJwtDecoder}.
247-
* The default webClient is created by {@code WebClient.create()}.
248-
* This is optional method if we need to set custom web client in {@link NimbusReactiveJwtDecoder}.
249-
*
250-
* @param webClient webclient
247+
* Sets the resolver that provides the {@link WebClient} that will be used in
248+
* {@link NimbusReactiveJwtDecoder}. The default resolver provides {@link WebClient}
249+
* that is created by {@code WebClient.create()}. This is optional method if we need
250+
* to set custom web client in {@link NimbusReactiveJwtDecoder}.
251+
* @param webClientResolver a function that will provide {@link WebClient} for a
252+
* {@link ClientRegistration}
251253
*/
252-
public void setWebClient(WebClient webClient) {
253-
this.webClient = webClient;
254+
public void setWebClientResolver(Function<ClientRegistration, WebClient> webClientResolver) {
255+
Assert.notNull(webClientResolver, "webClientResolver cannot be null");
256+
this.webClientResolver = webClientResolver;
254257
}
255258

256259
}

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/oidc/authentication/ReactiveOidcIdTokenDecoderFactoryTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
import org.springframework.security.oauth2.jose.jws.MacAlgorithm;
3535
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
3636
import org.springframework.security.oauth2.jwt.Jwt;
37+
import org.springframework.web.reactive.function.client.WebClient;
3738

3839
import static org.assertj.core.api.Assertions.assertThat;
3940
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -46,6 +47,7 @@
4647
/**
4748
* @author Joe Grandja
4849
* @author Rafael Dominguez
50+
* @author Ubaid ur Rehman
4951
* @since 5.2
5052
*/
5153
public class ReactiveOidcIdTokenDecoderFactoryTests {
@@ -94,6 +96,11 @@ public void setClaimTypeConverterFactoryWhenNullThenThrowIllegalArgumentExceptio
9496
.isThrownBy(() -> this.idTokenDecoderFactory.setClaimTypeConverterFactory(null));
9597
}
9698

99+
@Test
100+
public void setWebClientResolverWhenNullThenThrowIllegalArgumentException() {
101+
assertThatIllegalArgumentException().isThrownBy(() -> this.idTokenDecoderFactory.setWebClientResolver(null));
102+
}
103+
97104
@Test
98105
public void createDecoderWhenClientRegistrationNullThenThrowIllegalArgumentException() {
99106
assertThatIllegalArgumentException().isThrownBy(() -> this.idTokenDecoderFactory.createDecoder(null));
@@ -177,4 +184,15 @@ public void createDecoderWhenCustomClaimTypeConverterFactorySetThenApplied() {
177184
verify(customClaimTypeConverterFactory).apply(same(clientRegistration));
178185
}
179186

187+
@Test
188+
public void createDecoderWhenCustomWebClientResolverSetThenApplied() {
189+
WebClient webClient = mock(WebClient.class);
190+
Function<ClientRegistration, WebClient> customWebClientResolver = mock(Function.class);
191+
this.idTokenDecoderFactory.setWebClientResolver(customWebClientResolver);
192+
ClientRegistration clientRegistration = this.registration.build();
193+
given(customWebClientResolver.apply(clientRegistration)).willReturn(webClient);
194+
this.idTokenDecoderFactory.createDecoder(clientRegistration);
195+
verify(customWebClientResolver).apply(same(clientRegistration));
196+
}
197+
180198
}

0 commit comments

Comments
 (0)