Skip to content

Commit 6ff8bc2

Browse files
author
beostjer
committed
Cognito User Pools: renew sessions before expiry threshold and take into account client clock skew
1 parent 7388dd7 commit 6ff8bc2

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import com.amazonaws.AmazonClientException;
2626
import com.amazonaws.AmazonServiceException;
27+
import com.amazonaws.SDKGlobalConfiguration;
2728
import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation;
2829
import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationDetails;
2930
import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ForgotPasswordContinuation;
@@ -103,6 +104,8 @@
103104
*/
104105
public class CognitoUser {
105106
private final String TAG = "CognitoUser";
107+
/** Default threshold for refreshing session credentials */
108+
public static final int DEFAULT_THRESHOLD_SECONDS = 500;
106109

107110
/**
108111
* Application context.
@@ -620,6 +623,24 @@ public void getSession(final AuthenticationHandler callback) {
620623
}
621624
}
622625

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+
623644
/**
624645
* Call this method for valid, cached tokens for this user.
625646
*
@@ -630,19 +651,17 @@ private CognitoUserSession getCachedSession() {
630651
throw new CognitoNotAuthorizedException("User-ID is null");
631652
}
632653

633-
if (cipSession != null) {
634-
if (cipSession.isValid()) {
635-
return cipSession;
636-
}
654+
if (!needsNewSession(cipSession)) {
655+
return cipSession;
637656
}
638657

639658
// Read cached tokens
640659
CognitoUserSession cachedTokens = readCachedTokens();
641660

642-
// Return cached tokens if they are still valid
643-
if (cachedTokens.isValid()) {
661+
// Return cached tokens if they are still valid with some margin
662+
if (!needsNewSession(cachedTokens)) {
644663
cipSession = cachedTokens;
645-
return cipSession;
664+
return cipSession;
646665
}
647666

648667
if (cachedTokens.getRefreshToken() != null) {

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

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

1818
package com.amazonaws.mobileconnectors.cognitoidentityprovider;
1919

20+
import com.amazonaws.SDKGlobalConfiguration;
2021
import com.amazonaws.mobileconnectors.cognitoidentityprovider.tokens.CognitoAccessToken;
2122
import com.amazonaws.mobileconnectors.cognitoidentityprovider.tokens.CognitoIdToken;
2223
import com.amazonaws.mobileconnectors.cognitoidentityprovider.tokens.CognitoRefreshToken;
@@ -27,6 +28,8 @@
2728
* This wraps all Cognito tokens for a user.
2829
*/
2930
public class CognitoUserSession {
31+
/** Default threshold for refreshing session credentials */
32+
public static final int DEFAULT_THRESHOLD_SECONDS = 500;
3033
/**
3134
* Cognito identity token.
3235
*/
@@ -88,8 +91,8 @@ public CognitoRefreshToken getRefreshToken() {
8891
* @return boolean to indicate if the access and id tokens have not expired.
8992
*/
9093
public boolean isValid() {
91-
Date currentTimeStamp = new Date();
92-
94+
Date currentTimeStamp = new Date(System.currentTimeMillis()
95+
- SDKGlobalConfiguration.getGlobalTimeOffset() * 1000);
9396
try {
9497
return (currentTimeStamp.before(idToken.getExpiration())
9598
& currentTimeStamp.before(accessToken.getExpiration()));

0 commit comments

Comments
 (0)