1
1
package io .scalecube .security .tokens .jwt ;
2
2
3
- import java .io .IOException ;
3
+ import static io .scalecube .security .tokens .jwt .Utils .toRsaPublicKey ;
4
+
4
5
import java .security .Key ;
6
+ import java .time .Duration ;
5
7
import java .util .Collections ;
6
8
import java .util .Map ;
7
9
import java .util .Properties ;
10
12
import org .mockito .Mockito ;
11
13
import reactor .core .publisher .Mono ;
12
14
import reactor .test .StepVerifier ;
15
+ import reactor .test .scheduler .VirtualTimeScheduler ;
13
16
14
- class JwtTokenResolverTests extends BaseTest {
17
+ class JwtTokenResolverTests {
15
18
16
19
private static final Map <String , Object > BODY = Collections .singletonMap ("aud" , "aud" );
17
20
18
21
@ Test
19
- void testTokenResolver () throws IOException {
22
+ void testTokenResolver () throws Exception {
20
23
TokenWithKey tokenWithKey = new TokenWithKey ("token-and-pubkey.properties" );
21
24
22
25
JwtTokenParser tokenParser = Mockito .mock (JwtTokenParser .class );
@@ -32,7 +35,9 @@ void testTokenResolver() throws IOException {
32
35
KeyProvider keyProvider = Mockito .mock (KeyProvider .class );
33
36
Mockito .when (keyProvider .findKey (tokenWithKey .kid )).thenReturn (Mono .just (tokenWithKey .key ));
34
37
35
- JwtTokenResolverImpl tokenResolver = new JwtTokenResolverImpl (keyProvider , tokenParserFactory );
38
+ JwtTokenResolverImpl tokenResolver =
39
+ new JwtTokenResolverImpl (
40
+ keyProvider , tokenParserFactory , VirtualTimeScheduler .create (), Duration .ofSeconds (3 ));
36
41
37
42
// N times call resolve
38
43
StepVerifier .create (tokenResolver .resolve (tokenWithKey .token ).repeat (3 ))
@@ -45,7 +50,7 @@ void testTokenResolver() throws IOException {
45
50
}
46
51
47
52
@ Test
48
- void testTokenResolverWithRotatingKey () throws IOException {
53
+ void testTokenResolverWithRotatingKey () throws Exception {
49
54
TokenWithKey tokenWithKey = new TokenWithKey ("token-and-pubkey.properties" );
50
55
TokenWithKey tokenWithKeyAfterRotation =
51
56
new TokenWithKey ("token-and-pubkey.after-rotation.properties" );
@@ -70,7 +75,9 @@ void testTokenResolverWithRotatingKey() throws IOException {
70
75
Mockito .when (keyProvider .findKey (tokenWithKeyAfterRotation .kid ))
71
76
.thenReturn (Mono .just (tokenWithKeyAfterRotation .key ));
72
77
73
- JwtTokenResolverImpl tokenResolver = new JwtTokenResolverImpl (keyProvider , tokenParserFactory );
78
+ JwtTokenResolverImpl tokenResolver =
79
+ new JwtTokenResolverImpl (
80
+ keyProvider , tokenParserFactory , VirtualTimeScheduler .create (), Duration .ofSeconds (3 ));
74
81
75
82
// Call normal token first
76
83
StepVerifier .create (tokenResolver .resolve (tokenWithKey .token ))
@@ -90,7 +97,7 @@ void testTokenResolverWithRotatingKey() throws IOException {
90
97
}
91
98
92
99
@ Test
93
- void testTokenResolverWithWrongKey () throws IOException {
100
+ void testTokenResolverWithWrongKey () throws Exception {
94
101
TokenWithKey tokenWithWrongKey = new TokenWithKey ("token-and-wrong-pubkey.properties" );
95
102
96
103
JwtTokenParser tokenParser = Mockito .mock (JwtTokenParser .class );
@@ -106,7 +113,9 @@ void testTokenResolverWithWrongKey() throws IOException {
106
113
Mockito .when (keyProvider .findKey (tokenWithWrongKey .kid ))
107
114
.thenReturn (Mono .just (tokenWithWrongKey .key ));
108
115
109
- JwtTokenResolverImpl tokenResolver = new JwtTokenResolverImpl (keyProvider , tokenParserFactory );
116
+ JwtTokenResolverImpl tokenResolver =
117
+ new JwtTokenResolverImpl (
118
+ keyProvider , tokenParserFactory , VirtualTimeScheduler .create (), Duration .ofSeconds (3 ));
110
119
111
120
// Must fail (retry N times)
112
121
StepVerifier .create (tokenResolver .resolve (tokenWithWrongKey .token ).retry (1 ))
@@ -118,7 +127,7 @@ void testTokenResolverWithWrongKey() throws IOException {
118
127
}
119
128
120
129
@ Test
121
- void testTokenResolverWhenKeyProviderFailing () throws IOException {
130
+ void testTokenResolverWhenKeyProviderFailing () throws Exception {
122
131
TokenWithKey tokenWithKey = new TokenWithKey ("token-and-pubkey.properties" );
123
132
124
133
JwtTokenParser tokenParser = Mockito .mock (JwtTokenParser .class );
@@ -134,7 +143,9 @@ void testTokenResolverWhenKeyProviderFailing() throws IOException {
134
143
KeyProvider keyProvider = Mockito .mock (KeyProvider .class );
135
144
Mockito .when (keyProvider .findKey (tokenWithKey .kid )).thenThrow (RuntimeException .class );
136
145
137
- JwtTokenResolverImpl tokenResolver = new JwtTokenResolverImpl (keyProvider , tokenParserFactory );
146
+ JwtTokenResolverImpl tokenResolver =
147
+ new JwtTokenResolverImpl (
148
+ keyProvider , tokenParserFactory , VirtualTimeScheduler .create (), Duration .ofSeconds (3 ));
138
149
139
150
// Must fail with "hola" (retry N times)
140
151
StepVerifier .create (tokenResolver .resolve (tokenWithKey .token ).retry (1 )).expectError ().verify ();
@@ -149,13 +160,13 @@ static class TokenWithKey {
149
160
final Key key ;
150
161
final String kid ;
151
162
152
- TokenWithKey (String s ) throws IOException {
163
+ TokenWithKey (String s ) throws Exception {
153
164
ClassLoader classLoader = Thread .currentThread ().getContextClassLoader ();
154
165
Properties props = new Properties ();
155
166
props .load (classLoader .getResourceAsStream (s ));
156
167
this .token = props .getProperty ("token" );
157
168
this .kid = props .getProperty ("kid" );
158
- this .key = Utils . getRsaPublicKey (props .getProperty ("n" ), props .getProperty ("e" ));
169
+ this .key = toRsaPublicKey (props .getProperty ("n" ), props .getProperty ("e" ));
159
170
}
160
171
}
161
172
}
0 commit comments