# Sign-in chains

> Put sign-in methods in order and say what each answer means, so a passkey alone is enough, or a password must be followed by a code.

A chain is the recipe for a sign-in: which methods run, in what order, and what each answer means. "A passkey alone,
or a password and then a code from an authenticator app" is one chain. Every realm starts with a chain like that,
checked to be safe, so most realms never write their own.

Write a chain when you want a different recipe: a password alone for a test realm, a separate sign-in for
administrators, or a branch that sends a user with no passkey straight to the password page.

## Before you begin

- **Who can do it:** a realm administrator, in a group that holds `RealmOwner`
  ([Delegated administration](/platform/realms/delegated-administration/)). `RealmViewer` can read chains.
- **Where:** chains are written through the API; pointing the realm at one is in the dashboard under
  **Authentication → Settings**, or through the API ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`).
- **First:** the module instances the chain names must exist ([Sign-in modules](../sign-in-modules/)).

## How it works

A chain is an `authChain` object: an ordered list of entries, each naming a module instance and what its answer
means. Most entries use a **flag**:

| Flag | When the module succeeds | When it fails |
|---|---|---|
| `must` | Go on | Go on; the sign-in fails at the end |
| `gate` | Go on | The sign-in fails now |
| `enough` | The sign-in succeeds now, unless a `must` entry already failed | Go on |
| `optional` | Go on | Go on |

A module that steps aside, having nothing to check for this user, always goes on to the next entry.

As the sign-in walks the chain:

- **The first module to succeed names the user.** Later modules confirm that user.
- **Each module that succeeds adds its level.** The session's level is the highest of them.
- **A password is typed once.** An entry can share what it collected with later entries (`shareCredentials`), and a
  later entry can reuse it (`reuseCredentials`, with `reuseMode: reuseOnly` to fail rather than ask again).
- **The chain is fixed when the sign-in starts.** A change saved halfway through doesn't change the rules an
  in-progress sign-in is judged by.

### Branching with routes

For a branch, write an entry's **routes** in place of a flag: where to go for each answer (`succeeded`, `ignored`,
`failed`), as `next`, `succeed`, `fail`, or another entry by its instance name or position. A `severity` says whether
the entry's failure is `decisive` (it binds the outcome, as with `must` and `gate`) or `deferred` (it counts only if
nothing succeeds). An entry written with a flag behaves exactly like the routes that flag stands for.

This entry signs in a user who has a passkey, and sends one who has none to the `nativeStore` entry:

```json
{ "module": "webauthnAuth",
  "on": { "succeeded": "succeed", "ignored": "nativeStore", "failed": "next" },
  "severity": "deferred" }
```

### The chains a realm starts with

Every new realm is created with these chains and module instances, and its default chain and
administrators' chain both point at `universalLogin`:

| Chain | Entries | Used for |
|---|---|---|
| `universalLogin` | `webauthnAuth enough`, `nativeStore gate`, `totp gate` | Signing in: a passkey alone, or a password then an authenticator app |
| `webauthnRegChain` | `confirmIdentity gate`, `webauthnReg must` | Adding a passkey to a signed-in account |
| `nativeStoreChain` | `nativeStore must` | A password alone |

A new realm also requires a second step (`twoFactorMandatory: true`) and allows all five modules.

## Set it up

Write the chain through the API, then choose it as the default or administrators' chain, in the dashboard under
**Authentication → Settings** or through the API:

1. Write the chain:

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

   The realm checks the chain as it saves it, and refuses one that breaks a rule below with `400`.

2. Point the realm at it. `defaultChain` is what a sign-in runs when none is named; `adminChain` is what it runs
   when it starts with `?realmAdmin`. Both are links, so a chain that doesn't exist can't be named:

   ```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 '{ "defaultChain": [{ "id": "<chain-id>" }] }'
   ```

To run a named chain for a user who is already signed in, start the sign-in with `?service=<chain>`. See
[Making a session stronger](../sessions/#making-a-session-stronger).

## What the user sees

The sign-in page shows one module's prompts at a time, in the chain's order: a passkey prompt, a username and
password form, a code field. A module that stands aside shows nothing. When a decisive entry fails, the sign-in is
refused with `401` and the user starts again; the realm doesn't say which step failed beyond the module's own
message. A user who takes longer than `pageTimeoutSecs` on a page, or `loginTimeoutSecs` overall, is told the sign-in
expired and starts again.

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `defaultChain`, `adminChain` | `universalLogin` | any chain in the realm |
| `loginTimeoutSecs`: a whole sign-in | `120` | `1` to `86400` |
| `pageTimeoutSecs`: one page | `120` | |
| `returnToAllowlist` | empty: only the realm's own hosts, over `https` | URL patterns |
| `loginSuccessUrl` | the dashboard | a path or an allowed URL |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| Saving a chain answers `400` | It breaks one of the rules in Good to know | Read the message: it names the rule |
| Deleting a module answers `400` | A chain still names it | Take it out of every chain first |
| Nobody can sign in after a change | The default chain can't succeed for your users | Sign in with `?realmAdmin`, which runs `adminChain`, and point `defaultChain` back |
| A sign-in lands on the dashboard, not the app | Its `returnTo` isn't on the realm's host and matches no `returnToAllowlist` pattern | Add the app's address pattern to `returnToAllowlist` |

## Audit record

A change to a chain or to `authSettings` is recorded as `CREATED`, `UPDATED`, `PATCHED` or `DELETED` in
**Audit → Config** ([Change history](/audit-reports/audit-trail/change-history/)). Each sign-in through a chain
writes `LOGIN_STARTED`, one `MODULE_COMPLETED` per entry with how it went, and `LOGIN` or `LOGIN_FAILED`
(**Audit → Authentication**).

## Good to know

- **A chain is checked when it's saved, not when someone signs in.** Saving refuses a chain with no entries, an
  entry with neither a flag nor routes, a route to a name that isn't in the chain or is listed twice (address that
  one by position), and a last `enough` entry after a `must` or `gate` one, which could never fail the sign-in.
- **Routes only go forward.** A route back to an earlier entry is refused, so a chain can't loop.
- **A decisive failure vetoes success.** Even a route you wrote to `succeed` isn't taken after a `must` or `gate`
  entry failed.
- **A route names an entry, not a slot.** Inserting an entry doesn't repoint a route written by name.
- **Return addresses are checked.** A `returnTo` on the sign-in is followed only when it's a path on the same host, or
  matches a pattern in `returnToAllowlist`. With an empty list, only the realm's own host names over `https` are
  allowed. Other schemes, such as `javascript:`, are always refused.
- **The administrators' chain is your way back in.** Keep `adminChain` on a chain the realm can run by itself, so
  administrators can still sign in if the default chain is broken.

## Reference

### Settings

| Where | Setting | What it does |
|---|---|---|
| `authChain` | `chain` | The entries: `[{ "module", "flag", "options" }]`, or `[{ "module", "on", "severity", "options" }]` |
| `authSettings` | `defaultChain` | The chain a sign-in runs when none is named |
| `authSettings` | `adminChain` | The chain a sign-in runs when it starts with `?realmAdmin` |
| `authSettings` | `loginTimeoutSecs` (`120`), `pageTimeoutSecs` (`120`) | How long a whole sign-in may take, and how long one page may wait |
| `authSettings` | `loginSuccessUrl` (the dashboard), `loginFailureUrl` | Where a sign-in lands when nothing else is asked for |
| `authSettings` | `returnToAllowlist` | URL patterns a `returnTo` address may match |

## Related

- [Sign-in methods](../sign-in-methods/): what each method checks.
- [Sign-in modules](../sign-in-modules/): each module's name and settings.
- [Sessions](../sessions/): what a successful chain produces.
- [Step up for sensitive actions](../../guides/stronger-sign-in/step-up-for-sensitive-actions/): send a user
  through a stronger chain before a sensitive action.
