# Authenticator app for your users

A user's account can have a second sign-in step: a 6-digit code from an authenticator app (the standard kind, new every 30 seconds), with eight one-time recovery codes for a lost phone.

> **Being built.** Sign-in already honours the second step on every route, as described below. **Turning it on through the API is not built yet**, so a user of an application that only uses the API cannot switch it on today. Write your sign-in screen to handle `code_required` now and it will keep working when set-up arrives.

## What your sign-in code must handle

Whenever the password, provider or emailed link was right and the account has the step on, the answer is `401` with `code_required`. Nothing has been used up. Ask the person for their code and send the same request again with the code added.

| Sign-in route | Add this field |
|---|---|
| `POST /v1/auth/signin` | `"code": "123456"` |
| `POST /v1/auth/exchange` (Google, GitHub) | `"secondStepCode": "123456"` |
| `POST /v1/auth/verify` with `type: "magiclink"` | `"secondStepCode": "123456"` |

```
curl -s $API/v1/auth/signin -H "apikey: $KEY" -H "content-type: application/json" \
  -d '{"email":"sam@example.test","password":"a long passphrase here","code":"123456"}'
```

A recovery code is accepted in the same field and works once.

- A wrong password never answers `code_required`, so the answer does not reveal that a password was right unless it was.
- A wrong code counts toward the account's lockout exactly as a wrong password does: 5 in a row locks it for 15 minutes.
- A code cannot be used twice.
- On `exchange` and `verify`, a wrong authenticator code answers `401 bad_second_step`, and the one-time code or link is still good for another try.

## How it is kept

The shared secret is stored sealed, recovery codes only as hashes. Turning the step on or off sends the user a short notice by email and is on the audit record; the secret and the codes never are. Resetting a password does not remove the step.

## Setting it up through the API

For an application that talks to Rezin Core only through the API. Each call takes the project key and the signed-in account's token.

| Call | Does |
|---|---|
| `GET /v1/auth/second-step` | `{ "enabled": false, "recoveryCodesLeft": 0 }` |
| `POST /v1/auth/second-step/start` | Returns `{ "secret": "…", "link": "otpauth://…" }`, shown this once. Draw `link` as a QR code, or show `secret` to type in. Nothing is in force yet. `409 already_on` if the step is on. |
| `POST /v1/auth/second-step/confirm` with `{ "code": "123456" }` | The person proves their app shows the right code. Returns `{ "recoveryCodes": [ … ] }`, eight codes, shown this once. From now on signing in asks for a code. `400 bad_code` otherwise. |
| `POST /v1/auth/second-step/off` with `{ "password": "…" }` | Turns it off. `401 bad_password` otherwise. |

An account that still has to replace a one-time password is answered `403 password_change_required` until it has.

## Resetting it for someone

Sign-in, Accounts, open the account, **Reset authenticator**. For someone who has lost their phone and their recovery codes. The step is taken off and every sign-in of theirs ends; they sign in with their password and set the step up again. Make sure it is really them asking. See [Managing accounts in the console](https://rezincore.com/docs/auth/overview.md#managing-accounts-in-the-console).

## Requiring it

A project can require the step for everyone, or switch it off for everyone: Sign-in, Sessions and limits. See [Authentication settings](https://rezincore.com/docs/auth/settings.md#passwords-one-sign-in-at-a-time-and-the-authenticator-step).
