From 1c8c07ace3d4b08a99fcaab041a59e2f8975e5bb Mon Sep 17 00:00:00 2001 From: nikhil shekhawat Date: Wed, 29 Mar 2017 18:13:37 -0400 Subject: [PATCH 1/4] fixed bug for when multiple threads were calling CognitoUser.getSessionInBackground, right now there might be a scenario where 10 threads call this function and all of them go over the network to get the new ID token after the 1hour expiry. --- .../cognitoidentityprovider/CognitoUser.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java b/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java index 4a5ee982d8..1be99f72d7 100644 --- a/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java +++ b/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java @@ -163,6 +163,11 @@ public class CognitoUser { */ private CognitoUserSession cipSession; + /** + * The semaphore to provide mutex for the getSession call if called on multiple threads, this would avoid multiple threads going over the network and refreshing the tokens + */ + public static Semaphore semaphore = new Semaphore(1, true); + /** * Constructs a new Cognito User from a Cognito user identity pool {@link CognitoUserPool} and userId. * @@ -708,7 +713,7 @@ protected CognitoUserSession getCachedSession() { return cipSession; } } - + semaphore.aquire(); CognitoUserSession cachedTokens = readCachedTokens(); if (cachedTokens.isValidForThreshold()) { @@ -720,11 +725,14 @@ protected CognitoUserSession getCachedSession() { try { cipSession = refreshSession(cachedTokens); cacheTokens(cipSession); + semaphore.release(); return cipSession; } catch (NotAuthorizedException nae) { clearCachedTokens(); + semaphore.release(); throw new CognitoNotAuthorizedException("User is not authenticated", nae); } catch (Exception e) { + semaphore.release(); throw new CognitoInternalErrorException("Failed to authenticate user", e); } } From d5e28f37dff0702aef2c3b3b018824d5bf95db91 Mon Sep 17 00:00:00 2001 From: nikhil shekhawat Date: Wed, 29 Mar 2017 18:43:35 -0400 Subject: [PATCH 2/4] import statement --- .../mobileconnectors/cognitoidentityprovider/CognitoUser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java b/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java index 1be99f72d7..7876428932 100644 --- a/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java +++ b/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java @@ -99,7 +99,7 @@ import java.util.Locale; import java.util.Map; import java.util.TimeZone; - +import java.util.concurrent.Semaphore; /** * Represents a single Cognito User. *

From 628dd8472d7c6bd1090419a1bea291ac7e5e117d Mon Sep 17 00:00:00 2001 From: nikhil shekhawat Date: Wed, 29 Mar 2017 19:02:21 -0400 Subject: [PATCH 3/4] bugfix semaphore acquire --- .../cognitoidentityprovider/CognitoUser.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java b/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java index 7876428932..420571919b 100644 --- a/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java +++ b/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java @@ -713,7 +713,7 @@ protected CognitoUserSession getCachedSession() { return cipSession; } } - semaphore.aquire(); + CognitoUser.semaphore.acquire(); CognitoUserSession cachedTokens = readCachedTokens(); if (cachedTokens.isValidForThreshold()) { @@ -725,14 +725,14 @@ protected CognitoUserSession getCachedSession() { try { cipSession = refreshSession(cachedTokens); cacheTokens(cipSession); - semaphore.release(); + CognitoUser.semaphore.release(); return cipSession; } catch (NotAuthorizedException nae) { clearCachedTokens(); - semaphore.release(); + CognitoUser.semaphore.release(); throw new CognitoNotAuthorizedException("User is not authenticated", nae); } catch (Exception e) { - semaphore.release(); + CognitoUser.semaphore.release(); throw new CognitoInternalErrorException("Failed to authenticate user", e); } } From 7ac0976b7480b5eb5cf4fb962f68fe2f4eaf9505 Mon Sep 17 00:00:00 2001 From: nikhil shekhawat Date: Wed, 29 Mar 2017 19:10:35 -0400 Subject: [PATCH 4/4] Uncaught exception another error, need to download an IDE lol --- .../cognitoidentityprovider/CognitoUser.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java b/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java index 420571919b..8fcd4ab2e6 100644 --- a/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java +++ b/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java @@ -713,7 +713,12 @@ protected CognitoUserSession getCachedSession() { return cipSession; } } - CognitoUser.semaphore.acquire(); + try { + CognitoUser.semaphore.acquire(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + CognitoUserSession cachedTokens = readCachedTokens(); if (cachedTokens.isValidForThreshold()) {