2FA setup required: an account without TOTP reaches only setup, within a 72 h window
twoFactor.requireSetup (on by default with 2FA): an account of the 2FA collection that has not set TOTP up is refused by ipal-kit's second-factor check on every collection, global and plugin endpoint; the setup screen and /api/setup-totp are all it reaches. It has 72 hours from its creation or from an admin's reset (field totpSetupFrom, stamped by a hook); after that setup is refused too until an admin reopens it. Accounts that existed before get their window at the first start after the upgrade (backfill in onInit), so nobody is blocked when a site upgrades. Admin side: status and reset / 'Reopen setup' button on the account page (TwoFactorStatusField), POST /api/ipal/two-factor/reset, a dashboard notice (TwoFactorAlert), a notice on the setup screen; texts in English and Polish. ipal-doctor reports blocked accounts and a switched-off mode. Overridable: requireSetup false, windowHours (false / 0 = none), exempt for a service account, resetAccess; the per-entity second-factor opt-out applies. API keys, seeds, bearer-token routes and sites with twoFactor false are not affected. HTTP tests (REST, GraphQL, first admin on an empty database) run on SQLite, Postgres and MongoDB; a browser test on the reference site covers the first admin, a new editor and the admin's reset. Docs, guide, skills, changelog. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
45d52c4690
commit
2aaaaa115a
+83
-11
@@ -425,17 +425,79 @@ that session's cookie (images from `media`) are refused, so an editor who typed
|
||||
sees broken images on the site until the code is entered or they log out.
|
||||
|
||||
An account of the 2FA collection passes when it logged in with **password and code**, or uses an **API key**
|
||||
(`auth.useAPIKey`), or has **not set TOTP up yet** (the setup screen needs that; the package behaves the same).
|
||||
Everything else is refused, including a user object the check does not recognise. Accounts of other auth
|
||||
(`auth.useAPIKey`). An account that has **not set TOTP up yet** is refused as well ("Setup required" below; the
|
||||
package alone lets it through). Everything else is refused, including a user object the check does not recognise. Accounts of other auth
|
||||
collections, anonymous visitors, bearer-token routes (`/api/ipal/doctor`, the cookie-crawl callback, MCP) and
|
||||
the Local API with `overrideAccess: true` (the default) are not affected.
|
||||
|
||||
> **Until an account has set TOTP up, its password alone opens everything its role allows** — Site
|
||||
> integrations with the SMTP password included. Enforced setup is a redirect in the panel; over the API such an
|
||||
> account is an ordinary logged-in account (it has to be, or it could not set TOTP up). So: **set 2FA up right
|
||||
> after an account is created** — create the account, log in as it, scan the code, in one sitting — and never
|
||||
> leave an invited account waiting. `ipal-doctor --url` reports the number of accounts without 2FA
|
||||
> (`2fa`, deep check); it must be 0.
|
||||
### Setup required (`twoFactor.requireSetup`, on by default)
|
||||
|
||||
An account that has **not set TOTP up yet** reaches nothing at the access level — REST, GraphQL, the panel's
|
||||
data, the plugin's endpoints — until it has. It gets the setup screen and `POST /api/setup-totp`, nothing else.
|
||||
The 2FA package alone only redirects such an account inside the panel; over the API its password would open
|
||||
everything its role allows.
|
||||
|
||||
**The setup window.** An account has **72 hours** to set TOTP up, counted from its creation or from the last
|
||||
reset by an administrator. After that it is **blocked**: setup is refused too, until an administrator reopens
|
||||
it. Without the window, whoever learns the password of an account that never set TOTP up could enrol their own
|
||||
authenticator at any time; with it, that is possible only in the first days of the account.
|
||||
|
||||
| Situation | What happens |
|
||||
|---|---|
|
||||
| New account (also the first admin of a fresh site) | logs in → setup screen with the notice "Set up two-step verification by <date>…" → after the code, the panel |
|
||||
| Not set up within the window | login still works, every request answers 403, the setup screen says the time has passed and to ask an administrator |
|
||||
| Administrator reopens setup / resets a lost phone | the account's page in the panel → **Two-step verification** (sidebar) → "Reopen setup" / "Reset two-step verification"; or `POST /api/ipal/two-factor/reset` with `{ "userId": "…" }`. A new window starts |
|
||||
| Administrators' overview | a notice on the dashboard: accounts without two-step verification, and how many of them are blocked; `ipal-doctor --url` reports the same as an error |
|
||||
| API key (`auth.useAPIKey`) | passes, whatever the setup state |
|
||||
| Seeds, scripts, the Local API with `overrideAccess` (the default) | not affected |
|
||||
| Bearer-token routes (`/api/ipal/doctor`, the cookie-crawl callback), MCP | not affected (no user) |
|
||||
| `twoFactor: false` | none of this exists |
|
||||
|
||||
**Accounts that existed before this mode** (an upgrade): their window starts at the **first start of the site
|
||||
after the upgrade**, not at their creation date, so nobody is blocked the moment a site upgrades. The server log
|
||||
says how many there are: `[ipal] 2FA: N existing account(s) without two-step verification. They have 72 h from
|
||||
now to set it up…`. From that moment they can only set TOTP up; after 72 hours they are blocked until an
|
||||
administrator reopens setup. **Before upgrading a live site, run `pnpm exec ipal-doctor --url <site>`** (accounts
|
||||
without 2FA) and have those accounts set TOTP up, or remove the ones nobody uses.
|
||||
|
||||
```ts
|
||||
twoFactor: {
|
||||
issuer: 'Company Name',
|
||||
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: false, // the 2FA package's rule: redirect in the panel, API open until setup
|
||||
// resetAccess: ({ req }) => isAdmin(req.user, req.payload), // who may reset / reopen (default: a staff admin; the second factor is always required)
|
||||
}
|
||||
```
|
||||
|
||||
- **Data.** The mode adds a date field `totpSetupFrom` (hidden, written by the plugin only) and a panel-only field
|
||||
`totpSetupStatus` to the 2FA collection. MongoDB: nothing to do. **Postgres / SQLite: create and run a
|
||||
migration** (`pnpm payload migrate:create`); until it has run, the log shows `[ipal] 2FA: could not start the
|
||||
setup window…` and existing accounts have no time limit yet.
|
||||
- **Import map.** The status field and the dashboard notice are admin components: `pnpm payload
|
||||
generate:importmap` after upgrading (`TwoFactorStatusField`, `TwoFactorAlert`).
|
||||
- **Per collection or global:** the opt-out below (`custom.ipal.secondFactor`) also lets an account without TOTP
|
||||
in, because it takes the entity out of the second-factor check altogether.
|
||||
- **Needs** ipal-kit's check (`twoFactor.guard`, on by default); with `guard: false` the mode is off.
|
||||
- **Project code** that calls `payload.find({ …, user, overrideAccess: false })` with a user fetched by id: refused
|
||||
for every account of the 2FA collection now — before, only for accounts that had TOTP set up. Take the user from
|
||||
`payload.auth({ headers })`.
|
||||
- **In your own code:** `twoFactorSetupState(req)` → `'set-up' | 'open' | 'closed' | 'exempt' | 'not-applicable'`,
|
||||
`twoFactorSetupDeadline(req)` → `Date | null`.
|
||||
- **Texts** (English and Polish; override in `i18n.translations.<lang>.ipal`): `twoFactorSetupDeadline`,
|
||||
`twoFactorSetupRequired`, `twoFactorSetupClosed`, `twoFactorStatus*`, `twoFactorReopen`, `twoFactorReset*`,
|
||||
`twoFactorAlert*`.
|
||||
|
||||
**What it does not do.** Inside the window, someone who knows the password of an account that has not set TOTP up
|
||||
can still set it up with their own authenticator. The real owner then cannot ("already set") and has to ask an
|
||||
administrator — the takeover is visible, and the window keeps it to the first days. So still: **create the
|
||||
account, hand over the password, and have the owner set 2FA up at once.**
|
||||
|
||||
**Check:** create an account, log in over the API with its password → `403` from `/api/users/me` and
|
||||
`/api/globals/site-settings`; open the panel as that account → the setup screen with the notice; enter the code →
|
||||
the panel. As an administrator open the account → "Two-step verification: Set up."
|
||||
|
||||
**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. ipalKit
|
||||
@@ -449,7 +511,8 @@ it off (a limiter in front of the site).
|
||||
**What is NOT covered — the complete list.** (1) Anonymous requests: the access function decides. (2) Accounts
|
||||
of other auth collections: 2FA is about one collection; such an account is not staff unless its collection is in
|
||||
`access.staffCollections`, and then it is staff **without** a second factor (warning in the log at start).
|
||||
(3) API keys. (4) An account that has not set TOTP up yet. (5) Entities the project opted out (below).
|
||||
(3) API keys. (4) An account that has not set TOTP up yet — only with `requireSetup: false` or when the project
|
||||
exempts it ("Setup required" below); by default it is refused. (5) Entities the project opted out (below).
|
||||
(6) `access.admin`. (7) Endpoints that are not the plugin's or the 2FA package's: your own endpoints and other
|
||||
plugins' endpoints check whatever they check — add `secondFactorPassed(req)` to yours. (8) Server code using the
|
||||
Local API with `overrideAccess: true`. Nothing is skipped by slug, name or prefix, and a user object the check
|
||||
@@ -669,7 +732,8 @@ twoFactor: false // ONLY when the project really cannot use 2FA (rare)
|
||||
### How it works for the user
|
||||
|
||||
1. Logs in (email + password)
|
||||
2. On first login: redirected to TOTP Setup (`/admin/setup-totp?back=%2Fadmin`, QR + secret)
|
||||
2. On first login: redirected to TOTP Setup (`/admin/setup-totp?back=%2Fadmin`, QR + secret), with a notice
|
||||
saying until when it has to be done. Until it is done the account can do nothing else ("Setup required")
|
||||
3. Scans the QR with an app (Google Authenticator, Authy, 1Password, Microsoft Authenticator)
|
||||
4. Enters the code → 2FA is active, back to the page from `back`
|
||||
5. Next logins: email + password + TOTP code (`/admin/verify-totp?back=…`)
|
||||
@@ -680,7 +744,12 @@ show the QR code. `back=null` or a page that keeps reloading means the import ma
|
||||
|
||||
### Resetting 2FA (admin)
|
||||
|
||||
An admin can reset another user's 2FA (when they lose their phone) — through
|
||||
An admin resets another user's 2FA (a lost phone) or reopens setup for a blocked account on that account's page
|
||||
in the panel: **Two-step verification** in the sidebar → "Reset two-step verification" / "Reopen setup". Behind
|
||||
it: `POST /api/ipal/two-factor/reset` `{ userId }` — a staff admin who has passed the second factor
|
||||
(`twoFactor.resetAccess` changes who). The account must set TOTP up again within a new window.
|
||||
|
||||
The 2FA package has its own reset, off unless you turn it on — through
|
||||
`adminManageAccess` in the @clocklimited configuration. ipalKit passes it only `collection`,
|
||||
`forceSetup` and `issuer`; pass other options by wrapping the injected factory:
|
||||
`plugin: (opts) => payloadTotp({ ...opts, adminManageAccess: ({ req }) => isAdmin(req.user, req.payload) })`.
|
||||
@@ -759,6 +828,9 @@ size while reading, content signature, container and macro checks, an optional `
|
||||
`safeAdminBack`, `wrapTotpViews`, `copyViews`, `changedViewKeys`, `SAFE_TOTP_VIEW`, `SafeTotpView` are exported.
|
||||
The stale import-map notice that comes with it: `twoFactor.importMapCheck: false`; its parts
|
||||
(`findImportMapFile`, `missingImportMapEntries`) are exported too.
|
||||
- **Setup required:** `twoFactor.requireSetup: false` (off), `{ windowHours }` (another window, `false` / `0` = none),
|
||||
`{ exempt }` (a service account), `twoFactor.resetAccess` (who reopens); exported: `twoFactorSetupState`,
|
||||
`twoFactorSetupDeadline`.
|
||||
- **2FA check:** per entity `custom.ipal.secondFactor` / `custom.totp.disableAccessWrapper` (one operation or
|
||||
all); `twoFactor.guard: false` turns ipal-kit's own second-factor check off everywhere (not recommended);
|
||||
`requireSecondFactor` / `secondFactorPassed` are exported for your own access functions and endpoints.
|
||||
|
||||
Reference in New Issue
Block a user