Skip to content

Commit 89b07b0

Browse files
committed
WebClientReactiveClientCredentialsTokenResponseClient.getTokenResponse expects 2xx http status code
This ensures that token response is only extracted when ClientResponse has a successful status Fixes: gh-6089
1 parent 75a2c2b commit 89b07b0

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.util.StringUtils;
2626
import org.springframework.web.reactive.function.BodyInserters;
2727
import org.springframework.web.reactive.function.client.WebClient;
28+
import org.springframework.web.reactive.function.client.WebClientResponseException;
2829
import reactor.core.publisher.Mono;
2930

3031
import java.util.Set;
@@ -64,7 +65,17 @@ public Mono<OAuth2AccessTokenResponse> getTokenResponse(OAuth2ClientCredentialsG
6465
.headers(headers(clientRegistration))
6566
.body(body)
6667
.exchange()
67-
.flatMap(response -> response.body(oauth2AccessTokenResponse()))
68+
.flatMap(response ->{
69+
if (!response.statusCode().is2xxSuccessful()){
70+
// extract the contents of this into a method named oauth2AccessTokenResponse but has an argument for the response
71+
throw WebClientResponseException.create(response.rawStatusCode(),
72+
"Cannot get token, expected 2xx HTTP Status code",
73+
null,
74+
null,
75+
null
76+
);
77+
}
78+
return response.body(oauth2AccessTokenResponse()); })
6879
.map(response -> {
6980
if (response.getAccessToken().getScopes().isEmpty()) {
7081
response = OAuth2AccessTokenResponse.withResponse(response)

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

+18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
2929
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
3030
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
31+
import org.springframework.web.reactive.function.client.WebClientResponseException;
3132

3233
import static org.assertj.core.api.Assertions.*;
3334

@@ -116,6 +117,23 @@ public void getTokenResponseWhenNoScopeThenClientRegistrationScopesDefaulted() {
116117
assertThat(response.getAccessToken().getScopes()).isEqualTo(registration.getScopes());
117118
}
118119

120+
@Test(expected = WebClientResponseException.class)
121+
// gh-6089
122+
public void getTokenResponseWhenInvalidResponse() throws WebClientResponseException {
123+
ClientRegistration registration = this.clientRegistration.build();
124+
enqueueUnexpectedResponse();
125+
126+
OAuth2ClientCredentialsGrantRequest request = new OAuth2ClientCredentialsGrantRequest(registration);
127+
128+
OAuth2AccessTokenResponse response = this.client.getTokenResponse(request).block();
129+
}
130+
131+
private void enqueueUnexpectedResponse(){
132+
MockResponse response = new MockResponse()
133+
.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
134+
.setResponseCode(301);
135+
this.server.enqueue(response);
136+
}
119137

120138
private void enqueueJson(String body) {
121139
MockResponse response = new MockResponse()

0 commit comments

Comments
 (0)