AWS Security Blog
Implement step-up authentication with Amazon Cognito, Part 2: Deploy and test the solution
This solution consists of two parts. In the previous blog post Implement step-up authentication with Amazon Cognito, Part 1: Solution overview, you learned about the architecture and design of a step-up authentication solution that uses AWS services such as Amazon API Gateway, Amazon Cognito, Amazon DynamoDB, and AWS Lambda to protect privileged API operations. In this post, you will use a reference implementation to deploy and test the step-up authentication solution in your AWS account.
Solution deployment
The step-up authentication solution discussed in Part 1 uses a reference implementation that you can use for demonstration and learning purposes. You can also review the implementation code in the step-up-auth GitHub repository. The reference implementation includes a web application that you can use in the following sections to test the step-up implementation. Additionally, the implementation contains a sample privileged API action /transfer and a non-privileged API action /info, and two step-up authentication solution API operations /initiate-auth, and /respond-to-challenge. The web application invokes these API operations to demonstrate how to perform step-up authentication.
Deployment prerequisites
The following are prerequisites for deployment:
- The Node.js runtime and the node package manager (npm) are installed on your machine. You can use a package manager for your platform to install these. Note that the reference implementation code was tested using Node.js v16 LTS.
- The AWS Cloud Development Kit (AWS CDK) is installed in your environment.
- The AWS Command Line Interface (AWS CLI) is installed in your environment.
- You must have AWS credentials files that contain a profile with your account secret key and access key to perform the deployment. Make sure that your account has enough privileges to create, update, or delete the following resources:
- Amazon Cognito
- Amazon API Gateway API and resources
- Amazon Simple Storage Service (Amazon S3) buckets
- Amazon CloudFront distributions
- Amazon DynamoDB tables
- AWS Lambda functions
- AWS Identity and Access Management (AWS IAM) roles
- A two-factor authentication (2FA) mobile application, such as Google Authenticator, is installed on your mobile device.
Deploy the step-up solution
You can deploy the solution by using the AWS CDK, which will create a working reference implementation of the step-up authentication solution.
To deploy the solution
- Build the necessary resources by using the build.sh script in the deployment folder. Run the build script from a terminal window, using the following command:
cd deployment && ./build.sh
- Deploy the solution by using the deploy.sh script that is present in the deployment folder, using the following command. Be sure to replace the required environment variables with your own values.
export AWS_REGION=<your AWS Region of choice, for example us-east-2>
export AWS_ACCOUNT=<your account number>
export AWS_PROFILE=<a valid profile in .aws/credentials that contains the secret/access key to your account>
export NODE_ENV=development
export ENV_PREFIX=devThe account you specify in the AWS_ACCOUNT environment variable is used to bootstrap the AWS CDK deployment. Set AWS_PROFILE to point to your profile. Make sure that your account has sufficient privileges, as described in the prerequisites.
The NODE_ENV environment variable can be set to development or production. This variable controls the log output that the Lambda functions generate. The ENV_PREFIX environment variable allows you to prefix all resources with a tag, which enables a multi-tenant deployment of this solution.
- Still in the deployment folder, deploy the stack by using the following command:
./deploy.sh
- Make note of the CloudFront distribution URL that follows Sample Web App URL, as shown in Figure 1. In the next section, you will use this CloudFront distribution URL to load the sample web app in a web browser and test the step-up solution
After the deployment script deploy.sh completes successfully, the AWS CDK creates the following resources in your account:
- An Amazon Cognito user pool that is used as a user registry.
- An Amazon API Gateway API that contains three resources:
- A protected resource that requires step-up authentication.
- An initiate-auth resource to start the step-up challenge response.
- A respond-to-challenge resource to complete the step-up challenge.
- An API Gateway Lambda authorizer that is used to protect API actions.
- The following Amazon DynamoDB tables:
- A setting table that holds the configuration mapping of the API operations that require elevated privileges.
- A session table that holds temporary, user-initiated step-up sessions and their current status.
- A React web UI that demonstrates how to invoke a privileged API action and go through step-up authentication.
Test the step-up solution
In order to test the step-up solution, you’ll use the sample web application that you deployed in the previous section. Here’s an overview of the actions you’ll perform to test the flow:
- In the AWS Management Console, create items in the setting DynamoDB table that point to privileged API actions. After the solution deployment, the setting DynamoDB table is called step-up-auth-setting-<ENV_PREFIX>. For more information about ENV_PREFIX variable usage in a multi-tenant environment, see Deploy the step-up solution earlier in this post.
As discussed, in the Data design section in Part 1 of this series, the Lambda authorizer treats all API invocations as non-privileged (that is, they don’t require step-up authentication) unless there is a matching entry for the API action in the setting table. Additionally, you can switch a privileged API action to a non-privileged API action by simply changing the stepUpState attribute in the setting table. Create an item in the DynamoDB table for the sample /transfer API action and for the sample /info API action. The /transfer API action will require step-up authentication, whereas the /info API action will be a non-privileged invocation that does not require step-up authentication. Note that there is no need to define a non-privileged API action in the table; it is there for illustration purposes only.
- If you haven’t already, install Google Authenticator or a similar two-factor authentication (2FA) application on your mobile device.
- Using the sample web application, register a new user in Amazon Cognito.
- Log in to the sample web application by using the registered new user.
- Configure the preferred multi-factor authentication (MFA) settings for the logged in user in the application. This step is necessary so that Amazon Cognito can challenge the user with a one-time password (OTP).
- Using the sample web application, invoke the sample /transfer privileged API action that requires step-up authentication.
- The Lambda authorizer will intercept the API request and return a 401 Unauthorized response status code that the sample web application will handle. The application will perform step-up authentication by prompting you to provide additional security credentials, specifically the OTP. To complete the step-up authentication, enter the OTP, which is sent through short service message (SMS) or by using an authenticator mobile app.
- Invoke the sample /transfer privileged API action again in the sample web application, and verify that the API invocation is successful.
The following instructions assume that you’ve installed a 2FA mobile application, such as Google Authenticator, on your mobile device. You will configure the 2FA application in the following steps and use the OTP from this mobile application when prompted to enter the step-up challenge. You can configure Amazon Cognito to send you an SMS with the OTP. However, you must be aware of the Amazon Cognito throttling limits. See the Additional considerations section in Part 1 of this series. Read these limits carefully, especially if you set the user’s preferred MFA setting to SMS.
To test the step-up authentication solution
- Open the Amazon DynamoDB console and log in to your AWS account.
- On the left nav pane, under Tables, choose Explore items. In the right pane, choose the table named step-up-auth-setting* and choose Create item, as shown in Figure 2.
- In the Edit item screen as shown in Figure 3, ensure that JSON is selected, and the Attributes button for View DynamoDB JSON is off.
- To create an entry for the /info API action, copy the following JSON text:
{ "id": "/info", "lastUpdateTimestamp": "2021-08-23T08:25:29.023Z", "stepUpState": "STEP_UP_NOT_REQUIRED", "createTimestamp": "2021-08-23T08:25:29.023Z" }
- Paste the copied JSON text for the /info API action in the Attributes text area, as shown in Figure 4, and choose Create item.
- To create an entry for the /transfer API action, copy the following JSON text:
{ "id": "/transfer", "lastUpdateTimestamp": "2021-08-23T08:22:12.436Z", "stepUpState": "STEP_UP_REQUIRED", "createTimestamp": "2021-08-23T08:22:12.436Z" }
- Paste the copied JSON text for the /transfer API action in the Attributes text area, as shown in Figure 4, and choose Create item.
- Open your web browser and load the CloudFront URL that you made note of in step 4 of the Deploy the step-up solution procedure.
- On the login screen of the sample web application, enter the information for a new user. Make sure that the email address and phone numbers are valid. Choose Register. You will be prompted to enter a verification code. Check your email for the verification code, and enter it at the sample web application prompt.
- You will be sent back to the login screen. Log in as the user that you just registered. You will see the welcome screen, as shown in Figure 6.
- In the left nav pane choose Setting, choose the Configure button to the right of Software Token, as shown in Figure 7. Use your mobile device camera to capture the QR code on the screen in your 2FA application, for example Google Authenticator.
- Enter the temporary code from the 2FA application into the web application and choose Submit. You will see the message Software Token successfully configured!
- Still in the Setting menu, next to Select Preferred MFA, choose Software Token. You will see the message User preferred MFA set to Software Token, as shown in Figure 8.
- In the left nav pane choose StepUp Auth. In the right pane, choose Invoke Transfer API. You should see Response: 401 authorization challenge, as shown in Figure 9.
- On your mobile device, open the 2FA application, copy the OTP code from the 2FA application, and enter the code into the Enter OTP field, as shown in Figure 9. Choose Submit.
- This sends the OTP to the respond-to-challenge endpoint. After the OTP is verified, the endpoint will return a success or failure message. Figure 10 shows a successful OTP verification. You are prompted to invoke the /transfer privileged API action again.
- Invoke the transfer API action again by choosing Invoke Transfer API. You should see a success message as shown in Figure 11.
Congratulations! You’ve successfully performed step-up authentication.
Conclusion
In the previous post in this series, Implement step-up authentication with Amazon Cognito, Part 1: Solution overview, you learned about the architecture and implementation details for the step-up authentication solution. In this blog post, you learned how to deploy and test the step-up authentication solution in your AWS account. You deployed the solution by using scripts from the step-up-auth GitHub repository that use the AWS CDK to create resources in your account for Amazon Cognito, Amazon API Gateway, a Lambda authorizer, and Amazon DynamoDB. Finally, you tested the end-to-end solution on a sample web application by invoking a privileged API action that required step-up authentication. Using the 2FA application, you were able to pass in an OTP to complete the step-up authentication and subsequently successfully invoke the privileged API action.
For more information about AWS Cognito user pools and the new console experience, watch the video Amazon Cognito User Pools New Console Walkthrough on the AWS channel on YouTube. And for more information about how to protect your API actions with fine-grained access controls, see the blog post Building fine-grained authorization using Amazon Cognito, API Gateway, and IAM.
If you have feedback about this post, submit comments in the Comments section below. If you have any questions about this post, start a thread on the Amazon Cognito forum.
Want more AWS Security news? Follow us on Twitter.