# Step up for sensitive actions

> Ask a signed-in user for a stronger sign-in, such as a one-time code, before a payment or an admin change, and let the app and API see that they gave it.

Users sign in to your app with a light sign-in for everyday use. When they reach a sensitive action, such as approving
a payment, changing an email address or deleting an account, the app asks the realm for a stronger level by name. The
realm takes them through a stronger sign-in, such as a password plus a one-time code, and the new tokens say which
level they reached.

The level is an **acr** value: a name you choose, such as `urn:example:acr:mfa`. You tell the realm which
sign-in chain earns each name (see [Sign-in chains](../../../authentication/sign-in-chains/)).

**Before you start:**

- **Who can do it:** a realm administrator, in a group that holds `RealmOwner`
  ([Delegated administration](/platform/realms/delegated-administration/)).
- **Where:** step 1 in the dashboard, **Authentication → Settings**, or the API; steps 2 and 3 through the API
  ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`).
- **First:** this guide is for a realm whose everyday chain is lighter than the one it was born with (a passkey, or a
  password then an authenticator code, `universalLogin`), such as a password alone (`nativeStoreChain`). Users need an
  authenticator app set up to pass the stronger chain.

## Set up the stronger level

<Steps>

1. Make sure the second factor is offered. A realm is born with `totp` in its `authenticators` and a `totp` module
   instance. If yours lacks it, add it in **Authentication → Settings** to the recognized modules. In the API, read
   the list with `GET https://foobar.cvyuh.com/api/v0/am/objects/v1/authSettings/authSettings`, then send it back with `totp` added:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/authSettings/authSettings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "authenticators": ["…existing modules", "totp"] }'
   ```

   The list you send replaces the realm's whole list: a module you leave out can no longer run.
2. Create the stronger chain: a password, then a one-time code from an authenticator app.

   ```sh
   curl 'https://foobar.cvyuh.com/api/v0/am/objects/v1/authChain?id=mfa' \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "chain": [
         { "module": "nativeStore", "flag": "REQUIRED" },
         { "module": "totp", "flag": "REQUIRED" }
     ] }'
   ```

3. Name the levels, and the methods each module adds. The everyday chain earns `pwd`, the new chain earns `mfa`:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{
       "acrValues": { "urn:example:acr:pwd": "<your everyday chain>",
                      "urn:example:acr:mfa": "mfa" },
       "amrValues": { "nativeStore": "pwd", "totp": "otp" }
     }'
   ```

4. Tell app developers the level's name. For the sensitive action, the app sends a normal sign-in request with
   `acr_values=urn:example:acr:mfa`.

</Steps>

## What users and apps see

The user clicks **Approve payment**. The realm takes them through the stronger chain (their password, then their
authenticator code), and they're back in the app with the payment going through. Their session continues: it is the
same session, now stronger, and one sign-out still ends everything from before and after the step-up.

The new ID token and access token carry `acr: urn:example:acr:mfa`, and `amr` lists the methods used (for example
`pwd` and `otp`), if you map them.

<Aside type="caution" title="The app must check the acr it gets back">
  A value the realm doesn't know is ignored, so the user signs in as usual. With `prompt=none` a step-up can't happen:
  the app is told `login_required`.
</Aside>

**At an API behind the gateway**, a policy can require a stronger level. The gateway then answers `401` with
`error="insufficient_user_authentication"` and the `acr_values` or `max_age` it needs, and the app starts a step-up
with exactly that.

## Check it worked

Send a sign-in with `acr_values=urn:example:acr:mfa` as a signed-in test user. You should be asked for the code, and
the ID token's `acr` should be `urn:example:acr:mfa`.

The step-up is on the user's trail as a `SESSION_UPGRADED` record, then a `LOGIN` record whose steps show the
password and the code. A wrong code is a `LOGIN_FAILED` record, and counts toward
[Lockout](../../../authentication/lockout/). Find them in **Audit → Authentication** in the dashboard.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| The user signs in as usual and `acr` is the everyday value | The app asked for a value not in `acrValues`, which is ignored | Ask for the exact name; have the app check the `acr` it gets back |
| The user is asked for a code they never set up | They have no authenticator app yet | They set one up when the chain asks; see [Sign-in methods](../../../authentication/sign-in-methods/) |
| Saving the chain is refused | A module it names doesn't exist in the realm | Check the realm has a `totp` module instance (step 1) |
| `login_required` | The request carried `prompt=none` | Drop `prompt=none` for a step-up |

## Undo it

Remove the level by sending its key as `null` in `acrValues`:
`{ "acrValues": { "urn:example:acr:mfa": null } }`. Apps that ask for it then sign users in as usual.

## Related

- [Step-up and re-authentication](../../../applications/single-sign-on/step-up/): `acr`, `amr` and the challenges APIs send.
- [Sign-in chains](../../../authentication/sign-in-chains/): how a chain combines methods.
- [Sessions](../../../authentication/sessions/): how a session is made stronger in place.
- [Ask users to sign in again](../ask-to-sign-in-again/): a newer sign-in rather than a stronger one.
- [Requests and refusals](/gateway/protect-apis/requests-and-refusals/): where the gateway's step-up
  challenge comes from.
