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 4 - Use Multiple Roles with Amazon API Gateway
In this step, you’ll assign different AWS IAM roles to users based on authentication information:- Users authenticating with Social Connections will be treated as buyers;
- Users authenticating with Database Connections will be treated as admins.
- JavaScript;
- Auth0 rules.
Step 1. Create the PetPurchase API Resource
Using the Amazon API Gateway Console, select your Pets API. You will be taken to its Resources page. Click on Actions and Create Resource. Name the New Child ResourcePurchase
. Click Create Resource.
Add an OPTIONS method for the purchase
resource as outlined previously for pets
in the Set Up Cors and Deploy the API section of Step 2 - Securing and Deploying the Amazon API Gateway.
Create a new AWS Lambda function for purchasing a pet called PetPurchase
, which adds isSold
and soldTo
attributes to a pet as follows:
purchase
resource that calls the PetPurchase
Lambda. Be sure to also add the Access-Control-Allow-Origin
header with a value of *
to the POST method using the method response/integration response configuration found in Set Up Cors and Deploy the API section of Step 2 - Securing and Deploying the Amazon API Gateway.
Test the API gateway method, providing the following as an input message:
Step 2. Use IAM to Secure the PurchasePet API
Update IAM
To secure your API, follow the same process for adding a new role that you performed in Part 2 of this tutorial. Call the new roleauth0-api-social-role
.
The ARN for the method you will secure in the IAM policy should look something like:
/pets/purchase
resource. Select Method Request and change Authorization Type to AWS_IAM. Click the check to save the setting.
At this point, you have defined two roles that you can use with the API gateway:
auth0-api-role
: permits updating petsauth0-api-social-role
: permits purchasing a pet
Configure Login with Amazon and Update Auth0
You can create a social role using Login with Amazon (LWA). While this tutorial includes instructions for using Login with Amazon, please note that you can use other social providers as well.- Navigate to Auth0 Dashboard > Authentication > Social, and select Create Connection.
- Choose the connection you want to set up, and consent.
- Copy and paste the
Client ID
andClient Secret
from your social identity provider, select the Attributes (and Permissions, where applicable), and click Save. - Select the Applications view, enable the switch for each of your Auth0 applications that should be able to use this connection, and select Save.
https://johndoe.auth0.com/login/callback
. The Auth0 help page will show you specifically what to enter.
Navigate to Auth0 Dashboard > Applications > Applications, and select your Application to view its settings. Select the Connections view, locate the Social section, and ensure that Amazon is enabled.
Deploy the API and Update the Single-Page Application
Deploy the API
Using the Amazon API Gateway Console, you will again deploy the API and generate a new JavaScript SDK. At this point, you have made the necessary configuration changes to enable pet purchases. To make this live, copy your newly downloaded SDK over the previous one in yourpets
folder, as well as your Amazon S3 bucket.
Update the Login Controller Logic to Choose Different Roles for Different Types of Users
The login controller logic usesgetOptionsForRole
to select different roles for different users. When you obtain the delegation token, you can tell Auth0 which role to use (that is, the user is an admin or not).
In the pets/login/login.js
file, modify the role
and principal
values for the non-admin user for the social user IAM role you just created.
At this point, you should be able to log in using Amazon credentials or the database user you previously created. Notice that the UI lets a social user buy pets, while an admin user can add and remove pets.
To test this functionality, you can temporarily hide the remove button in the UI by removing ng-show="isAdmin"
in /pets/home/home.html
:
Update the Home Controller Logic to Allow Social Users to Purchase Pets
Inhome.js
, modify the buyPet
function to enable pet purchases:
Enforce Role Assignment with Auth0 Rules
In some cases, you might determine the appropriate role using the Application (as shown here), but for security reasons (you might want to prevent the user from assuming a more privileged role than necessary), you might want to determine user privileges on the server-side. With Auth0, this is done via rules, which are service logic statements you define that are then run during the Auth0 authentication process. For example, you could create rules to:- Eliminate the passing of role information from the browser to the Application;
- Insert role information into the delegation request based on the authentication source.
Enforce Role Assignment
You will add a rule that will check to see if the role requested by the user is allowed, depending on its association with a Social or Database Connection.- Navigate to Auth0 Dashboard > Auth Pipeline > Rules, and select Create Rule.
- Choose the Empty rule template
-
Name the rule AWS Pets (or something similar), then populate the body of the rule with the following JavaScript code:
Be sure to adjust the above code with the correct values for your integration. The fields are Princial ARN, Role ARN, and Client Secret.
- Save your changes.
Caveats
- Rules run at a global scope for every authentication. You should only run the logic on authentication requests associated with a given application (which is why the script used asks for the clientID. Without this information, the logic runs for every authentication request associated with your Auth0 account.
- Information is passed into the rule with the context and the user.
- You can extend the objects passed in to the rule. In the code above, the rule checks the body of the request for the role information. The role is set into the context addonConfiguration of the allowed role, which always overrides settings in the request body.