Skip to content

Commit 02ab6e0

Browse files
committed
Simplify oauth2Login Test Support
Remove nameAttributeKey as this is easily done by constructing a DefaultOAuth2User instance. Issue spring-projectsgh-7789 Issue spring-projectsgh-7828
1 parent 0ba3ff6 commit 02ab6e0

File tree

3 files changed

+22
-40
lines changed

3 files changed

+22
-40
lines changed

test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurers.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -701,12 +701,13 @@ private Instant getInstant(Map<String, Object> attributes, String name) {
701701
* @since 5.3
702702
*/
703703
public final static class OAuth2LoginMutator implements WebTestClientConfigurer, MockServerConfigurer {
704+
private final String nameAttributeKey = "sub";
705+
704706
private ClientRegistration clientRegistration;
705707
private OAuth2AccessToken accessToken;
706708

707709
private Supplier<Collection<GrantedAuthority>> authorities = this::defaultAuthorities;
708710
private Supplier<Map<String, Object>> attributes = this::defaultAttributes;
709-
private String nameAttributeKey = "sub";
710711
private Supplier<OAuth2User> oauth2User = this::defaultPrincipal;
711712

712713
private final ServerOAuth2AuthorizedClientRepository authorizedClientRepository =
@@ -752,28 +753,14 @@ public OAuth2LoginMutator authorities(GrantedAuthority... authorities) {
752753
public OAuth2LoginMutator attributes(Consumer<Map<String, Object>> attributesConsumer) {
753754
Assert.notNull(attributesConsumer, "attributesConsumer cannot be null");
754755
this.attributes = () -> {
755-
Map<String, Object> attrs = new HashMap<>();
756-
attrs.put(this.nameAttributeKey, "test-subject");
757-
attributesConsumer.accept(attrs);
758-
return attrs;
756+
Map<String, Object> attributes = defaultAttributes();
757+
attributesConsumer.accept(attributes);
758+
return attributes;
759759
};
760760
this.oauth2User = this::defaultPrincipal;
761761
return this;
762762
}
763763

764-
/**
765-
* Use the provided key for the attribute containing the principal's name
766-
*
767-
* @param nameAttributeKey The attribute key to use
768-
* @return the {@link OAuth2LoginMutator} for further configuration
769-
*/
770-
public OAuth2LoginMutator nameAttributeKey(String nameAttributeKey) {
771-
Assert.notNull(nameAttributeKey, "nameAttributeKey cannot be null");
772-
this.nameAttributeKey = nameAttributeKey;
773-
this.oauth2User = this::defaultPrincipal;
774-
return this;
775-
}
776-
777764
/**
778765
* Use the provided {@link OAuth2User} as the authenticated user.
779766
*
@@ -856,7 +843,9 @@ private Collection<GrantedAuthority> defaultAuthorities() {
856843
}
857844

858845
private Map<String, Object> defaultAttributes() {
859-
return Collections.singletonMap(this.nameAttributeKey, "test-subject");
846+
Map<String, Object> attributes = new HashMap<>();
847+
attributes.put(this.nameAttributeKey, "test-subject");
848+
return attributes;
860849
}
861850

862851
private OAuth2User defaultPrincipal() {

test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessors.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,12 +1319,13 @@ private Instant getInstant(Map<String, Object> attributes, String name) {
13191319
* @since 5.3
13201320
*/
13211321
public final static class OAuth2LoginRequestPostProcessor implements RequestPostProcessor {
1322+
private final String nameAttributeKey = "sub";
1323+
13221324
private ClientRegistration clientRegistration;
13231325
private OAuth2AccessToken accessToken;
13241326

13251327
private Supplier<Collection<GrantedAuthority>> authorities = this::defaultAuthorities;
13261328
private Supplier<Map<String, Object>> attributes = this::defaultAttributes;
1327-
private String nameAttributeKey = "sub";
13281329
private Supplier<OAuth2User> oauth2User = this::defaultPrincipal;
13291330

13301331
private OAuth2LoginRequestPostProcessor(OAuth2AccessToken accessToken) {
@@ -1367,28 +1368,14 @@ public OAuth2LoginRequestPostProcessor authorities(GrantedAuthority... authoriti
13671368
public OAuth2LoginRequestPostProcessor attributes(Consumer<Map<String, Object>> attributesConsumer) {
13681369
Assert.notNull(attributesConsumer, "attributesConsumer cannot be null");
13691370
this.attributes = () -> {
1370-
Map<String, Object> attrs = new HashMap<>();
1371-
attrs.put(this.nameAttributeKey, "test-subject");
1372-
attributesConsumer.accept(attrs);
1373-
return attrs;
1371+
Map<String, Object> attributes = defaultAttributes();
1372+
attributesConsumer.accept(attributes);
1373+
return attributes;
13741374
};
13751375
this.oauth2User = this::defaultPrincipal;
13761376
return this;
13771377
}
13781378

1379-
/**
1380-
* Use the provided key for the attribute containing the principal's name
1381-
*
1382-
* @param nameAttributeKey The attribute key to use
1383-
* @return the {@link OAuth2LoginRequestPostProcessor} for further configuration
1384-
*/
1385-
public OAuth2LoginRequestPostProcessor nameAttributeKey(String nameAttributeKey) {
1386-
Assert.notNull(nameAttributeKey, "nameAttributeKey cannot be null");
1387-
this.nameAttributeKey = nameAttributeKey;
1388-
this.oauth2User = this::defaultPrincipal;
1389-
return this;
1390-
}
1391-
13921379
/**
13931380
* Use the provided {@link OAuth2User} as the authenticated user.
13941381
*
@@ -1447,7 +1434,9 @@ private Collection<GrantedAuthority> defaultAuthorities() {
14471434
}
14481435

14491436
private Map<String, Object> defaultAttributes() {
1450-
return Collections.singletonMap(this.nameAttributeKey, "test-subject");
1437+
Map<String, Object> attributes = new HashMap<>();
1438+
attributes.put(this.nameAttributeKey, "test-subject");
1439+
return attributes;
14511440
}
14521441

14531442
private OAuth2User defaultPrincipal() {

test/src/test/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessorsOAuth2LoginTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,16 @@ public void oauth2LoginWhenAttributeSpecifiedThenUserHasAttribute() throws Excep
119119

120120
@Test
121121
public void oauth2LoginWhenNameSpecifiedThenUserHasName() throws Exception {
122+
OAuth2User oauth2User = new DefaultOAuth2User(
123+
AuthorityUtils.commaSeparatedStringToAuthorityList("SCOPE_user"),
124+
Collections.singletonMap("custom-attribute", "test-subject"),
125+
"custom-attribute");
122126
this.mvc.perform(get("/attributes/custom-attribute")
123-
.with(oauth2Login().nameAttributeKey("custom-attribute")))
127+
.with(oauth2Login().oauth2User(oauth2User)))
124128
.andExpect(content().string("test-subject"));
125129

126130
this.mvc.perform(get("/name")
127-
.with(oauth2Login().nameAttributeKey("custom-attribute")))
131+
.with(oauth2Login().oauth2User(oauth2User)))
128132
.andExpect(content().string("test-subject"));
129133
}
130134

0 commit comments

Comments
 (0)