Issue 1313
feat: Store passkeys and one-time challenges, with the passkey rules
Back to our planDone
enhancementcmsauthmigrationpriority: low
Problem
Passkeys need somewhere safe to live, and each sign-in or add needs a one-time challenge that can't be used twice. Neither exists. The rules for who may add, use or remove a passkey also need to be plain, tested code before any screen uses them.
Proposed fix
Following the decision record (#1312):
- Rules in
domain/passkeys.ts, pure and tested: who may add, use or remove; the cap per account; the relying party ID and expected origin from the configured site address; challenge expiry; the label shown to the resident; that removing the last passkey is allowed. - Two collections in
cms/:passkeys(credential ID unique and indexed, public key, counter, transports, device type, backed-up flag, label, added, last used, owning account) andpasskey-challenges(purpose, expiry, optional account). Every field server-only; no REST or GraphQL access for residents; admins can see and delete passkeys for support. Not shown in the admin navigation unless an admin needs it (record decides). - Data layer in
data/passkeys.ts: save, list, find by credential ID, update counter and last used, delete own; create a challenge and take it once by the adapter'supdateOnewith awhere(one UPDATE that setsusedAtwhere the id and purpose match,usedAtis empty and the expiry is ahead; success only when a row comes back, asmarkConfirmedinverification/cms/store.ts).payload.db.deleteOneand the Local APIdeleteread first and don't qualify. A one-per-account opaque user handle is stored server-only; registration only inserts (a known credential ID is refused); the counter is written only when higher.createandupdateonpasskeysare refused for everyone, admins read and delete only, andpasskey-challengeshas no admin access. - Deleting an account deletes its passkeys (a hook on
users), so #310 and admin deletes leave nothing behind. - Expired challenges are removed by a task on the existing retention queue.
- The switch: the server setting that turns passkeys on, read in one place, off by default.
- Migration, regenerated
payload-types.ts, regenerated content-type reference.
No screen changes and no new package in this step.
Existing work compared
- Searched: issues for passkey, WebAuthn, challenge and recovery;
src/modules/accounts/;src/platform/payload/(retention queue,serverOnlyFieldAccess); data model. - Native: Payload collections, field access and the database adapter's
updateOnewith awherecover storage.payload.kvwas checked for challenges and doesn't fit (no expiry, no atomic take).markConfirmed(one conditional update) is the pattern. - Official and community plugins: none fit (#313's table); the plugins that store passkeys do so inside a second auth system.
- Our code: chosen on #313. Reuses
signedValue.ts,serverOnlyFieldAccess, the retention queue and the collection setup of #505's subject fields. Nothing here exists yet, so it is new.
Acceptance criteria
Tests first: each line starts as a failing test.
- Not done: Domain tests: each rule above, including another account's passkey refused and the cap.
- Not done: Relying party ID and origin come from the configured site address for production,
localhostand a preview address, and never from a request header. - Not done: Against real Payload (in-memory SQLite): a challenge is taken once; a second take, an expired one and one for another purpose all fail; two takes at once give one success.
- Not done: A credential ID already stored on any account refuses the add and leaves its key as it was; a lower counter never overwrites a higher one.
- Not done: A resident can't read, create or change
passkeysorpasskey-challengesover REST or the Local API with access on, and an admin can't create or update either (read and delete only onpasskeys; nothing on challenges) (extendpayload.config.protectedFields.test.ts). - Not done: Deleting an account deletes its passkeys.
- Not done: The retention task removes expired challenges and nothing else (used rows expire soon after, so expired-only is enough).
- Not done: The take puts the identifier inside the
where, never asid(anidskips the condition), and conditions only on the collection's own columns (a join falls back to read then update). It uses Payload's database adapter (payload.db.updateOne), not the WebAuthn adapter. - Not done: The per-account user handle is a server-only field on
users(add it to the file list), written only while empty so two adds at once keep one value. - Not done: Migration runs up, down and up again on a copy of a dev database.
- Not done: The accounts note, data model, content-type reference and
docs/project.md(the setting) describe it in the same PR.
Files likely touched
src/modules/accounts/domain/passkeys.ts(+ test)src/modules/accounts/cms/passkeys.ts,cms/passkeyChallenges.ts, a delete hook oncms/users.ts(+ tests)src/modules/accounts/data/passkeys.ts(+.payload.test.ts)src/modules/accounts/cms.ts,src/payload.config.ts,src/platform/payload/retentionJobs.ts(task registration only)src/migrations/(new),src/migrations/index.ts,src/payload-types.tssrc/payload.config.protectedFields.test.tssrc/modules/accounts/README.md,docs/architecture/data-model.md,docs/reference/content-types/,docs/project.md,.env.local.example
Must not touch
src/app/**,src/modules/accounts/ui/**,package.json- Payload's login, lockout and verify settings on
users - Other modules
Open questions
- The passkey's
userName: a default from the domain, or the account's email or display name with the label kept on our side (options are made before a label is saved). Author decides, noted on the issue. - Record 0021 still says retention removes used and expired rows, wraps one long line, and has status Proposed: those follow in a later record PR (changing the status needs a short re-review).
Area
auth, cms, migration
Priority
low
Generated by Claude Code, opens in a new tab