I recently attended a training session with the Auth0 Dev Rel team on a very cool new feature they have added called Auth0 Actions. In this article, I am going to explain what is Auth0 Actions, why to use them, and how to set one up.
Actions are secure, tenant-specific, versioned functions written in Node.js that execute at certain points during the Auth0 runtime. Actions are used to customize and extend Auth0's capabilities with custom logic.
Above, you can see a sample flow where once the user logs into the system, you add a trigger to verify the user's identity using Onfido and then confirm consent using OneTrust before completing the login flow and issuing the token.
In brief, an action is a programmatic way to add custom business logic into your login flow.
For the purpose of this demo, we are going to be creating an action to enforce Multi-Factor Authentication (MFA) for a specific role. I will take you through the process of:
Let's get started:
The first step to secure your application is to access the Auth0 Dashboard in order to create your Auth0 application. If you haven’t created an Auth0 account, you can sign up for a free one now.
Once in the dashboard, move to the Applications tab in the left sidebar.
Click on Create Application
Provide a friendly name for your application (e.g. - Test Actions App) and choose Single Page Web Applications as an application type
From the quick start tab, choose React. Download the sample app. This will have most of the necessary details already in place
We also need to set up a few settings for this application. Choose the Settings tab (next to quick start). Add your localhost URL to the following places:
Unzip the code we downloaded in a location of your choice.
Open it in the code editor of your choice.
Cross verify that the details of your application are correctly configured in src/auth_config.json
npm install & npm run dev
Click on the User Management tab in the left sidebar.
Go to the Users tab and click on the Create User button. We need to create 2 users:
Admin User
Test User
Remember these credentials, as these are the test users we will use for this demo.
Admin
, and once created, go to the user tab and assign it to your Admin user.
+
button in Add Action and select Build Custom.
onExecutePostLogin
function
if (event.authorization != undefined && event.authorization.roles.includes("Admin")) {
api.multifactor.enable("any");
};
On the left side you can see a play button. This is your testing environment inside the actions editor. You will find the event object in which you can test the actions flow by adding Admin
to the authorization.roles
array.
When you add the Admin
role, you should see a response with MFA like below, and when not present, you should get an empty array.
MFA for Roles
action into the flow. Click on Apply such that this new flow will work with your login box.
You will also need to enable MFA on the Auth0 dashboard. Open the Securities tab and choose multi-factor auth. In the following screen enable One-time Password. This will enable users to use an application like Google Authenticator for a one-time password. There are other factors you can enforce as well, like SMS or Email-based OTP, etc., but for this demo, we will be using just the one-time password.
In the policies section, leave everything as is and save your changes.
Now when you go to login in on the locally running application, we should be triggered to do a MFA for the admin user. So let's test that.
if (event.authorization != undefined && event.authorization.roles.includes("Admin")) {
api.multifactor.enable("any");
};
Congrats, you have just created a custom Auth0 Actions flow and tested it. This was a simple example to enable you to understand what they are, how they can be built and used in your workflows. There are many more complex flows you can build for, and you can find some examples provided by Auth0 below. Just click on the trigger, and you will find specific examples.
Hopefully, this enables you to understand what actions are and how you can use them in your login workflows.
Thanks for reading! I really hope that you find this article useful. I invite you to participate in the discussion in the comments below; I'm always interested to know your thoughts and happy to answer any questions you might have in your mind. If you think this post was useful, please like the post to help promote this piece to others.
Thanks for reading! 😃
P.S Do feel free to connect with me on LinkedIn or Twitter
The following have been great material that helped me write this article:
Also Published Here