-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Support nested username attribute in DefaultOAuth2User #14265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2002-2022 the original author or authors. | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -16,6 +16,7 @@ | |
|
||
package org.springframework.security.oauth2.client.oidc.userinfo; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.Collections; | ||
|
@@ -24,6 +25,8 @@ | |
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
@@ -32,13 +35,17 @@ | |
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.AuthorityUtils; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistration; | ||
import org.springframework.security.oauth2.client.registration.TestClientRegistrations; | ||
import org.springframework.security.oauth2.client.userinfo.DefaultReactiveOAuth2UserService; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; | ||
import org.springframework.security.oauth2.client.userinfo.ReactiveOAuth2UserService; | ||
import org.springframework.security.oauth2.core.AuthenticationMethod; | ||
import org.springframework.security.oauth2.core.OAuth2AccessToken; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.TestOAuth2AccessTokens; | ||
|
@@ -203,8 +210,62 @@ public void loadUserWhenTokenDoesNotContainScopesThenNoScopeAuthorities() { | |
assertThat(userAuthority.getAttributes()).isEqualTo(user.getAttributes()); | ||
} | ||
|
||
@Test | ||
public void loadUserWhenNestedUserInfoSuccessThenReturnUser() throws IOException { | ||
// @formatter:off | ||
String userInfoResponse = "{\n" | ||
+ " \"user\": {\"user-name\": \"user1\"},\n" | ||
+ " \"sub\" : \"" + this.idToken.getSubject() + "\",\n" | ||
+ " \"first-name\": \"first\",\n" | ||
+ " \"last-name\": \"last\",\n" | ||
+ " \"middle-name\": \"middle\",\n" | ||
+ " \"address\": \"address\",\n" | ||
+ " \"email\": \"[email protected]\"\n" | ||
+ "}\n"; | ||
// @formatter:on | ||
try (MockWebServer server = new MockWebServer()) { | ||
server.start(); | ||
enqueueApplicationJsonBody(server, userInfoResponse); | ||
String userInfoUri = server.url("/user").toString(); | ||
ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration() | ||
.userInfoUri(userInfoUri) | ||
.userInfoAuthenticationMethod(AuthenticationMethod.HEADER) | ||
.userNameAttributeName("user-name") | ||
.build(); | ||
OidcReactiveOAuth2UserService userService = new OidcReactiveOAuth2UserService(); | ||
DefaultReactiveOAuth2UserService oAuth2UserService = new DefaultReactiveOAuth2UserService(); | ||
oAuth2UserService.setAttributesConverter((request) -> (attributes) -> { | ||
Map<String, Object> user = (Map<String, Object>) attributes.get("user"); | ||
attributes.put("user-name", user.get("user-name")); | ||
return attributes; | ||
}); | ||
userService.setOauth2UserService(oAuth2UserService); | ||
OAuth2User user = userService | ||
.loadUser(new OidcUserRequest(clientRegistration, this.accessToken, this.idToken)) | ||
.block(); | ||
assertThat(user.getName()).isEqualTo("user1"); | ||
assertThat(user.getAttributes()).hasSize(13); | ||
assertThat(((Map<?, ?>) user.getAttribute("user")).get("user-name")).isEqualTo("user1"); | ||
assertThat((String) user.getAttribute("first-name")).isEqualTo("first"); | ||
assertThat((String) user.getAttribute("last-name")).isEqualTo("last"); | ||
assertThat((String) user.getAttribute("middle-name")).isEqualTo("middle"); | ||
assertThat((String) user.getAttribute("address")).isEqualTo("address"); | ||
assertThat((String) user.getAttribute("email")).isEqualTo("[email protected]"); | ||
assertThat(user.getAuthorities()).hasSize(2); | ||
assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class); | ||
OAuth2UserAuthority userAuthority = (OAuth2UserAuthority) user.getAuthorities().iterator().next(); | ||
assertThat(userAuthority.getAuthority()).isEqualTo("OIDC_USER"); | ||
assertThat(userAuthority.getAttributes()).isEqualTo(user.getAttributes()); | ||
} | ||
} | ||
|
||
private OidcUserRequest userRequest() { | ||
return new OidcUserRequest(this.registration.build(), this.accessToken, this.idToken); | ||
} | ||
|
||
private void enqueueApplicationJsonBody(MockWebServer server, String json) { | ||
server.enqueue( | ||
new MockResponse().setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).setBody(json)); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2002-2022 the original author or authors. | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -52,6 +52,8 @@ | |
import org.springframework.security.oauth2.core.oidc.TestOidcIdTokens; | ||
import org.springframework.security.oauth2.core.oidc.user.OidcUser; | ||
import org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.security.oauth2.core.user.OAuth2UserAuthority; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
@@ -492,6 +494,49 @@ public void loadUserWhenTokenDoesNotContainScopesAndUserInfoUriThenUserInfoReque | |
assertThat(user.getUserInfo()).isNotNull(); | ||
} | ||
|
||
@Test | ||
public void loadUserWhenNestedUserInfoSuccessThenReturnUser() { | ||
// @formatter:off | ||
String userInfoResponse = "{\n" | ||
+ " \"user\": {\"user-name\": \"user1\"},\n" | ||
+ " \"sub\" : \"subject1\",\n" | ||
+ " \"first-name\": \"first\",\n" | ||
+ " \"last-name\": \"last\",\n" | ||
+ " \"middle-name\": \"middle\",\n" | ||
+ " \"address\": \"address\",\n" | ||
+ " \"email\": \"[email protected]\"\n" | ||
+ "}\n"; | ||
// @formatter:on | ||
this.server.enqueue(jsonResponse(userInfoResponse)); | ||
String userInfoUri = this.server.url("/user").toString(); | ||
ClientRegistration clientRegistration = this.clientRegistrationBuilder.userInfoUri(userInfoUri) | ||
.userInfoAuthenticationMethod(AuthenticationMethod.HEADER) | ||
.userNameAttributeName("user-name") | ||
.build(); | ||
OidcUserService userService = new OidcUserService(); | ||
DefaultOAuth2UserService oAuth2UserService = new DefaultOAuth2UserService(); | ||
oAuth2UserService.setAttributesConverter((request) -> (attributes) -> { | ||
Map<String, Object> user = (Map<String, Object>) attributes.get("user"); | ||
attributes.put("user-name", user.get("user-name")); | ||
return attributes; | ||
}); | ||
userService.setOauth2UserService(oAuth2UserService); | ||
OAuth2User user = userService.loadUser(new OidcUserRequest(clientRegistration, this.accessToken, this.idToken)); | ||
assertThat(user.getName()).isEqualTo("user1"); | ||
assertThat(user.getAttributes()).hasSize(9); | ||
assertThat(((Map<?, ?>) user.getAttribute("user")).get("user-name")).isEqualTo("user1"); | ||
assertThat((String) user.getAttribute("first-name")).isEqualTo("first"); | ||
assertThat((String) user.getAttribute("last-name")).isEqualTo("last"); | ||
assertThat((String) user.getAttribute("middle-name")).isEqualTo("middle"); | ||
assertThat((String) user.getAttribute("address")).isEqualTo("address"); | ||
assertThat((String) user.getAttribute("email")).isEqualTo("[email protected]"); | ||
assertThat(user.getAuthorities()).hasSize(3); | ||
assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class); | ||
OAuth2UserAuthority userAuthority = (OAuth2UserAuthority) user.getAuthorities().iterator().next(); | ||
assertThat(userAuthority.getAuthority()).isEqualTo("OIDC_USER"); | ||
assertThat(userAuthority.getAttributes()).isEqualTo(user.getAttributes()); | ||
} | ||
|
||
private MockResponse jsonResponse(String json) { | ||
// @formatter:off | ||
return new MockResponse() | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.