Skip to content

Commit 5cd1ec7

Browse files
antonin-arqueyjgrandja
authored andcommitted
Add AuthoritiesMapper setter for reactive OAuth2Login
Allow the configuration of a custom GrantedAuthorityMapper for reactive OAuth2Login - Add setter in OidcAuthorizationCodeReactiveAuthenticationManager and OAuth2LoginReactiveAuthenticationManager - Use an available GrantedAuthorityMapper bean to configure the default ReactiveAuthenticationManager Fixes gh-8324
1 parent 2cccf22 commit 5cd1ec7

File tree

6 files changed

+136
-8
lines changed

6 files changed

+136
-8
lines changed

config/src/main/java/org/springframework/security/config/web/server/ServerHttpSecurity.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.function.Function;
3232
import java.util.function.Supplier;
3333

34+
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
3435
import reactor.core.publisher.Mono;
3536
import reactor.util.context.Context;
3637

@@ -1056,8 +1057,11 @@ private ReactiveAuthenticationManager getAuthenticationManager() {
10561057

10571058
private ReactiveAuthenticationManager createDefault() {
10581059
ReactiveOAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> client = getAccessTokenResponseClient();
1059-
ReactiveAuthenticationManager result = new OAuth2LoginReactiveAuthenticationManager(client, getOauth2UserService());
1060-
1060+
OAuth2LoginReactiveAuthenticationManager oauth2Manager = new OAuth2LoginReactiveAuthenticationManager(client, getOauth2UserService());
1061+
GrantedAuthoritiesMapper authoritiesMapper = getBeanOrNull(GrantedAuthoritiesMapper.class);
1062+
if (authoritiesMapper != null) {
1063+
oauth2Manager.setAuthoritiesMapper(authoritiesMapper);
1064+
}
10611065
boolean oidcAuthenticationProviderEnabled = ClassUtils.isPresent(
10621066
"org.springframework.security.oauth2.jwt.JwtDecoder", this.getClass().getClassLoader());
10631067
if (oidcAuthenticationProviderEnabled) {
@@ -1069,9 +1073,12 @@ private ReactiveAuthenticationManager createDefault() {
10691073
if (jwtDecoderFactory != null) {
10701074
oidc.setJwtDecoderFactory(jwtDecoderFactory);
10711075
}
1072-
result = new DelegatingReactiveAuthenticationManager(oidc, result);
1076+
if (authoritiesMapper != null) {
1077+
oidc.setAuthoritiesMapper(authoritiesMapper);
1078+
}
1079+
return new DelegatingReactiveAuthenticationManager(oidc, oauth2Manager);
10731080
}
1074-
return result;
1081+
return oauth2Manager;
10751082
}
10761083

10771084
/**

docs/manual/src/docs/asciidoc/_includes/reactive/oauth2/login.adoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,21 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
160160
return http.build();
161161
}
162162
----
163+
164+
You may register a `GrantedAuthoritiesMapper` `@Bean` to have it automatically applied to the default configuration, as shown in the following example:
165+
166+
[source,java]
167+
----
168+
@Bean
169+
public GrantedAuthoritiesMapper userAuthoritiesMapper() {
170+
...
171+
}
172+
173+
@Bean
174+
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
175+
http
176+
// ...
177+
.oauth2Login(withDefaults());
178+
return http.build();
179+
}
180+
----

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/authentication/OAuth2LoginReactiveAuthenticationManager.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -95,6 +95,18 @@ public Mono<Authentication> authenticate(Authentication authentication) {
9595
});
9696
}
9797

98+
/**
99+
* Sets the {@link GrantedAuthoritiesMapper} used for mapping {@link OAuth2User#getAuthorities()}
100+
* to a new set of authorities which will be associated to the {@link OAuth2LoginAuthenticationToken}.
101+
*
102+
* @since 5.4
103+
* @param authoritiesMapper the {@link GrantedAuthoritiesMapper} used for mapping the user's authorities
104+
*/
105+
public final void setAuthoritiesMapper(GrantedAuthoritiesMapper authoritiesMapper) {
106+
Assert.notNull(authoritiesMapper, "authoritiesMapper cannot be null");
107+
this.authoritiesMapper = authoritiesMapper;
108+
}
109+
98110
private Mono<OAuth2LoginAuthenticationToken> onSuccess(OAuth2AuthorizationCodeAuthenticationToken authentication) {
99111
OAuth2AccessToken accessToken = authentication.getAccessToken();
100112
Map<String, Object> additionalParameters = authentication.getAdditionalParameters();

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/oidc/authentication/OidcAuthorizationCodeReactiveAuthenticationManager.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -156,6 +156,18 @@ public final void setJwtDecoderFactory(ReactiveJwtDecoderFactory<ClientRegistrat
156156
this.jwtDecoderFactory = jwtDecoderFactory;
157157
}
158158

159+
/**
160+
* Sets the {@link GrantedAuthoritiesMapper} used for mapping {@link OidcUser#getAuthorities()}
161+
* to a new set of authorities which will be associated to the {@link OAuth2LoginAuthenticationToken}.
162+
*
163+
* @since 5.4
164+
* @param authoritiesMapper the {@link GrantedAuthoritiesMapper} used for mapping the user's authorities
165+
*/
166+
public final void setAuthoritiesMapper(GrantedAuthoritiesMapper authoritiesMapper) {
167+
Assert.notNull(authoritiesMapper, "authoritiesMapper cannot be null");
168+
this.authoritiesMapper = authoritiesMapper;
169+
}
170+
159171
private Mono<OAuth2LoginAuthenticationToken> authenticationResult(OAuth2AuthorizationCodeAuthenticationToken authorizationCodeAuthentication, OAuth2AccessTokenResponse accessTokenResponse) {
160172
OAuth2AccessToken accessToken = accessTokenResponse.getAccessToken();
161173
ClientRegistration clientRegistration = authorizationCodeAuthentication.getClientRegistration();

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/authentication/OAuth2LoginReactiveAuthenticationManagerTests.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -20,10 +20,13 @@
2020
import static org.assertj.core.api.Assertions.assertThatCode;
2121
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2222
import static org.mockito.ArgumentMatchers.any;
23+
import static org.mockito.ArgumentMatchers.anyCollection;
24+
import static org.mockito.Mockito.mock;
2325
import static org.mockito.Mockito.when;
2426

2527
import java.util.Collections;
2628
import java.util.HashMap;
29+
import java.util.List;
2730
import java.util.Map;
2831

2932
import org.junit.Before;
@@ -33,8 +36,11 @@
3336
import org.mockito.ArgumentCaptor;
3437
import org.mockito.Mock;
3538
import org.mockito.junit.MockitoJUnitRunner;
39+
import org.mockito.stubbing.Answer;
3640
import org.springframework.security.authentication.TestingAuthenticationToken;
41+
import org.springframework.security.core.GrantedAuthority;
3742
import org.springframework.security.core.authority.AuthorityUtils;
43+
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
3844
import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientService;
3945
import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest;
4046
import org.springframework.security.oauth2.client.endpoint.ReactiveOAuth2AccessTokenResponseClient;
@@ -96,6 +102,12 @@ public void constructorWhenNullUserServiceThenIllegalArgumentException() {
96102
.isInstanceOf(IllegalArgumentException.class);
97103
}
98104

105+
@Test
106+
public void setAuthoritiesMapperWhenAuthoritiesMapperIsNullThenThrowIllegalArgumentException() {
107+
assertThatThrownBy(() -> this.manager.setAuthoritiesMapper(null))
108+
.isInstanceOf(IllegalArgumentException.class);
109+
}
110+
99111
@Test
100112
public void authenticateWhenNoSubscriptionThenDoesNothing() {
101113
// we didn't do anything because it should cause a ClassCastException (as verified below)
@@ -178,6 +190,24 @@ public void authenticateWhenTokenSuccessResponseThenAdditionalParametersAddedToU
178190
.containsAllEntriesOf(accessTokenResponse.getAdditionalParameters());
179191
}
180192

193+
@Test
194+
public void authenticateWhenAuthoritiesMapperSetThenReturnMappedAuthorities() {
195+
OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("foo")
196+
.tokenType(OAuth2AccessToken.TokenType.BEARER)
197+
.build();
198+
when(this.accessTokenResponseClient.getTokenResponse(any())).thenReturn(Mono.just(accessTokenResponse));
199+
DefaultOAuth2User user = new DefaultOAuth2User(AuthorityUtils.createAuthorityList("ROLE_USER"), Collections.singletonMap("user", "rob"), "user");
200+
when(this.userService.loadUser(any())).thenReturn(Mono.just(user));
201+
List<GrantedAuthority> mappedAuthorities = AuthorityUtils.createAuthorityList("ROLE_OAUTH_USER");
202+
GrantedAuthoritiesMapper authoritiesMapper = mock(GrantedAuthoritiesMapper.class);
203+
when(authoritiesMapper.mapAuthorities(anyCollection())).thenAnswer((Answer<List<GrantedAuthority>>) invocation -> mappedAuthorities);
204+
manager.setAuthoritiesMapper(authoritiesMapper);
205+
206+
OAuth2LoginAuthenticationToken result = (OAuth2LoginAuthenticationToken) this.manager.authenticate(loginToken()).block();
207+
208+
assertThat(result.getAuthorities()).isEqualTo(mappedAuthorities);
209+
}
210+
181211
private OAuth2AuthorizationCodeAuthenticationToken loginToken() {
182212
ClientRegistration clientRegistration = this.registration.build();
183213
OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/oidc/authentication/OidcAuthorizationCodeReactiveAuthenticationManagerTests.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -21,6 +21,7 @@
2121
import java.util.Base64;
2222
import java.util.Collections;
2323
import java.util.HashMap;
24+
import java.util.List;
2425
import java.util.Map;
2526

2627
import org.junit.Before;
@@ -29,6 +30,10 @@
2930
import org.mockito.ArgumentCaptor;
3031
import org.mockito.Mock;
3132
import org.mockito.junit.MockitoJUnitRunner;
33+
import org.mockito.stubbing.Answer;
34+
import org.springframework.security.core.Authentication;
35+
import org.springframework.security.core.GrantedAuthority;
36+
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
3237
import reactor.core.publisher.Mono;
3338

3439
import org.springframework.security.authentication.TestingAuthenticationToken;
@@ -63,6 +68,8 @@
6368
import static org.assertj.core.api.Assertions.assertThatCode;
6469
import static org.assertj.core.api.Assertions.assertThatThrownBy;
6570
import static org.mockito.ArgumentMatchers.any;
71+
import static org.mockito.ArgumentMatchers.anyCollection;
72+
import static org.mockito.Mockito.mock;
6673
import static org.mockito.Mockito.when;
6774
import static org.springframework.security.oauth2.client.oidc.authentication.OidcAuthorizationCodeReactiveAuthenticationManager.createHash;
6875
import static org.springframework.security.oauth2.jwt.TestJwts.jwt;
@@ -123,6 +130,12 @@ public void setJwtDecoderFactoryWhenNullThenIllegalArgumentException() {
123130
.isInstanceOf(IllegalArgumentException.class);
124131
}
125132

133+
@Test
134+
public void setAuthoritiesMapperWhenAuthoritiesMapperIsNullThenThrowIllegalArgumentException() {
135+
assertThatThrownBy(() -> this.manager.setAuthoritiesMapper(null))
136+
.isInstanceOf(IllegalArgumentException.class);
137+
}
138+
126139
@Test
127140
public void authenticateWhenNoSubscriptionThenDoesNothing() {
128141
// we didn't do anything because it should cause a ClassCastException (as verified below)
@@ -316,6 +329,42 @@ public void authenticateWhenTokenSuccessResponseThenAdditionalParametersAddedToU
316329
.containsAllEntriesOf(accessTokenResponse.getAdditionalParameters());
317330
}
318331

332+
@Test
333+
public void authenticateWhenAuthoritiesMapperSetThenReturnMappedAuthorities() {
334+
ClientRegistration clientRegistration = this.registration.build();
335+
OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("foo")
336+
.tokenType(OAuth2AccessToken.TokenType.BEARER)
337+
.additionalParameters(Collections.singletonMap(OidcParameterNames.ID_TOKEN, this.idToken.getTokenValue()))
338+
.build();
339+
340+
OAuth2AuthorizationCodeAuthenticationToken authorizationCodeAuthentication = loginToken();
341+
342+
Map<String, Object> claims = new HashMap<>();
343+
claims.put(IdTokenClaimNames.ISS, "https://issuer.example.com");
344+
claims.put(IdTokenClaimNames.SUB, "rob");
345+
claims.put(IdTokenClaimNames.AUD, Collections.singletonList(clientRegistration.getClientId()));
346+
claims.put(IdTokenClaimNames.NONCE, this.nonceHash);
347+
Jwt idToken = jwt().claims(c -> c.putAll(claims)).build();
348+
349+
350+
when(this.accessTokenResponseClient.getTokenResponse(any())).thenReturn(Mono.just(accessTokenResponse));
351+
DefaultOidcUser user = new DefaultOidcUser(AuthorityUtils.createAuthorityList("ROLE_USER"), this.idToken);
352+
ArgumentCaptor<OidcUserRequest> userRequestArgCaptor = ArgumentCaptor.forClass(OidcUserRequest.class);
353+
when(this.userService.loadUser(userRequestArgCaptor.capture())).thenReturn(Mono.just(user));
354+
355+
List<GrantedAuthority> mappedAuthorities = AuthorityUtils.createAuthorityList("ROLE_OIDC_USER");
356+
GrantedAuthoritiesMapper authoritiesMapper = mock(GrantedAuthoritiesMapper.class);
357+
when(authoritiesMapper.mapAuthorities(anyCollection())).thenAnswer(
358+
(Answer<List<GrantedAuthority>>) invocation -> mappedAuthorities);
359+
when(this.jwtDecoder.decode(any())).thenReturn(Mono.just(idToken));
360+
this.manager.setJwtDecoderFactory(c -> this.jwtDecoder);
361+
this.manager.setAuthoritiesMapper(authoritiesMapper);
362+
363+
Authentication result = this.manager.authenticate(authorizationCodeAuthentication).block();
364+
365+
assertThat(result.getAuthorities()).isEqualTo(mappedAuthorities);
366+
}
367+
319368
private OAuth2AuthorizationCodeAuthenticationToken loginToken() {
320369
ClientRegistration clientRegistration = this.registration.build();
321370
Map<String, Object> attributes = new HashMap<>();

0 commit comments

Comments
 (0)