2222import android .os .Handler ;
2323import android .util .Log ;
2424
25- import com .amazonaws .AmazonClientException ;
2625import com .amazonaws .AmazonServiceException ;
27- import com .amazonaws .SDKGlobalConfiguration ;
2826import com .amazonaws .mobileconnectors .cognitoidentityprovider .continuations .AuthenticationContinuation ;
2927import com .amazonaws .mobileconnectors .cognitoidentityprovider .continuations .AuthenticationDetails ;
3028import com .amazonaws .mobileconnectors .cognitoidentityprovider .continuations .ForgotPasswordContinuation ;
104102 */
105103public class CognitoUser {
106104 private final String TAG = "CognitoUser" ;
107- /** Default threshold for refreshing session credentials */
108- public static final int DEFAULT_THRESHOLD_SECONDS = 500 ;
109105
110106 /**
111107 * Application context.
@@ -623,24 +619,6 @@ public void getSession(final AuthenticationHandler callback) {
623619 }
624620 }
625621
626- /**
627- * Returns true if a new session needs to be started. A new session
628- * is needed when no session has been started yet, or if the last session is
629- * within the configured refresh threshold.
630- *
631- * @return True if a new session needs to be started.
632- */
633- private boolean needsNewSession (CognitoUserSession userSession ) {
634- if (userSession == null ) {
635- return true ;
636- }
637- long currentTime = System .currentTimeMillis ()
638- - SDKGlobalConfiguration .getGlobalTimeOffset () * 1000 ;
639- long timeRemaining = userSession .getIdToken ().getExpiration ().getTime ()
640- - currentTime ;
641- return timeRemaining < (DEFAULT_THRESHOLD_SECONDS * 1000 );
642- }
643-
644622 /**
645623 * Call this method for valid, cached tokens for this user.
646624 *
@@ -651,36 +629,33 @@ private CognitoUserSession getCachedSession() {
651629 throw new CognitoNotAuthorizedException ("User-ID is null" );
652630 }
653631
654- if (!needsNewSession (cipSession )) {
655- return cipSession ;
632+ if (cipSession != null ) {
633+ if (cipSession .isValid ()) {
634+ return cipSession ;
635+ }
656636 }
657637
658638 // Read cached tokens
659639 CognitoUserSession cachedTokens = readCachedTokens ();
660640
661- // Return cached tokens if they are still valid with some margin
662- if (! needsNewSession ( cachedTokens )) {
641+ // Return cached tokens if they are still valid
642+ if (cachedTokens . isValid ( )) {
663643 cipSession = cachedTokens ;
664- return cipSession ;
644+ return cipSession ;
665645 }
666646
647+ // Clear any cached tokens, since none of them are valid.
648+ clearCachedTokens ();
649+
667650 if (cachedTokens .getRefreshToken () != null ) {
668651 // Use Refresh token to get new tokens
669652 try {
670653 cipSession = refreshSessionInternal (cachedTokens .getRefreshToken ());
671654 cacheTokens (cipSession );
672655 return cipSession ;
673- } catch (CognitoNotAuthorizedException e ) {
674- // Clear any cached tokens, since none of them are valid.
675- clearCachedTokens ();
676- // Could not get new tokens from refresh. Should authenticate user.
677- throw new CognitoNotAuthorizedException ("user is not authenticated" ,e );
678- } catch (AmazonClientException e ) {
679- // General IO errors - not clearing cached tokens
680- throw new AmazonClientException ("failed to get new tokens from refresh" ,e );
681656 } catch (Exception e ) {
682- // Errors like NetworkOnMainThreadException etc - not clearing cached tokens .
683- throw new AmazonClientException ( "failed to get new tokens from refresh" , e );
657+ // Could not get new tokens from refresh. Should authenticate user .
658+ throw new CognitoNotAuthorizedException ( "user is not authenticated" );
684659 }
685660 }
686661 throw new CognitoNotAuthorizedException ("user is not authenticated" );
@@ -2043,7 +2018,7 @@ private CognitoUserSession refreshSessionInternal(CognitoRefreshToken refreshTok
20432018 cognitoIdentityProviderClient .refreshTokens (refreshTokensRequest );
20442019 AuthenticationResultType authenticationResult = refreshTokensResult .getAuthenticationResult ();
20452020
2046- if (authenticationResult = = null ) {
2021+ if (authenticationResult ! = null ) {
20472022 throw new CognitoNotAuthorizedException ("user is not authenticated" );
20482023 }
20492024
0 commit comments