|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
52 | 52 | import org.springframework.security.oauth2.core.oidc.TestOidcIdTokens;
|
53 | 53 | import org.springframework.security.oauth2.core.oidc.user.OidcUser;
|
54 | 54 | import org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority;
|
| 55 | +import org.springframework.security.oauth2.core.user.OAuth2User; |
| 56 | +import org.springframework.security.oauth2.core.user.OAuth2UserAuthority; |
55 | 57 |
|
56 | 58 | import static org.assertj.core.api.Assertions.assertThat;
|
57 | 59 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
@@ -492,6 +494,49 @@ public void loadUserWhenTokenDoesNotContainScopesAndUserInfoUriThenUserInfoReque
|
492 | 494 | assertThat(user.getUserInfo()).isNotNull();
|
493 | 495 | }
|
494 | 496 |
|
| 497 | + @Test |
| 498 | + public void loadUserWhenNestedUserInfoSuccessThenReturnUser() { |
| 499 | + // @formatter:off |
| 500 | + String userInfoResponse = "{\n" |
| 501 | + + " \"user\": {\"user-name\": \"user1\"},\n" |
| 502 | + + " \"sub\" : \"subject1\",\n" |
| 503 | + + " \"first-name\": \"first\",\n" |
| 504 | + + " \"last-name\": \"last\",\n" |
| 505 | + + " \"middle-name\": \"middle\",\n" |
| 506 | + + " \"address\": \"address\",\n" |
| 507 | + + " \"email\": \"user1@example.com\"\n" |
| 508 | + + "}\n"; |
| 509 | + // @formatter:on |
| 510 | + this.server.enqueue(jsonResponse(userInfoResponse)); |
| 511 | + String userInfoUri = this.server.url("/user").toString(); |
| 512 | + ClientRegistration clientRegistration = this.clientRegistrationBuilder.userInfoUri(userInfoUri) |
| 513 | + .userInfoAuthenticationMethod(AuthenticationMethod.HEADER) |
| 514 | + .userNameAttributeName("user-name") |
| 515 | + .build(); |
| 516 | + OidcUserService userService = new OidcUserService(); |
| 517 | + DefaultOAuth2UserService oAuth2UserService = new DefaultOAuth2UserService(); |
| 518 | + oAuth2UserService.setAttributesConverter((request) -> (attributes) -> { |
| 519 | + Map<String, Object> user = (Map<String, Object>) attributes.get("user"); |
| 520 | + attributes.put("user-name", user.get("user-name")); |
| 521 | + return attributes; |
| 522 | + }); |
| 523 | + userService.setOauth2UserService(oAuth2UserService); |
| 524 | + OAuth2User user = userService.loadUser(new OidcUserRequest(clientRegistration, this.accessToken, this.idToken)); |
| 525 | + assertThat(user.getName()).isEqualTo("user1"); |
| 526 | + assertThat(user.getAttributes()).hasSize(9); |
| 527 | + assertThat(((Map<?, ?>) user.getAttribute("user")).get("user-name")).isEqualTo("user1"); |
| 528 | + assertThat((String) user.getAttribute("first-name")).isEqualTo("first"); |
| 529 | + assertThat((String) user.getAttribute("last-name")).isEqualTo("last"); |
| 530 | + assertThat((String) user.getAttribute("middle-name")).isEqualTo("middle"); |
| 531 | + assertThat((String) user.getAttribute("address")).isEqualTo("address"); |
| 532 | + assertThat((String) user.getAttribute("email")).isEqualTo("user1@example.com"); |
| 533 | + assertThat(user.getAuthorities()).hasSize(3); |
| 534 | + assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class); |
| 535 | + OAuth2UserAuthority userAuthority = (OAuth2UserAuthority) user.getAuthorities().iterator().next(); |
| 536 | + assertThat(userAuthority.getAuthority()).isEqualTo("OIDC_USER"); |
| 537 | + assertThat(userAuthority.getAttributes()).isEqualTo(user.getAttributes()); |
| 538 | + } |
| 539 | + |
495 | 540 | private MockResponse jsonResponse(String json) {
|
496 | 541 | // @formatter:off
|
497 | 542 | return new MockResponse()
|
|
0 commit comments