22
22
import io .vertx .ext .auth .authentication .CredentialValidationException ;
23
23
import io .vertx .ext .auth .authentication .TokenCredentials ;
24
24
import io .vertx .ext .auth .authorization .PermissionBasedAuthorization ;
25
+ import io .vertx .ext .auth .impl .jose .JWK ;
25
26
import io .vertx .ext .auth .jwt .JWTAuth ;
26
27
import io .vertx .ext .auth .jwt .JWTAuthOptions ;
27
28
import io .vertx .ext .auth .jwt .authorization .JWTAuthorization ;
34
35
import org .junit .Test ;
35
36
import org .junit .runner .RunWith ;
36
37
38
+ import javax .crypto .Mac ;
39
+ import java .io .InputStream ;
37
40
import java .nio .charset .StandardCharsets ;
41
+ import java .security .KeyStore ;
42
+ import java .security .KeyStoreException ;
43
+ import java .text .SimpleDateFormat ;
44
+ import java .time .*;
45
+ import java .time .temporal .ChronoUnit ;
46
+ import java .time .temporal .TemporalUnit ;
38
47
import java .util .Base64 ;
48
+ import java .util .Date ;
49
+ import java .util .List ;
50
+ import java .util .Optional ;
51
+ import java .util .concurrent .TimeUnit ;
39
52
40
53
import static org .junit .Assert .assertNotEquals ;
41
54
@@ -48,14 +61,18 @@ public class JWTAuthProviderTest {
48
61
private JWTAuth authProvider ;
49
62
50
63
// {"sub":"Paulo","exp":1747055313,"iat":1431695313,"permissions":["read","write","execute"],"roles":["admin","developer","user"]}
51
- private static final String JWT_VALID = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJQYXVsbyIsImV4cCI6MTc0NzA1NTMxMywiaWF0IjoxNDMxNjk1MzEzLCJwZXJtaXNzaW9ucyI6WyJyZWFkIiwid3JpdGUiLCJleGVjdXRlIl0sInJvbGVzIjpbImFkbWluIiwiZGV2ZWxvcGVyIiwidXNlciJdfQ.UdA6oYDn9s_k7uogFFg8jvKmq9RgITBnlq4xV6JGsCY " ;
64
+ private static final String JWT_CLAIMS = "{ \" sub \" : \" Paulo \" , \" exp \" :1747055313, \" iat \" :1431695313, \" permissions \" :[ \" read \" , \" write \" , \" execute \" ], \" roles \" :[ \" admin \" , \" developer \" , \" user \" ]} " ;
52
65
53
66
// {"sub":"Paulo","iat":1400159434,"exp":1400245834,"roles":["admin","developer","user"],"permissions":["read","write","execute"]}
54
67
private static final String JWT_INVALID = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJQYXVsbyIsImlhdCI6MTQwMDE1OTQzNCwiZXhwIjoxNDAwMjQ1ODM0LCJyb2xlcyI6WyJhZG1pbiIsImRldmVsb3BlciIsInVzZXIiXSwicGVybWlzc2lvbnMiOlsicmVhZCIsIndyaXRlIiwiZXhlY3V0ZSJdfQ==.NhHul0OFlmUaatFwNeGBbshVNzac2z_3twEEg57x80s=" ;
55
68
69
+ private final long exp = LocalDateTime .now ().plusDays (1 ).toEpochSecond (ZoneOffset .UTC );
70
+ private String jwtValid ;
71
+
56
72
@ Before
57
73
public void setUp () throws Exception {
58
74
authProvider = JWTAuth .create (rule .vertx (), getConfig ());
75
+ jwtValid = authProvider .generateToken (new JsonObject (JWT_CLAIMS ).put ("exp" , exp ));
59
76
}
60
77
61
78
private JWTAuthOptions getConfig () {
@@ -90,7 +107,7 @@ public void testCreateWithoutFailureWhenAliasDoesNotExist() {
90
107
public void testValidJWT (TestContext should ) {
91
108
final Async test = should .async ();
92
109
93
- TokenCredentials authInfo = new TokenCredentials (JWT_VALID );
110
+ TokenCredentials authInfo = new TokenCredentials (jwtValid );
94
111
authProvider
95
112
.authenticate (authInfo )
96
113
.onFailure (should ::fail )
@@ -137,7 +154,7 @@ public void testInvalidJWT(TestContext should) {
137
154
public void testJWTValidPermission (TestContext should ) {
138
155
final Async test = should .async ();
139
156
140
- TokenCredentials authInfo = new TokenCredentials (JWT_VALID );
157
+ TokenCredentials authInfo = new TokenCredentials (jwtValid );
141
158
authProvider .authenticate (authInfo )
142
159
.onFailure (should ::fail )
143
160
.onSuccess (user -> {
@@ -154,7 +171,7 @@ public void testJWTValidPermission(TestContext should) {
154
171
public void testJWTInvalidPermission (TestContext should ) {
155
172
final Async test = should .async ();
156
173
157
- TokenCredentials authInfo = new TokenCredentials (JWT_VALID );
174
+ TokenCredentials authInfo = new TokenCredentials (jwtValid );
158
175
authProvider .authenticate (authInfo )
159
176
.onFailure (should ::fail )
160
177
.onSuccess (user -> {
@@ -172,7 +189,7 @@ public void testGenerateNewToken(TestContext should) {
172
189
173
190
JsonObject payload = new JsonObject ()
174
191
.put ("sub" , "Paulo" )
175
- .put ("exp" , 1747055313 )
192
+ .put ("exp" , exp )
176
193
.put ("iat" , 1431695313 )
177
194
.put ("permissions" , new JsonArray ()
178
195
.add ("read" )
@@ -185,7 +202,7 @@ public void testGenerateNewToken(TestContext should) {
185
202
186
203
String token = authProvider .generateToken (payload , new JWTOptions ().setSubject ("Paulo" ));
187
204
should .assertNotNull (token );
188
- should .assertEquals (JWT_VALID , token );
205
+ should .assertEquals (jwtValid , token );
189
206
}
190
207
191
208
@ Test
@@ -506,7 +523,7 @@ public void testAcceptInvalidJWT(TestContext should) {
506
523
}
507
524
508
525
@ Test
509
- public void testAlgNone (TestContext should ) {
526
+ public void testAlgNone (TestContext should ) throws Exception {
510
527
final Async test = should .async ();
511
528
512
529
JWTAuth authProvider = JWTAuth .create (rule .vertx (), new JWTAuthOptions ());
@@ -515,7 +532,7 @@ public void testAlgNone(TestContext should) {
515
532
.put ("sub" , "UserUnderTest" )
516
533
.put ("aud" , "OrganizationUnderTest" )
517
534
.put ("iat" , 1431695313 )
518
- .put ("exp" , 1747055313 )
535
+ .put ("exp" , LocalDateTime . now (). plusDays ( 1 ). toEpochSecond ( ZoneOffset . UTC ) )
519
536
.put ("roles" , new JsonArray ().add ("admin" ).add ("developer" ).add ("user" ))
520
537
.put ("permissions" , new JsonArray ().add ("read" ).add ("write" ).add ("execute" ));
521
538
0 commit comments