11package io .scalecube .security .tokens .jwt ;
22
3- import java .io .IOException ;
3+ import static io .scalecube .security .tokens .jwt .Utils .toRsaPublicKey ;
4+
45import java .security .Key ;
6+ import java .time .Duration ;
57import java .util .Collections ;
68import java .util .Map ;
79import java .util .Properties ;
1012import org .mockito .Mockito ;
1113import reactor .core .publisher .Mono ;
1214import reactor .test .StepVerifier ;
15+ import reactor .test .scheduler .VirtualTimeScheduler ;
1316
14- class JwtTokenResolverTests extends BaseTest {
17+ class JwtTokenResolverTests {
1518
1619 private static final Map <String , Object > BODY = Collections .singletonMap ("aud" , "aud" );
1720
1821 @ Test
19- void testTokenResolver () throws IOException {
22+ void testTokenResolver () throws Exception {
2023 TokenWithKey tokenWithKey = new TokenWithKey ("token-and-pubkey.properties" );
2124
2225 JwtTokenParser tokenParser = Mockito .mock (JwtTokenParser .class );
@@ -32,7 +35,9 @@ void testTokenResolver() throws IOException {
3235 KeyProvider keyProvider = Mockito .mock (KeyProvider .class );
3336 Mockito .when (keyProvider .findKey (tokenWithKey .kid )).thenReturn (Mono .just (tokenWithKey .key ));
3437
35- JwtTokenResolverImpl tokenResolver = new JwtTokenResolverImpl (keyProvider , tokenParserFactory );
38+ JwtTokenResolverImpl tokenResolver =
39+ new JwtTokenResolverImpl (
40+ keyProvider , tokenParserFactory , VirtualTimeScheduler .create (), Duration .ofSeconds (3 ));
3641
3742 // N times call resolve
3843 StepVerifier .create (tokenResolver .resolve (tokenWithKey .token ).repeat (3 ))
@@ -45,7 +50,7 @@ void testTokenResolver() throws IOException {
4550 }
4651
4752 @ Test
48- void testTokenResolverWithRotatingKey () throws IOException {
53+ void testTokenResolverWithRotatingKey () throws Exception {
4954 TokenWithKey tokenWithKey = new TokenWithKey ("token-and-pubkey.properties" );
5055 TokenWithKey tokenWithKeyAfterRotation =
5156 new TokenWithKey ("token-and-pubkey.after-rotation.properties" );
@@ -70,7 +75,9 @@ void testTokenResolverWithRotatingKey() throws IOException {
7075 Mockito .when (keyProvider .findKey (tokenWithKeyAfterRotation .kid ))
7176 .thenReturn (Mono .just (tokenWithKeyAfterRotation .key ));
7277
73- JwtTokenResolverImpl tokenResolver = new JwtTokenResolverImpl (keyProvider , tokenParserFactory );
78+ JwtTokenResolverImpl tokenResolver =
79+ new JwtTokenResolverImpl (
80+ keyProvider , tokenParserFactory , VirtualTimeScheduler .create (), Duration .ofSeconds (3 ));
7481
7582 // Call normal token first
7683 StepVerifier .create (tokenResolver .resolve (tokenWithKey .token ))
@@ -90,7 +97,7 @@ void testTokenResolverWithRotatingKey() throws IOException {
9097 }
9198
9299 @ Test
93- void testTokenResolverWithWrongKey () throws IOException {
100+ void testTokenResolverWithWrongKey () throws Exception {
94101 TokenWithKey tokenWithWrongKey = new TokenWithKey ("token-and-wrong-pubkey.properties" );
95102
96103 JwtTokenParser tokenParser = Mockito .mock (JwtTokenParser .class );
@@ -106,7 +113,9 @@ void testTokenResolverWithWrongKey() throws IOException {
106113 Mockito .when (keyProvider .findKey (tokenWithWrongKey .kid ))
107114 .thenReturn (Mono .just (tokenWithWrongKey .key ));
108115
109- JwtTokenResolverImpl tokenResolver = new JwtTokenResolverImpl (keyProvider , tokenParserFactory );
116+ JwtTokenResolverImpl tokenResolver =
117+ new JwtTokenResolverImpl (
118+ keyProvider , tokenParserFactory , VirtualTimeScheduler .create (), Duration .ofSeconds (3 ));
110119
111120 // Must fail (retry N times)
112121 StepVerifier .create (tokenResolver .resolve (tokenWithWrongKey .token ).retry (1 ))
@@ -118,7 +127,7 @@ void testTokenResolverWithWrongKey() throws IOException {
118127 }
119128
120129 @ Test
121- void testTokenResolverWhenKeyProviderFailing () throws IOException {
130+ void testTokenResolverWhenKeyProviderFailing () throws Exception {
122131 TokenWithKey tokenWithKey = new TokenWithKey ("token-and-pubkey.properties" );
123132
124133 JwtTokenParser tokenParser = Mockito .mock (JwtTokenParser .class );
@@ -134,7 +143,9 @@ void testTokenResolverWhenKeyProviderFailing() throws IOException {
134143 KeyProvider keyProvider = Mockito .mock (KeyProvider .class );
135144 Mockito .when (keyProvider .findKey (tokenWithKey .kid )).thenThrow (RuntimeException .class );
136145
137- JwtTokenResolverImpl tokenResolver = new JwtTokenResolverImpl (keyProvider , tokenParserFactory );
146+ JwtTokenResolverImpl tokenResolver =
147+ new JwtTokenResolverImpl (
148+ keyProvider , tokenParserFactory , VirtualTimeScheduler .create (), Duration .ofSeconds (3 ));
138149
139150 // Must fail with "hola" (retry N times)
140151 StepVerifier .create (tokenResolver .resolve (tokenWithKey .token ).retry (1 )).expectError ().verify ();
@@ -149,13 +160,13 @@ static class TokenWithKey {
149160 final Key key ;
150161 final String kid ;
151162
152- TokenWithKey (String s ) throws IOException {
163+ TokenWithKey (String s ) throws Exception {
153164 ClassLoader classLoader = Thread .currentThread ().getContextClassLoader ();
154165 Properties props = new Properties ();
155166 props .load (classLoader .getResourceAsStream (s ));
156167 this .token = props .getProperty ("token" );
157168 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" ));
159170 }
160171 }
161172}
0 commit comments