exanubes

Passwordless authentication flow in Cognito User Pool

Following up on setting up a custom mailer in cognito we are going to configure and implement custom authentication flow for AWS Cognito User Pool. To do that we will use the cognito stack created in the previous article, change the authentication configuration and implement custom lambdas to handle the new authentication flow. Then we will have to go into the backend application and update the api to facilitate the new flow as well.

The authentication flow we'll be implementing is a passwordless approach where users provide their username and in turn they will receive a login link on their email address.

You can find the finished code on GitHub or run the following command if you'd like to follow along

Double click to copy
git clone -b start git@github.com:exanubes/cognito-passwordless-authentication-flow.git

Custom authentication flow #

Custom authentication flow diagram There are several steps Cognito has to take when using a custom authentication flow, many of which happen behind the scenes. However, it is useful to know the entire flow.

Naturally, it all starts with a user request to begin the authentication process. That leads Cognito to create a new session token for us – which expires after 3 minutes – and send it over to defineChallenge lambda. This is where we come in, as we're going to have to implement the lambda handler to begin the authentication process. This lambda acts as a state machine for the entire authentication flow. As this is the first time the lambda is invoked in this session, we're going to return one of the challenge types that have been predefined by AWS. You can find ChallengeNameType enum in the AWS SDK repository to see all possible challenge types.

Subsequently, Cognito takes that challenge type and triggers createChallenge lambda. Again, it will be up to us to implement handler logic and decide how we want the custom authentication to work. Once all the challenge parameters have been set we return public params to the client along with the session token.

We're back to the user now. Once he enters the challenge response, we're gonna take the username, the response and the session token and send the respondToAuthChallenge command. Cognito will then take our response and trigger the verifyChallenge lambda. Here, we're gonna check if the provided challenge response matches the correct answer set by us in createChallenge lambda.

If successful, Cognito will invoke defineAuth lambda again, which will respond with authentication tokens this time. After all that Cognito sends tokens to the client and the user is authenticated.

Lambda resources #

typescript
Double click to copy
1// stacks/cognito.stack.ts
2 private createCustomChallengeLambdas(): Function[] {
3 const defineChallengeLambda = new Function(
4 this,
5 "define-challenge-lambda",
6 {
7 runtime: Runtime.NODEJS_14_X,
8 handler: "index.handler",
9 code: Code.fromAsset(join(__dirname, "..", "lambdas/define-challenge")),
10 environment: {
11 CHALLENGE_NAME: ChallengeNameType.CUSTOM_CHALLENGE,
12 },
13 }
14 );
15 const createChallengeLambda = new Function(
16 this,
17 "create-challenge-lambda",
18 {
19 runtime: Runtime.NODEJS_14_X,
20 handler: "index.handler",
21 code: Code.fromAsset(join(__dirname, "..", "lambdas/create-challenge")),
22 layers: [this.layer],
23 environment: {
24 SENDGRID_API_KEY: String(process.env.SENDGRID_API_KEY),
25 },
26 }
27 );
28 const verifyChallengeLambda = new Function(
29 this,
30 "verify-challenge-lambda",
31 {
32 runtime: Runtime.NODEJS_14_X,
33 handler: "index.handler",
34 code: Code.fromAsset(join(__dirname, "..", "lambdas/verify-challenge")),
35 }
36 );
37 return [defineChallengeLambda, createChallengeLambda, verifyChallengeLambda]
38 }

Picking up where we left off in Cognito with custom mailer repository. As described in Custom Authentication Flow, we need to create three lambdas for defining, creating and verifying the authentication challenge. We're passing CHALLENGE_NAME as environment variable to defineChallenge lambda to determine what kind of challenge we want to use. Then we're giving create lambda a variable with SENDGRID_API_KEY so that we can send an email to the user with the correct answer to the challenge.

Permissions #

typescript
Double click to copy
1// stacks/cognito.stack.ts
2 private static grantLambdaInvokePermission(lambda: Function, alias: string) {
3 lambda.addPermission(`exanubes-cognito-invoke-permission-${alias}`, {
4 principal: new ServicePrincipal("cognito-idp.amazonaws.com"),
5 action: "lambda:InvokeFunction",
6 });
7 }

Cognito needs to have permission to invoke the lambdas so using this small helper method we can give it permission for all three authentication lambdas.

typescript
Double click to copy
1// stacks/cognito.stack.ts
2CognitoStack.grantLambdaInvokePermission(
3 defineChallengeLambda,
4 'define-challenge-lambda'
5);
6CognitoStack.grantLambdaInvokePermission(
7 createChallengeLambda,
8 'create-challenge-lambda'
9);
10CognitoStack.grantLambdaInvokePermission(
11 verifyChallengeLambda,
12 'verify-challenge-lambda'
13);

Custom authentication triggers #

typescript
Double click to copy
1// stacks/cognito.stack.ts
2const [defineChallenge, createChallenge, verifyChallenge] =
3 this.createCustomChallengeLambdas();
4
5userPool.addTrigger(UserPoolOperation.DEFINE_AUTH_CHALLENGE, defineChallenge);
6userPool.addTrigger(UserPoolOperation.CREATE_AUTH_CHALLENGE, createChallenge);
7userPool.addTrigger(
8 UserPoolOperation.VERIFY_AUTH_CHALLENGE_RESPONSE,
9 verifyChallenge
10);

Here, we're actually telling our User Pool which lambda should be invoked depending on which User Pool Operation is triggered.

Custom authentication user pool agent #

typescript
Double click to copy
1// stacks/cognito.stack.ts
2const client = userPool.addClient('exanubes-user-pool-client', {
3 userPoolClientName: 'exanubes-cognito-app',
4 authFlows: {
5 custom: true,
6 },
7 accessTokenValidity: Duration.days(1),
8 idTokenValidity: Duration.days(1),
9 refreshTokenValidity: Duration.days(30),
10 preventUserExistenceErrors: true,
11});

One more thing we need to change in the previous infrastructure is the selected authentication flow in the user pool client. Previously it was userPassword now we want it to be custom.

Implementing Custom Lambdas #

Define Challenge lambda #

typescript
Double click to copy
1// lambdas/define-challenge/index.ts
2import { DefineAuthChallengeTriggerEvent } from 'aws-lambda';
3
4const ALLOWED_ATTEMPTS = Infinity;
5const challengeName = process.env.CHALLENGE_NAME || '';
6
7exports.handler = async (event: DefineAuthChallengeTriggerEvent) => {
8 const [challenge] = event.request.session.reverse();
9 const challengeAttempts = event.request.session.length;
10 if (challenge) {
11 if (challengeAttempts >= ALLOWED_ATTEMPTS) {
12 event.response.issueTokens = false;
13 event.response.failAuthentication = true;
14 return event;
15 }
16 if (challenge.challengeName === challengeName) {
17 event.response.issueTokens = challenge.challengeResult;
18 event.response.failAuthentication = !challenge.challengeResult;
19 return event;
20 }
21 }
22 event.response.issueTokens = false;
23 event.response.failAuthentication = false;
24 event.response.challengeName = challengeName;
25 return event;
26};

This is probably the most complicated one of the lambdas as it is supposed to work as a state machine for the entire authentication flow. First of all, Cognito gives us access to the current session in the request, which is an array of all challenge answers. This is why we reverse the array and destructure first element from it, as that will always be the latest attempt.

In order to create an appropriate response for user pool to handle, we have three properties on the response object we can use. issueTokens, failAuthentication and challengeName.

In the event where session is an empty array, we're going to issue the challenge as that's the very first time the lambda has been triggered. Otherwise, we're checking the validity of the challenge. In this case we're checking if the attempt threshold has been exceeded in which case we're going to fail authentication and not issue tokens.

If, however, the attempts have not been exceeded and the challenge name matches the one we've configured the lambda for, we're going to issue tokens if challengeResult is truthy and fail authentication if it's falsy.

Create Challenge Lambda #

typescript
Double click to copy
1// lambdas/create-challenge/index.ts
2import { CreateAuthChallengeTriggerEvent } from 'aws-lambda';
3
4const { generateRandomString } = require('@exanubes/cdk-utils');
5const emailClient = require('@sendgrid/mail');
6const sendgridApiKey = String(process.env.SENDGRID_API_KEY);
7emailClient.setApiKey(sendgridApiKey);
8
9exports.handler = async (event: CreateAuthChallengeTriggerEvent) => {
10 const code = generateRandomString();
11 const user = event.request.userAttributes;
12 const email = {
13 to: `${event.userName} <${user.email}>`,
14 from: 'Exanubes.com <example@email.com>',
15 subject: `[${event.triggerSource}] Your login token`,
16 text: `Use the link below to log in to exanubes.com\n http://localhost:4000/verify-login?code=${code}&username=${event.userName} \n this link will expire in three minutes`,
17 };
18 await emailClient.send(email);
19 event.response.publicChallengeParameters = {
20 email: user.email,
21 };
22 event.response.privateChallengeParameters = {
23 code,
24 };
25 event.response.challengeMetadata = `EXANUBES_CHALLENGE`;
26 return event;
27};

Create challenge lambda is responsible for actually creating the temporary password that has to be entered in order to log in. This is also the place where we're going to notify the user of the correct password whether it's email, a text message or a push notification. I've opted for just sending an email with a login link to my frontend application which will take the code and username values from the query parameter and call my backend API with those values to respond to the challenge. We can also set public and private challenge parameters. The public params will be sent back to the client so it probably shouldn't include the answer to the challenge, but the private params will only be accessible withing the auth flow. The challengeMetada prop is used for assigning a custom name to the challenge if we want to do so.

Verify Challenge Lambda #

typescript
Double click to copy
1// lambdas/verify-challenge/index.ts
2import { VerifyAuthChallengeResponseTriggerEvent } from 'aws-lambda';
3
4exports.handler = async (event: VerifyAuthChallengeResponseTriggerEvent) => {
5 const validCode = event.request.privateChallengeParameters.code;
6 event.response.answerCorrect = validCode === event.request.challengeAnswer;
7 return event;
8};

The most straight-forward of the lambdas, here we just want to compare the user input with the correct answer saved in privateChallengeParameters in create challenge lambda and assign the value to the answerCorrect param on the response object.

Recap #

So far we've gone over the overall custom authentication flow. We've created the lambdas, added user pool triggers and gave the necessary permissions to Cognito for invoking the lambda functions. Then we covered the implementation of each one of the functions and now with the AWS CDK part finished we have to move to the server code and make the necessary adjustments to migrate from userPassword to the custom authentication flow.

AWS SDK #

Login #

typescript
Double click to copy
1// auth/auth.service.ts
2async login({ username }: LoginDto) {
3 const input: InitiateAuthCommandInput = {
4 ClientId: this.awsConfigService.userPoolClientId,
5 AuthFlow: AuthFlowType.CUSTOM_AUTH,
6 AuthParameters: {
7 USERNAME: username,
8 },
9 };
10 const command = new InitiateAuthCommand(input);
11 return this.client.send(command);
12}

This time the AuthFlow is set to CUSTOM_AUTH so that cognito knows to trigger our define challenge handler as well as not expect a PASSWORD field in the AuthParameters. Those are the only changes to the login method.

Verify login #

typescript
Double click to copy
1// auth/auth.service.ts
2async verifyLogin({ code, username, session }: VerifyLoginDto) {
3 const input: RespondToAuthChallengeCommandInput = {
4 ClientId: this.awsConfigService.userPoolClientId,
5 ChallengeName: ChallengeNameType.CUSTOM_CHALLENGE,
6 Session: session,
7 ChallengeResponses: {
8 ANSWER: code,
9 USERNAME: username,
10 },
11 };
12 const command = new RespondToAuthChallengeCommand(input);
13 const response = await this.client.send(command);
14 return response.AuthenticationResult;
15}

Similar to verify signup, we now have to verify login as we've split the authentication process rather than providing username and password at once. With the response we need the session token which was returned from the initialAuth command in login method. The challenge name should be the same as in the defineChallenge lambda and lastly username and answer to the challenge. If successful, the command will respond with authentication tokens.

Signup #

typescript
Double click to copy
1// auth/auth.service.ts
2 async signup({ username, email }: SignUpDto) {
3 const input: SignUpCommandInput = {
4 Password: generateRandomString(),
5 Username: username,
6 ClientId: this.awsConfigService.userPoolClientId,
7 UserAttributes: [{ Name: 'email', Value: email }],
8 };
9 const command = new SignUpCommand(input);
10 return this.client.send(command);
11 }

Last but not least, we have to slightly adjust the signup input for the sign up command as there's no need for the user to provide us with a password, however, the command still expects to receive a password nonetheless. That's why we're gonna generate a random string to keep it satisfied.

Summary #

In this article we've learned how to setup an infrastructure using the AWS CDK for custom authentication flow in AWS Cognito. We've gone over setting up lambda resources, adding user pool triggers and giving Cognito permission to invoke the lambdas. Then we've covered the implementation of each of the lambdas necessary for custom authentication flow. Last but not least we needed to adjust the backend API to facilitate the different auth flow. In the signup process user no longer provides a password so we generate one for him as that's a requirement in the SDK API. We also needed to change the login setup as now we no longer receive tokens from that API call and we needed to change the authentication flow it's supposed to initiate. Finally, we had to implement a new API endpoint for answering the Cognito challenge and receiving authentication tokens.

Thanks!

A verification email has been sent to

Keep in Touch

Join other developers in Exanubes Newsletter

© 2022