Skip to content

Commit d85a7cf

Browse files
BenjaminFaaljgrandja
authored andcommitted
Make user info response status check error only
Closes gh-9336
1 parent 76229cf commit d85a7cf

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 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.
@@ -108,7 +108,7 @@ public Mono<OAuth2User> loadUser(OAuth2UserRequest userRequest) throws OAuth2Aut
108108
authenticationMethod);
109109
// @formatter:off
110110
Mono<Map<String, Object>> userAttributes = requestHeadersSpec.retrieve()
111-
.onStatus((s) -> s != HttpStatus.OK, (response) ->
111+
.onStatus(HttpStatus::isError, (response) ->
112112
parse(response)
113113
.map((userInfoErrorResponse) -> {
114114
String description = userInfoErrorResponse.getErrorObject().getDescription();

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 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.
@@ -51,6 +51,7 @@
5151

5252
import static org.assertj.core.api.Assertions.assertThat;
5353
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
54+
import static org.assertj.core.api.Assertions.assertThatNoException;
5455
import static org.mockito.ArgumentMatchers.any;
5556
import static org.mockito.BDDMockito.given;
5657
import static org.mockito.Mockito.mock;
@@ -144,6 +145,24 @@ public void loadUserWhenUserInfoSuccessResponseThenReturnUser() {
144145
assertThat(userAuthority.getAttributes()).isEqualTo(user.getAttributes());
145146
}
146147

148+
// gh-9336
149+
@Test
150+
public void loadUserWhenUserInfo201CreatedResponseThenReturnUser() {
151+
// @formatter:off
152+
String userInfoResponse = "{\n"
153+
+ " \"id\": \"user1\",\n"
154+
+ " \"first-name\": \"first\",\n"
155+
+ " \"last-name\": \"last\",\n"
156+
+ " \"middle-name\": \"middle\",\n"
157+
+ " \"address\": \"address\",\n"
158+
+ " \"email\": \"[email protected]\"\n"
159+
+ "}\n";
160+
// @formatter:on
161+
this.server.enqueue(new MockResponse().setResponseCode(201)
162+
.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).setBody(userInfoResponse));
163+
assertThatNoException().isThrownBy(() -> this.userService.loadUser(oauth2UserRequest()).block());
164+
}
165+
147166
// gh-5500
148167
@Test
149168
public void loadUserWhenAuthenticationMethodHeaderSuccessResponseThenHttpMethodGet() throws Exception {

0 commit comments

Comments
 (0)