Access Tokens for the Management API
Amazon API Gateway Tutorial - Flowing Identity
Step 5 - Use Identity Tokens to Flow Identity
In this final step, you will:
- Flow identity to the service by passing your JSON Web Token (JWT)OpenID scopesJSON Web Token (JWT);
- Validate the token;
- Extract profile information to assign a buyer for a pet.
Use an Identity Token
You can use your Lambda function to process and obtain information about the user. For example, during a purchasing transaction, you retrieved the username from the profile returned with the identity token. However, you can also choose to have the user's information embedded with the identity itself, which is a JSON Web Token (JWT).
The advantages of using JWTs is that you can:
- Verify the authenticity of the JWT;
- Be sure that the calling user is authenticated (instead of relying on a plain-text parameter that could have been tampered with).
In addition, you can use the JWT for authorization, which allows you to bypass the IAM integration with Amazon API Gateway. Please note, however, that using the API Gateway for authorization allows you to halt the API call prior to invocation of your Lambda function.
Add Information to the JWT
There are several ways of adding a user's information to the JWT. The following example adds the user's email address to the JWT, but the concepts are same for other user datapoints.
One way to add a user's email address to the JWT is to use a rule. This is a good approach if you want to make sure that this value is always available in the JWT for an authenticating user.
login.js, you can see this scope specified in the parameters passed to
While you can include the full profile of the user within the JWT, you will want to include only what is necessary since the JWT is typically passed with every request.
Validate the JWT Token
Because the AWS Lambda console has access to a limited number of Node modules that can be used when you enter your Node.js code using the browser console, you'll need to include additional modules and upload the Lambda function as a package to process the identity token.
The following seed project contains the code you'll need for your updated AWS Lambda function.
Download a sample project specific to this tutorial configured with your Auth0 API Keys.
index.js: contains your main code;
auth0-variables: contains the code you need to update.
In addition to the custom files, there is a standard Node.js
The code adds functionality to extract information from and validate the JWT. By default, Auth0 uses a symmetric key for signing the JWT, though you may opt to use asymmetric keys (if you need to allow third-party validation of your token, you should use an asymmetric key and share only your public key).
For more information about token verification, see Identity Protocols Supported by Auth0.
auth0-variables.js with your secret key, which can be found on the Settings tab of your Application in the Auth0 Dashboard:
Run npm install from the directory where your files are, zip up the contents (
index.js must be at the root of the zip), and upload it for use by the
PurchasePet Lambda function. If you test this, you should see an authorization failure, since the JWT is not in the message body.
Take a look at the logic in
index.js. You will see logic around line 60 that validates the token and extracts the decoded information that contains the identity information used for the purchase logic:
Extract Profile Information to Assign a Buyer
The final step is to pass the JWT to the method used by the browser client.
The standard method comes with an
Authorization header as a bearer token, and you can use this method by turning off IAM authorization and relying solely on the OpenID Token for authorization (you will also need to map the Authorization header into the event data passed to the AWS Lambda function).
If, however, you are using IAM, then the AWS API Gateway uses the
Authorization header to contain the signature of the message, and you will break the authentication by inserting the JWT into this header. To do this, you can either:
- Add a custom header for the JWT;
- Put the custom header into the body of the message.
If you choose to use a custom header, you'll also need to do some mapping for the Integration Request of the POST method for
To keep the validation process simple, pass the JWT in the body of the post to the AWS Lambda function. To do this, update the
buyPet method in
home.js by removing the
userName from the body, and adding
authToken as follows:
Upload your code to your S3 bucket and try to purchase a pet. You will see the email of the purchaser in the resulting message.
If you have any errors, double check that you have properly set your secret key. One useful tool for checking issues with your token decoding is jwt.io.
In this tutorial, you have:
- Created an API using AWS API Gateway that includes methods using AWS Lamdba functions;
- Secured access to your API using IAM roles;
- Integrated a SAML identity provider with IAM to tie access to the API to your user base;
- Provided different levels of access based on whether a user authenticated from the Database or Social Connection;
- Used an Auth0 rule to enforce role assignment;
- Used a JWT to provide further authorization context and pass identity information into the appropriate Lambda function.