1919
2020package com .auth0 .jwt .creators ;
2121
22- import static com .auth0 .jwt .TimeUtil .generateRandomExpDateInFuture ;
23- import static com .auth0 .jwt .TimeUtil .generateRandomIatDateInPast ;
2422import com .auth0 .jwt .algorithms .Algorithm ;
25- import com .auth0 .jwt .creators .GoogleJwtCreator ;
2623import com .auth0 .jwt .exceptions .InvalidClaimException ;
2724import com .auth0 .jwt .exceptions .TokenExpiredException ;
2825import com .auth0 .jwt .impl .PublicClaims ;
3128import com .auth0 .jwt .interfaces .GoogleVerification ;
3229import com .auth0 .jwt .jwts .GoogleJWT ;
3330import com .auth0 .jwt .jwts .JWT ;
34- import static java .util .Arrays .asList ;
35- import static org .junit .Assert .assertTrue ;
3631import org .junit .Rule ;
3732import org .junit .Test ;
3833import org .junit .rules .ExpectedException ;
3934
35+ import java .util .Calendar ;
36+ import java .util .Date ;
37+ import java .util .Map ;
4038
41- import java .text .SimpleDateFormat ;
42- import java .util .*;
39+ import static com .auth0 .jwt .TimeUtil .generateRandomExpDateInFuture ;
40+ import static com .auth0 .jwt .TimeUtil .generateRandomIatDateInPast ;
41+ import static java .util .Arrays .asList ;
42+ import static org .junit .Assert .assertTrue ;
4343
4444public class GoogleJwtCreatorTest {
4545
@@ -133,7 +133,7 @@ public void testGoogleJwtCreatorWhenCertainRequiredClaimIsntProvided() throws Ex
133133
134134 GoogleVerification verification = GoogleJWT .require (algorithm );
135135 JWT verifier = verification .createVerifierForGoogle (PICTURE , EMAIL , asList ("issuer" ), asList ("audience" ),
136- NAME , 1 , 1 ).build ();
136+ NAME , 1 , 1 ).build ();
137137 DecodedJWT jwt = verifier .decode (token );
138138 }
139139
@@ -157,7 +157,7 @@ public void testGoogleJwtCreatorNoneAlgorithmNotAllowed() throws Exception {
157157
158158 GoogleVerification verification = GoogleJWT .require (algorithm );
159159 JWT verifier = verification .createVerifierForGoogle (PICTURE , EMAIL , asList ("issuer" ), asList ("audience" ),
160- NAME , 1 , 1 ).build ();
160+ NAME , 1 , 1 ).build ();
161161 DecodedJWT jwt = verifier .decode (token );
162162 }
163163
@@ -180,7 +180,7 @@ public void testGoogleJwtCreatorNoneAlgorithmNotSpecifiedButStillNotAllowed() th
180180
181181 GoogleVerification verification = GoogleJWT .require (algorithm );
182182 JWT verifier = verification .createVerifierForGoogle (PICTURE , EMAIL , asList ("issuer" ), asList ("audience" ),
183- NAME , 1 , 1 ).build ();
183+ NAME , 1 , 1 ).build ();
184184 DecodedJWT jwt = verifier .decode (token );
185185 }
186186
@@ -201,7 +201,7 @@ public void testGoogleJwtCreatorNoneAlgorithmAllowed() throws Exception {
201201
202202 GoogleVerification verification = GoogleJWT .require (algorithm );
203203 JWT verifier = verification .createVerifierForGoogle (PICTURE , EMAIL , asList ("issuer" ), asList ("audience" ),
204- NAME , 1 , 1 ).build ();
204+ NAME , 1 , 1 ).build ();
205205 DecodedJWT jwt = verifier .decode (token );
206206 Map <String , Claim > claims = jwt .getClaims ();
207207 verifyClaims (claims , exp );
@@ -251,7 +251,7 @@ public void testGoogleJwtCreatorInvalidIssuer() throws Exception {
251251
252252 GoogleVerification verification = GoogleJWT .require (algorithm );
253253 JWT verifier = verification .createVerifierForGoogle (PICTURE , EMAIL , asList ("issuer" ), asList ("audience" ),
254- NAME , 1 , 1 ).build ();
254+ NAME , 1 , 1 ).build ();
255255 DecodedJWT jwt = verifier .decode (token );
256256 }
257257
@@ -363,7 +363,7 @@ public void testGoogleJwtCreatorNonStandardClaimString() throws Exception {
363363 .sign (algorithm );
364364 GoogleVerification verification = GoogleJWT .require (algorithm );
365365 JWT verifier = verification .createVerifierForGoogle (PICTURE , EMAIL , asList ("issuer" ), asList ("audience" ),
366- NAME , 1 , 1 ).build ();
366+ NAME , 1 , 1 ).build ();
367367 DecodedJWT jwt = verifier .decode (token );
368368 Map <String , Claim > claims = jwt .getClaims ();
369369 verifyClaims (claims , exp );
@@ -481,14 +481,11 @@ public void testGoogleJwtCreatorNonStandardClaimDate() throws Exception {
481481
482482 @ Test
483483 public void testGoogleJwtCreatorExpTimeHasPassed () throws Exception {
484- thrown . expect ( TokenExpiredException . class );
485- thrown . expectMessage ( "The Token has expired on Wed Oct 29 00:00:00 PDT 2014." );
484+ Calendar calendar = Calendar . getInstance ( );
485+ calendar . set ( 2014 , Calendar . OCTOBER , 29 );
486486
487- String myDate = "2014/10/29" ;
488- SimpleDateFormat sdf = new SimpleDateFormat ("yyyy/MM/dd" );
489- Date date = sdf .parse (myDate );
490- long expLong = date .getTime ();
491- Date expDate = new Date (expLong );
487+ thrown .expect (TokenExpiredException .class );
488+ thrown .expectMessage (String .format ("The Token has expired on %s" , calendar .getTime ()));
492489
493490 Algorithm algorithm = Algorithm .HMAC256 ("secret" );
494491 String token = GoogleJwtCreator .build ()
@@ -497,7 +494,7 @@ public void testGoogleJwtCreatorExpTimeHasPassed() throws Exception {
497494 .withIssuer ("issuer" )
498495 .withSubject ("subject" )
499496 .withAudience ("audience" )
500- .withExp (expDate )
497+ .withExp (calendar . getTime () )
501498 .withIat (iat )
502499 .withName (NAME )
503500 .sign (algorithm );
@@ -509,14 +506,11 @@ public void testGoogleJwtCreatorExpTimeHasPassed() throws Exception {
509506
510507 @ Test
511508 public void testGoogleJwtCreatorTokenCantBeUsedBefore () throws Exception {
512- thrown . expect ( InvalidClaimException . class );
513- thrown . expectMessage ( "The Token can't be used before Mon Oct 29 00:00:00 PDT 2018." );
509+ Calendar calendar = Calendar . getInstance ( );
510+ calendar . set ( 2018 , Calendar . OCTOBER , 29 );
514511
515- String myDate = "2018/10/29" ;
516- SimpleDateFormat sdf = new SimpleDateFormat ("yyyy/MM/dd" );
517- Date date = sdf .parse (myDate );
518- long expLong = date .getTime ();
519- Date iatDate = new Date (expLong );
512+ thrown .expect (InvalidClaimException .class );
513+ thrown .expectMessage (String .format ("The Token can't be used before %s" , calendar .getTime ()));
520514
521515 Algorithm algorithm = Algorithm .HMAC256 ("secret" );
522516 String token = GoogleJwtCreator .build ()
@@ -526,7 +520,7 @@ public void testGoogleJwtCreatorTokenCantBeUsedBefore() throws Exception {
526520 .withSubject ("subject" )
527521 .withAudience ("audience" )
528522 .withExp (exp )
529- .withIat (iatDate )
523+ .withIat (calendar . getTime () )
530524 .withName (NAME )
531525 .sign (algorithm );
532526 GoogleVerification verification = GoogleJWT .require (algorithm );
@@ -536,14 +530,14 @@ public void testGoogleJwtCreatorTokenCantBeUsedBefore() throws Exception {
536530 }
537531
538532 @ Test
539- public void testCreateVerifierForExtended () throws Exception {
533+ public void testCreateVerifierForExtended () throws Exception {
540534 thrown .expect (UnsupportedOperationException .class );
541535 thrown .expectMessage ("you shouldn't be calling this method" );
542536 GoogleVerification verification = GoogleJWT .require (Algorithm .HMAC256 ("secret" ));
543537 verification .createVerifierForExtended (null , null , null , null , null , 1L , 1L , 1L );
544538 }
545539
546- protected static void verifyClaims (Map <String ,Claim > claims , Date exp ) {
540+ protected static void verifyClaims (Map <String , Claim > claims , Date exp ) {
547541 assertTrue (claims .get (PICTURE ).asString ().equals (PICTURE ));
548542 assertTrue (claims .get (EMAIL ).asString ().equals (EMAIL ));
549543 assertTrue (claims .get (PublicClaims .ISSUER ).asList (String .class ).get (0 ).equals ("issuer" ));
0 commit comments