Skip to content

Commit a77a0ea

Browse files
committed
1 parent 6658312 commit a77a0ea

File tree

11 files changed

+70
-83
lines changed

11 files changed

+70
-83
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/authorization/OidcUserInfoEndpointConfigurer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public final class OidcUserInfoEndpointConfigurer extends AbstractOAuth2Configur
5757

5858
/**
5959
* Sets the {@link Function} used to extract claims from an {@link OAuth2AuthenticationContext}
60-
* to an instance of {@link OidcUserInfo}.
60+
* to an instance of {@link OidcUserInfo} for the UserInfo response.
6161
*
6262
* <p>
6363
* The {@link OAuth2AuthenticationContext} gives the mapper access to the {@link OidcUserInfoAuthenticationToken}.
@@ -109,4 +109,5 @@ <B extends HttpSecurityBuilder<B>> void configure(B builder) {
109109
RequestMatcher getRequestMatcher() {
110110
return this.requestMatcher;
111111
}
112+
112113
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/core/oidc/http/converter/OidcUserInfoHttpMessageConverter.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.springframework.util.Assert;
3838

3939
/**
40-
* A {@link HttpMessageConverter} for an {@link OidcUserInfo OpenID Connect UserInfo Request and Response}.
40+
* A {@link HttpMessageConverter} for an {@link OidcUserInfo OpenID Connect UserInfo Response}.
4141
*
4242
* @author Ido Salomon
4343
* @author Steve Riesenberg
@@ -75,7 +75,7 @@ protected OidcUserInfo readInternal(Class<? extends OidcUserInfo> clazz, HttpInp
7575
return this.userInfoConverter.convert(userInfoParameters);
7676
} catch (Exception ex) {
7777
throw new HttpMessageNotReadableException(
78-
"An error occurred reading the UserInfo: " + ex.getMessage(), ex, inputMessage);
78+
"An error occurred reading the UserInfo response: " + ex.getMessage(), ex, inputMessage);
7979
}
8080
}
8181

@@ -101,8 +101,7 @@ protected void writeInternal(OidcUserInfo oidcUserInfo, HttpOutputMessage output
101101
* Sets the {@link Converter} used for converting the UserInfo parameters
102102
* to an {@link OidcUserInfo}.
103103
*
104-
* @param userInfoConverter the {@link Converter} used for converting to an
105-
* {@link OidcUserInfo}
104+
* @param userInfoConverter the {@link Converter} used for converting to an {@link OidcUserInfo}
106105
*/
107106
public final void setUserInfoConverter(Converter<Map<String, Object>, OidcUserInfo> userInfoConverter) {
108107
Assert.notNull(userInfoConverter, "userInfoConverter cannot be null");
@@ -123,7 +122,6 @@ public final void setUserInfoParametersConverter(
123122
}
124123

125124
private static final class MapOidcUserInfoConverter implements Converter<Map<String, Object>, OidcUserInfo> {
126-
127125
private static final ClaimConversionService CLAIM_CONVERSION_SERVICE = ClaimConversionService.getSharedInstance();
128126
private static final TypeDescriptor OBJECT_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Object.class);
129127
private static final TypeDescriptor BOOLEAN_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Boolean.class);

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/config/ProviderSettings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public String getOidcClientRegistrationEndpoint() {
100100
/**
101101
* Returns the Provider's OpenID Connect 1.0 UserInfo endpoint. The default is {@code /userinfo}.
102102
*
103-
* @return the OpenID Connect 1.0 User Info endpoint
103+
* @return the OpenID Connect 1.0 UserInfo endpoint
104104
*/
105105
public String getOidcUserInfoEndpoint() {
106106
return getSetting(ConfigurationSettingNames.Provider.OIDC_USER_INFO_ENDPOINT);
@@ -215,7 +215,7 @@ public Builder oidcClientRegistrationEndpoint(String oidcClientRegistrationEndpo
215215
/**
216216
* Sets the Provider's OpenID Connect 1.0 UserInfo endpoint.
217217
*
218-
* @param oidcUserInfoEndpoint the OpenID Connect 1.0 User Info endpoint
218+
* @param oidcUserInfoEndpoint the OpenID Connect 1.0 UserInfo endpoint
219219
* @return the {@link Builder} for further configuration
220220
*/
221221
public Builder oidcUserInfoEndpoint(String oidcUserInfoEndpoint) {

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/oidc/authentication/OidcUserInfoAuthenticationProvider.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@
5050
* @see <a href="https://openid.net/specs/openid-connect-core-1_0.html#UserInfo">5.3. UserInfo Endpoint</a>
5151
*/
5252
public final class OidcUserInfoAuthenticationProvider implements AuthenticationProvider {
53-
5453
private final OAuth2AuthorizationService authorizationService;
55-
5654
private Function<OAuth2AuthenticationContext, OidcUserInfo> userInfoMapper = new DefaultOidcUserInfoMapper();
5755

5856
/**
@@ -107,6 +105,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
107105
userInfoAuthentication, context);
108106

109107
OidcUserInfo userInfo = this.userInfoMapper.apply(authenticationContext);
108+
110109
return new OidcUserInfoAuthenticationToken(accessTokenAuthentication, userInfo);
111110
}
112111

@@ -116,7 +115,7 @@ public boolean supports(Class<?> authentication) {
116115
}
117116

118117
/**
119-
* Sets the {@link Function} used when mapping from an {@link OAuth2AuthenticationContext}
118+
* Sets the {@link Function} used to extract claims from an {@link OAuth2AuthenticationContext}
120119
* to an instance of {@link OidcUserInfo} for the UserInfo response.
121120
*
122121
* <p>
@@ -128,7 +127,7 @@ public boolean supports(Class<?> authentication) {
128127
* {@link OAuth2AccessToken} associated with the bearer token used to make the request.</li>
129128
* </ul>
130129
*
131-
* @param userInfoMapper the {@link Function} used when mapping from an {@link OAuth2AuthenticationContext}
130+
* @param userInfoMapper the {@link Function} used to extract claims from an {@link OAuth2AuthenticationContext} to an instance of {@link OidcUserInfo}
132131
*/
133132
public void setUserInfoMapper(Function<OAuth2AuthenticationContext, OidcUserInfo> userInfoMapper) {
134133
Assert.notNull(userInfoMapper, "userInfoMapper cannot be null");
@@ -173,7 +172,7 @@ public OidcUserInfo apply(OAuth2AuthenticationContext authenticationContext) {
173172
return new OidcUserInfo(scopeRequestedClaims);
174173
}
175174

176-
private Map<String, Object> getClaimsRequestedByScope(Map<String, Object> claims, Set<String> requestedScopes) {
175+
private static Map<String, Object> getClaimsRequestedByScope(Map<String, Object> claims, Set<String> requestedScopes) {
177176
Set<String> scopeRequestedClaimNames = new HashSet<>(32);
178177
scopeRequestedClaimNames.add(StandardClaimNames.SUB);
179178

@@ -195,5 +194,7 @@ private Map<String, Object> getClaimsRequestedByScope(Map<String, Object> claims
195194

196195
return requestedClaims;
197196
}
197+
198198
}
199+
199200
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/oidc/authentication/OidcUserInfoAuthenticationToken.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,14 @@
3333
* @see OidcUserInfoAuthenticationProvider
3434
*/
3535
public class OidcUserInfoAuthenticationToken extends AbstractAuthenticationToken {
36-
3736
private static final long serialVersionUID = Version.SERIAL_VERSION_UID;
38-
3937
private final Authentication principal;
4038
private final OidcUserInfo userInfo;
4139

4240
/**
4341
* Constructs an {@code OidcUserInfoAuthenticationToken} using the provided parameters.
4442
*
45-
* @param principal the authenticated principal
43+
* @param principal the principal
4644
*/
4745
public OidcUserInfoAuthenticationToken(Authentication principal) {
4846
super(Collections.emptyList());
@@ -64,7 +62,7 @@ public OidcUserInfoAuthenticationToken(Authentication principal, OidcUserInfo us
6462
Assert.notNull(userInfo, "userInfo cannot be null");
6563
this.principal = principal;
6664
this.userInfo = userInfo;
67-
setAuthenticated(principal.isAuthenticated());
65+
setAuthenticated(true);
6866
}
6967

7068
@Override
@@ -85,4 +83,5 @@ public Object getCredentials() {
8583
public OidcUserInfo getUserInfo() {
8684
return this.userInfo;
8785
}
86+
8887
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/oidc/web/OidcUserInfoEndpointFilter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import org.springframework.http.HttpMethod;
2626
import org.springframework.http.HttpStatus;
27-
import org.springframework.http.MediaType;
2827
import org.springframework.http.converter.HttpMessageConverter;
2928
import org.springframework.http.server.ServletServerHttpResponse;
3029
import org.springframework.security.authentication.AuthenticationManager;
@@ -125,7 +124,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
125124

126125
private void sendUserInfoResponse(HttpServletResponse response, OidcUserInfo userInfo) throws IOException {
127126
ServletServerHttpResponse httpResponse = new ServletServerHttpResponse(response);
128-
this.userInfoHttpMessageConverter.write(userInfo, MediaType.APPLICATION_JSON, httpResponse);
127+
this.userInfoHttpMessageConverter.write(userInfo, null, httpResponse);
129128
}
130129

131130
private void sendErrorResponse(HttpServletResponse response, OAuth2Error error) throws IOException {

oauth2-authorization-server/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/authorization/OidcUserInfoTests.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.time.Instant;
1919
import java.util.Arrays;
20+
import java.util.Collections;
2021
import java.util.HashSet;
2122
import java.util.Set;
2223
import java.util.function.Function;
@@ -123,7 +124,7 @@ public void requestWhenUserInfoRequestPostThenUserInfoResponse() throws Exceptio
123124
}
124125

125126
@Test
126-
public void requestWhenSignedJwtAndCustomUserInfoMapperThenUserInfoResponse() throws Exception {
127+
public void requestWhenSignedJwtAndCustomUserInfoMapperThenMapJwtClaimsToUserInfoResponse() throws Exception {
127128
this.spring.register(CustomUserInfoConfiguration.class).autowire();
128129

129130
OAuth2Authorization authorization = createAuthorization();
@@ -159,7 +160,7 @@ private static ResultMatcher userInfoResponse() {
159160
jsonPath("locale").value("en-US"),
160161
jsonPath("phone_number").value("+1 (604) 555-1234;ext=5678"),
161162
jsonPath("phone_number_verified").value("false"),
162-
jsonPath("address").value("Champ de Mars\n5 Av. Anatole France\n75007 Paris\nFrance"),
163+
jsonPath("address.formatted").value("Champ de Mars\n5 Av. Anatole France\n75007 Paris\nFrance"),
163164
jsonPath("updated_at").value("1970-01-01T00:00:00Z")
164165
);
165166
// @formatter:on
@@ -210,7 +211,7 @@ private static OidcUserInfo createUserInfo() {
210211
.locale("en-US")
211212
.phoneNumber("+1 (604) 555-1234;ext=5678")
212213
.phoneNumberVerified("false")
213-
.address("Champ de Mars\n5 Av. Anatole France\n75007 Paris\nFrance")
214+
.claim("address", Collections.singletonMap("formatted", "Champ de Mars\n5 Av. Anatole France\n75007 Paris\nFrance"))
214215
.updatedAt("1970-01-01T00:00:00Z")
215216
.build();
216217
// @formatter:on
@@ -304,5 +305,7 @@ JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
304305
JwtEncoder jwtEncoder(JWKSource<SecurityContext> jwkSource) {
305306
return new NimbusJwsEncoder(jwkSource);
306307
}
308+
307309
}
310+
308311
}

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/core/oidc/http/converter/OidcUserInfoHttpMessageConverterTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,18 @@ public void readInternalWhenFailingConverterThenThrowException() {
130130

131131
assertThatExceptionOfType(HttpMessageNotReadableException.class)
132132
.isThrownBy(() -> this.messageConverter.readInternal(OidcUserInfo.class, response))
133-
.withMessageContaining("An error occurred reading the UserInfo")
133+
.withMessageContaining("An error occurred reading the UserInfo response")
134134
.withMessageContaining(errorMessage);
135135
}
136136

137137
@Test
138138
public void readInternalWhenInvalidResponseThenThrowException() {
139-
String providerConfigurationResponse = "{}";
140-
MockClientHttpResponse response = new MockClientHttpResponse(providerConfigurationResponse.getBytes(), HttpStatus.OK);
139+
String userInfoResponse = "{}";
140+
MockClientHttpResponse response = new MockClientHttpResponse(userInfoResponse.getBytes(), HttpStatus.OK);
141141

142142
assertThatExceptionOfType(HttpMessageNotReadableException.class)
143143
.isThrownBy(() -> this.messageConverter.readInternal(OidcUserInfo.class, response))
144-
.withMessageContaining("An error occurred reading the UserInfo")
144+
.withMessageContaining("An error occurred reading the UserInfo response")
145145
.withMessageContaining("claims cannot be empty");
146146
}
147147

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/oidc/authentication/OidcUserInfoAuthenticationProviderTests.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public class OidcUserInfoAuthenticationProviderTests {
6060
private OidcUserInfoAuthenticationProvider authenticationProvider;
6161

6262
@Before
63-
public void setUp() throws Exception {
63+
public void setUp() {
6464
this.authorizationService = mock(OAuth2AuthorizationService.class);
65-
this.authenticationProvider = new OidcUserInfoAuthenticationProvider(authorizationService);
65+
this.authenticationProvider = new OidcUserInfoAuthenticationProvider(this.authorizationService);
6666
}
6767

6868
@Test
@@ -224,7 +224,7 @@ public void authenticateWhenValidAccessTokenThenReturnUserInfo() {
224224
assertThat(userInfo.getLocale()).isEqualTo("en-US");
225225
assertThat(userInfo.getPhoneNumber()).isEqualTo("+1 (604) 555-1234;ext=5678");
226226
assertThat(userInfo.getPhoneNumberVerified()).isEqualTo(false);
227-
assertThat(userInfo.getClaimAsString(StandardClaimNames.ADDRESS))
227+
assertThat(userInfo.getAddress().getFormatted())
228228
.isEqualTo("Champ de Mars\n5 Av. Anatole France\n75007 Paris\nFrance");
229229
assertThat(userInfo.getUpdatedAt()).isEqualTo(Instant.parse("1970-01-01T00:00:00Z"));
230230

@@ -259,6 +259,7 @@ private static JwtAuthenticationToken createJwtAuthenticationToken(String tokenV
259259
}
260260

261261
private static OidcUserInfo createUserInfo() {
262+
// @formatter:off
262263
return OidcUserInfo.builder()
263264
.subject("user1")
264265
.name("First Last")
@@ -278,8 +279,10 @@ private static OidcUserInfo createUserInfo() {
278279
.locale("en-US")
279280
.phoneNumber("+1 (604) 555-1234;ext=5678")
280281
.phoneNumberVerified("false")
281-
.address("Champ de Mars\n5 Av. Anatole France\n75007 Paris\nFrance")
282+
.claim("address", Collections.singletonMap("formatted", "Champ de Mars\n5 Av. Anatole France\n75007 Paris\nFrance"))
282283
.updatedAt("1970-01-01T00:00:00Z")
283284
.build();
285+
// @formatter:on
284286
}
287+
285288
}

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/oidc/authentication/OidcUserInfoAuthenticationTokenTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* @author Steve Riesenberg
3333
*/
3434
public class OidcUserInfoAuthenticationTokenTests {
35+
3536
@Test
3637
public void constructorWhenPrincipalNullThenThrowIllegalArgumentException() {
3738
assertThatIllegalArgumentException()
@@ -55,6 +56,6 @@ public void constructorWhenPrincipalAndUserInfoProvidedThenCreated() {
5556
OidcUserInfoAuthenticationToken authentication = new OidcUserInfoAuthenticationToken(principal, userInfo);
5657
assertThat(authentication.getPrincipal()).isEqualTo(principal);
5758
assertThat(authentication.getUserInfo()).isEqualTo(userInfo);
58-
assertThat(authentication.isAuthenticated()).isFalse();
59+
assertThat(authentication.isAuthenticated()).isTrue();
5960
}
6061
}

0 commit comments

Comments
 (0)