Parcourir la source

JwtAuthenticationToken Polish

Aligned JavaDoc and added tests to better assess getName's
functionality.

Issue: gh-6893
Josh Cummings il y a 6 ans
Parent
commit
936d28d328

+ 1 - 1
oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtAuthenticationToken.java

@@ -82,7 +82,7 @@ public class JwtAuthenticationToken extends AbstractOAuth2TokenAuthenticationTok
 	}
 
 	/**
-	 * The {@link Jwt}'s subject, if any
+	 * The principal name which is, by default, the {@link Jwt}'s subject
 	 */
 	@Override
 	public String getName() {

+ 25 - 3
oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/authentication/JwtAuthenticationTokenTests.java

@@ -33,6 +33,7 @@ import org.springframework.security.oauth2.jwt.Jwt;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.springframework.security.oauth2.jwt.JwtClaimNames.SUB;
 
 /**
  * Tests for {@link JwtAuthenticationToken}
@@ -99,8 +100,8 @@ public class JwtAuthenticationTokenTests {
 	}
 
 	@Test
-	public void constructorWhenProvidingJwtAndAuthoritiesThenSetsNameCorrectly() {
-		Map claims = Maps.newHashMap("sub", "Hayden");
+	public void getNameWhenConstructedWithJwtThenReturnsSubject() {
+		Map claims = Maps.newHashMap(SUB, "Hayden");
 		Jwt jwt = this.jwt(claims);
 
 		JwtAuthenticationToken token = new JwtAuthenticationToken(jwt);
@@ -109,8 +110,18 @@ public class JwtAuthenticationTokenTests {
 	}
 
 	@Test
-	public void constructorWhenUsingAllParametersThenReturnsCorrectName() {
+	public void getNameWhenConstructedWithJwtAndAuthoritiesThenReturnsSubject() {
+		Collection authorities = Arrays.asList(new SimpleGrantedAuthority("test"));
+		Map claims = Maps.newHashMap(SUB, "Hayden");
+		Jwt jwt = this.jwt(claims);
+
+		JwtAuthenticationToken token = new JwtAuthenticationToken(jwt, authorities);
 
+		assertThat(token.getName()).isEqualTo("Hayden");
+	}
+
+	@Test
+	public void getNameWhenConstructedWithNameThenReturnsProvidedName() {
 		Collection authorities = Arrays.asList(new SimpleGrantedAuthority("test"));
 		Map claims = Maps.newHashMap("claim", "value");
 		Jwt jwt = this.jwt(claims);
@@ -120,6 +131,17 @@ public class JwtAuthenticationTokenTests {
 		assertThat(token.getName()).isEqualTo("Hayden");
 	}
 
+	@Test
+	public void getNameWhenConstructedWithNoSubjectThenReturnsNull() {
+		Collection authorities = Arrays.asList(new SimpleGrantedAuthority("test"));
+		Map claims = Maps.newHashMap("claim", "value");
+		Jwt jwt = this.jwt(claims);
+
+		assertThat(new JwtAuthenticationToken(jwt, authorities, null).getName()).isNull();
+		assertThat(new JwtAuthenticationToken(jwt, authorities).getName()).isNull();
+		assertThat(new JwtAuthenticationToken(jwt).getName()).isNull();
+	}
+
 	private Jwt jwt(Map<String, Object> claims) {
 		Map<String, Object> headers = new HashMap<>();
 		headers.put("alg", JwsAlgorithms.RS256);