Skip to content

Commit 7388dd7

Browse files
author
beostjer
committed
Fix Cognito User Pools token refresh and failure handling
1 parent 755b796 commit 7388dd7

File tree

1 file changed

+12
-6
lines changed
  • aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider

1 file changed

+12
-6
lines changed

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

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

25+
import com.amazonaws.AmazonClientException;
2526
import com.amazonaws.AmazonServiceException;
2627
import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation;
2728
import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationDetails;
@@ -644,18 +645,23 @@ private CognitoUserSession getCachedSession() {
644645
return cipSession;
645646
}
646647

647-
// Clear any cached tokens, since none of them are valid.
648-
clearCachedTokens();
649-
650648
if (cachedTokens.getRefreshToken() != null) {
651649
// Use Refresh token to get new tokens
652650
try {
653651
cipSession = refreshSessionInternal(cachedTokens.getRefreshToken());
654652
cacheTokens(cipSession);
655653
return cipSession;
656-
} catch (Exception e) {
654+
} catch (CognitoNotAuthorizedException e) {
655+
// Clear any cached tokens, since none of them are valid.
656+
clearCachedTokens();
657657
// Could not get new tokens from refresh. Should authenticate user.
658-
throw new CognitoNotAuthorizedException("user is not authenticated");
658+
throw new CognitoNotAuthorizedException("user is not authenticated",e);
659+
} catch (AmazonClientException e) {
660+
// General IO errors - not clearing cached tokens
661+
throw new AmazonClientException("failed to get new tokens from refresh",e);
662+
} catch (Exception e) {
663+
// Errors like NetworkOnMainThreadException etc - not clearing cached tokens.
664+
throw new AmazonClientException("failed to get new tokens from refresh",e);
659665
}
660666
}
661667
throw new CognitoNotAuthorizedException("user is not authenticated");
@@ -2018,7 +2024,7 @@ private CognitoUserSession refreshSessionInternal(CognitoRefreshToken refreshTok
20182024
cognitoIdentityProviderClient.refreshTokens(refreshTokensRequest);
20192025
AuthenticationResultType authenticationResult = refreshTokensResult.getAuthenticationResult();
20202026

2021-
if (authenticationResult != null) {
2027+
if (authenticationResult == null) {
20222028
throw new CognitoNotAuthorizedException("user is not authenticated");
20232029
}
20242030

0 commit comments

Comments
 (0)