JwtAuthenticationToken Polish

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

Issue: gh-6893
This commit is contained in:
Josh Cummings 2019-05-23 10:57:10 -06:00
parent f84ab3a255
commit 936d28d328
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443
2 changed files with 26 additions and 4 deletions

View File

@ -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 @Override
public String getName() { public String getName() {

View File

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