feat(auth): add opt-in TOTP two-factor authentication (Phase 1)#247
Merged
Conversation
Phase 1 of S1-03: opt-in authenticator-app 2FA for password login. Strictly additive and non-breaking - with 2FA disabled (the default for every existing user) the login flow is unchanged; OAuth, sessions and JWT are untouched. Backend: otplib + qrcode; a twoFactor field on userAuth (TOTP secret AES-256-GCM encrypted at rest, bcrypt single-use recovery codes); pure helpers twoFactorRules.js with 10 unit tests; controller status/setup/verify/disable/validate; loginSession.js extracts finalizeSession and adds the 2FA gate (tempToken signed with a separate secret, validated only at /2fa/validate); routes + middleware allow-list; optional env vars in .env.example. Frontend: Login.vue second-step (TOTP or recovery code) reusing the shared post-login flow; new Settings > Two-Factor Authentication view + route + menu for enroll/disable; endpoint constants + i18n. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
33 user-facing cases: enrollment, recovery codes, login second-step, recovery-code single-use, disable, rate-limit/session, regression (non-2FA + OAuth + reset unchanged), and UI. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Phase 1 of S1-03: opt-in authenticator-app 2FA for password login. Strictly additive and non-breaking — with 2FA disabled (the default for every existing user) the login flow is unchanged. OAuth logins, sessions, and the JWT mechanism are untouched.
Backend
otplib(TOTP) +qrcode(enrollment QR).twoFactorfield on theuserAuthschema (Mixed object): TOTP secret AES-256-GCM encrypted at rest; recovery codes bcrypt-hashed and single-use.Modules/Auth/helpers/twoFactorRules.js— pure, self-contained helpers (TOTP, recovery codes, AES, tempToken). 10 unit tests intests/twofactor-rules.test.js.Modules/Auth/controller/twoFactor.js— status / setup / verify / disable / validate.loginSession.js— extractsfinalizeSession(the existing session-issuance code, unchanged) and adds the 2FA gate. WhentwoFactorEnabled, login returns a short-livedtempToken(signed with a SEPARATE secret +twoFactorPendingclaim) instead of a session; the normal access-token middleware verifies againstJWT_SECRET, so it can never accept the tempToken./2fa/validateexchanges tempToken + code for a real session via the samefinalizeSession.manageAttempt)..env.example: optionalTWO_FACTOR_ENC_KEY/TWO_FACTOR_TEMP_SECRET/TWO_FACTOR_ISSUER(derived fromJWT_SECRETif unset).Frontend
Login.vue— second-step code form (TOTP or recovery code) shown when the API returnstwoFactorRequired, reusing the sharedproceedAfterAuthpost-login flow.Out of scope (Phase 2)
Company-wide enforcement and extending the gate to OAuth logins.
Verification
.vuefiles compile via@vue/compiler-sfc; JS configs parse; backend module loads with all handlers.🤖 Generated with Claude Code