Skip to content

Commit 9bffd33

Browse files
authored
Merge pull request #177 from aws/revert-164-master
Revert "Fix Cognito User Pools token refresh and failure handling"
2 parents bfae00e + 1130556 commit 9bffd33

File tree

2 files changed

+15
-43
lines changed

2 files changed

+15
-43
lines changed

aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import android.os.Handler;
2323
import android.util.Log;
2424

25-
import com.amazonaws.AmazonClientException;
2625
import com.amazonaws.AmazonServiceException;
27-
import com.amazonaws.SDKGlobalConfiguration;
2826
import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation;
2927
import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationDetails;
3028
import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ForgotPasswordContinuation;
@@ -104,8 +102,6 @@
104102
*/
105103
public 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

aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUserSession.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package com.amazonaws.mobileconnectors.cognitoidentityprovider;
1919

20-
import com.amazonaws.SDKGlobalConfiguration;
2120
import com.amazonaws.mobileconnectors.cognitoidentityprovider.tokens.CognitoAccessToken;
2221
import com.amazonaws.mobileconnectors.cognitoidentityprovider.tokens.CognitoIdToken;
2322
import com.amazonaws.mobileconnectors.cognitoidentityprovider.tokens.CognitoRefreshToken;
@@ -28,8 +27,6 @@
2827
* This wraps all Cognito tokens for a user.
2928
*/
3029
public class CognitoUserSession {
31-
/** Default threshold for refreshing session credentials */
32-
public static final int DEFAULT_THRESHOLD_SECONDS = 500;
3330
/**
3431
* Cognito identity token.
3532
*/
@@ -91,8 +88,8 @@ public CognitoRefreshToken getRefreshToken() {
9188
* @return boolean to indicate if the access and id tokens have not expired.
9289
*/
9390
public boolean isValid() {
94-
Date currentTimeStamp = new Date(System.currentTimeMillis()
95-
- SDKGlobalConfiguration.getGlobalTimeOffset() * 1000);
91+
Date currentTimeStamp = new Date();
92+
9693
try {
9794
return (currentTimeStamp.before(idToken.getExpiration())
9895
& currentTimeStamp.before(accessToken.getExpiration()));

0 commit comments

Comments
 (0)