33import com .auth0 .jwt .algorithms .Algorithm ;
44import com .auth0 .jwt .interfaces .Clock ;
55import com .auth0 .jwt .interfaces .DecodedJWT ;
6+ import org .apache .commons .codec .binary .Base64 ;
67import org .hamcrest .collection .IsCollectionWithSize ;
78import org .hamcrest .core .IsCollectionContaining ;
89import org .junit .Rule ;
910import org .junit .Test ;
1011import org .junit .rules .ExpectedException ;
1112
13+ import java .nio .charset .StandardCharsets ;
1214import java .security .interfaces .ECKey ;
1315import java .security .interfaces .RSAKey ;
1416import java .util .Date ;
@@ -353,11 +355,14 @@ public void shouldGetCustomClaims() throws Exception {
353355
354356 @ Test
355357 public void shouldCreateAnEmptyHMAC256SignedToken () throws Exception {
356- String headerAndPayload = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.e30." ;
357-
358358 String signed = JWT .create ().sign (Algorithm .HMAC256 ("secret" ));
359359 assertThat (signed , is (notNullValue ()));
360- assertThat (signed , startsWith (headerAndPayload ));
360+
361+ String [] parts = signed .split ("\\ ." );
362+ String headerJson = new String (Base64 .decodeBase64 (parts [0 ]), StandardCharsets .UTF_8 );
363+ assertThat (headerJson , JsonMatcher .hasEntry ("alg" , "HS256" ));
364+ assertThat (headerJson , JsonMatcher .hasEntry ("typ" , "JWT" ));
365+ assertThat (parts [1 ], is ("e30" ));
361366
362367 JWTVerifier verified = JWT .require (Algorithm .HMAC256 ("secret" ))
363368 .build ();
@@ -366,11 +371,14 @@ public void shouldCreateAnEmptyHMAC256SignedToken() throws Exception {
366371
367372 @ Test
368373 public void shouldCreateAnEmptyHMAC384SignedToken () throws Exception {
369- String headerAndPayload = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzM4NCJ9.e30." ;
370-
371374 String signed = JWT .create ().sign (Algorithm .HMAC384 ("secret" ));
372375 assertThat (signed , is (notNullValue ()));
373- assertThat (signed , startsWith (headerAndPayload ));
376+
377+ String [] parts = signed .split ("\\ ." );
378+ String headerJson = new String (Base64 .decodeBase64 (parts [0 ]), StandardCharsets .UTF_8 );
379+ assertThat (headerJson , JsonMatcher .hasEntry ("alg" , "HS384" ));
380+ assertThat (headerJson , JsonMatcher .hasEntry ("typ" , "JWT" ));
381+ assertThat (parts [1 ], is ("e30" ));
374382
375383 JWTVerifier verified = JWT .require (Algorithm .HMAC384 ("secret" ))
376384 .build ();
@@ -379,11 +387,14 @@ public void shouldCreateAnEmptyHMAC384SignedToken() throws Exception {
379387
380388 @ Test
381389 public void shouldCreateAnEmptyHMAC512SignedToken () throws Exception {
382- String headerAndPayload = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.e30." ;
383-
384390 String signed = JWT .create ().sign (Algorithm .HMAC512 ("secret" ));
385391 assertThat (signed , is (notNullValue ()));
386- assertThat (signed , startsWith (headerAndPayload ));
392+
393+ String [] parts = signed .split ("\\ ." );
394+ String headerJson = new String (Base64 .decodeBase64 (parts [0 ]), StandardCharsets .UTF_8 );
395+ assertThat (headerJson , JsonMatcher .hasEntry ("alg" , "HS512" ));
396+ assertThat (headerJson , JsonMatcher .hasEntry ("typ" , "JWT" ));
397+ assertThat (parts [1 ], is ("e30" ));
387398
388399 JWTVerifier verified = JWT .require (Algorithm .HMAC512 ("secret" ))
389400 .build ();
@@ -392,11 +403,14 @@ public void shouldCreateAnEmptyHMAC512SignedToken() throws Exception {
392403
393404 @ Test
394405 public void shouldCreateAnEmptyRSA256SignedToken () throws Exception {
395- String headerAndPayload = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.e30." ;
396-
397406 String signed = JWT .create ().sign (Algorithm .RSA256 ((RSAKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_RSA , "RSA" )));
398407 assertThat (signed , is (notNullValue ()));
399- assertThat (signed , startsWith (headerAndPayload ));
408+
409+ String [] parts = signed .split ("\\ ." );
410+ String headerJson = new String (Base64 .decodeBase64 (parts [0 ]), StandardCharsets .UTF_8 );
411+ assertThat (headerJson , JsonMatcher .hasEntry ("alg" , "RS256" ));
412+ assertThat (headerJson , JsonMatcher .hasEntry ("typ" , "JWT" ));
413+ assertThat (parts [1 ], is ("e30" ));
400414
401415 JWTVerifier verified = JWT .require (Algorithm .RSA256 ((RSAKey ) PemUtils .readPublicKeyFromFile (PUBLIC_KEY_FILE_RSA , "RSA" )))
402416 .build ();
@@ -405,11 +419,14 @@ public void shouldCreateAnEmptyRSA256SignedToken() throws Exception {
405419
406420 @ Test
407421 public void shouldCreateAnEmptyRSA384SignedToken () throws Exception {
408- String headerAndPayload = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzM4NCJ9.e30." ;
409-
410422 String signed = JWT .create ().sign (Algorithm .RSA384 ((RSAKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_RSA , "RSA" )));
411423 assertThat (signed , is (notNullValue ()));
412- assertThat (signed , startsWith (headerAndPayload ));
424+
425+ String [] parts = signed .split ("\\ ." );
426+ String headerJson = new String (Base64 .decodeBase64 (parts [0 ]), StandardCharsets .UTF_8 );
427+ assertThat (headerJson , JsonMatcher .hasEntry ("alg" , "RS384" ));
428+ assertThat (headerJson , JsonMatcher .hasEntry ("typ" , "JWT" ));
429+ assertThat (parts [1 ], is ("e30" ));
413430
414431 JWTVerifier verified = JWT .require (Algorithm .RSA384 ((RSAKey ) PemUtils .readPublicKeyFromFile (PUBLIC_KEY_FILE_RSA , "RSA" )))
415432 .build ();
@@ -418,11 +435,14 @@ public void shouldCreateAnEmptyRSA384SignedToken() throws Exception {
418435
419436 @ Test
420437 public void shouldCreateAnEmptyRSA512SignedToken () throws Exception {
421- String headerAndPayload = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzUxMiJ9.e30." ;
422-
423438 String signed = JWT .create ().sign (Algorithm .RSA512 ((RSAKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_RSA , "RSA" )));
424439 assertThat (signed , is (notNullValue ()));
425- assertThat (signed , startsWith (headerAndPayload ));
440+
441+ String [] parts = signed .split ("\\ ." );
442+ String headerJson = new String (Base64 .decodeBase64 (parts [0 ]), StandardCharsets .UTF_8 );
443+ assertThat (headerJson , JsonMatcher .hasEntry ("alg" , "RS512" ));
444+ assertThat (headerJson , JsonMatcher .hasEntry ("typ" , "JWT" ));
445+ assertThat (parts [1 ], is ("e30" ));
426446
427447 JWTVerifier verified = JWT .require (Algorithm .RSA512 ((RSAKey ) PemUtils .readPublicKeyFromFile (PUBLIC_KEY_FILE_RSA , "RSA" )))
428448 .build ();
@@ -431,11 +451,14 @@ public void shouldCreateAnEmptyRSA512SignedToken() throws Exception {
431451
432452 @ Test
433453 public void shouldCreateAnEmptyECDSA256SignedToken () throws Exception {
434- String headerAndPayload = "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.e30." ;
435-
436454 String signed = JWT .create ().sign (Algorithm .ECDSA256 ((ECKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_EC_256 , "EC" )));
437455 assertThat (signed , is (notNullValue ()));
438- assertThat (signed , startsWith (headerAndPayload ));
456+
457+ String [] parts = signed .split ("\\ ." );
458+ String headerJson = new String (Base64 .decodeBase64 (parts [0 ]), StandardCharsets .UTF_8 );
459+ assertThat (headerJson , JsonMatcher .hasEntry ("alg" , "ES256" ));
460+ assertThat (headerJson , JsonMatcher .hasEntry ("typ" , "JWT" ));
461+ assertThat (parts [1 ], is ("e30" ));
439462
440463 JWTVerifier verified = JWT .require (Algorithm .ECDSA256 ((ECKey ) PemUtils .readPublicKeyFromFile (PUBLIC_KEY_FILE_EC_256 , "EC" )))
441464 .build ();
@@ -444,11 +467,14 @@ public void shouldCreateAnEmptyECDSA256SignedToken() throws Exception {
444467
445468 @ Test
446469 public void shouldCreateAnEmptyECDSA384SignedToken () throws Exception {
447- String headerAndPayload = "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzM4NCJ9.e30." ;
448-
449470 String signed = JWT .create ().sign (Algorithm .ECDSA384 ((ECKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_EC_384 , "EC" )));
450471 assertThat (signed , is (notNullValue ()));
451- assertThat (signed , startsWith (headerAndPayload ));
472+
473+ String [] parts = signed .split ("\\ ." );
474+ String headerJson = new String (Base64 .decodeBase64 (parts [0 ]), StandardCharsets .UTF_8 );
475+ assertThat (headerJson , JsonMatcher .hasEntry ("alg" , "ES384" ));
476+ assertThat (headerJson , JsonMatcher .hasEntry ("typ" , "JWT" ));
477+ assertThat (parts [1 ], is ("e30" ));
452478
453479 JWTVerifier verified = JWT .require (Algorithm .ECDSA384 ((ECKey ) PemUtils .readPublicKeyFromFile (PUBLIC_KEY_FILE_EC_384 , "EC" )))
454480 .build ();
@@ -457,11 +483,14 @@ public void shouldCreateAnEmptyECDSA384SignedToken() throws Exception {
457483
458484 @ Test
459485 public void shouldCreateAnEmptyECDSA512SignedToken () throws Exception {
460- String headerAndPayload = "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzUxMiJ9.e30." ;
461-
462486 String signed = JWT .create ().sign (Algorithm .ECDSA512 ((ECKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_EC_512 , "EC" )));
463487 assertThat (signed , is (notNullValue ()));
464- assertThat (signed , startsWith (headerAndPayload ));
488+
489+ String [] parts = signed .split ("\\ ." );
490+ String headerJson = new String (Base64 .decodeBase64 (parts [0 ]), StandardCharsets .UTF_8 );
491+ assertThat (headerJson , JsonMatcher .hasEntry ("alg" , "ES512" ));
492+ assertThat (headerJson , JsonMatcher .hasEntry ("typ" , "JWT" ));
493+ assertThat (parts [1 ], is ("e30" ));
465494
466495 JWTVerifier verified = JWT .require (Algorithm .ECDSA512 ((ECKey ) PemUtils .readPublicKeyFromFile (PUBLIC_KEY_FILE_EC_512 , "EC" )))
467496 .build ();
0 commit comments