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. Solution overview 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 auth flow includes the following steps: User sign-in with MetaMask. Get nonce from DynamoDB. Generate one if nonce doesn’t exist. Sign messages off-chain with the private key of the current account. Verify signature with Web3. Get developer authenticated identities. Get credentials for the returned developer authenticated identity ID. Signing AWS requests with signature version 4. Control access to AWS API Gateway APIs with IAM authorization. The following diagram illustrates the auth flow. Prerequisites Setup an AWS account Set up a wallet MetaMask Install MetaMask chrome extension Install or update the Serverless framework Let’s begin! React frontend DApp 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: web3.eth.personal.sign 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. web3.eth.personal.sign 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 ( , , ). accessKeyId secretAccessKey sessionToken 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: login Now we have created our frontend DApp, let’s move on to the backend. Handling authentication and authorization in serverless application 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 guide. Get started with Serverless Framework Open Source & AWS Creating nonce and Geting nonce 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 . user address Let’s navigate into the file inside your serverless application folder. First, add the following lines to the section: serverless.yml Resources 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. functions getNonce Let's create related Lambda functions: and . getNonce signup lambda would look like the following: getNonce and Lambda function would look like the following: signup Next, We will create an IAM policy that allows access to the DynamoDB table. Add the following lines to the section of : Get/Put/update user provider serverless.yml Building authentication flow First, we need to create a Cognito Identity Pool associated with the developer provider . my.ether.login 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: CognitoIdentityPool CognitoAuthorizedRole unAuthorizedRole CognitoIdentityPoolRolesMapping Resources Next, it’s time to build our serverless authentication flow. The flow covers the following steps: Verify signature with Web3. Get developer authenticated identities. Get credentials for the returned developer authenticated identity ID. Signature verification 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, outputs the 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. web3.personal.ecRecover ecRecover signing_address signing_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 and as attributes. identityId token 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. getCredentialsForIdentity 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: updateNonce login API endpoint in section of : login functions serverless.yml Lambda function: login CONGRATULATIONS! We have just completed building our blockchain authentication flow. Now, We can create HTTP Endpoints with Authorizers. After login successfully, the user can submit the IAM user’s access keys to be authenticated to invoke our Lambda Function. /hello AWS_IAM Copy following lines to section of : functions serverless.yml and Lambda function would look like below: hello Then use in React frontend to sign the hello request with signature version 4 and . aws4fetch fetch() Deploy the serverless application and react app, log in with MetaMask. We should see the “ ” message on the dashboard page. npm start Welcome your IAM role is authorized Conclusion 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 . repository GitHub