When you build a blockchain DApp, you can use AWS services with custom logic, such as monitoring and troubleshooting your contract event logs using AWS CloudWatch. With Amazon Cognito, you can deliver temporary, limited-privilege credentials to your application to access AWS resources.
In this article, We will introduce a cryptographically secure authentication flow using the Amazon Cognito enhanced flow with the MetaMask extension and Web3.
By the end of this article, we will have a website that allows users to log in using MetaMask and having access to our Amazon API Gateway APIs with IAM authorization.
here is a demo we are going to build:
The following diagram illustrates the auth flow.
I created a react frontend DApp for this article. The project can be seen in my GitHub repository.
When users click the login button on the login page, they need to connect to their MetaMask Wallet and get the current MetaMask account’s public address. The MetaMask connection function would look like the following:
Once we have the public address, we will use it to get the nonce by the public address. If no nonce is found in DynamoDB, we will call signup API to create a cryptographically strong pseudo-random data as the nonce and save it to the DynamoDB table.
Next, we use
to sign string message with nonce:
Running the above code snippet will prompt a Signature Request popup for signing the message in MetaMask.
When the user clicks the sign button in the popup window, the callback function of
then returns a signature.
After this, the frontend makes an authentication API call, passing signature, and public address. if the tokens and signature are verified in the backend process, the frontend will be given the AWS STS credentials (
After the authentication process is completed, we can use AWS STS credentials to sign our requests using Signature Version 4, then connect to the API Gateway endpoints secured using AWS IAM permissions.
The sample code of the
page would look like the following:
Now we have created our frontend DApp, let’s move on to the backend.
As a heads up, I will assume you already have Serverless Framework installed and are familiar with it. If you are not, take a look at the Get started with Serverless Framework Open Source & AWS guide.
Step2: Get nonce from DynamoDB; generate one if nonce doesn’t exist.
First of all, we need to store nonce and user’s public address in DynamoDB; we will create a
table where each item is uniquely identified by
Let’s navigate into the
file inside your serverless application folder. First, add the following lines to the
Now we have the user table defined, let’s build an API Gateway REST API with Lambda functions to get/set nonce by user’s public address.
Add the following config to the functions section,
The code snippets from the
section defines 2 API endpoints
& signup (create nonce), the frontend can send requests to Lambda functions via the 2 API Gateway HTTPS endpoints.
Let's create related Lambda functions:
lambda would look like the following:
Lambda function would look like the following:
Next, We will create an IAM policy that allows
access to the
DynamoDB table. Add the following lines to the
First, we need to create a Cognito Identity Pool associated with the developer provider
With developer authenticated identities, you can register and authenticate users via your own existing authentication process, while still using Amazon Cognito to synchronize user data and access AWS resources.
Now, let’s create resources: (
), copy the following lines to
the section looks like this:
Next, it’s time to build our serverless authentication flow. The flow covers the following steps:
When the user logins to the site by sending a POST request, the first step is to verify that the user has correctly signed the nonce. we can use
function to verify a signed message,
used to sign the same message. If the
matches our address from the request body, then the user who made the request successfully proved their ownership of the public address.
Developer authenticated identities
Once the signature is verified, we will implement our own identity provider in the Lambda function, and the identity provider function should return a response object containing
Getting credentials for the authenticated identity ID
After we establish identity ID and token, we can then call
to return STS credentials for the provided identity ID.
To prevent the user from logging in with the same signature every time, we will change nonce (
) at the bottom of
Lambda function. The full sample code of auth flow would look like below:
API endpoint in
CONGRATULATIONS! We have just completed building our blockchain authentication flow.
Now, We can create HTTP Endpoints
Authorizers. After login successfully, the user can submit the IAM user’s access keys to be authenticated to invoke our Lambda Function.
Copy following lines to
Lambda function would look like below:
in React frontend to sign the hello request with signature version 4 and
Deploy the serverless application and
react app, log in with MetaMask. We should see the “Welcome your IAM role is authorized” message on the dashboard page.
This article introduced the approach to authenticating users with MetaMask and Cognito Identity Pool developer provider. I showed you how to invoke AWS API gateway endpoints with AWS_IAM authorizer. I hope you have found this article useful. You can find the complete project in my GitHub repository.
Create your free account to unlock your custom reading experience.