Simplify oauth2Login Test Support

Remove nameAttributeKey as this is easily done by constructing
a DefaultOAuth2User instance.

Issue gh-7789
Issue gh-7828
This commit is contained in:
Josh Cummings 2020-03-02 17:47:47 -07:00
parent c729fee7bc
commit 30adabb685
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443
3 changed files with 22 additions and 40 deletions

View File

@ -701,12 +701,13 @@ public class SecurityMockServerConfigurers {
* @since 5.3 * @since 5.3
*/ */
public final static class OAuth2LoginMutator implements WebTestClientConfigurer, MockServerConfigurer { public final static class OAuth2LoginMutator implements WebTestClientConfigurer, MockServerConfigurer {
private final String nameAttributeKey = "sub";
private ClientRegistration clientRegistration; private ClientRegistration clientRegistration;
private OAuth2AccessToken accessToken; private OAuth2AccessToken accessToken;
private Supplier<Collection<GrantedAuthority>> authorities = this::defaultAuthorities; private Supplier<Collection<GrantedAuthority>> authorities = this::defaultAuthorities;
private Supplier<Map<String, Object>> attributes = this::defaultAttributes; private Supplier<Map<String, Object>> attributes = this::defaultAttributes;
private String nameAttributeKey = "sub";
private Supplier<OAuth2User> oauth2User = this::defaultPrincipal; private Supplier<OAuth2User> oauth2User = this::defaultPrincipal;
private final ServerOAuth2AuthorizedClientRepository authorizedClientRepository = private final ServerOAuth2AuthorizedClientRepository authorizedClientRepository =
@ -752,28 +753,14 @@ public class SecurityMockServerConfigurers {
public OAuth2LoginMutator attributes(Consumer<Map<String, Object>> attributesConsumer) { public OAuth2LoginMutator attributes(Consumer<Map<String, Object>> attributesConsumer) {
Assert.notNull(attributesConsumer, "attributesConsumer cannot be null"); Assert.notNull(attributesConsumer, "attributesConsumer cannot be null");
this.attributes = () -> { this.attributes = () -> {
Map<String, Object> attrs = new HashMap<>(); Map<String, Object> attributes = defaultAttributes();
attrs.put(this.nameAttributeKey, "test-subject"); attributesConsumer.accept(attributes);
attributesConsumer.accept(attrs); return attributes;
return attrs;
}; };
this.oauth2User = this::defaultPrincipal; this.oauth2User = this::defaultPrincipal;
return this; return this;
} }
/**
* Use the provided key for the attribute containing the principal's name
*
* @param nameAttributeKey The attribute key to use
* @return the {@link OAuth2LoginMutator} for further configuration
*/
public OAuth2LoginMutator nameAttributeKey(String nameAttributeKey) {
Assert.notNull(nameAttributeKey, "nameAttributeKey cannot be null");
this.nameAttributeKey = nameAttributeKey;
this.oauth2User = this::defaultPrincipal;
return this;
}
/** /**
* Use the provided {@link OAuth2User} as the authenticated user. * Use the provided {@link OAuth2User} as the authenticated user.
* *
@ -856,7 +843,9 @@ public class SecurityMockServerConfigurers {
} }
private Map<String, Object> defaultAttributes() { private Map<String, Object> defaultAttributes() {
return Collections.singletonMap(this.nameAttributeKey, "test-subject"); Map<String, Object> attributes = new HashMap<>();
attributes.put(this.nameAttributeKey, "test-subject");
return attributes;
} }
private OAuth2User defaultPrincipal() { private OAuth2User defaultPrincipal() {

View File

@ -1319,12 +1319,13 @@ public final class SecurityMockMvcRequestPostProcessors {
* @since 5.3 * @since 5.3
*/ */
public final static class OAuth2LoginRequestPostProcessor implements RequestPostProcessor { public final static class OAuth2LoginRequestPostProcessor implements RequestPostProcessor {
private final String nameAttributeKey = "sub";
private ClientRegistration clientRegistration; private ClientRegistration clientRegistration;
private OAuth2AccessToken accessToken; private OAuth2AccessToken accessToken;
private Supplier<Collection<GrantedAuthority>> authorities = this::defaultAuthorities; private Supplier<Collection<GrantedAuthority>> authorities = this::defaultAuthorities;
private Supplier<Map<String, Object>> attributes = this::defaultAttributes; private Supplier<Map<String, Object>> attributes = this::defaultAttributes;
private String nameAttributeKey = "sub";
private Supplier<OAuth2User> oauth2User = this::defaultPrincipal; private Supplier<OAuth2User> oauth2User = this::defaultPrincipal;
private OAuth2LoginRequestPostProcessor(OAuth2AccessToken accessToken) { private OAuth2LoginRequestPostProcessor(OAuth2AccessToken accessToken) {
@ -1367,28 +1368,14 @@ public final class SecurityMockMvcRequestPostProcessors {
public OAuth2LoginRequestPostProcessor attributes(Consumer<Map<String, Object>> attributesConsumer) { public OAuth2LoginRequestPostProcessor attributes(Consumer<Map<String, Object>> attributesConsumer) {
Assert.notNull(attributesConsumer, "attributesConsumer cannot be null"); Assert.notNull(attributesConsumer, "attributesConsumer cannot be null");
this.attributes = () -> { this.attributes = () -> {
Map<String, Object> attrs = new HashMap<>(); Map<String, Object> attributes = defaultAttributes();
attrs.put(this.nameAttributeKey, "test-subject"); attributesConsumer.accept(attributes);
attributesConsumer.accept(attrs); return attributes;
return attrs;
}; };
this.oauth2User = this::defaultPrincipal; this.oauth2User = this::defaultPrincipal;
return this; return this;
} }
/**
* Use the provided key for the attribute containing the principal's name
*
* @param nameAttributeKey The attribute key to use
* @return the {@link OAuth2LoginRequestPostProcessor} for further configuration
*/
public OAuth2LoginRequestPostProcessor nameAttributeKey(String nameAttributeKey) {
Assert.notNull(nameAttributeKey, "nameAttributeKey cannot be null");
this.nameAttributeKey = nameAttributeKey;
this.oauth2User = this::defaultPrincipal;
return this;
}
/** /**
* Use the provided {@link OAuth2User} as the authenticated user. * Use the provided {@link OAuth2User} as the authenticated user.
* *
@ -1447,7 +1434,9 @@ public final class SecurityMockMvcRequestPostProcessors {
} }
private Map<String, Object> defaultAttributes() { private Map<String, Object> defaultAttributes() {
return Collections.singletonMap(this.nameAttributeKey, "test-subject"); Map<String, Object> attributes = new HashMap<>();
attributes.put(this.nameAttributeKey, "test-subject");
return attributes;
} }
private OAuth2User defaultPrincipal() { private OAuth2User defaultPrincipal() {

View File

@ -119,12 +119,16 @@ public class SecurityMockMvcRequestPostProcessorsOAuth2LoginTests {
@Test @Test
public void oauth2LoginWhenNameSpecifiedThenUserHasName() throws Exception { public void oauth2LoginWhenNameSpecifiedThenUserHasName() throws Exception {
OAuth2User oauth2User = new DefaultOAuth2User(
AuthorityUtils.commaSeparatedStringToAuthorityList("SCOPE_user"),
Collections.singletonMap("custom-attribute", "test-subject"),
"custom-attribute");
this.mvc.perform(get("/attributes/custom-attribute") this.mvc.perform(get("/attributes/custom-attribute")
.with(oauth2Login().nameAttributeKey("custom-attribute"))) .with(oauth2Login().oauth2User(oauth2User)))
.andExpect(content().string("test-subject")); .andExpect(content().string("test-subject"));
this.mvc.perform(get("/name") this.mvc.perform(get("/name")
.with(oauth2Login().nameAttributeKey("custom-attribute"))) .with(oauth2Login().oauth2User(oauth2User)))
.andExpect(content().string("test-subject")); .andExpect(content().string("test-subject"));
} }