|
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.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.security.oauth2.client.oidc.userinfo;
|
18 | 18 |
|
| 19 | +import java.io.IOException; |
19 | 20 | import java.time.Duration;
|
20 | 21 | import java.time.Instant;
|
21 | 22 | import java.util.Collections;
|
|
24 | 25 | import java.util.Map;
|
25 | 26 | import java.util.function.Function;
|
26 | 27 |
|
| 28 | +import okhttp3.mockwebserver.MockResponse; |
| 29 | +import okhttp3.mockwebserver.MockWebServer; |
27 | 30 | import org.junit.jupiter.api.BeforeEach;
|
28 | 31 | import org.junit.jupiter.api.Test;
|
29 | 32 | import org.junit.jupiter.api.extension.ExtendWith;
|
|
32 | 35 | import reactor.core.publisher.Mono;
|
33 | 36 |
|
34 | 37 | import org.springframework.core.convert.converter.Converter;
|
| 38 | +import org.springframework.http.HttpHeaders; |
| 39 | +import org.springframework.http.MediaType; |
35 | 40 | import org.springframework.security.core.GrantedAuthority;
|
36 | 41 | import org.springframework.security.core.authority.AuthorityUtils;
|
37 | 42 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
38 | 43 | import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
39 | 44 | import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
|
| 45 | +import org.springframework.security.oauth2.client.userinfo.DefaultReactiveOAuth2UserService; |
40 | 46 | import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
|
41 | 47 | import org.springframework.security.oauth2.client.userinfo.ReactiveOAuth2UserService;
|
| 48 | +import org.springframework.security.oauth2.core.AuthenticationMethod; |
42 | 49 | import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
43 | 50 | import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
44 | 51 | import org.springframework.security.oauth2.core.TestOAuth2AccessTokens;
|
@@ -203,8 +210,62 @@ public void loadUserWhenTokenDoesNotContainScopesThenNoScopeAuthorities() {
|
203 | 210 | assertThat(userAuthority.getAttributes()).isEqualTo(user.getAttributes());
|
204 | 211 | }
|
205 | 212 |
|
| 213 | + @Test |
| 214 | + public void loadUserWhenNestedUserInfoSuccessThenReturnUser() throws IOException { |
| 215 | + // @formatter:off |
| 216 | + String userInfoResponse = "{\n" |
| 217 | + + " \"user\": {\"user-name\": \"user1\"},\n" |
| 218 | + + " \"sub\" : \"" + this.idToken.getSubject() + "\",\n" |
| 219 | + + " \"first-name\": \"first\",\n" |
| 220 | + + " \"last-name\": \"last\",\n" |
| 221 | + + " \"middle-name\": \"middle\",\n" |
| 222 | + + " \"address\": \"address\",\n" |
| 223 | + + " \"email\": \"[email protected]\"\n" |
| 224 | + + "}\n"; |
| 225 | + // @formatter:on |
| 226 | + try (MockWebServer server = new MockWebServer()) { |
| 227 | + server.start(); |
| 228 | + enqueueApplicationJsonBody(server, userInfoResponse); |
| 229 | + String userInfoUri = server.url("/user").toString(); |
| 230 | + ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration() |
| 231 | + .userInfoUri(userInfoUri) |
| 232 | + .userInfoAuthenticationMethod(AuthenticationMethod.HEADER) |
| 233 | + .userNameAttributeName("user-name") |
| 234 | + .build(); |
| 235 | + OidcReactiveOAuth2UserService userService = new OidcReactiveOAuth2UserService(); |
| 236 | + DefaultReactiveOAuth2UserService oAuth2UserService = new DefaultReactiveOAuth2UserService(); |
| 237 | + oAuth2UserService.setAttributesConverter((request) -> (attributes) -> { |
| 238 | + Map<String, Object> user = (Map<String, Object>) attributes.get("user"); |
| 239 | + attributes.put("user-name", user.get("user-name")); |
| 240 | + return attributes; |
| 241 | + }); |
| 242 | + userService.setOauth2UserService(oAuth2UserService); |
| 243 | + OAuth2User user = userService |
| 244 | + .loadUser(new OidcUserRequest(clientRegistration, this.accessToken, this.idToken)) |
| 245 | + .block(); |
| 246 | + assertThat(user.getName()).isEqualTo("user1"); |
| 247 | + assertThat(user.getAttributes()).hasSize(13); |
| 248 | + assertThat(((Map<?, ?>) user.getAttribute("user")).get("user-name")).isEqualTo("user1"); |
| 249 | + assertThat((String) user.getAttribute("first-name")).isEqualTo("first"); |
| 250 | + assertThat((String) user.getAttribute("last-name")).isEqualTo("last"); |
| 251 | + assertThat((String) user.getAttribute("middle-name")).isEqualTo("middle"); |
| 252 | + assertThat((String) user.getAttribute("address")).isEqualTo("address"); |
| 253 | + assertThat(( String) user. getAttribute( "email")). isEqualTo( "[email protected]"); |
| 254 | + assertThat(user.getAuthorities()).hasSize(2); |
| 255 | + assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class); |
| 256 | + OAuth2UserAuthority userAuthority = (OAuth2UserAuthority) user.getAuthorities().iterator().next(); |
| 257 | + assertThat(userAuthority.getAuthority()).isEqualTo("OIDC_USER"); |
| 258 | + assertThat(userAuthority.getAttributes()).isEqualTo(user.getAttributes()); |
| 259 | + } |
| 260 | + } |
| 261 | + |
206 | 262 | private OidcUserRequest userRequest() {
|
207 | 263 | return new OidcUserRequest(this.registration.build(), this.accessToken, this.idToken);
|
208 | 264 | }
|
209 | 265 |
|
| 266 | + private void enqueueApplicationJsonBody(MockWebServer server, String json) { |
| 267 | + server.enqueue( |
| 268 | + new MockResponse().setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).setBody(json)); |
| 269 | + } |
| 270 | + |
210 | 271 | }
|
0 commit comments