Aws-sdk-android: AWSMobileCLient getTokens returns Exception: No cached session.

Created on 13 Feb 2020  路  16Comments  路  Source: aws-amplify/aws-sdk-android

State your question
I followed the Tutorial on https://aws-amplify.github.io/docs/sdk/android/authentication. After a sucessfully calling the signIn Method from the AWSMobileCLient as stated in the tutorial, I get back the message "Tokens are invalid, please sign-in again. Exception: No chached session."
The userState of my user is logged in.
The essential code:

    AWSMobileClient.getInstance().signIn(username, password, null, new Callback<SignInResult>               () {
        @Override
        public void onResult(final SignInResult signInResult) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.d("CognitoService", "Sign-in callback state: " + signInResult
                            .getSignInState());
                    switch (signInResult.getSignInState()) {
                        case DONE:
                            makeToast(context, "Sign-in done.");
                            confirmSignIn();
                            try {
                                Log.d("Debug", AWSMobileClient.getInstance().getTokens().getAccessToken
                                        ().toString());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            Intent intent = new Intent(getApplicationContext(), MainActivity
                                    .class);
                            startActivity(intent);
                            break;
                        case SMS_MFA:
                            makeToast(context, "Please confirm sign-in with SMS.");
                            break;
                        case NEW_PASSWORD_REQUIRED:
                            makeToast(context, "Please confirm sign-in with new password.");
                            break;
                        default:
                            makeToast(context, "Unsupported sign-in confirmation: "
                                    + signInResult.getSignInState());
                            break;
                    }
                }
            });
        }`

Here the stacktrace of the exception:
It is weird that the getTokens() call delegates the logic to a _getHostedUITokens() method, even if I don't use the hosted ui in this setup. I only use the hosted UI when clicking a login with FB button.

02-13 10:44:13.647 28585-28585/com.example.tobi.androidapp W/AWSMobileClient: Tokens are invalid, please sign-in again.
java.lang.Exception: No cached session.
    at com.amazonaws.mobile.client.AWSMobileClient$12.onFailure(AWSMobileClient.java:1818)
    at com.amazonaws.mobileconnectors.cognitoauth.AuthClient.getSession(AuthClient.java:182)
    at com.amazonaws.mobileconnectors.cognitoauth.Auth.getSession(Auth.java:674)
    at com.amazonaws.mobile.client.AWSMobileClient._getHostedUITokens(AWSMobileClient.java:1821)
    at com.amazonaws.mobile.client.AWSMobileClient.access$800(AWSMobileClient.java:161)
    at com.amazonaws.mobile.client.AWSMobileClient$11.run(AWSMobileClient.java:1743)
    at com.amazonaws.mobile.client.internal.InternalCallback.await(InternalCallback.java:115)
    at com.amazonaws.mobile.client.AWSMobileClient.getTokens(AWSMobileClient.java:1717)
    at com.amazonaws.mobile.client.AWSMobileClient.getUserStateDetails(AWSMobileClient.java:1024)
    at com.amazonaws.mobile.client.AWSMobileClient.waitForSignIn(AWSMobileClient.java:903)
    at com.amazonaws.mobile.client.AWSMobileClient$11.run(AWSMobileClient.java:1733)
    at com.amazonaws.mobile.client.internal.InternalCallback.await(InternalCallback.java:115)
    at com.amazonaws.mobile.client.AWSMobileClient.getTokens(AWSMobileClient.java:1699)
    at com.example.tobi.androidapp.LoginActivity$3$1.run(LoginActivity.java:120)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.Exception: No cached session
    at com.amazonaws.mobileconnectors.cognitoauth.AuthClient.getSession(AuthClient.java:182)聽
    at com.amazonaws.mobileconnectors.cognitoauth.Auth.getSession(Auth.java:674)聽
    at com.amazonaws.mobile.client.AWSMobileClient._getHostedUITokens(AWSMobileClient.java:1821)聽
    at com.amazonaws.mobile.client.AWSMobileClient.access$800(AWSMobileClient.java:161)聽
    at com.amazonaws.mobile.client.AWSMobileClient$11.run(AWSMobileClient.java:1743)聽
    at com.amazonaws.mobile.client.internal.InternalCallback.await(InternalCallback.java:115)聽
    at com.amazonaws.mobile.client.AWSMobileClient.getTokens(AWSMobileClient.java:1717)聽
    at com.amazonaws.mobile.client.AWSMobileClient.getUserStateDetails(AWSMobileClient.java:1024)聽
    at com.amazonaws.mobile.client.AWSMobileClient.waitForSignIn(AWSMobileClient.java:903)聽
    at com.amazonaws.mobile.client.AWSMobileClient$11.run(AWSMobileClient.java:1733)聽
    at com.amazonaws.mobile.client.internal.InternalCallback.await(InternalCallback.java:115)聽
    at com.amazonaws.mobile.client.AWSMobileClient.getTokens(AWSMobileClient.java:1699)聽
    at com.example.tobi.androidapp.LoginActivity$3$1.run(LoginActivity.java:120)聽
    at android.os.Handler.handleCallback(Handler.java:790)聽
    at android.os.Handler.dispatchMessage(Handler.java:99)聽
    at android.os.Looper.loop(Looper.java:164)聽
    at android.app.ActivityThread.main(ActivityThread.java:6494)聽
    at java.lang.reflect.Method.invoke(Native Method)聽
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)聽
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Which AWS Services are you utilizing?
I am using the Amplify Android SDK for authenticating my users.
Version: 2.16.+ (aws-android-sdk-mobile-client, com.amazonaws:aws-android-sdk-cognitoauth, ...)

Environment(please complete the following information):

Device Information (please complete the following information):

  • Emulated Device: Nexus 6 API 27
AWSMobileClient Bug Needs Info from Requester

Most helpful comment

Alright thanks - we'll look into this. Since it sounds like you're not blocked by it for now we'll keep it as a lower priority item but does sound like something we'll want to ultimately address.

All 16 comments

Hey,

It seems this issue is caused by running the non-async getTokens() method on the main UI thread. You can either run it on a background thread or run the version of getTokens that takes a callback:

AWSMobileClient.getInstance().getTokens(Callback)

Hi,
In my MainActivity I tried to access the tokens with the callback version, but I also get the exception:

2-22 14:54:53.611 6558-6605/com.example.tobi.androidapp D/JWT-error:: java.lang.Exception: No cached session. 02-22 14:54:53.614 6558-6605/com.example.tobi.androidapp D/JWT-error:: java.lang.Exception: No cached session. at com.amazonaws.mobile.client.AWSMobileClient$12.onFailure(AWSMobileClient.java:1818) at com.amazonaws.mobileconnectors.cognitoauth.AuthClient.getSession(AuthClient.java:182) at com.amazonaws.mobileconnectors.cognitoauth.Auth.getSession(Auth.java:674) at com.amazonaws.mobile.client.AWSMobileClient._getHostedUITokens(AWSMobileClient.java:1821) at com.amazonaws.mobile.client.AWSMobileClient.access$800(AWSMobileClient.java:161) at com.amazonaws.mobile.client.AWSMobileClient$11.run(AWSMobileClient.java:1743) at com.amazonaws.mobile.client.internal.InternalCallback$1.run(InternalCallback.java:101) at java.lang.Thread.run(Thread.java:764) Caused by: java.lang.Exception: No cached session

here the corresponding code, where the exception is thrown.
I call this in the onCreate method form my MainActivity Class:

 AWSMobileClient.getInstance().getTokens(
            new Callback<Tokens>() {
                @Override
                public void
                onResult(Tokens result) {
                    Log.d("JWT:" , "method Callback");
                    Log.d("JWT: ", result.getAccessToken()
                            .toString());
                    jwtTextView.setText(result.getAccessToken().toString());
                }

                @Override
                public void
                onError(Exception e) {
                    Log.d("JWT-error: ", e.toString());
                    StringWriter sw = new StringWriter();
                    PrintWriter pw = new PrintWriter(sw);
                    e.printStackTrace(pw);
                    String sStackTrace = sw.toString(); // stack trace as a string
                    Log.d("JWT-error: ", sStackTrace);
                }
            });

I do not understand why the AWS SDK calls the method AWSMobileClient._getHostedUITokens, even if I don't use the HostedUi in this Login flow.
It shouldn't be a problem that I call "getTokens" within the onCreate method right?

Gotcha - yeah are you calling it after a successful callback from the AWSMobileClient initialize though?

My initialization is done in my starting activity as following:

AWSMobileClient.getInstance().initialize(getApplicationContext(), new
            Callback<UserStateDetails>() {

                @Override
                public void onResult(UserStateDetails userStateDetails) {
                    Log.i("CognitoService", "onResult: " + userStateDetails.getUserState());
                }

                @Override
                public void onError(Exception e) {
                    Log.e("CognitoService", "Initialization error.", e);
                }
            }
    );

I have retried everything with a completeley new emulator and the problem did not appear then.
So I guess it was just a problem with a wrong cached state in my emulators. Apparently restarting the app in the emulators over android studio is not enough sometimes...
Thanks for your help and sorry for your time expenses.

Ok gotcha - thanks for the update!

Hey @TobiS97 - just wanted to let you know I happened to come across this issue in my own testing and found you are 100% right and it is a bug on our end where once a user has signed in with Hosted UI, it permanently caches that which messes up getting tokens if you sign in with the regular sign in method. We will be fixing this.

What version would fix this issue? I'm hitting that problem at the moment.

Looks like it's in 2.16.10 or later - any reason you can't just use the latest version?

@TrekSoft my bad, I've been hitting that problem somehow else. Might not be interesting, but it happened when:

  • logged in with user1
  • logged in with user2 (without calling signOut)
  • logged out with user2
  • tried to log in with user1 -> that error
  • tried to log in with user2 -> success.

I'm calling signOut between the first 2 steps now. Not sure if it's a bug, just letting you know here :)

Hmm gotcha - and this was with signing in with Hosted UI for both users?

Nope, as far as I know (I've never used these SDKs many times, I'm part of a project).
I'm using the aws-android-sdk-mobile-client-2.16.12-sources.jar

Got it - what I'm asking is, how did you sign in the users? What method were you calling to do it?

I used this one:

@AnyThread
public void signIn(final String username,
                   final String password,
                   final Map<String, String> validationData,
                   final Callback<SignInResult> callback) {

    final InternalCallback<SignInResult> internalCallback = new InternalCallback<SignInResult>(callback);
    internalCallback.async(_signIn(username, password, validationData, internalCallback));
}

Hmm, ok - and can you post the specific error you're getting with the stack trace?

W/AWSMobileClient: signalTokensNotAvailable
W/AWSMobileClient: Tokens are invalid, please sign-in again.
java.lang.Exception: No cached session.
at com.amazonaws.mobile.client.AWSMobileClient$11$1.signalTokensNotAvailable(AWSMobileClient.java:1801)
at com.amazonaws.mobile.client.AWSMobileClient$11$1.getAuthenticationDetails(AWSMobileClient.java:1781)
at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.getSession(CognitoUser.java:778)
at com.amazonaws.mobile.client.AWSMobileClient$11.run(AWSMobileClient.java:1763)
at com.amazonaws.mobile.client.internal.InternalCallback.await(InternalCallback.java:115)
at com.amazonaws.mobile.client.AWSMobileClient.getTokens(AWSMobileClient.java:1729)
at com.amazonaws.mobile.client.AWSMobileClient.getUserStateDetails(AWSMobileClient.java:1031)
at com.amazonaws.mobile.client.AWSMobileClient.waitForSignIn(AWSMobileClient.java:910)
at com.amazonaws.mobile.client.AWSMobileClient$11.run(AWSMobileClient.java:1745)
at com.amazonaws.mobile.client.internal.InternalCallback.await(InternalCallback.java:115)
at com.amazonaws.mobile.client.AWSMobileClient.getTokens(AWSMobileClient.java:1711)
at com.xxx.xxx.engine.comms.aws.xxx.getTokens(AWSClient.kt:92)
at com.xxx.xxx.engine.comms.api.CognitoLoginAPI.getToken(LoginAPI.kt:60)
at xxx.xxx.xxx.engine.comms.retrofit.OkHttpClientBuilder$$special$$inlined$invoke$1.intercept(Interceptor.kt:81)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:197)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:502)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
D/AWSMobileClient: waitForSignIn: userState:SIGNED_OUT_USER_POOLS_TOKENS_INVALID

Alright thanks - we'll look into this. Since it sounds like you're not blocked by it for now we'll keep it as a lower priority item but does sound like something we'll want to ultimately address.

Was this page helpful?
0 / 5 - 0 ratings