fix(auth): Fix issue with extracting username during USER_PASSWORD_AUTH #3149
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue #, if available: #3145
Description of changes:
This PR changes how we read the actual username back from the InitiateAuthResponse and RespondToAuthChallengeResponse.
Amplify stores various pieces of data under keys derived from the user's username, including device metadata used for device tracking. We need to be able to retrieve that metadata or later requests can fail, as in the linked issue.
The "username" supplied when signing in, however, may not be the user's actual username, because Cognito can be configured to allow customers to sign in via other attributes such as email and phone number. In these cases the identifier used to sign in does not have the same value as the username.
Amplify does not store the identifier used to sign in, instead for signed-in users it reads the username back from the stored JWT credentials, which can be different from the alias used to sign in initially. Therefore we need to be careful and ensure that any information we are storing is done under the actual username, and not just the identifier used during sign in.
The previous implementation only ever checked the
challengeParamsto see if they specified a username. That generally works for SRP, UserAuth, and CustomAuth because the first thing that happens is that you get back a challenge containing challengeParams. For PasswordAuth however you only get back a challenge in some situations (e.g. have enabled MFA). Otherwise you directly get back an AuthenticationResult on the initial InitiateAuth and there are no challengeParams.This PR changes the handling so that we can get the username from either the AuthenticationResult or challengeParams, which resolves the issue.
NB: Cognito discourages storing information under the username (see warning here), so this whole setup is not ideal. We should look into migrating to use the
subas the key instead.How did you test these changes?
(Please add a line here how the changes were tested)
Documentation update required?
General Checklist
fix(storage): message,feat(auth): message,chore(all): message)By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.