Aws-sdk-android: getUsername() returning null

Created on 29 Feb 2020  路  4Comments  路  Source: aws-amplify/aws-sdk-android

State your question

userId = AWSMobileClient.getInstance().getUsername()

When this is called, it either returns null, or the direct email ID without any encryption when upload to DynamoDB on Android.

Which AWS Services are you utilizing?

Amplify / DynamoDB/ Cognito

Provide code snippets (if applicable)

AWSMobileClient.getInstance().signIn(email, password, null, new Callback<SignInResult>() {
    @Override
    public void onResult(final SignInResult signInResult) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "Sign-in callback state: " + signInResult.getSignInState());
                switch (signInResult.getSignInState()) {
                    case DONE:
                        Toast.makeText(getActivity(),"Sign-in done.",Toast.LENGTH_SHORT).show();
                        Navigation.findNavController(view).navigate(R.id.action_login_to_mainActivity);
                        //--SAVE Data
                        SharedPreferences preferences = getActivity().getPreferences(Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putString("UserState", "true");
                        editor.putString("Userid", AWSMobileClient.getInstance().getUsername());
                        Log.e("UserId-Login", AWSMobileClient.getInstance().getUsername());
                        editor.apply();

                        break;
                    case SMS_MFA:
                        Toast.makeText(getActivity(),"Please confirm sign-in with SMS.",Toast.LENGTH_SHORT).show();
                        break;
                    case NEW_PASSWORD_REQUIRED:
                        Toast.makeText(getActivity(),"Please confirm sign-in with new password.",Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        Toast.makeText(getActivity(),"Unsupported sign-in confirmation: " + signInResult.getSignInState(),Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    @Override
    public void onError(Exception e) {
        Log.e(TAG, "Sign-in error", e);

        String s = e.getLocalizedMessage().toString();
        int spacePos = s.indexOf("(");

        String s1 = s.substring(0,spacePos-1);
        s1.trim();
        //Log.e("s1",s1);

        Alerter.create(getActivity())
            .setTitle("Alert")
            .setText(s1)
            .setBackgroundColorRes(R.color.colorAccent)
            .show();
    }
});

We were following this document specifically: https://aws-amplify.github.io/docs/sdk/android/authentication

Error Stub:

I/AWSKeyValueStore: Detected Android API Level = 28
    Creating the AWSKeyValueStore with key for sharedPreferencesForData = CognitoIdentityProviderCache
E/AWSKeyValueStore: com.amazonaws.internal.keyvaluestore.KeyNotFoundException: Error occurred while accessing AndroidKeyStore to retrieve the key for keyAlias: com.amazonaws.mobile.client.aesKeyStoreAlias
I/AWSKeyValueStore: Deleting the encryption key identified by the keyAlias: com.amazonaws.mobile.client.aesKeyStoreAlias
E/AWSKeyValueStore: Error in retrieving the decryption key used to decrypt the data from the persistent store. Returning null for the requested dataKey = provider
E/AWSKeyValueStore: com.amazonaws.internal.keyvaluestore.KeyNotFoundException: Error occurred while accessing AndroidKeyStore to retrieve the key for keyAlias: com.amazonaws.mobile.client.aesKeyStoreAlias
I/AWSKeyValueStore: Deleting the encryption key identified by the keyAlias: com.amazonaws.mobile.client.aesKeyStoreAlias
E/AWSKeyValueStore: Error in retrieving the decryption key used to decrypt the data from the persistent store. Returning null for the requested dataKey = token
E/AWSKeyValueStore: com.amazonaws.internal.keyvaluestore.KeyNotFoundException: Error occurred while accessing AndroidKeyStore to retrieve the key for keyAlias: com.amazonaws.mobile.client.aesKeyStoreAlias
I/AWSKeyValueStore: Deleting the encryption key identified by the keyAlias: com.amazonaws.mobile.client.aesKeyStoreAlias
E/AWSKeyValueStore: Error in retrieving the decryption key used to decrypt the data from the persistent store. Returning null for the requested dataKey = cognitoIdentityId
E/AWSKeyValueStore: com.amazonaws.internal.keyvaluestore.KeyNotFoundException: Error occurred while accessing AndroidKeyStore to retrieve the key for keyAlias: com.amazonaws.mobile.client.aesKeyStoreAlias
I/AWSKeyValueStore: Deleting the encryption key identified by the keyAlias: com.amazonaws.mobile.client.aesKeyStoreAlias
E/AWSKeyValueStore: Error in retrieving the decryption key used to decrypt the data from the persistent store. Returning null for the requested dataKey = isFederationEnabled
D/AWSMobileClient: Inspecting user state details
I/INIT: onResult: SIGNED_OUT

If this stub does not show, then typically the username (email) is shown directly without any encryption

Environment(please complete the following information):

  • SDK Version: Amplify Android SDK 2.0

Device Information (please complete the following information):

  • Device: simulator and multiple devices
AWSMobileClient Bug

Most helpful comment

The problem is I'm not using federated login. We are using the AWS Cognito service to sign up/ sign in. The discussion seems to indicate that AWS does not support username retrieval for federated log-in but should work fine otherwise

All 4 comments

Hi @zgao67, thanks for the report. Are you saying that you don't get any value back for AWSMobileClient.getInstance().getUsername(), inside your Callback's onResult(...)? Or are you just concerned about the logs from AWSKeyValueStore? Can you update this report with a statement of the expected and actual behaviors? Thanks!

The expected return is the encrypted userName (this is the result we get when the corresponding code is run on IOS)

Actual:
But now I'm getting this stub with a null value even though it still allows me to sign in using the same credentials
E/AWSKeyValueStore: com.amazonaws.internal.keyvaluestore.KeyNotFoundException: Error occurred while accessing AndroidKeyStore to retrieve the key for keyAlias: com.amazonaws.mobile.client.aesKeyStoreAlias
I/AWSKeyValueStore: Deleting the encryption key identified by the keyAlias: com.amazonaws.mobile.client.aesKeyStoreAlias
E/AWSKeyValueStore: Error in retrieving the decryption key used to decrypt the data from the persistent store. Returning null for the requested dataKey = provider
E/User聽ID: null
At other times, we get an unencrypted userName (the email).

@zgao67 Got it - If I understand correctly, you're saying that on iOS, AWSMobileClient.default().username will return the username you provided during sign-in. But on Android, AWSMobileClient.getInstance().getUsername() sometimes returns null, instead of the value you provided during sign-in.

I notice some similar discussion here: https://github.com/aws-amplify/aws-sdk-android/issues/622

Sounds potentially like a bug.

The problem is I'm not using federated login. We are using the AWS Cognito service to sign up/ sign in. The discussion seems to indicate that AWS does not support username retrieval for federated log-in but should work fine otherwise

Was this page helpful?
0 / 5 - 0 ratings