aws-amplify / aws-sdk-android

AWS SDK for Android. For more information, see our web site:
https://docs.amplify.aws
Other
1.03k stars 551 forks source link

No cached session while getting access token on version 2.69.0 #3433

Open m-zagorski opened 11 months ago

m-zagorski commented 11 months ago

Describe the bug We are using aws-android-sdk-mobile-client version 2.69.0 and are seeing a lot of No cached session - almost 1k per day. So what is happening is that when we get Unauthorized we are calling AWSMobileClient.getTokens with a callback - all calls to this method are synchronized and we're waiting for the AWSMobileClient to be fully initialized (we think its initialized when awsMobileClient.configuration is not null). In return we're getting No cached session exception thrown from within signalTokensNotAvailable method inside the client. Currently we are unable to update the library to amplify due to the changes that would be required on the backend side. however from analytics we can see that this issue is not only related to the current version - we're seeing it for quite few months. Also important thing is that we have the device tracking turned off.

To Reproduce We were unable to reproduce it locally. What we see from logs, most cases are for the users that were not using the application for quite some time, so that the access token is expired, then when trying to refresh the tokens we get the No cached session. We have checked that AWS Client is initialized, also we have user data in the app - its only that getTokens that makes the session invalid.

Which AWS service(s) are affected? Only android - aws-android-sdk-mobile-client

Expected behavior getTokens return correct access tokens or more meaningful exception so we can act on our end

Screenshots N/a

Environment Information (please complete the following information):

Additional context N/a

tylerjroach commented 11 months ago

What we see from logs, most cases are for the users that were not using the application for quite some time, so that the access token is expired, then when trying to refresh the tokens we get the No cached session.

Refresh tokens also have an expiration configured. What is the refresh token expiration set to in Cognito? Once a refresh token has expired, the user will need to sign in again.

m-zagorski commented 11 months ago

What we see from logs, most cases are for the users that were not using the application for quite some time, so that the access token is expired, then when trying to refresh the tokens we get the No cached session.

Refresh tokens also have an expiration configured. What is the refresh token expiration set to in Cognito? Once a refresh token has expired, the user will need to sign in again.

We have 730 days of expiration set for the project. So thats not the case. Also we're not experiencing similar issues on iOS where we have even more users.

tylerjroach commented 11 months ago

Can you provide your awsconfiguration file with sensitive information obfuscated? Additionally, what sign in type are you using?

m-zagorski commented 11 months ago

Do you mean this one? Screenshot 2023-10-19 at 09 24 38

As for the sign in type - I'm not sure which one is the most affected, but we're having password, facebook and google - we dont know which method was used when we get No cached session

tylerjroach commented 11 months ago

Hi @m-zagorski, that is helpful but looking for the awsconfiguration.json file in your app/src/res/raw folder.

I was trying to also figure out what sign in method you were using. It sounds like if you are using password, facebook, and google, you are using the hosted (web) ui sign in flow and not SRP.

If this is the case, please check the awsconfiguration.json file and see if openid is listed under the Scopes. If this is missing, the refresh token will not work.

m-zagorski commented 11 months ago

Hey, we have openid set in scopes: "Scopes": ["openid", "email", "profile"] - and for the sign in method - we're using mostly the hosted (web) ui sign in flow (we have changed google recently but this did not affect the case of No cached session

tylerjroach commented 11 months ago

@m-zagorski I've got a few questions to continue asking.

Please try and provide the awsconfiguration.json file so that we can take a further look and make sure nothing looks misconfigured on that file.

m-zagorski commented 11 months ago

Hello, so:

As for the awsconfiguration.json file here it comes: (staging one has exactly the same structure):

{
  "IdentityManager": {
    "Default": {},
    "Custom": {}
  },
  "CognitoUserPool": {
    "Default": {
      "PoolId": "eu-west-1_****",
      "AppClientId": "****",
      "Region": "eu-west-1"
    },
    "Custom": {
      "PoolId": "eu-west-1_****",
      "AppClientId": "****",
      "Region": "eu-west-1"
    }
  },
  "Auth": {
    "Default": {
      "OAuth": {
        "WebDomain": "accounts.hidden.com",
        "AppClientId": "****",
        "SignInRedirectURI": "hidden://sign-in",
        "SignOutRedirectURI": "hidden://",
        "Scopes": ["openid", "email", "profile"]
      }
    },
    "Custom": {
      "OAuth": {
        "WebDomain": "accounts.hidden.com",
        "AppClientId": "****",
        "SignInRedirectURI": "hidden://sign-in",
        "SignOutRedirectURI": "hidden://",
        "Scopes": ["openid", "email", "profile"]
      },
      "authenticationFlowType": "CUSTOM_AUTH"
    }
  }
}
ankpshah commented 11 months ago

@m-zagorski is it possible for you to provide detailed logs/stacktrace with sensitive information obfuscated? It would be helpful to understand under what condition no cached session messages are being logged to further identify root cause.

m-zagorski commented 10 months ago

@ankpshah Unfortunately we dont have full stacktrace - only the message from exception coming from cognito, I will check if we can change the logs so that we have full stacktrace

m-zagorski commented 10 months ago

@ankpshah Here is the stacktrace we're getting:

Non-fatal Exception: java.lang.Exception: No cached session.
       at com.amazonaws.mobile.client.AWSMobileClient$11$1.signalTokensNotAvailable(AWSMobileClient.java:1968)
       at com.amazonaws.mobile.client.AWSMobileClient$11$1.getAuthenticationDetails(AWSMobileClient.java:1948)
       at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.getSession(CognitoUser.java:1032)
       at com.amazonaws.mobile.client.AWSMobileClient$11.run(AWSMobileClient.java:1929)
       at com.amazonaws.mobile.client.internal.InternalCallback$1.run(InternalCallback.java:101)
       at java.lang.Thread.run(Thread.java:923)
ankpshah commented 7 months ago

Hello @m-zagorski, Could you upgrade to AWS Android SDK v2.75.0 which provides a clear message during getTokens when there are no valid tokens on device. When doing so, please make sure that you update your implementation for the logic to check for failures.

The changes can be found here: https://github.com/aws-amplify/aws-sdk-android/commit/46fb3c3895db3135aa507bcda9aa6d6b88db8d33

Here is a quick summary:

If the token fails to refresh, the caller will receive an exception with "No cached session". The caller is already aware of this. callback.onError(new Exception("No cached session.", exception));

However, sometimes provided inner exception was null. This is no longer the case, you will receive additional details about exception.

If you receive CognitoNotAuthorizedException, this means that the tokens are no longer valid. The user can be signed out. callback.onError(new Exception("No cached session.", new CognitoNotAuthorizedException("No valid tokens on device.")));

In any other exception case, the issue is likely to be transient. You may want to log this exception so you can report if you are seeing an abnormal amount of any other exception types.

lolucosmin commented 3 months ago

@m-zagorski @ankpshah please check my post: https://github.com/aws-amplify/aws-sdk-android/issues/3572 Can you check if this issue is on Huawei devices? Also bellow and above that log error: Non-fatal Exception: java.lang.Exception: No cached session. at com.amazonaws.mobile.client.AWSMobileClient$11$1.signalTokensNotAvailable(AWSMobileClient.java:1968) at com.amazonaws.mobile.client.AWSMobileClient$11$1.getAuthenticationDetails(AWSMobileClient.java:1948) at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.getSession(CognitoUser.java:1032) at com.amazonaws.mobile.client.AWSMobileClient$11.run(AWSMobileClient.java:1929) at com.amazonaws.mobile.client.internal.InternalCallback$1.run(InternalCallback.java:101) at java.lang.Thread.run(Thread.java:923)

can you tell us if you get more information? On my side there I get few information why there no "No cached session".