@@ -351,7 +351,7 @@ public static SessionInformationHolder getSession(AppIdentifier appIdentifier, M
351
351
accessToken .sessionHandle ,
352
352
Utils .hashSHA256 (accessToken .refreshTokenHash1 ),
353
353
System .currentTimeMillis () +
354
- config .getRefreshTokenValidity ());
354
+ config .getRefreshTokenValidity (), sessionInfo . useStaticKey );
355
355
}
356
356
storage .commitTransaction (con );
357
357
@@ -423,7 +423,7 @@ public static SessionInformationHolder getSession(AppIdentifier appIdentifier, M
423
423
Utils .hashSHA256 (accessToken .refreshTokenHash1 ),
424
424
System .currentTimeMillis () + Config .getConfig (tenantIdentifierWithStorage , main )
425
425
.getRefreshTokenValidity (),
426
- sessionInfo .lastUpdatedSign );
426
+ sessionInfo .lastUpdatedSign , sessionInfo . useStaticKey );
427
427
if (!success ) {
428
428
continue ;
429
429
}
@@ -473,7 +473,7 @@ public static SessionInformationHolder refreshSession(Main main, @Nonnull String
473
473
UnsupportedJWTSigningAlgorithmException , AccessTokenPayloadError {
474
474
try {
475
475
return refreshSession (new AppIdentifier (null , null ), main , refreshToken , antiCsrfToken ,
476
- enableAntiCsrf , accessTokenVersion );
476
+ enableAntiCsrf , accessTokenVersion , null );
477
477
} catch (TenantOrAppNotFoundException e ) {
478
478
throw new IllegalStateException (e );
479
479
}
@@ -482,7 +482,8 @@ public static SessionInformationHolder refreshSession(Main main, @Nonnull String
482
482
public static SessionInformationHolder refreshSession (AppIdentifier appIdentifier , Main main ,
483
483
@ Nonnull String refreshToken ,
484
484
@ Nullable String antiCsrfToken , boolean enableAntiCsrf ,
485
- AccessToken .VERSION accessTokenVersion )
485
+ AccessToken .VERSION accessTokenVersion ,
486
+ Boolean shouldUseStaticKey )
486
487
throws StorageTransactionLogicException ,
487
488
UnauthorisedException , StorageQueryException , TokenTheftDetectedException ,
488
489
UnsupportedJWTSigningAlgorithmException , AccessTokenPayloadError , TenantOrAppNotFoundException {
@@ -498,14 +499,15 @@ public static SessionInformationHolder refreshSession(AppIdentifier appIdentifie
498
499
499
500
return refreshSessionHelper (refreshTokenInfo .tenantIdentifier .withStorage (
500
501
StorageLayer .getStorage (refreshTokenInfo .tenantIdentifier , main )),
501
- main , refreshToken , refreshTokenInfo , enableAntiCsrf , accessTokenVersion );
502
+ main , refreshToken , refreshTokenInfo , enableAntiCsrf , accessTokenVersion , shouldUseStaticKey );
502
503
}
503
504
504
505
private static SessionInformationHolder refreshSessionHelper (
505
506
TenantIdentifierWithStorage tenantIdentifierWithStorage , Main main , String refreshToken ,
506
507
RefreshToken .RefreshTokenInfo refreshTokenInfo ,
507
508
boolean enableAntiCsrf ,
508
- AccessToken .VERSION accessTokenVersion )
509
+ AccessToken .VERSION accessTokenVersion ,
510
+ Boolean shouldUseStaticKey )
509
511
throws StorageTransactionLogicException , UnauthorisedException , StorageQueryException ,
510
512
TokenTheftDetectedException , UnsupportedJWTSigningAlgorithmException , AccessTokenPayloadError ,
511
513
TenantOrAppNotFoundException {
@@ -530,7 +532,16 @@ private static SessionInformationHolder refreshSessionHelper(
530
532
throw new UnauthorisedException ("Session missing in db or has expired" );
531
533
}
532
534
535
+ boolean useStaticKey = shouldUseStaticKey != null ? shouldUseStaticKey : sessionInfo .useStaticKey ;
536
+
533
537
if (sessionInfo .refreshTokenHash2 .equals (Utils .hashSHA256 (Utils .hashSHA256 (refreshToken )))) {
538
+ if (useStaticKey != sessionInfo .useStaticKey ) {
539
+ // We do not update anything except the static key status
540
+ storage .updateSessionInfo_Transaction (tenantIdentifierWithStorage , con , sessionHandle ,
541
+ sessionInfo .refreshTokenHash2 , sessionInfo .expiry ,
542
+ useStaticKey );
543
+ }
544
+
534
545
// at this point, the input refresh token is the parent one.
535
546
storage .commitTransaction (con );
536
547
String antiCsrfToken = enableAntiCsrf ? UUID .randomUUID ().toString () : null ;
@@ -542,7 +553,7 @@ private static SessionInformationHolder refreshSessionHelper(
542
553
main , sessionHandle ,
543
554
sessionInfo .userId , Utils .hashSHA256 (newRefreshToken .token ),
544
555
Utils .hashSHA256 (refreshToken ), sessionInfo .userDataInJWT , antiCsrfToken ,
545
- null , accessTokenVersion , sessionInfo . useStaticKey );
556
+ null , accessTokenVersion , useStaticKey );
546
557
547
558
TokenInfo idRefreshToken = new TokenInfo (UUID .randomUUID ().toString (),
548
559
newRefreshToken .expiry , newRefreshToken .createdTime );
@@ -560,13 +571,13 @@ private static SessionInformationHolder refreshSessionHelper(
560
571
.equals (sessionInfo .refreshTokenHash2 ))) {
561
572
storage .updateSessionInfo_Transaction (tenantIdentifierWithStorage , con , sessionHandle ,
562
573
Utils .hashSHA256 (Utils .hashSHA256 (refreshToken )),
563
- System .currentTimeMillis () + config .getRefreshTokenValidity ());
574
+ System .currentTimeMillis () + config .getRefreshTokenValidity (), useStaticKey );
564
575
565
576
storage .commitTransaction (con );
566
577
567
578
return refreshSessionHelper (tenantIdentifierWithStorage , main , refreshToken ,
568
579
refreshTokenInfo , enableAntiCsrf ,
569
- accessTokenVersion );
580
+ accessTokenVersion , shouldUseStaticKey );
570
581
}
571
582
572
583
storage .commitTransaction (con );
@@ -613,7 +624,19 @@ private static SessionInformationHolder refreshSessionHelper(
613
624
throw new UnauthorisedException ("Session missing in db or has expired" );
614
625
}
615
626
627
+ boolean useStaticKey = shouldUseStaticKey != null ? shouldUseStaticKey : sessionInfo .useStaticKey ;
628
+
616
629
if (sessionInfo .refreshTokenHash2 .equals (Utils .hashSHA256 (Utils .hashSHA256 (refreshToken )))) {
630
+ if (sessionInfo .useStaticKey != useStaticKey ) {
631
+ // We do not update anything except the static key status
632
+ boolean success = storage .updateSessionInfo_Transaction (sessionHandle ,
633
+ sessionInfo .refreshTokenHash2 , sessionInfo .expiry ,
634
+ sessionInfo .lastUpdatedSign , useStaticKey );
635
+ if (!success ) {
636
+ continue ;
637
+ }
638
+ }
639
+
617
640
// at this point, the input refresh token is the parent one.
618
641
String antiCsrfToken = enableAntiCsrf ? UUID .randomUUID ().toString () : null ;
619
642
@@ -624,7 +647,7 @@ private static SessionInformationHolder refreshSessionHelper(
624
647
sessionHandle ,
625
648
sessionInfo .userId , Utils .hashSHA256 (newRefreshToken .token ),
626
649
Utils .hashSHA256 (refreshToken ), sessionInfo .userDataInJWT , antiCsrfToken ,
627
- null , accessTokenVersion , sessionInfo . useStaticKey );
650
+ null , accessTokenVersion , useStaticKey );
628
651
629
652
TokenInfo idRefreshToken = new TokenInfo (UUID .randomUUID ().toString (), newRefreshToken .expiry ,
630
653
newRefreshToken .createdTime );
@@ -644,13 +667,13 @@ private static SessionInformationHolder refreshSessionHelper(
644
667
Utils .hashSHA256 (Utils .hashSHA256 (refreshToken )),
645
668
System .currentTimeMillis () +
646
669
Config .getConfig (tenantIdentifierWithStorage , main ).getRefreshTokenValidity (),
647
- sessionInfo .lastUpdatedSign );
670
+ sessionInfo .lastUpdatedSign , useStaticKey );
648
671
if (!success ) {
649
672
continue ;
650
673
}
651
674
return refreshSessionHelper (tenantIdentifierWithStorage , main , refreshToken , refreshTokenInfo ,
652
675
enableAntiCsrf ,
653
- accessTokenVersion );
676
+ accessTokenVersion , shouldUseStaticKey );
654
677
}
655
678
656
679
throw new TokenTheftDetectedException (sessionHandle , sessionInfo .userId );
0 commit comments