2FA verify limit: a right code gives its place back; exempt by an admin-only field, not e-mail (docs)

The verify limit took a place for every attempt and kept it, so the sixth
correct login of one account within 10 minutes was refused (IAWL idea 27).
Now an attempt takes its place before the compare (a parallel burst still
cannot slip under the limit), a right code gives it back, an attempt refused
without a compare gives it back at once, and a new attempt held out only by
attempts still being checked gets 429 + Retry-After: 2 "try again in a
moment". buildVerifyLimit returns limit.settle(req, correct); the plugin's
guard calls it with the package's answer. No schema change: payload-kv keeps
{ count, resetAt }.

Docs: the exempt example keyed on e-mail, which every account may change
itself (shown over REST and GraphQL): now an admin-only checkbox, with a test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 20:15:51 +01:00
co-authored by Claude Opus 5.5
parent e821d52d19
commit 91770ec17f
15 changed files with 384 additions and 25 deletions
+30 -4
View File
@@ -523,7 +523,7 @@ twoFactor: {
plugin: payloadTotp,
// requireSetup: { windowHours: 24 }, // another window
// requireSetup: { windowHours: false }, // no time limit (0 works too): locked until set up, never blocked
// requireSetup: { exempt: ({ user }) => user.email === '[email protected]' }, // a service account that logs in with a password, on purpose
// requireSetup: { exempt: ({ user }) => user.serviceAccount === true }, // a service account that logs in with a password, on purpose (the field: below)
// requireSetup: { dashboardNotice: false, statusField: false, setupNotice: false }, // each panel part can be switched off; the lock stays
// requireSetup: false, // the 2FA package's rule: redirect in the panel, API open until setup
// who may reset / reopen. Default: a staff admin whose session entered its own TOTP code. Your function replaces
@@ -547,6 +547,24 @@ twoFactor: {
`async` function stops the site at start with a clear error; any answer other than exactly `true` (a Promise,
a string, an error) exempts nobody and is logged once. Decide from the fields on `user`. An exempt account
works on its password alone, so give it the lowest role that does its job.
- **Decide `exempt` from a field only an administrator can write — not from the e-mail address.** Every account
may change its own e-mail (REST, GraphQL and the account page, with no password asked), so an e-mail list
(`user.email === '[email protected]'`, a list of addresses) lets an editor rename itself into it, and an admin's
later reset of that account's TOTP would leave it on its password alone. An account also cannot be told apart
from an address that does not exist yet. A checkbox that only admins can set:
```ts
import { adminOnlyField } from '@intecion/ipal-kit'
// in the 2FA collection's fields:
{ name: 'serviceAccount', type: 'checkbox', defaultValue: false, access: { create: adminOnlyField, update: adminOnlyField } },
// twoFactor.requireSetup:
{ exempt: ({ user }) => user.serviceAccount === true }
```
On Postgres / SQLite the new field is a column: a migration in the same deploy. A project that keeps an e-mail
list makes the e-mail field admin-only too (`access: { update: adminOnlyField }` on `email`) — then no account
changes its own address. **Check:** as an editor, `PATCH /api/users/<own id>` with `{ "serviceAccount": true }`
→ the value stays `false`.
- **`windowHours`** must be a number (0 or more) or `false`; anything else (`'24'`, `-1`) stops the site at start.
- **Import map.** The status field and the dashboard notice are admin components: `pnpm payload
generate:importmap` after upgrading (`TwoFactorStatusField`, `TwoFactorAlert`); `iapl-doctor` reports a map
@@ -589,8 +607,14 @@ the panel. As an administrator open the account → "Two-step verification: Set
**Verify attempts are limited** (`twoFactor.verifyLimit`). The package checks the code and never counts tries;
a six-digit code has about three right answers in a million, so whoever held the password could guess. iapl
counts attempts per account on `POST /api/verify-totp`: by default the **sixth attempt within 10 minutes** —
right or wrong — is refused with `429` and `Retry-After`, and a line goes to the log. Only a session that already
counts **wrong codes** per account on `POST /api/verify-totp`: by default, after **five wrong codes within 10
minutes** the next attempt — right or wrong — is refused with `429` and `Retry-After`, and a line goes to the log.
An attempt takes its place before the code is compared, so a burst of parallel guesses cannot slip under the limit,
and a right code gives its place back: an account that logs in correctly many times (an office sharing one login,
several devices) is never refused (since 1.17.1; before, every attempt counted, right or wrong, so the sixth correct
login in the window was refused). While other attempts of the same account are still being checked, a new one that
only they hold out gets `429` with `Retry-After: 2` and "try again in a moment" instead of the long wait; an attempt
that never ends counts as wrong. Only a session that already
passed the password reaches the endpoint, so nobody without the password can lock an account's owner out; someone
who has the password can delay the owner's login by ten minutes at a time (and that shows in the log). Counted in
memory per server instance **and in the database** (Payload's key-value store, `payload.kv` — the `payload-kv`
@@ -1084,7 +1108,9 @@ the full text, as before. The components take the same as props
`writeOnlySecrets` (with `boundTo`), `maskSecretsOnRestore` for your own fields.
- **2FA attempts from another origin:** `twoFactor.sameOrigin: false`, or `{ origins: [...] }`; `requestFromOwnOrigin`,
`ownOrigins` are exported. **Verify count:** `verifyLimit: { store: 'memory' }`; `buildVerifyLimit`,
`verifyLimitKey`, `clearVerifyLimits` (tests) are exported.
`verifyLimitKey`, `clearVerifyLimits` (tests) are exported. A project that wires `buildVerifyLimit` into an
endpoint of its own calls `limit.settle(req, correct)` after its check (a right code gives its place back;
without it every attempt stays counted, as before 1.17.1).
- **Dashboard alerts:** `dashboardAlerts: { integrations, cookies }`.
- **The mail service:** `mail: { relay: false }`; `mail.relay: { enrol: 'manual' | false, token, accountMail,
confirmRateLimit }`; `relayConfirmEndpoint`, `enrolRelay`, `disconnectRelay`, `rotateRelayToken`, `setRelayToken`