Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(mfa): show regenerated recovery codes in a modal; fix download revoke
Elian Dorand9d77a6acaf5
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex c07f342b0b..12d79e3a61 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1703,12 +1703,9 @@"electron_disabled": "Multi-Factor Authentication is not supported in the desktop build currently.","totp_title": "Time-based One-Time Password (TOTP)","totp_description": "TOTP (Time-Based One-Time Password) is a security feature that generates a unique, temporary code which changes every 30 seconds. You use this code, along with your password to log into your account, making it much harder for anyone else to access it.",- "totp_secret_title": "Generate TOTP Secret",- "totp_secret_generate": "Generate TOTP Secret",- "totp_secret_regenerate": "Regenerate TOTP Secret",- "no_totp_secret_warning": "To enable TOTP, you need to generate a TOTP secret first.",- "totp_secret_description_warning": "After generating a new TOTP secret, you will be required to login again with the new TOTP secret.",- "totp_secret_regenerate_confirm": "Are you sure you want to regenerate the TOTP secret? This will invalidate previous TOTP secret and all existing recovery codes.",+ "totp_setup_label": "Set up two-factor authentication",+ "totp_setup_description": "Generate a time-based code from an authenticator app as your second factor. You'll scan a QR code and confirm a code to finish — recovery codes are issued at the end.",+ "totp_setup_button": "Set up","totp_enroll_title": "Confirm your authenticator","totp_enroll_instructions": "TOTP won't be enabled until you confirm a valid code below, so you can't lock yourself out by mistake.","totp_enroll_step1_title": "1. Add the account to your authenticator app",@@ -1730,7 +1727,7 @@"totp_enroll_saved_ack": "I've saved my recovery codes somewhere safe","totp_enroll_finish": "Finish setup","totp_enroll_enable_error": "Couldn't enable two-factor authentication. Please try again.",- "totp_section_title": "TOTP",+ "totp_section_title": "Time-based One-Time Password (TOTP)","totp_remove_label": "Remove two-factor authentication","totp_remove_description": "Completely remove your two-factor authentication setup. Unlike switching it off above, you'd have to set it up again from scratch to use it.","totp_remove_button": "Remove",@@ -1741,6 +1738,7 @@"recovery_keys_no_key_set": "No recovery codes set","recovery_keys_regenerate": "Regenerate recovery codes","recovery_keys_regenerate_confirm": "Regenerate recovery codes? Your current recovery codes will stop working immediately, and you'll need to save the new ones.",+ "recovery_keys_regenerated_title": "New recovery codes","recovery_keys_regenerated_instructions": "Here are your new recovery codes. Save them now: they replace your previous codes and won't be shown again.","recovery_keys_done": "Done","recovery_keys_label": "Recovery codes",diff --git a/apps/client/src/widgets/type_widgets/options/multi_factor_authentication.css b/apps/client/src/widgets/type_widgets/options/multi_factor_authentication.cssindex 73a802990e..fb64e5afb7 100644--- a/apps/client/src/widgets/type_widgets/options/multi_factor_authentication.css+++ b/apps/client/src/widgets/type_widgets/options/multi_factor_authentication.css@@ -92,14 +92,17 @@letter-spacing: 0.15em;}-/* Recovery-codes step: two monospaced columns so all eight codes fit compactly. */-.modal.totp-enrollment-modal .totp-recovery-codes {+/* Recovery-codes list (shared by the enrollment dialog and the regenerate dialog): two monospaced+ columns so all eight codes fit compactly. */+.totp-recovery-codes {column-count: 2;font-family: var(--monospace-font-family, monospace);+ font-size: 0.9em;+ user-select: text;}/* Copy / download actions sit in a row beneath the codes. */-.modal.totp-enrollment-modal .totp-recovery-actions {+.totp-recovery-actions {display: flex;gap: 8px;margin-block: 12px;diff --git a/apps/client/src/widgets/type_widgets/options/multi_factor_authentication.tsx b/apps/client/src/widgets/type_widgets/options/multi_factor_authentication.tsxindex 75c8640a18..2b3fd77d52 100644--- a/apps/client/src/widgets/type_widgets/options/multi_factor_authentication.tsx+++ b/apps/client/src/widgets/type_widgets/options/multi_factor_authentication.tsx@@ -197,40 +197,24 @@ function TotpSettings({ totpStatus, refreshTotpStatus }: {}, [ refreshRecoveryKeys ]);return (<>- <OptionsSection title={t("multi_factor_authentication.totp_secret_title")}>- {totpStatus?.set- ? <Admonition type="warning">{t("multi_factor_authentication.totp_secret_description_warning")}</Admonition>- : <Admonition type="note">{t("multi_factor_authentication.no_totp_secret_warning")}</Admonition>- }-- <Button- text={totpStatus?.set- ? t("multi_factor_authentication.totp_secret_regenerate")- : t("multi_factor_authentication.totp_secret_generate")}- onClick={async () => {- if (totpStatus?.set && !await dialog.confirm(t("multi_factor_authentication.totp_secret_regenerate_confirm"))) {- return;- }-- // Don't generate-and-persist here. The enrollment modal fetches a secret, has the- // user confirm a code for it, and only then is it persisted server-side — so a- // botched setup leaves the existing (or no) secret untouched instead of locking- // the user out on next login.- setShowEnroll(true);- }}- />- </OptionsSection>-- {/* Recovery codes are part of TOTP, so they only appear once a secret is enrolled. They can- be regenerated for the existing secret, but never minted while TOTP is inactive. */}- {totpStatus?.set &&- <TotpRecoveryKeys+ {/* Before enrollment, a single call-to-action opens the dialog — which is where the secret is+ generated, verified, and (only on Finish) persisted. Once a secret is set, that's replaced+ by the recovery-codes panel; recovery codes are part of TOTP and never exist without it. */}+ {totpStatus?.set+ ? <TotpRecoveryKeysstatus={recoveryStatus}- generatedCodes={regeneratedCodes}onRegenerate={regenerateRecoveryCodes}- onDismissGenerated={() => setRegeneratedCodes(undefined)}onRemoveTotp={removeTotp}- />}+ />+ : <OptionsSection title={t("multi_factor_authentication.totp_section_title")}>+ <OptionsRowWithButton+ label={t("multi_factor_authentication.totp_setup_label")}+ description={t("multi_factor_authentication.totp_setup_description")}+ icon="bx-plus"+ buttonText={t("multi_factor_authentication.totp_setup_button")}+ onClick={() => setShowEnroll(true)}+ />+ </OptionsSection>}{createPortal(<TotpEnrollmentModal@@ -240,6 +224,11 @@ function TotpSettings({ totpStatus, refreshTotpStatus }: {/>,document.body)}++ {createPortal(+ <RecoveryCodesModal codes={regeneratedCodes} onHidden={() => setRegeneratedCodes(undefined)} />,+ document.body+ )}</>);}@@ -522,6 +511,33 @@ function RecoveryCodesList({ codes }: { codes: string[] }) {);}+/**+ * Shows a freshly issued batch of recovery codes once, in a modal — the same presentation used by the+ * enrollment dialog's final step — so regenerating codes feels identical to setting them up. Open+ * when `codes` is set; dismissing clears them on the caller's side.+ */+function RecoveryCodesModal({ codes, onHidden }: {+ codes?: string[],+ onHidden: () => void+}) {+ return (+ <Modal+ className="totp-recovery-modal"+ title={t("multi_factor_authentication.recovery_keys_regenerated_title")}+ size="md"+ show={!!codes}+ onHidden={onHidden}+ stackable+ footer={<Button text={t("multi_factor_authentication.recovery_keys_done")} kind="primary" onClick={onHidden} />}+ >+ {codes && <>+ <FormText>{t("multi_factor_authentication.recovery_keys_regenerated_instructions")}</FormText>+ <RecoveryCodesList codes={codes} />+ </>}+ </Modal>+ );+}+/** Downloads the recovery codes as a plain-text file. Uses a Blob so it works over HTTP, unlike the clipboard API. */function downloadRecoveryCodes(codes: string[]) {const blob = new Blob([ `${codes.join("\n")}\n` ], { type: "text/plain" });@@ -532,35 +548,22 @@ function downloadRecoveryCodes(codes: string[]) {document.body.appendChild(link);link.click();link.remove();- URL.revokeObjectURL(url);+ // Defer revocation so the in-flight download isn't cancelled (immediate revoke breaks it in some+ // browsers — see the same pattern in services/image.ts).+ setTimeout(() => URL.revokeObjectURL(url), 10_000);}/*** Recovery-codes management for an enrolled TOTP: a read-only status (how many remain), a confirmed* regenerate action (which replaces the codes for the existing secret), and the destructive- * remove-TOTP action. Codes are only ever shown once — right after enrollment or regeneration.+ * remove-TOTP action. The codes themselves are only ever shown once — in a modal right after+ * enrollment or regeneration (see {@link RecoveryCodesModal}).*/-function TotpRecoveryKeys({ status, generatedCodes, onRegenerate, onDismissGenerated, onRemoveTotp }: {+function TotpRecoveryKeys({ status, onRegenerate, onRemoveTotp }: {status?: string[],- /** Freshly regenerated codes to show once; when set, the section switches to the save-them view. */- generatedCodes?: string[],onRegenerate: () => void,- onDismissGenerated: () => void,onRemoveTotp: () => void}) {- // Just regenerated: show the new codes once so the user can save them, then return to the status.- if (generatedCodes) {- return (- <OptionsSection title={t("multi_factor_authentication.totp_section_title")}>- <FormText>{t("multi_factor_authentication.recovery_keys_regenerated_instructions")}</FormText>-- <RecoveryCodesList codes={generatedCodes} />-- <Button text={t("multi_factor_authentication.recovery_keys_done")} onClick={onDismissGenerated} />- </OptionsSection>- );- }-const remaining = status?.filter(isUnusedRecoveryCode).length ?? 0;return (