From 936d28d328e505c75ec028e24ba5bcdea40e69f8 Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Thu, 23 May 2019 10:57:10 -0600 Subject: [PATCH] JwtAuthenticationToken Polish Aligned JavaDoc and added tests to better assess getName's functionality. Issue: gh-6893 --- .../JwtAuthenticationToken.java | 2 +- .../JwtAuthenticationTokenTests.java | 28 +++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtAuthenticationToken.java b/oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtAuthenticationToken.java index dec49412f4..44edd9bb55 100644 --- a/oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtAuthenticationToken.java +++ b/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() { diff --git a/oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/authentication/JwtAuthenticationTokenTests.java b/oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/authentication/JwtAuthenticationTokenTests.java index 090c648fd6..3eb7d60241 100644 --- a/oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/authentication/JwtAuthenticationTokenTests.java +++ b/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 claims) { Map headers = new HashMap<>(); headers.put("alg", JwsAlgorithms.RS256);