Skip to content

Commit 936d28d

Browse files
committed
JwtAuthenticationToken Polish
Aligned JavaDoc and added tests to better assess getName's functionality. Issue: gh-6893
1 parent f84ab3a commit 936d28d

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public Map<String, Object> getTokenAttributes() {
8282
}
8383

8484
/**
85-
* The {@link Jwt}'s subject, if any
85+
* The principal name which is, by default, the {@link Jwt}'s subject
8686
*/
8787
@Override
8888
public String getName() {

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import static org.assertj.core.api.Assertions.assertThat;
3535
import static org.assertj.core.api.Assertions.assertThatCode;
36+
import static org.springframework.security.oauth2.jwt.JwtClaimNames.SUB;
3637

3738
/**
3839
* Tests for {@link JwtAuthenticationToken}
@@ -99,8 +100,8 @@ public void constructorWhenUsingOnlyJwtThenConstructedCorrectly() {
99100
}
100101

101102
@Test
102-
public void constructorWhenProvidingJwtAndAuthoritiesThenSetsNameCorrectly() {
103-
Map claims = Maps.newHashMap("sub", "Hayden");
103+
public void getNameWhenConstructedWithJwtThenReturnsSubject() {
104+
Map claims = Maps.newHashMap(SUB, "Hayden");
104105
Jwt jwt = this.jwt(claims);
105106

106107
JwtAuthenticationToken token = new JwtAuthenticationToken(jwt);
@@ -109,8 +110,18 @@ public void constructorWhenProvidingJwtAndAuthoritiesThenSetsNameCorrectly() {
109110
}
110111

111112
@Test
112-
public void constructorWhenUsingAllParametersThenReturnsCorrectName() {
113+
public void getNameWhenConstructedWithJwtAndAuthoritiesThenReturnsSubject() {
114+
Collection authorities = Arrays.asList(new SimpleGrantedAuthority("test"));
115+
Map claims = Maps.newHashMap(SUB, "Hayden");
116+
Jwt jwt = this.jwt(claims);
117+
118+
JwtAuthenticationToken token = new JwtAuthenticationToken(jwt, authorities);
113119

120+
assertThat(token.getName()).isEqualTo("Hayden");
121+
}
122+
123+
@Test
124+
public void getNameWhenConstructedWithNameThenReturnsProvidedName() {
114125
Collection authorities = Arrays.asList(new SimpleGrantedAuthority("test"));
115126
Map claims = Maps.newHashMap("claim", "value");
116127
Jwt jwt = this.jwt(claims);
@@ -120,6 +131,17 @@ public void constructorWhenUsingAllParametersThenReturnsCorrectName() {
120131
assertThat(token.getName()).isEqualTo("Hayden");
121132
}
122133

134+
@Test
135+
public void getNameWhenConstructedWithNoSubjectThenReturnsNull() {
136+
Collection authorities = Arrays.asList(new SimpleGrantedAuthority("test"));
137+
Map claims = Maps.newHashMap("claim", "value");
138+
Jwt jwt = this.jwt(claims);
139+
140+
assertThat(new JwtAuthenticationToken(jwt, authorities, null).getName()).isNull();
141+
assertThat(new JwtAuthenticationToken(jwt, authorities).getName()).isNull();
142+
assertThat(new JwtAuthenticationToken(jwt).getName()).isNull();
143+
}
144+
123145
private Jwt jwt(Map<String, Object> claims) {
124146
Map<String, Object> headers = new HashMap<>();
125147
headers.put("alg", JwsAlgorithms.RS256);

0 commit comments

Comments
 (0)