# Sign-in methods

> Passkeys, passwords, authenticator apps, recovery codes and identity confirmation, the methods a realm signs users in with, and what each one holds to.

A realm signs users in with passkeys, passwords and authenticator apps, and every new realm offers all three. A
passkey alone is enough; a password must be followed by a code from an authenticator app. Recovery codes stand in
for a lost app, and before a user changes how they sign in, the realm checks they still hold a factor they already
have.

Each method is a **module**: one check in a sign-in. [Sign-in chains](../sign-in-chains/) explains how modules are
put in order and what each answer means. You choose which methods a realm may run, and tune each one through its
own settings ([Sign-in modules](../sign-in-modules/)).

## 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 the settings.
- **Where:** which methods run, and whether a second step is required, in the dashboard under
  **Authentication → Settings** or through the API; each method's settings and the passkey domain, today through the
  API ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`).
- **First:** for passkeys, the realm needs its passkey domain (`rpId`); a realm created with a host name has it.

## How it works

| Method | What the user sees | What it checks |
|---|---|---|
| Passkey | Their device's passkey prompt: a fingerprint, a face or a PIN, with nothing to type | A passkey they registered with the realm |
| Password | A username and password form | The password against the realm's own users |
| Authenticator app | A field for a six-digit code, or a set-up page with a QR code the first time | A code from their app, after an earlier step named them |
| Recovery code | A link on the authenticator app's page to use a recovery code instead | One of the one-time codes they were given with their first factor |
| Add a passkey | Their device's prompt to create a passkey, from their account | Makes a new passkey for a user who is already signed in |
| Confirm identity | A prompt for their app's code or a passkey, before they change how they sign in | That a signed-in user still holds a factor they already have |

A method answers in one of three ways: it **succeeded**, it **failed**, or it **stepped aside** because it had
nothing to check for this user. A sign-in can take several pages, and each method picks up where it left off on the
next page.

### Passkeys

A passkey signs a user in on its own, with nothing to type. With no user named yet, the realm asks for the username
first. A user with no passkey, or a name the realm doesn't hold, makes the passkey step stand aside, so the next page
reveals nothing about which it was.

Passkeys are bound to one domain, the realm's **relying party ID** (`rpId` in `webauthnSettings`). A realm created
with a host name has that host as its `rpId`, so passkeys work from the start. A realm created without one has no
`rpId`, and offers no passkeys until you set it: see [Set it up](#set-it-up).

- **Each passkey answers one challenge,** from `https://<rpId>` or another origin you list in `origins`.
- **A signature counter that didn't advance is let through by default** (`counterPolicy: warn`). With `counterPolicy: reject` it's refused.
- **A user may hold several passkeys.** The ones they hold are sent to the browser, so one authenticator isn't
  registered twice.
- **Right after a fresh sign-in,** adding a passkey is first tried without a prompt, so the browser can offer one
  quietly; otherwise the user is shown a button.
- **Repeated failures disable a passkey.** A passkey refused `maxFailuresBeforeDisable` times in a row is disabled,
  and the user signs in another way. A user whose passkeys are all disabled isn't offered another until they remove
  the disabled one.

Adding a passkey runs in its own chain, which a user's account page starts for a signed-in session, not in the
sign-in chain.

**Which passkeys work.** Any WebAuthn authenticator: passkeys synced by the user's platform or password manager,
which follow them to their other devices, and hardware security keys, which stay on the key. The realm asks for no
attestation, so it doesn't restrict users to particular makers.

**Register two.** Ask users to add a second passkey on another device, or a security key, so losing one device
doesn't lock them out. A user who loses their only passkey signs in with their password and authenticator app
instead, then adds a new passkey; a recovery code for a lost passkey is
[planned](../whats-next/#a-recovery-code-for-a-lost-passkey).

### Passwords

The password method checks a username and password against the realm's own users.

- **Names are matched without regard to case:** `maya`, `Maya` and `MAYA` are one user.
- **A wrong password and an unknown name are refused the same way.**
- **Inactive and expired accounts are refused** after the chain, when the account is inactive or its
  `accountExpiry` has passed. A missing or unreadable expiry never locks anyone out.

A weak password is refused when it's set, by the [Password rules](../password-rules/) you declare.

### Authenticator app

A code from an authenticator app (RFC 6238) is a second step: it confirms the user an earlier step named, and fails
if no one is named yet.

- **Set-up happens during sign-in.** A user with no app is given its set-up address (`otpauth://`) to add. The app is
  saved only once its first code is right.
- **Each code works once.** A code that signed someone in is refused if it's sent again, even by two sign-ins at the
  same moment.
- **Skipping.** When the realm doesn't require a second step, a user with no app may skip setting one up. The skip
  lasts `twoFactorSkipDays`. A skip isn't a success: the step stands aside. See
  [Requiring a second step](#requiring-a-second-step).
- **Guessing disables the app.** After `maxFailuresBeforeDisable` wrong codes in a row, the app is disabled. A user
  whose apps are all disabled is refused, not offered a new one.
- **An app keeps the settings it was set up with.** Changing the algorithm, digits or period affects new apps only.

### Recovery codes

When a user sets up their first second factor, app or passkey, the realm shows them a set of one-time recovery
codes, once. On the authenticator app's page, a recovery code signs the user in in place of a code, and lets them
set up a new app. Each code is spent when used.

### Confirm identity

Before a chain that changes how a user signs in goes on, this step checks it's still the same user.

- **Within 10 minutes of signing in,** it stands aside: the sign-in is fresh enough.
- **After that,** the user answers with their authenticator app or an existing passkey, whichever they hold.

### Requiring a second step

A new realm requires a second step (`twoFactorMandatory: true`): a user who signs in with a password must also give
a code from an authenticator app. What a user with no app yet sees depends on that setting:

| `twoFactorMandatory` | At the first sign-in with no app | When they skip |
|---|---|---|
| `true` (a new realm's default) | "This account needs an authenticator app.", with **Set up** only | They can't: setting up an app is the only way on |
| `false` | "Protect your account with an authenticator app.", with **Set up** and **Skip for now** | They're signed in without one, and not asked again for `twoFactorSkipDays` days (30 by default) |

When the skip runs out, the user is offered the same choice again at their next sign-in, and may skip again. With
`twoFactorSkipDays` set to `0`, one skip lasts for good. To make every user set one up, with no skip, set
`twoFactorMandatory` to `true`: users who skipped are asked to set up at their next sign-in.

Setting up shows a QR code to scan ("Scan this code with your authenticator app."), then asks for the first code,
then shows the recovery codes once ("Save these recovery codes somewhere safe. They won't be shown again."). A user
who signs in with a passkey isn't asked for an app.

### Users' own factors

A signed-in user sees their passkeys in the dashboard under **FIDO Devices**, and can remove one there. Through the
API, they list their passkeys, authenticator apps and recovery codes with
`GET https://foobar.cvyuh.com/api/v0/am/devices/v1`, and remove a passkey or an app with
`DELETE https://foobar.cvyuh.com/api/v0/am/devices/v1/<id>`. Each removal is recorded, and sent to anyone following
the user's credential changes through [Send signals](../../signals/shared-signals/). Recovery codes aren't removed
one at a time.

```json
{ "data": [
  { "id": "<device id>", "kind": "webauthn", "name": "MacBook", "lastUsedAt": "<RFC 3339 time>", "disabled": false, "backupEligible": true, "…": "…" },
  { "id": "<device id>", "kind": "totp", "name": "Authenticator", "lastUsedAt": "<RFC 3339 time>", "disabled": false, "digits": 6, "period": 30, "…": "…" },
  { "id": "<device id>", "kind": "recovery", "remaining": 9, "…": "…" }
] }
```

`disabled` is `true` for a factor that reached its failure cap. `backupEligible` tells a synced passkey from one held
on a single device.

An administrator can't remove a user's factors today; only the user can, while signed in.

### Who am I

`POST https://foobar.cvyuh.com/api/v0/am/whoami/v1` tells a signed-in user, or a script, who the session belongs to:

```json
{
  "id": "<user id>",
  "realm": "foobar",
  "roles": ["ui-realm-admin", "ui-self-service-user"],
  "user": { "uid": "maya", "mail": "maya@example.com", "webAuthnDeviceCount": 1, "…": "the rest of their profile, without the password" },
  "session": { "authLevel": 10, "authType": "…", "authInstant": "<RFC 3339 time>", "expiresInSecs": 6840 }
}
```

`roles` holds `ui-realm-admin` when the user can administer the realm. With no session, it answers `401`.

## Set it up

New realms start with every method on, and a second step required. Each change is a call to the admin API;
[Call the API](../../../develop/api/) shows how to get a session.

1. **Check the passkey domain.** Read `webauthnSettings`:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/webauthnSettings/webauthnSettings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

   An empty `rpId` means the realm has no passkeys. Set it to the host users sign in at, or a parent domain of it
   that no other realm's hosts share, and list any other origin that may use it:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/webauthnSettings/webauthnSettings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "rpId": "foobar.cvyuh.com", "rpName": "Foobar" }'
   ```

   **Changing `rpId` orphans every passkey the realm holds:** each one is bound to the domain it was made under, and
   works nowhere else. Choose it once.

2. **Choose the methods** the realm may run, and whether a second step is required. `authenticators` is a list, and
   the list you send replaces the realm's, so read `authSettings` first and send every method you want to keep:

   ```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 methods"], "twoFactorMandatory": true }'
   ```

   The method names are in [Sign-in modules](../sign-in-modules/#module-names).

3. **Tune one method** on its own settings object; see [Sign-in modules](../sign-in-modules/#tune-a-module).

## What the user sees

| Method | The user sees | When it fails |
|---|---|---|
| Passkey | Their device's prompt: a fingerprint, a face or a PIN | "That passkey did not work. Try again." |
| Password | A username and password form | The sign-in is refused with `401`; the same for an unknown name |
| Authenticator app | "Enter verification code:" | "That code did not work. Try again." |
| Recovery code | **Use a recovery code**, then "Enter one of the recovery codes you saved…" | The code is refused; each works once |
| Confirm identity | "Verify it's you", with their app or a passkey | The change they started doesn't go on |

Wrong passwords and codes count toward [Lockout](../lockout/).

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `twoFactorMandatory` | `true` in a new realm | `true` or `false` |
| `twoFactorSkipDays`: how long a skip lasts | `30` | `0` (for good) or more |
| Passkey and app tries per sign-in (`attemptsPerLogin`) | `3` | |
| Wrong answers in a row before a passkey or app is disabled (`maxFailuresBeforeDisable`) | `20` | |
| Recovery codes given with a first factor (`recoveryCodeCount`) | `10` | |
| Authenticator app codes | 6 digits, 30 seconds, SHA-256 | set on `totpModule` |
| Confirm identity stands aside | within 10 minutes of signing in | fixed |
| Passkey ceremony timeout (`timeoutSeconds`) | `300` | |

Every setting is listed in [Sign-in modules](../sign-in-modules/#settings).

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| Users aren't offered passkeys | The realm has no `rpId` | Set `rpId` (step 1). Choose it once: changing it orphans every passkey |
| A passkey works on one site but not another | The other origin isn't under the `rpId` or listed in `origins`/`relatedOrigins` | Add the origin |
| A user's codes are always refused | Their device's clock is off, or the app ignored SHA-256 | Have them set the clock to automatic; `allowSha1Fallback` (on by default) accepts apps that use SHA-1 |
| A user is refused at the code step, every time, even with a recovery code | Every authenticator app they hold was disabled after repeated wrong codes | An administrator can't remove the app today; contact support |
| A user who lost their phone can't sign in | They have no passkey, and no recovery code left | Contact support; ask users to keep their recovery codes and add a passkey |

## Audit record

Each method writes a `MODULE_COMPLETED` record in the sign-in, with the method and the level it gave, then `LOGIN`
or `LOGIN_FAILED` (**Audit → Authentication**). A factor added or removed is a change to the user's devices, in
their [Change history](/audit-reports/audit-trail/change-history/), and a change to a method's settings is in
**Audit → Config**.

## Good to know

- **An answer is taken before it's checked.** Two requests answering the same page at once get one check, not two,
  for passwords, codes and passkeys alike.
- **Wrong answers count toward lockout.** See [Lockout](../lockout/).
- **Passkey attestation is limited.** The realm accepts `none` and self-attested `packed` statements only; it doesn't
  check an authenticator's maker against a trust chain.
- **Users can't rename factors or make new recovery codes yet.** They can list and remove them, as above.

## Coming next

Passkeys offered as the user types their name, a recovery code for a lost passkey, and users renaming their factors
and making new recovery codes. See [What's next for authentication](../whats-next/).

## Related

- [Sign-in chains](../sign-in-chains/): how methods combine.
- [Sign-in modules](../sign-in-modules/): each module's name and every setting.
- [Sessions](../sessions/): what a sign-in produces, and how guessing is stopped.
- [Password rules](../password-rules/): what a password must hold.
- [Step up for sensitive actions](../../guides/stronger-sign-in/step-up-for-sensitive-actions/): ask for a
  stronger method before a sensitive action.
