16
16
17
17
package org .springframework .security .oauth2 .server .resource .authentication ;
18
18
19
- import java .time .Instant ;
20
- import java .util .Arrays ;
21
19
import java .util .Collection ;
22
- import java .util .HashMap ;
23
- import java .util .Map ;
24
20
25
- import org .assertj .core .util .Maps ;
26
21
import org .junit .Test ;
27
22
import org .junit .runner .RunWith ;
28
23
import org .mockito .junit .MockitoJUnitRunner ;
29
24
30
- import org .springframework .security .core .authority . SimpleGrantedAuthority ;
31
- import org .springframework .security .oauth2 . jose . jws . JwsAlgorithms ;
25
+ import org .springframework .security .core .GrantedAuthority ;
26
+ import org .springframework .security .core . authority . AuthorityUtils ;
32
27
import org .springframework .security .oauth2 .jwt .Jwt ;
33
28
34
29
import static org .assertj .core .api .Assertions .assertThat ;
35
30
import static org .assertj .core .api .Assertions .assertThatCode ;
36
- import static org .springframework .security .oauth2 .jwt . JwtClaimNames . SUB ;
31
+ import static org .springframework .security .oauth2 .jose . jws . JwsAlgorithms . RS256 ;
37
32
38
33
/**
39
34
* Tests for {@link JwtAuthenticationToken}
@@ -45,17 +40,15 @@ public class JwtAuthenticationTokenTests {
45
40
46
41
@ Test
47
42
public void getNameWhenJwtHasSubjectThenReturnsSubject () {
48
- Jwt jwt = this .jwt (Maps .newHashMap ("sub" , "Carl" ));
49
-
43
+ Jwt jwt = builder ().subject ("Carl" ).build ();
50
44
JwtAuthenticationToken token = new JwtAuthenticationToken (jwt );
51
45
52
46
assertThat (token .getName ()).isEqualTo ("Carl" );
53
47
}
54
48
55
49
@ Test
56
50
public void getNameWhenJwtHasNoSubjectThenReturnsNull () {
57
- Jwt jwt = this .jwt (Maps .newHashMap ("claim" , "value" ));
58
-
51
+ Jwt jwt = builder ().claim ("claim" , "value" ).build ();
59
52
JwtAuthenticationToken token = new JwtAuthenticationToken (jwt );
60
53
61
54
assertThat (token .getName ()).isNull ();
@@ -70,82 +63,68 @@ public void constructorWhenJwtIsNullThenThrowsException() {
70
63
71
64
@ Test
72
65
public void constructorWhenUsingCorrectParametersThenConstructedCorrectly () {
73
- Collection authorities = Arrays .asList (new SimpleGrantedAuthority ("test" ));
74
- Map claims = Maps .newHashMap ("claim" , "value" );
75
- Jwt jwt = this .jwt (claims );
76
-
66
+ Collection <GrantedAuthority > authorities = AuthorityUtils .createAuthorityList ("test" );
67
+ Jwt jwt = builder ().claim ("claim" , "value" ).build ();
77
68
JwtAuthenticationToken token = new JwtAuthenticationToken (jwt , authorities );
78
69
79
70
assertThat (token .getAuthorities ()).isEqualTo (authorities );
80
71
assertThat (token .getPrincipal ()).isEqualTo (jwt );
81
72
assertThat (token .getCredentials ()).isEqualTo (jwt );
82
73
assertThat (token .getToken ()).isEqualTo (jwt );
83
- assertThat (token .getTokenAttributes ()).isEqualTo (claims );
74
+ assertThat (token .getTokenAttributes ()).isEqualTo (jwt . getClaims () );
84
75
assertThat (token .isAuthenticated ()).isTrue ();
85
76
}
86
77
87
78
@ Test
88
79
public void constructorWhenUsingOnlyJwtThenConstructedCorrectly () {
89
- Map claims = Maps .newHashMap ("claim" , "value" );
90
- Jwt jwt = this .jwt (claims );
91
-
80
+ Jwt jwt = builder ().claim ("claim" , "value" ).build ();
92
81
JwtAuthenticationToken token = new JwtAuthenticationToken (jwt );
93
82
94
83
assertThat (token .getAuthorities ()).isEmpty ();
95
84
assertThat (token .getPrincipal ()).isEqualTo (jwt );
96
85
assertThat (token .getCredentials ()).isEqualTo (jwt );
97
86
assertThat (token .getToken ()).isEqualTo (jwt );
98
- assertThat (token .getTokenAttributes ()).isEqualTo (claims );
87
+ assertThat (token .getTokenAttributes ()).isEqualTo (jwt . getClaims () );
99
88
assertThat (token .isAuthenticated ()).isFalse ();
100
89
}
101
90
102
91
@ Test
103
92
public void getNameWhenConstructedWithJwtThenReturnsSubject () {
104
- Map claims = Maps .newHashMap (SUB , "Hayden" );
105
- Jwt jwt = this .jwt (claims );
106
-
93
+ Jwt jwt = builder ().subject ("Hayden" ).build ();
107
94
JwtAuthenticationToken token = new JwtAuthenticationToken (jwt );
108
95
109
96
assertThat (token .getName ()).isEqualTo ("Hayden" );
110
97
}
111
98
112
99
@ Test
113
100
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
-
101
+ Collection <GrantedAuthority > authorities = AuthorityUtils .createAuthorityList ("test" );
102
+ Jwt jwt = builder ().subject ("Hayden" ).build ();
118
103
JwtAuthenticationToken token = new JwtAuthenticationToken (jwt , authorities );
119
104
120
105
assertThat (token .getName ()).isEqualTo ("Hayden" );
121
106
}
122
107
123
108
@ Test
124
109
public void getNameWhenConstructedWithNameThenReturnsProvidedName () {
125
- Collection authorities = Arrays .asList (new SimpleGrantedAuthority ("test" ));
126
- Map claims = Maps .newHashMap ("claim" , "value" );
127
- Jwt jwt = this .jwt (claims );
128
-
110
+ Collection <GrantedAuthority > authorities = AuthorityUtils .createAuthorityList ("test" );
111
+ Jwt jwt = builder ().claim ("claim" , "value" ).build ();
129
112
JwtAuthenticationToken token = new JwtAuthenticationToken (jwt , authorities , "Hayden" );
130
113
131
114
assertThat (token .getName ()).isEqualTo ("Hayden" );
132
115
}
133
116
134
117
@ Test
135
118
public void getNameWhenConstructedWithNoSubjectThenReturnsNull () {
136
- Collection authorities = Arrays .asList (new SimpleGrantedAuthority ("test" ));
137
- Map claims = Maps .newHashMap ("claim" , "value" );
138
- Jwt jwt = this .jwt (claims );
119
+ Collection <GrantedAuthority > authorities = AuthorityUtils .createAuthorityList ("test" );
120
+ Jwt jwt = builder ().claim ("claim" , "value" ).build ();
139
121
140
122
assertThat (new JwtAuthenticationToken (jwt , authorities , null ).getName ()).isNull ();
141
123
assertThat (new JwtAuthenticationToken (jwt , authorities ).getName ()).isNull ();
142
124
assertThat (new JwtAuthenticationToken (jwt ).getName ()).isNull ();
143
125
}
144
126
145
- private Jwt jwt (Map <String , Object > claims ) {
146
- Map <String , Object > headers = new HashMap <>();
147
- headers .put ("alg" , JwsAlgorithms .RS256 );
148
-
149
- return new Jwt ("token" , Instant .now (), Instant .now ().plusSeconds (3600 ), headers , claims );
127
+ private Jwt .Builder builder () {
128
+ return Jwt .withTokenValue ("token" ).header ("alg" , RS256 );
150
129
}
151
130
}
0 commit comments