2
0
Эх сурвалжийг харах

Simplify oauth2Login Test Support

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

Issue gh-7789
Issue gh-7828
Josh Cummings 5 жил өмнө
parent
commit
30adabb685

+ 8 - 19
test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurers.java

@@ -701,12 +701,13 @@ public class SecurityMockServerConfigurers {
 	 * @since 5.3
 	 */
 	public final static class OAuth2LoginMutator implements WebTestClientConfigurer, MockServerConfigurer {
+		private final String nameAttributeKey = "sub";
+
 		private ClientRegistration clientRegistration;
 		private OAuth2AccessToken accessToken;
 
 		private Supplier<Collection<GrantedAuthority>> authorities = this::defaultAuthorities;
 		private Supplier<Map<String, Object>> attributes = this::defaultAttributes;
-		private String nameAttributeKey = "sub";
 		private Supplier<OAuth2User> oauth2User = this::defaultPrincipal;
 
 		private final ServerOAuth2AuthorizedClientRepository authorizedClientRepository =
@@ -752,28 +753,14 @@ public class SecurityMockServerConfigurers {
 		public OAuth2LoginMutator attributes(Consumer<Map<String, Object>> attributesConsumer) {
 			Assert.notNull(attributesConsumer, "attributesConsumer cannot be null");
 			this.attributes = () -> {
-				Map<String, Object> attrs = new HashMap<>();
-				attrs.put(this.nameAttributeKey, "test-subject");
-				attributesConsumer.accept(attrs);
-				return attrs;
+				Map<String, Object> attributes = defaultAttributes();
+				attributesConsumer.accept(attributes);
+				return attributes;
 			};
 			this.oauth2User = this::defaultPrincipal;
 			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.
 		 *
@@ -856,7 +843,9 @@ public class SecurityMockServerConfigurers {
 		}
 
 		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() {

+ 8 - 19
test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessors.java

@@ -1319,12 +1319,13 @@ public final class SecurityMockMvcRequestPostProcessors {
 	 * @since 5.3
 	 */
 	public final static class OAuth2LoginRequestPostProcessor implements RequestPostProcessor {
+		private final String nameAttributeKey = "sub";
+
 		private ClientRegistration clientRegistration;
 		private OAuth2AccessToken accessToken;
 
 		private Supplier<Collection<GrantedAuthority>> authorities = this::defaultAuthorities;
 		private Supplier<Map<String, Object>> attributes = this::defaultAttributes;
-		private String nameAttributeKey = "sub";
 		private Supplier<OAuth2User> oauth2User = this::defaultPrincipal;
 
 		private OAuth2LoginRequestPostProcessor(OAuth2AccessToken accessToken) {
@@ -1367,28 +1368,14 @@ public final class SecurityMockMvcRequestPostProcessors {
 		public OAuth2LoginRequestPostProcessor attributes(Consumer<Map<String, Object>> attributesConsumer) {
 			Assert.notNull(attributesConsumer, "attributesConsumer cannot be null");
 			this.attributes = () -> {
-				Map<String, Object> attrs = new HashMap<>();
-				attrs.put(this.nameAttributeKey, "test-subject");
-				attributesConsumer.accept(attrs);
-				return attrs;
+				Map<String, Object> attributes = defaultAttributes();
+				attributesConsumer.accept(attributes);
+				return attributes;
 			};
 			this.oauth2User = this::defaultPrincipal;
 			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.
 		 *
@@ -1447,7 +1434,9 @@ public final class SecurityMockMvcRequestPostProcessors {
 		}
 
 		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() {

+ 6 - 2
test/src/test/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessorsOAuth2LoginTests.java

@@ -119,12 +119,16 @@ public class SecurityMockMvcRequestPostProcessorsOAuth2LoginTests {
 
 	@Test
 	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")
-				.with(oauth2Login().nameAttributeKey("custom-attribute")))
+				.with(oauth2Login().oauth2User(oauth2User)))
 				.andExpect(content().string("test-subject"));
 
 		this.mvc.perform(get("/name")
-				.with(oauth2Login().nameAttributeKey("custom-attribute")))
+				.with(oauth2Login().oauth2User(oauth2User)))
 				.andExpect(content().string("test-subject"));
 	}