2323import com .auth0 .jwt .PemUtils ;
2424import com .auth0 .jwt .TokenUtils ;
2525import com .auth0 .jwt .algorithms .Algorithm ;
26+ import com .auth0 .jwt .exceptions .SignatureVerificationException ;
2627import com .auth0 .jwt .interfaces .DecodedJWT ;
2728import com .auth0 .jwt .interfaces .ECDSAKeyProvider ;
2829import com .auth0 .jwt .interfaces .RSAKeyProvider ;
4950public class JWTCreatorTest {
5051
5152 private static final String PRIVATE_KEY_FILE_RSA = "src/test/resources/rsa-private-base16-64.pem" ;
53+ private static final String PUBLIC_KEY_FILE_RSA = "src/test/resources/rsa-public-base16-64.pem" ;
54+ private static final String PRIVATE_KEY_FILE = "src/test/resources/rsa-private.pem" ;
55+ private static final String PUBLIC_KEY_FILE = "src/test/resources/rsa-public.pem" ;
56+ private static final String PUBLIC_KEY_FILE_INVALID = "src/test/resources/rsa-public_invalid.pem" ;
57+ private static final String PRIVATE_KEY_FILE_PKCS8 = "./src/test/resources/example_key_pcks8.pem" ;
58+ private static final String PEM_FILE = "./src/main/java/com/auth0/jwt/algorithms/jwks.pem" ;
59+ private static final String JWKS_FILE = "./jwksRSA.json" ;
60+ private static final String JWKS_FILE_ANOTHER_EXAMPLE = "./src/test/resources/example_jwk.json" ;
61+ private static final String INVALID_JWKS_FILE = "./jwksRSA.doc" ;
5262 private static final String PRIVATE_KEY_FILE_EC_256 = "src/test/resources/ec256-key-private.pem" ;
5363
5464 @ Rule
@@ -119,8 +129,79 @@ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase16() throws Excepti
119129 .sign (algorithm , EncodeType .Base16 );
120130
121131 JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("auth0" ).build ();
122- DecodedJWT decoded = jwt .decode16BytesWithX509 (signed ,"./jwksRSA.json" , "./src/main/java/com/auth0/jwt/algorithms/jwks.pem" );
123- algorithm .verifyWithX509 (decoded , EncodeType .Base16 ,"./jwksRSA.json" , "./src/main/java/com/auth0/jwt/algorithms/jwks.pem" );
132+ DecodedJWT decoded = jwt .decode16BytesWithX509 (signed , JWKS_FILE , PEM_FILE );
133+ }
134+
135+ @ Test
136+ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase16NotAProperJwksFile () throws Exception {
137+ exception .expect (IllegalArgumentException .class );
138+ exception .expectMessage ("Not a proper jwks file" );
139+ RSAPrivateKey privateKey = (RSAPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_RSA , "RSA" );
140+ RSAKeyProvider provider = mock (RSAKeyProvider .class );
141+ when (provider .getPrivateKeyId ()).thenReturn ("8RGoVdVjD8fItyR3FFo0hVNaZYtPGwoP6xKi9e_V7bI" );
142+ when (provider .getPrivateKey ()).thenReturn (privateKey );
143+ Algorithm algorithm = Algorithm .RSA256 (provider );
144+
145+ String signed = JWTCreator .init ()
146+ .withKeyId ("8RGoVdVjD8fItyR3FFo0hVNaZYtPGwoP6xKi9e_V7bI" )
147+ .withIssuer ("auth0" )
148+ .sign (algorithm , EncodeType .Base16 );
149+
150+ JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("auth0" ).build ();
151+ DecodedJWT decoded = jwt .decode16BytesWithX509 (signed , INVALID_JWKS_FILE , PEM_FILE );
152+ }
153+
154+ @ Test
155+ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase16UsingRolandKeys () throws Exception {
156+ RSAPrivateKey privateKey = (RSAPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_PKCS8 , "RSA" );
157+ RSAKeyProvider provider = mock (RSAKeyProvider .class );
158+ when (provider .getPrivateKeyId ()).thenReturn ("dnVrY0tQcUZCUWh2T25EaVk3Q0ZLRW9VdnRxU0tiUHNiVkVGS3k1V1Jidw" );
159+ when (provider .getPrivateKey ()).thenReturn (privateKey );
160+ Algorithm algorithm = Algorithm .RSA256 (provider );
161+
162+ String signed = JWTCreator .init ()
163+ .withKeyId ("dnVrY0tQcUZCUWh2T25EaVk3Q0ZLRW9VdnRxU0tiUHNiVkVGS3k1V1Jidw" )
164+ .withIssuer ("auth0" )
165+ .sign (algorithm , EncodeType .Base16 );
166+
167+ JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("auth0" ).build ();
168+ DecodedJWT decoded = jwt .decode16BytesWithX509 (signed , JWKS_FILE_ANOTHER_EXAMPLE , PUBLIC_KEY_FILE_RSA );
169+ }
170+
171+ @ Test
172+ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase16UsingAuth0Keys () throws Exception {
173+ RSAPrivateKey privateKey = (RSAPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE , "RSA" );
174+ RSAKeyProvider provider = mock (RSAKeyProvider .class );
175+ when (provider .getPrivateKeyId ()).thenReturn ("8RGoVdVjD8fItyR3FFo0hVNaZYtPGwoP6xKi9e_V7bI" );
176+ when (provider .getPrivateKey ()).thenReturn (privateKey );
177+ Algorithm algorithm = Algorithm .RSA256 (provider );
178+
179+ String signed = JWTCreator .init ()
180+ .withKeyId ("8RGoVdVjD8fItyR3FFo0hVNaZYtPGwoP6xKi9e_V7bI" )
181+ .withIssuer ("auth0" )
182+ .sign (algorithm , EncodeType .Base16 );
183+
184+ JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("auth0" ).build ();
185+ DecodedJWT decoded = jwt .decode16BytesWithX509 (signed , JWKS_FILE , PUBLIC_KEY_FILE );
186+ }
187+
188+ @ Test
189+ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase16UsingAuth0KeysInvalidSignatureVerification () throws Exception {
190+ exception .expect (SignatureVerificationException .class );
191+ exception .expectMessage ("The Token's Signature resulted invalid when verified using the Algorithm: SHA256withRSA" );
192+ RSAPrivateKey privateKey = (RSAPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE , "RSA" );
193+ RSAKeyProvider provider = mock (RSAKeyProvider .class );
194+ when (provider .getPrivateKeyId ()).thenReturn ("8RGoVdVjD8fItyR3FFo0hVNaZYtPGwoP6xKi9e_V7bI" );
195+ when (provider .getPrivateKey ()).thenReturn (privateKey );
196+ Algorithm algorithm = Algorithm .RSA256 (provider );
197+
198+ String signed = JWTCreator .init ()
199+ .withKeyId ("8RGoVdVjD8fItyR3FFo0hVNaZYtPGwoP6xKi9e_V7bI" )
200+ .withIssuer ("auth0" )
201+ .sign (algorithm , EncodeType .Base16 );
202+
203+ JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("auth0" ).build ();
204+ DecodedJWT decoded = jwt .decode16BytesWithX509 (signed , JWKS_FILE , PUBLIC_KEY_FILE_INVALID );
124205 }
125206
126207 @ Test
@@ -137,10 +218,42 @@ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase32() throws Excepti
137218 .sign (algorithm , EncodeType .Base32 );
138219
139220 JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("auth0" ).build ();
140- DecodedJWT decoded = jwt .decode32BytesWithX509 (signed , "./jwksRSA.json" , "./src/main/java/com/auth0/jwt/algorithms/jwks.pem" );
141- algorithm .verifyWithX509 (decoded , EncodeType .Base32 ,"./jwksRSA.json" , "./src/main/java/com/auth0/jwt/algorithms/jwks.pem" );
221+ DecodedJWT decoded = jwt .decode32BytesWithX509 (signed , JWKS_FILE , PEM_FILE );
222+ }
223+
224+ @ Test
225+ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase32UsingRolandKeys () throws Exception {
226+ RSAPrivateKey privateKey = (RSAPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_PKCS8 , "RSA" );
227+ RSAKeyProvider provider = mock (RSAKeyProvider .class );
228+ when (provider .getPrivateKeyId ()).thenReturn ("dnVrY0tQcUZCUWh2T25EaVk3Q0ZLRW9VdnRxU0tiUHNiVkVGS3k1V1Jidw" );
229+ when (provider .getPrivateKey ()).thenReturn (privateKey );
230+ Algorithm algorithm = Algorithm .RSA256 (provider );
231+
232+ String signed = JWTCreator .init ()
233+ .withKeyId ("dnVrY0tQcUZCUWh2T25EaVk3Q0ZLRW9VdnRxU0tiUHNiVkVGS3k1V1Jidw" )
234+ .withIssuer ("auth0" )
235+ .sign (algorithm , EncodeType .Base32 );
236+
237+ JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("auth0" ).build ();
238+ DecodedJWT decoded = jwt .decode32BytesWithX509 (signed , JWKS_FILE_ANOTHER_EXAMPLE , PUBLIC_KEY_FILE_RSA );
142239 }
143240
241+ @ Test
242+ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase32UsingAuth0Keys () throws Exception {
243+ RSAPrivateKey privateKey = (RSAPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE , "RSA" );
244+ RSAKeyProvider provider = mock (RSAKeyProvider .class );
245+ when (provider .getPrivateKeyId ()).thenReturn ("8RGoVdVjD8fItyR3FFo0hVNaZYtPGwoP6xKi9e_V7bI" );
246+ when (provider .getPrivateKey ()).thenReturn (privateKey );
247+ Algorithm algorithm = Algorithm .RSA256 (provider );
248+
249+ String signed = JWTCreator .init ()
250+ .withKeyId ("8RGoVdVjD8fItyR3FFo0hVNaZYtPGwoP6xKi9e_V7bI" )
251+ .withIssuer ("auth0" )
252+ .sign (algorithm , EncodeType .Base32 );
253+
254+ JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("auth0" ).build ();
255+ DecodedJWT decoded = jwt .decode32BytesWithX509 (signed , JWKS_FILE , PUBLIC_KEY_FILE );
256+ }
144257 @ Test
145258 public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase64 () throws Exception {
146259 RSAPrivateKey privateKey = (RSAPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_RSA , "RSA" );
@@ -160,10 +273,42 @@ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase64() throws Excepti
160273 JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("https://agaton-sax.com/" )
161274 .withNonStandardClaim ("foo" ,"bar" )
162275 .withNonStandardClaim ("kit" , "kat" ).build ();
163- DecodedJWT decoded = jwt .decodeWithX509 (signed , "./jwksRSA.json" , "./src/main/java/com/auth0/jwt/algorithms/jwks.pem" );
164- algorithm .verifyWithX509 (decoded , EncodeType .Base64 , "./jwksRSA.json" , "./src/main/java/com/auth0/jwt/algorithms/jwks.pem" );
276+ DecodedJWT decoded = jwt .decodeWithX509 (signed , JWKS_FILE , PEM_FILE );
277+ }
278+
279+ @ Test
280+ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase64UsingRolandKeys () throws Exception {
281+ RSAPrivateKey privateKey = (RSAPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_PKCS8 , "RSA" );
282+ RSAKeyProvider provider = mock (RSAKeyProvider .class );
283+ when (provider .getPrivateKeyId ()).thenReturn ("dnVrY0tQcUZCUWh2T25EaVk3Q0ZLRW9VdnRxU0tiUHNiVkVGS3k1V1Jidw" );
284+ when (provider .getPrivateKey ()).thenReturn (privateKey );
285+ Algorithm algorithm = Algorithm .RSA256 (provider );
286+
287+ String signed = JWTCreator .init ()
288+ .withKeyId ("dnVrY0tQcUZCUWh2T25EaVk3Q0ZLRW9VdnRxU0tiUHNiVkVGS3k1V1Jidw" )
289+ .withIssuer ("auth0" )
290+ .sign (algorithm , EncodeType .Base64 );
291+
292+ JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("auth0" ).build ();
293+ DecodedJWT decoded = jwt .decodeWithX509 (signed , JWKS_FILE_ANOTHER_EXAMPLE , PUBLIC_KEY_FILE_RSA );
165294 }
166295
296+ @ Test
297+ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase64UsingAuth0Keys () throws Exception {
298+ RSAPrivateKey privateKey = (RSAPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE , "RSA" );
299+ RSAKeyProvider provider = mock (RSAKeyProvider .class );
300+ when (provider .getPrivateKeyId ()).thenReturn ("8RGoVdVjD8fItyR3FFo0hVNaZYtPGwoP6xKi9e_V7bI" );
301+ when (provider .getPrivateKey ()).thenReturn (privateKey );
302+ Algorithm algorithm = Algorithm .RSA256 (provider );
303+
304+ String signed = JWTCreator .init ()
305+ .withKeyId ("8RGoVdVjD8fItyR3FFo0hVNaZYtPGwoP6xKi9e_V7bI" )
306+ .withIssuer ("auth0" )
307+ .sign (algorithm , EncodeType .Base64 );
308+
309+ JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("auth0" ).build ();
310+ DecodedJWT decoded = jwt .decodeWithX509 (signed , JWKS_FILE , PUBLIC_KEY_FILE );
311+ }
167312
168313 @ Test
169314 public void shouldNotOverwriteKeyIdIfAddedFromRSAAlgorithms () throws Exception {
0 commit comments