Skip to content

Commit 880c887

Browse files
committed
Author: Shraiysh Vaishay [email protected]
Add WebClientReactiveAuthorizationCodeTokenResponseClient.setWebClient Fixes spring-projectsgh-6182
1 parent 63f2b60 commit 880c887

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/endpoint/WebClientReactiveAuthorizationCodeTokenResponseClient.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponse;
2424
import org.springframework.web.reactive.function.BodyInserters;
2525
import org.springframework.web.reactive.function.client.WebClient;
26+
import org.springframework.util.Assert;
2627
import reactor.core.publisher.Mono;
2728

2829
import static org.springframework.security.oauth2.core.web.reactive.function.OAuth2BodyExtractors.oauth2AccessTokenResponse;
@@ -48,11 +49,18 @@ public class WebClientReactiveAuthorizationCodeTokenResponseClient implements Re
4849
private WebClient webClient = WebClient.builder()
4950
.build();
5051

52+
/**
53+
* @param webClient the webClient to set
54+
*/
55+
public void setWebClient(WebClient webClient) {
56+
Assert.notNull(webClient, "webClient cannot be null");
57+
this.webClient = webClient;
58+
}
59+
5160
@Override
5261
public Mono<OAuth2AccessTokenResponse> getTokenResponse(OAuth2AuthorizationCodeGrantRequest authorizationGrantRequest) {
5362
return Mono.defer(() -> {
5463
ClientRegistration clientRegistration = authorizationGrantRequest.getClientRegistration();
55-
5664
OAuth2AuthorizationExchange authorizationExchange = authorizationGrantRequest.getAuthorizationExchange();
5765
String tokenUri = clientRegistration.getProviderDetails().getTokenUri();
5866
BodyInserters.FormInserter<String> body = body(authorizationExchange);

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/WebClientReactiveAuthorizationCodeTokenResponseClientTests.java

+29
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@
3232
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationExchange;
3333
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
3434
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponse;
35+
import org.springframework.web.reactive.function.client.WebClient;
3536

3637
import java.time.Instant;
3738

3839
import static org.assertj.core.api.Assertions.assertThat;
3940
import static org.assertj.core.api.Assertions.assertThatThrownBy;
41+
import static org.mockito.Mockito.*;
4042

4143
/**
4244
* @author Rob Winch
@@ -259,4 +261,31 @@ private MockResponse jsonResponse(String json) {
259261
.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
260262
.setBody(json);
261263
}
264+
265+
@Test(expected=IllegalArgumentException.class)
266+
public void setWebClientNullThenIllegalArgumentException(){
267+
tokenResponseClient.setWebClient(null);
268+
}
269+
270+
@Test
271+
public void setCustomWebClientThenCustomWebClientIsUsed() {
272+
WebClient customClient = mock(WebClient.class);
273+
when(customClient.post()).thenReturn(WebClient.builder().build().post());
274+
275+
tokenResponseClient.setWebClient(customClient);
276+
277+
String accessTokenSuccessResponse = "{\n" +
278+
" \"access_token\": \"access-token-1234\",\n" +
279+
" \"token_type\": \"bearer\",\n" +
280+
" \"expires_in\": \"3600\",\n" +
281+
" \"scope\": \"openid profile\"\n" +
282+
"}\n";
283+
this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
284+
285+
this.clientRegistration.scope("openid", "profile", "email", "address");
286+
287+
OAuth2AccessTokenResponse response = this.tokenResponseClient.getTokenResponse(authorizationCodeGrantRequest()).block();
288+
289+
verify(customClient, atLeastOnce()).post();
290+
}
262291
}

0 commit comments

Comments
 (0)