Desired Flow:
- Begin with Cognito SRP flow to verify user's username and password combination
- If Username and password are correct, then move to CUSTOM_CHALLENGE
- Upon completion of CUSTOM_CHALLENGE (OTP verification), then issue token
When logging in using Cognito with MFA, I want the OTP generated by Cognito to be of the format ABC-123456. However after investigation I believe that this configuration is not available out of the box. i.e. Cognito seems to only generate OTPs of format 123456 (6-digits)
Therefore I utilised the createAuth, defineAuth and verifyAuth lambda triggers to handle the OTP generation and verification
However, I still need Cognito to handle username and password verification before handing things off to my lambda triggers.
I read that Cognito allows SRP Authentication (not plaintext username and password) followed by CUSTOM_CHALLENGE
I'm using @aws-sdk/client-cognito-identity-provider library, but cannot seem to get the initiateAuth method to behave correctly. It skips the SRP Authentication and moves straight to my custom challanges.
Looking at the documentation here
For CUSTOM_AUTH: USERNAME (required), SECRET_HASH (if app client is configured with client secret), DEVICE_KEY. To start the authentication flow with password verification, include ChallengeName: SRP_A and SRP_A: (The SRP_A Value).
Note: I'm not adding code snippets here as I will be answering below
There are two parts that need to be tackled
Handling SRP Authentication
SRP authentication flow goes as such (NOTE this is to begin with SRP and then move to CUSTOM_CHALLENGE)
SRP_ASALT,SECRET_BLOCK,SRP_B,USER_ID_FOR_SRP, andSession(and more)PASSWORD_CLAIM_SIGNATUREandTIMESTAMPfrom user's password, userId, srpB and saltLambda Triggers
I was referencing this blog post
Create Auth Trigger
Define Auth Trigger
And lastly Verify Auth Trigger