This feature uses delegation. By default, delegation is disabled for tenants without an add-on in use as of 8 June 2017. Legacy tenants who currently use an add-on that requires delegation may continue to use this feature. If delegation functionality is changed or removed from service at some point, customers who currently use it will be notified beforehand and given ample time to migrate. In addition, note that delegation does not support the use of custom domains so any features depending on it may not be fully functional alongside a custom domain.
Step 5 - Use Identity Tokens to Flow Identity
In this final step, you will:- Flow identity to the service by passing your (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 . 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).
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.Use Rules
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. Inlogin.js
, you can see this scope specified in the parameters passed to auth.signin
:
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. For additional details, see Creating Deployment Packages using Node.js and Uploading Deployment Packages and Testing. The following seed project contains the code you’ll need for your updated AWS Lambda function.index.js
: contains your main code;auth0-variables
: contains the code you need to update.
package.json
file.
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.
Update auth0-variables.js
with your secret key, which can be found on the Settings tab of your Application in the :
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 anAuthorization
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.
pets/purchase
.
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:
Summary
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 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.