amplify-js: Auth.setupTOTP - Invalid session for the user
Describe the bug I set up Amplify with Cognito authentication with SMS MFA enabled. I have configured my own UI to provide the credentials and it used to work just fine. However, the SMS text messages suddenly stopped being delivered. I could tell the messages were trying to be sent. CloudWatch had the errors logged and said “Phone is currently unreachable/unavailable.” In all the research I’ve done, I haven’t been able to find a reason why. Increased spending limits did not solve the problem. I encountered another issue that sounded similar [#5137] and although it wasn’t technically resolved I decided to implement the workaround from that issue.
The workaround was to implement TOTP MFA. However, I am unable to get it to work. Every time
Auth.setupTOTP(user)
is called, I get this error:
{code: "NotAuthorizedException", name: "NotAuthorizedException", message: "Invalid session for the user."}
This is the CognitoUser object returned from the sign in method provided by Amplify:
{
Session: "xxxxxx"
authenticationFlowType: "USER_SRP_AUTH"
challengeName: "SMS_MFA"
challengeParam: {CODE_DELIVERY_DELIVERY_MEDIUM: "SMS", CODE_DELIVERY_DESTINATION: "+*******9088"}
client: Client {endpoint: "https://cognito-idp.us-west-2.amazonaws.com/", userAgent: "aws-amplify/0.1.x js"}
keyPrefix: "CognitoIdentityServiceProvider.xxx"
pool: CognitoUserPool {userPoolId: "us-west-2_xxx", clientId: "xxx", client: Client, advancedSecurityDataCollectionFlag: true, storage: Storage}
signInUserSession: null
storage: Storage {aws.cognito.identity-id.us-west-2:xxx: "us-west-2:xxx", aws.cognito.identity-id.us-west-2:xxx: "us-west-2:xxx", CognitoIdentityId-us-west-2:xxx: "us-west-2:xxx", CognitoIdentityId-us-west-2:xxx: "us-west-2:xxx", length: 4}
userDataKey: "CognitoIdentityServiceProvider.xxx.michael.userData"
username: "michael"
}
The Session variable has a value, but the signInUserSession variable is null. I assume the signInUserSession being null is the reason why this isn’t working.
To Reproduce Steps to reproduce the behavior:
- Configure app via amplify cli to have MFA (TOTP) required
- Sign up a new user (confirm email and everything)
- Sign in newly created user using
Auth.signIn - Attempt to set up MFA TOTP via
Auth.setupTOTPwith theuserobject returned from the sign in method
Expected behavior
Auth.setupTOTP(user) would accept the user object returned from the Auth.signIn(username, password) method to allow MFA TOTP to be configured. With this, the setupMFA method would return a code to generate a QR code for end user to configure their Authenticator application.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 27 (7 by maintainers)
Just wanna confirm that following workflow works: initiateAuth -> associcateSoftwareToken -> VerifySoftwareToken -> RespondToAuthChallenge.
I’m currently seeing this issue.
this topic helped clarify the TOTP MFA registration process for me so thank you for that.
for anyone else in a similar situation:
you can hack apart the below (hacks) to fit your needs. in our case we are using cognito to secure alb traffic to a 3rd party app dashboard (traefik) and basic auth just wont side on the security side.
using a python script for secret_hash (because we have app_client secret) and a bash script to init/verify i was able to register MFA. the below is given without warranty and your mileage may very 😃
the trick is getting the output of the bash script into the MFA generator quickly enough to confirm a response before the session ends. if your set up in advance you can copy/paste it + send it to your phone for quick access to enter into google authenticator. you can use any (hopefully more secure) means of doing this then say text message or email.
the starting point for these scripts came from (credits):
secrethash.pyinitauth.shHi @chrisbonifacio , I’ve used
@aws-sdk/client-cognito-identity-providerwith the same flow proposed by you and still getting thisInvalid session for the user.in the same way as I did with Amplify.Everything was good unless I call to VerifySoftwareToken using
Sessionreturned from InitAuth that is requiring “MFA_SETUP” challenge. I know it’s not an Amplify’s issue. Shall I report the issue in theaws-sdkrepo or you can help me somehow?Hi @chrisbonifacio , thank you for investigation. Let me try to reproduce it with
amazon-cognito-identity-jsas I’m using it on my backend.