Skip to content

feat: add support for passkeys as a second factor for authentication - #682

Merged
pikann merged 6 commits into
masterfrom
feature/add-support-for-passkeys
Nov 1, 2026
Merged

pikann merged 6 commits into
masterfrom
feature/add-support-for-passkeys

Conversation

@pikann

@pikann pikann commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds WebAuthn passkeys as a second sign-in factor alongside TOTP. A user can register any number of passkeys; after the password, either a passkey or a TOTP/recovery code completes the sign-in. A passkey also satisfies the "require MFA" policy. Passkeys are not a passwordless login: the password is still the first step.

Behaviour

Sign-in

  • POST /auth/login now returns mfa_methods (totp, passkey) with the MFA challenge. The challenge page offers "Use a passkey" above the code form, or the passkey alone for an account with no authenticator app.
  • New POST /auth/mfa/passkey/options and /verify finish a challenge with a passkey, mirroring /auth/mfa/verify. The challenge stays single-use, and the IP allow-lists still apply.
  • A wrong passkey assertion is not counted toward the TOTP lockout; it is a signature over a fresh server challenge, so there is nothing to guess. A counter that goes backwards (clone warning) is rejected.

Managing passkeys (Account → Security → Passkeys)

  • Add (named, via the browser prompt), list with added / last-used dates, and remove.
  • Removing a passkey requires approving a passkey prompt (POST /users/me/passkeys/verify/options, then DELETE with the assertion), the same bar as turning TOTP off with a code. Any of the account's passkeys can approve, including the one being removed.
  • An admin MFA reset also deletes the user's passkeys. A user who has lost the device holding their only passkey needs that reset.

When policy requires MFA

  • The setup screen shown after login is redesigned: a chooser with passkey recommended and preselected, a step indicator, a retry state when the browser prompt fails, and a required "I've saved these codes" confirmation before leaving the recovery-codes step.
  • New password-authenticated POST /auth/mfa/setup/passkey/options and /finish let such an account register a passkey before it can sign in. They are refused for accounts policy does not oblige, and for accounts that already have any second factor, so a stolen password cannot add a passkey to a protected account.
  • A passkey counts as enrolled, so the TOTP-only setup path closes once one exists.

Implementation notes

  • Backend uses github.com/go-webauthn/webauthn; the web app uses @simplewebauthn/browser. go mod tidy also bumped a few indirect packages (testify, x/crypto, x/sync, x/sys, x/text).
  • Migration 000078_add_user_passkeys.sql: user_passkeys holds the credential id, name, and the JSON credential record. The public key is not a secret, so passkeys work without ENCRYPTION_KEY.
  • Ceremony state (challenges) lives in Redis (GETDEL, 5 min TTL), so the finish request can reach a different API instance.
  • Registration and removal failures use PASSKEY_INVALID (400) and PASSKEY_LIMIT (409), not AUTH_MFA_INVALID_CODE (401), because a 401 on a signed-in session makes the web client refresh its tokens and retry. At most 20 passkeys per account.
  • New routes are listed in mfaOpenRoutes (self-service or credential exchange, like the existing MFA routes); no new permissions.
  • UI strings are added in all nine locales.

Configuration

  • Passkeys derive their relying party from PUBLIC_URL: the host is the RP ID and its origin the only accepted origin. They are off when PUBLIC_URL is unset (the card is hidden, mfa_methods never lists passkey, and accounts keep using TOTP).
  • Optional PASSKEY_RP_ID overrides the RP ID (for example a parent domain to share across subdomains); documented in deploy/README.md. Changing the host or RP ID invalidates every registered passkey.
  • Browsers only expose WebAuthn on HTTPS or localhost, and the page's host must match PUBLIC_URL. On plain-HTTP non-localhost hosts the card says passkeys are unsupported.

Testing

  • Go: go test ./... passes. New service tests run real register / sign-in / removal ceremonies against the library using a small software authenticator (ES256, none attestation), covering replay, a foreign key, a wrong ceremony, the IP allow-list, the policy interplay, and reset. Handler, auth-service and route-coverage tests are extended.
  • Web: tsc -b, Biome and Vitest pass, including new tests for the challenge panel, the setup flow, and the passkeys card.
  • E2E: new passkeys.feature / passkeys.spec.ts use Chromium's virtual authenticator, and mfa.spec.ts is updated for the new setup screen. These were typechecked and linted but not run.

Not verified

  • No manual test with a real authenticator or a real browser prompt.
  • The migration and the new Postgres and Redis stores have not run against live services (the repository code has no new integration test).
  • The e2e specs have not been run; the "authenticator lost its credential" and "removal fails with no authenticator" scenarios assume Chromium rejects immediately when no credential matches.
  • Not included: passwordless sign-in, a self-service way to remove a passkey after losing the device, and renaming passkeys.

🤖 Generated with Claude Code

- Implemented passkey registration and login flows in the MFA service.
- Created new DTOs for passkey-related requests and responses.
- Added HTTP handlers for passkey options and verification endpoints.
- Updated the MFA handler to manage passkey enrollment and deletion.
- Introduced a new in-memory store for managing passkeys during tests.
- Added database migration for user_passkeys table to store passkey credentials.
- Enhanced error handling for passkey-related operations.
- Updated route coverage tests to include new passkey endpoints.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — minor suggestions only. I traced the authorization paths and found no way for a password-holding attacker without a passkey to complete a passkey login, add a passkey to a protected account, or otherwise bypass the second factor.

Reviewed changes — Full passkey second-factor feature: the backend WebAuthn ceremonies, storage, routing and config; the web challenge/setup/management UI; and the new e2e specs.

  • Backend passkey service — mfadom domain types and mfasvc register/login/verify ceremonies, with Redis single-use ceremony state keyed reg: / login: / verify:.
  • Login integration — Login now returns mfa_methods; BeginPasskeyLogin/CompletePasskeyLogin are bound to the challenge JWT's jti; CheckLogin/Methods treat a passkey as a second factor.
  • Pre-login setup — password-authenticated /auth/mfa/setup/passkey/{options,finish}, gated by SetupRequired and refused once any factor exists.
  • Management — /users/me/passkeys list/register/verify/delete; deletion requires a passkey assertion.
  • Storage & config — migration 000078_add_user_passkeys; relying party derived from PUBLIC_URL with optional PASSKEY_RP_ID.
  • Web UI — passkey option in MfaChallengePanel, redesigned MfaLoginSetup, new PasskeysCard, API helpers, i18n across nine locales.
  • Tests — Go service/handler/route tests, Vitest tests, and Chromium virtual-authenticator e2e specs (e2e not run per the PR).

ℹ️ Silent second-factor downgrade if passkeys become unavailable

hasPasskeys returns false whenever the relying party is unconfigured (wa == nil). For a non-privileged account whose only factor was a passkey, CheckLogin then sees totpOn=false, passkeyOn=false, skips the privileged && RequireMFA branch, and returns nil, so /auth/login issues a full session on the password alone. The code comment anticipates the fallback, but its reasoning ("policy then demands enrollment of a method that works") only holds for privileged roles: an ordinary user who enrolled a passkey is silently downgraded to password-only whenever PUBLIC_URL is unset — for instance after an operator acts on the "changing the host or RP ID" guidance. Worth deciding explicitly whether that is the intended outcome for non-privileged accounts, or whether such accounts should be pushed to re-enroll rather than losing the factor.

Technical details
# Silent 2FA downgrade for non-privileged passkey-only accounts

## Affected sites
- `services/api/internal/service/mfa/passkey.go:102-108` — `hasPasskeys` returns `false` unconditionally when `s.wa == nil` (RP unavailable), without distinguishing "user has no passkeys" from "passkeys are turned off".
- `services/api/internal/service/mfa/mfa_service.go:85-96` — `CheckLogin`: with `totpOn=false` and `passkeyOn=false`, the non-privileged path falls through to `return nil`; only `privileged && policy.RequireMFA` returns `ErrSetupRequired`.
- `services/api/internal/bootstrap/app.go:255-263` — passkeys are disabled whenever `RelyingPartyFromURL` fails (e.g. `PUBLIC_URL` unset).

## Required outcome
- Decide the policy for an account that has registered passkeys but currently has no usable path (passkeys off, no TOTP): silently allow password-only, or treat it as "factor required but unavailable" and fail closed / force enrollment.
- If the fallback is intended, correct the `hasPasskeys` comment, which currently implies policy protects all such accounts.

## Open questions for the human
- Is passing the relying party into the store so `hasPasskeys` can distinguish "no credentials" from "passkeys disabled" desirable, or is a startup-time guard (refuse to boot / log loudly when passkeys are disabled but `user_passkeys` is non-empty) the better lever?

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — re-review of the commit since the prior pullfrog review (b2154cb → 44e72759); the delta addresses all three prior findings and adds regression tests.

  • Failed closed when the relying party is unavailable — hasPasskeys now gates on the installed passkey store instead of WebAuthn, so an account that registered a passkey keeps owing a second factor when PUBLIC_URL goes missing; WithPasskeyStore installs the store even while WebAuthn stays off.
  • Requested user verification on enrollment — AuthenticatorSelection.UserVerification is set to VerificationPreferred, covering both creation and assertion options.
  • Reset the add-passkey dialog on open — AddPasskeyDialog clears name, error and mutation in an effect keyed on open, replacing the reset path that onOpenChange(true) never reached.
  • Added regression tests — Go tests for the fail-closed path and the userVerification preference, and a Vitest case for the dialog reset.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

The autofill consumer reads autofill_enabled when it processes a task, not
when the task is created. The test disabled autofill, created a task, then
re-enabled autofill straight away, so a slow consumer could see "enabled" and
call Jev, failing "Jev must not be called while autofill is disabled".

Keep autofill off for the rest of the test and use a task in a second project
(autofill on) as a barrier: once it is filled, the disabled project's earlier
task has already gone through the same stream.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — re-review of the commit since the prior pullfrog review (44e72759 → c4d21963); a test-only race fix with no production surface.

  • De-raced the Jev autofill-disabled e2e test — TestE2EJev_AutofillRespectsProjectSettings no longer re-enables autofill right after creating the disabled task; it keeps autofill off and uses a task in a second project as a same-stream barrier (mirroring the existing TestE2EJev_UnconfiguredProjectNeverCallsJev pattern), so a slow consumer can no longer read "enabled" and call Jev.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — re-review of the commits since the prior pullfrog review (c4d21963 → 2b0daf20); a backend hardening pass with tests and no web changes.

  • Added a HasPasskeys store method — hasPasskeys now runs an EXISTS query instead of loading every passkey, so one unparseable row can no longer make the second-factor check error.
  • Mapped duplicate credential inserts to PASSKEY_INVALID — AddPasskey translates the Postgres 23505 violation of the credential_id uniqueness constraint into ErrPasskeyInvalid (400) rather than surfacing it as a 500.
  • Guarded the sign-counter write — UpdatePasskeyCredential now skips the update when the incoming sign counter sits behind the stored one, so two concurrent sign-ins cannot roll the stored counter back.
  • Skipped unreadable credential records — loadUser logs and skips an undecodable credential instead of failing the whole ceremony, so one corrupt row no longer blocks sign-in.
  • Required user verification to remove a passkey — BeginPasskeyVerification now issues WithUserVerification(VerificationRequired); login continues to use VerificationPreferred.
  • Added regression tests — Go tests for the removal UV requirement and the corrupt-sibling-record skip.
  • Documented the Redis GETDEL requirement in deploy/README.md.

I verified the load-bearing library contracts directly against go-webauthn v0.18.2: Authenticator.SignCount carries json:"signCount,omitempty" under Credential.Authenticator (json:"authenticator"), so the new SQL guard's JSON path resolves correctly, and UpdateCounter sets CloneWarning (already rejected by verifyAssertion) before any regression could reach the update. go build ./..., the mfa, postgres, and HTTP test packages all pass.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — re-review of the commit since the prior pullfrog review (2b0daf20 → 51df10e1); a title/localization pass with no server changes.

  • Localized the challenge-panel heading — MfaChallengePanel now selects login.mfa.titleBoth, title, or titlePasskey from the account's available methods, matching the existing subtitle ternary instead of always showing the generic title.
  • Added the two new strings to all nine locales — login.mfa.titleBoth and login.mfa.titlePasskey are present and parse cleanly in en, es, fr, ja, ko, pt-BR, ru, vi, and zh-CN.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@pikann
pikann merged commit fee4aac into master Nov 1, 2026
7 checks passed
@pikann
pikann deleted the feature/add-support-for-passkeys branch November 1, 2026 04:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant