# Step-up and re-authentication

> Ask for a stronger or more recent sign-in before a sensitive action, and see in the token what the user proved.

Some actions need more than "signed in": a payment, a change to security settings, reading a secret. With step-up,
an app or an API asks for a stronger or a more recent sign-in, and the realm sends the user through the sign-in
chain that earns it. The user's session carries on, stronger or fresher, and one sign-out still ends everything
granted before and after.

An app can ask for three things:

- **How strongly** (`acr_values`). You name levels, such as `urn:example:acr:mfa`, and tell the realm which sign-in
  chain earns each one. A user who signed in with less is sent through the stronger chain.
- **How recently** (`max_age`, `prompt=login`). A user whose sign-in is too old signs in again.
- **Who** (`id_token_hint`). Only the named user may answer.

Tokens record the outcome: `acr` names the level reached, `amr` the methods used (such as `pwd`, `otp`), and
`auth_time` when.

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) names the levels and builds the chains;
  `RealmViewer` can read them. The app asks for a level on each sign-in.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`).
- **First:** a chain that earns the level, such as a password and an authenticator app
  ([Sign-in chains](../../../authentication/sign-in-chains/)).

## How it works

An API behind the [gateway](../../../../gateway/) can demand a level or an age in its
policy. The app then steps up:

**1. The API refuses the token.** The gateway answers `401` with a challenge that names what the policy needs:

```http
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer error="insufficient_user_authentication",
  acr_values="urn:example:acr:mfa", max_age=300,
  resource_metadata="https://api.example.com/.well-known/oauth-protected-resource"
```

**2. The app sends the user back to sign in**, passing on what the challenge named, in an ordinary
[browser sign-in](../authorization-code/):

```http
GET /oauth2/authorize?response_type=code&client_id=team-wiki
    &redirect_uri=https%3A%2F%2Fwiki.example.com%2Fcallback&scope=openid
    &acr_values=urn%3Aexample%3Aacr%3Amfa&max_age=300
    &state=k2j3h4&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&code_challenge_method=S256
Host: foobar.cvyuh.com
```

**3. The app retries the call.** If the user's session already holds that level and is recent enough, they aren't
asked to sign in again. If not, they sign in through the stronger chain. The new tokens carry
`acr: "urn:example:acr:mfa"`.

## Set it up

Set the levels on the realm with
`PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings` (see [Call the API](../../../../develop/api/)):

| Where | Setting | What it does |
|---|---|---|
| Realm | `acrValues` | Each level's name, mapped to the chain that earns it (empty by default). Discovery lists the names as `acr_values_supported` |
| Realm | `amrValues` | Each sign-in module, mapped to the `amr` value it adds (empty by default) |
| Realm | an `authChain` object | The stronger chain itself |

## What the user sees

The realm's sign-in pages for only the steps the stronger chain adds: a user already signed in with a password is
asked for their authenticator code or passkey, not the password again. For `max_age` or `prompt=login`, they sign in
again. Then the browser returns to the app. If they can't complete the step, the app is told `access_denied`.

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `acrValues` | none | level names mapped to chains |
| `amrValues` | none | module names mapped to `amr` values |
| Step-up attempts per request | one | fixed |
| `max_age` | none | a whole number of seconds, zero or more |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| The token's `acr` isn't the level asked for | The realm doesn't know the value, so it was ignored | Add it to `acrValues`, and have the app check `acr` |
| `access_denied` after a re-sign-in | The chain didn't earn the level, or the session records no chain | Check the chain the level names; the user signs out and in again |
| `login_required` with `prompt=none` | Step-up needs the user present | Send the request without `prompt=none` |

## Audit record

A step-up that continues the session writes `SESSION_UPGRADED` (the session keeps its `sid`), beside the sign-in's
`LOGIN` and its steps; the new tokens are a `TOKEN_ISSUED` record naming the `acr` reached. See them in
**Audit → Authentication**.

## Good to know

- **Levels match exactly.** There is no ranking, so the app lists every value it accepts.
- **A value the realm doesn't know is ignored.** The app must check the `acr` it receives.
- **A step-up is tried once per request.** If the chain still doesn't earn the level, the answer is
  `access_denied`.
- **`prompt=none` never steps up.** It answers `login_required`.
- **`max_age` is a whole number of seconds**, zero or more.
- **An `id_token_hint` must be an ID token this realm issued to the same app.** Its expiry doesn't matter, since it
  only says who the app expects.
- **A session that records no sign-in chain can't sign in again through one.** `max_age` or `prompt=login` on such
  a session is answered `access_denied`.

## Reference

### Errors

These go back to the app's return address, as any sign-in error does.

| Error | When |
|---|---|
| `access_denied` | The step-up didn't reach the level asked for, or the session records no chain to sign in again through |
| `login_required` | `prompt=none` and the user would have to sign in; or the user signed in isn't the one `id_token_hint` names |
| `invalid_request` | `id_token_hint` isn't an ID token this realm issued to the app, or `max_age` isn't a whole number |

## Related

- [Step up for sensitive actions](../../../guides/stronger-sign-in/step-up-for-sensitive-actions/): levels,
  chains and the gateway policy, start to finish.
- [Ask users to sign in again](../../../guides/stronger-sign-in/ask-to-sign-in-again/): `max_age` and `prompt=login`.
- [OpenID Connect sign-in](../openid-connect/): where `acr`, `amr` and `auth_time` appear.
- [First-party app sign-in](../first-party-apps/): picking a chain by name in a native app.
