Skip to content

Commit 0f7360e

Browse files
BenjaminFaaljgrandja
authored andcommitted
Make user info response status check error only
Closes gh-9336
1 parent d722ac7 commit 0f7360e

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/userinfo/DefaultReactiveOAuth2UserService.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.security.oauth2.client.userinfo;
1817

1918

@@ -22,6 +21,11 @@
2221
import java.util.Map;
2322
import java.util.Set;
2423

24+
import com.nimbusds.oauth2.sdk.ErrorObject;
25+
import com.nimbusds.openid.connect.sdk.UserInfoErrorResponse;
26+
import net.minidev.json.JSONObject;
27+
import reactor.core.publisher.Mono;
28+
2529
import org.springframework.core.ParameterizedTypeReference;
2630
import org.springframework.http.HttpHeaders;
2731
import org.springframework.http.HttpStatus;
@@ -41,12 +45,6 @@
4145
import org.springframework.web.reactive.function.client.ClientResponse;
4246
import org.springframework.web.reactive.function.client.WebClient;
4347

44-
import com.nimbusds.oauth2.sdk.ErrorObject;
45-
import com.nimbusds.openid.connect.sdk.UserInfoErrorResponse;
46-
47-
import net.minidev.json.JSONObject;
48-
import reactor.core.publisher.Mono;
49-
5048
/**
5149
* An implementation of an {@link ReactiveOAuth2UserService} that supports standard OAuth 2.0 Provider's.
5250
* <p>
@@ -119,7 +117,7 @@ public Mono<OAuth2User> loadUser(OAuth2UserRequest userRequest)
119117
}
120118
Mono<Map<String, Object>> userAttributes = requestHeadersSpec
121119
.retrieve()
122-
.onStatus(s -> s != HttpStatus.OK, response -> parse(response).map(userInfoErrorResponse -> {
120+
.onStatus(HttpStatus::isError, response -> parse(response).map(userInfoErrorResponse -> {
123121
String description = userInfoErrorResponse.getErrorObject().getDescription();
124122
OAuth2Error oauth2Error = new OAuth2Error(
125123
INVALID_USER_INFO_RESPONSE_ERROR_CODE, description,

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/DefaultReactiveOAuth2UserServiceTests.java

+20-2
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-2021 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.
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.security.oauth2.client.userinfo;
1817

1918
import java.time.Duration;
@@ -50,6 +49,7 @@
5049
import org.springframework.web.reactive.function.client.WebClient;
5150

5251
import static org.assertj.core.api.Assertions.assertThat;
52+
import static org.assertj.core.api.Assertions.assertThatCode;
5353
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5454
import static org.mockito.ArgumentMatchers.any;
5555
import static org.mockito.Mockito.mock;
@@ -152,6 +152,24 @@ public void loadUserWhenUserInfoSuccessResponseThenReturnUser() {
152152
assertThat(userAuthority.getAttributes()).isEqualTo(user.getAttributes());
153153
}
154154

155+
// gh-9336
156+
@Test
157+
public void loadUserWhenUserInfo201CreatedResponseThenReturnUser() {
158+
// @formatter:off
159+
String userInfoResponse = "{\n"
160+
+ " \"id\": \"user1\",\n"
161+
+ " \"first-name\": \"first\",\n"
162+
+ " \"last-name\": \"last\",\n"
163+
+ " \"middle-name\": \"middle\",\n"
164+
+ " \"address\": \"address\",\n"
165+
+ " \"email\": \"[email protected]\"\n"
166+
+ "}\n";
167+
// @formatter:on
168+
this.server.enqueue(new MockResponse().setResponseCode(201)
169+
.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).setBody(userInfoResponse));
170+
assertThatCode(() -> this.userService.loadUser(oauth2UserRequest()).block()).doesNotThrowAnyException();
171+
}
172+
155173
// gh-5500
156174
@Test
157175
public void loadUserWhenAuthenticationMethodHeaderSuccessResponseThenHttpMethodGet() throws Exception {

0 commit comments

Comments
 (0)