Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getUsername() returning null #1495

Closed
zgao67 opened this issue Feb 29, 2020 · 6 comments
Closed

getUsername() returning null #1495

zgao67 opened this issue Feb 29, 2020 · 6 comments
Labels
bug Something isn't working closing soon Issue will auto-close if there is no additional activity within 7 days. mobile client Issues with AWS Mobile's client-side Cognito wrapper

Comments

@zgao67
Copy link

zgao67 commented Feb 29, 2020

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
@jamesonwilliams jamesonwilliams added the mobile client Issues with AWS Mobile's client-side Cognito wrapper label Feb 29, 2020
@jamesonwilliams
Copy link
Contributor

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!

@jamesonwilliams jamesonwilliams added the pending-community-response Issue is pending response from the issue requestor label Feb 29, 2020
@zgao67
Copy link
Author

zgao67 commented Feb 29, 2020

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).

@jamesonwilliams
Copy link
Contributor

@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: #622

Sounds potentially like a bug.

@jamesonwilliams jamesonwilliams added bug Something isn't working and removed pending-community-response Issue is pending response from the issue requestor labels Feb 29, 2020
@zgao67
Copy link
Author

zgao67 commented Feb 29, 2020

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

@jpignata jpignata changed the title UserID encryption not working getUsername() returning null Apr 14, 2020
@div5yesh
Copy link
Contributor

div5yesh commented May 6, 2022

Closing due to inactivity. Please comment or reopen this issue if you are still facing the issue with the latest version of the SDK.

@div5yesh div5yesh closed this as completed May 6, 2022
@div5yesh div5yesh added the closing soon Issue will auto-close if there is no additional activity within 7 days. label May 6, 2022
@zi6xuan
Copy link

zi6xuan commented Mar 30, 2024

@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: #622

Sounds potentially like a bug.

I also encountered a similar problem. The sdkusername of iOS returns the real userid set in the cognito background, but the Android usually returns the account number in the account password entered by the user. It is not clear why the results of the SDK performance of the two platforms are inconsistent.
At present, my test results are consistent at least for the ios and javascript versions of the sdk. Only the android version is different.
Officials have no intention of fixing this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working closing soon Issue will auto-close if there is no additional activity within 7 days. mobile client Issues with AWS Mobile's client-side Cognito wrapper
Projects
None yet
Development

No branches or pull requests

4 participants