---
title: "Multi-Factor Authentication Using Actions"
description: "Learn to use authentication actions to control multi-factor authentication"
url: "https://curity.io/resources/learn/add-authentication-action/"
date: "2020-11-30"
area: "tutorials"
topic: "howtos"
pagegroup: "authentication-actions"
---

# Multi-Factor Authentication Using Actions

Authentication Actions are a powerful way to perform additional abilities on top of user authentication. Authentication Actions occur after credential validation but before the authentication concludes. For example, an HTML form authenticator could validate user credentials. Next, Actions could manipulate attributes related to the subject or trigger a second factor, which may be necessary if the subject is authenticating from a new country or outside office hours.

Actions can use the attributes which were fetched or manipulated by preceding Actions. In this way, you can create workflows by using multiple, simple Actions. You can also use these attributes in tokens later on. This tutorial shows how to create a simple workflow using three Actions.

## Prerequisites

You will need an installation of Curity Identity Server with the basic setup completed. You can achieve this by following our [Getting Started Guides](https://curity.io/resources/getting-started/). Alternatively, if you have a system up and running with your own configuration, you can use that as well. Just be aware that you probably have different names set for certain components.

This tutorial also uses another authenticator, [Google Authenticator](https://curity.io/resources/learn/authenticate-with-google-authenticator/), for a second factor. However, you can use any other authenticator.

## Enabling Multi-Factor Authentication (MFA) for Internal Users

Say you have a user authenticating with a username and password — this is one factor. For user accounts registered with an internal email, you want to introduce a second authentication factor, Google Authenticator.

To enable this, you can set up Actions to:

-   Fetch all attributes belonging to the Account
-   Trigger Google Authenticator if the domain of the email is `email.example.com`

The diagram below shows how it would be setup:

![Actions diagram](https://curity.io/images/resources/operate/tutorials/authentication/mfa-action/diagram.svg)

### Fetch All Attributes

To fetch the attributes, use the `Data Source` Action. It uses the attribute query configured for that data source, so you need to set it up. To do so, enter the Admin UI and click on **Facilities** → **Data Sources** → **default-datasource**. In the **Attribute** tab, set the Attribute Query to `SELECT * FROM "accounts" WHERE "username" = :subject`. NOTE: This query will fetch all attributes, and you can be more specific.

![Updating the data source](https://curity.io/images/resources/operate/tutorials/authentication/mfa-action/update-datasource.jpg)

When done, open **Actions** in your authentication profile and click on the **New Action** button. Give the Action a name and click on the **Data Source** action.

Here, select the Attribute Data Source to be `default-datasource`. Finish by clicking on **Close**.

![Adding data source action](https://curity.io/images/resources/operate/tutorials/authentication/mfa-action/create-action.jpg)

![Adding data source action](https://curity.io/images/resources/operate/tutorials/authentication/mfa-action/datasource1.jpg)

![Adding data source action](https://curity.io/images/resources/operate/tutorials/authentication/mfa-action/datasource2.jpg)

### Trigger MFA for a Specific Email Domain

Next, you must add a second action. This time, add a **Multi-Factor Condition** action. This action can filter which factor the user should authenticate with. All MFA users should use the same second factor so our filter should match everyone. Set the **Condition** to `subject-condition` to filter based upon our subject. Next, press **Add** to create a new condition. The pattern is a regular expression pattern. Since all MFA users should use the same Authenticator, set the pattern to `.*`, which matches all subjects. Then, click **Select authenticator** and select `Google-Authenticator`. Finish by clicking on **Close**.

![Adding conditional MFA action](https://curity.io/images/resources/operate/tutorials/authentication/mfa-action/mfa.jpg)

The last action to create is a **Switch action**. This action is responsible for filtering out which users should use an additional factor. You can set any **Name** for the case. The **Condition** uses Javascript. You can use string functions for your conditions or a regular expression, as in this tutorial. Set it to `/.*@email.example\.com$/.test(attributes.subject.email)` which means that the script will determine if `email` in the `subject` attributes ends with `@email.example.com`. Set **Execute** to our `MFA` action. Set the **else** case to `Succeed if no conditions match` to let users with other email domains in without a second factor.

![Adding switch action](https://curity.io/images/resources/operate/tutorials/authentication/mfa-action/switch.jpg)

### Add the Actions to Your Authenticator

Now assign these actions to your Authenticator. Open **Authenticators** and click on the **username-password** authenticator. To the right, you have actions. Click on the **plus** sign next to **Login** to add an action. The actions will be executed in order. First, add the **user\_account** to fetch the attributes, and then add the **switch**. The **switch** action takes care of running the **MFA** action.

![Assigning actions](https://curity.io/images/resources/operate/tutorials/authentication/mfa-action/add-action.jpg)

![Assigning actions](https://curity.io/images/resources/operate/tutorials/authentication/mfa-action/actions.jpg)

## Try It Out

You can now try out the `username-password` login and see how the system behaves when a user attempts to authenticate with or without the `email.example.com` domain. You will also notice that the `subject` is changed to reflect the email in your issued tokens. For information on how to register new accounts, visit the `Try It Out` section of the [Getting Started Guides](https://curity.io/resources/getting-started/).

## Conclusion

This tutorial quickly looked at using Authentication Actions to achieve MFA. It used a **Switch** action to select which users that should use a second factor based upon their email. Feel free to explore other conditions you can use. As you have seen, it is also possible to fetch additional attributes and use them in subsequent Actions. A powerful way to manipulate attributes is by using [Script Action](https://curity.io/resources/learn/scripted-attribute-transformation/).

The Curity Identity Server ships with about 20 pluggable Actions, and more are available on [GitHub](https://github.com/curityio). You can also create your own Action with the [Curity SDK](https://curity.io/docs/identity-server/developer-guide/sdk/). [This guide](https://curity.io/resources/learn/authentication-action/) also explains how to implement your own Authentication Action.

This article focused on using Actions in the `login` flow. You can use the same actions in the `sso` flow as well.
