Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Copy TOTP to clipboard after passkey authentication in browser extension (#2305)

Leendert de Borst921e36df1041
diff --git a/apps/browser-extension/src/entrypoints/contentScript/ConditionalPasskey.ts b/apps/browser-extension/src/entrypoints/contentScript/ConditionalPasskey.ts
index 995a8780f..74b09c11b 100644
--- a/apps/browser-extension/src/entrypoints/contentScript/ConditionalPasskey.ts
+++ b/apps/browser-extension/src/entrypoints/contentScript/ConditionalPasskey.ts
@@ -14,6 +14,7 @@
import { sendMessage } from '@/utils/messaging/ExtensionMessaging';
import type { ConditionalPasskeyOption, PasskeyGetCredentialResponse, WebAuthnPublicKeyGetPayload } from '@/utils/passkey/types';
import type { WebAuthnGetEventDetail } from '@/utils/passkey/webauthn.types';
+import { copyTotpToClipboardIfEnabled } from '@/utils/TotpClipboard';
/**
* The response detail dispatched back to the page to settle the pending `get()` promise.
@@ -156,6 +157,14 @@ export async function completeConditionalWithPasskey(passkeyId: string): Promise
});
if (result.success && result.credential) {
+ /*
+ * Copy the linked TOTP code to the clipboard (if any).
+ */
+ const selected = request.passkeys.find((passkey) => passkey.id === passkeyId);
+ if (selected) {
+ await copyTotpToClipboardIfEnabled(selected.itemId);
+ }
+
request.respond({ requestId: request.requestId, credential: result.credential });
return true;
}
diff --git a/apps/browser-extension/src/entrypoints/contentScript/Form.ts b/apps/browser-extension/src/entrypoints/contentScript/Form.ts
index 1e1d302bf..71a81cff6 100644
--- a/apps/browser-extension/src/entrypoints/contentScript/Form.ts
+++ b/apps/browser-extension/src/entrypoints/contentScript/Form.ts
@@ -6,11 +6,11 @@ import { itemToCredential, FieldKey } from '@/utils/dist/core/models/vault';
import { FormDetector } from '@/utils/formDetector/FormDetector';
import { FormFiller } from '@/utils/formDetector/FormFiller';
import { DetectedFieldType } from '@/utils/formDetector/types/FormFields';
-import { LocalPreferencesService } from '@/utils/LocalPreferencesService';
import type { LastAutofilledCredential } from '@/utils/loginDetector';
import { sendMessage } from '@/utils/messaging/ExtensionMessaging';
import { ClickValidator } from '@/utils/security/ClickValidator';
import { SqliteClient } from '@/utils/SqliteClient';
+import { copyTotpToClipboardIfEnabled } from '@/utils/TotpClipboard';
/**
* Global timestamp to track popup debounce time.
@@ -146,25 +146,7 @@ export async function fillItem(item: Item, input: HTMLInputElement): Promise<voi
});
// Auto-copy TOTP to clipboard if enabled and item has TOTP after autofill.
- const autoCopyTotpEnabled = await LocalPreferencesService.getAutoCopyTotpOnAutofill();
- if (autoCopyTotpEnabled) {
- try {
- // Generate TOTP code via background
- const response = await sendMessage('GENERATE_TOTP_CODE', { itemId: item.Id });
-
- if (response.success && response.code) {
- // Copy TOTP code to clipboard
- await navigator.clipboard.writeText(response.code);
-
- // Notify background script that clipboard was copied to start countdown
- sendMessage('CLIPBOARD_COPIED').catch(() => {
- // Ignore errors as background script might not be ready
- });
- }
- } catch {
- // Silently fail in case TOTP code is not available which is possible.
- }
- }
+ await copyTotpToClipboardIfEnabled(item.Id);
}
/**
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyAuthenticate.tsx b/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyAuthenticate.tsx
index 0a86fe743..8b98c06aa 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyAuthenticate.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyAuthenticate.tsx
@@ -15,6 +15,7 @@ import { buildPasskeyAssertion } from '@/utils/passkey/PasskeyAssertionService';
import { PasskeyHelper } from '@/utils/passkey/PasskeyHelper';
import type { PendingPasskeyGetRequest } from '@/utils/passkey/types';
import { extractDomain } from '@/utils/RustCore';
+import { copyTotpToClipboardIfEnabled } from '@/utils/TotpClipboard';
/**
* PasskeyAuthenticate
@@ -27,7 +28,7 @@ const PasskeyAuthenticate: React.FC = () => {
const [request, setRequest] = useState<PendingPasskeyGetRequest | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
- const [availablePasskeys, setAvailablePasskeys] = useState<Array<{ id: string; displayName: string; rpId: string; serviceName?: string | null }>>([]);
+ const [availablePasskeys, setAvailablePasskeys] = useState<Array<{ id: string; itemId: string; displayName: string; rpId: string; serviceName?: string | null }>>([]);
const [showBypassDialog, setShowBypassDialog] = useState(false);
const { isLocked } = useVaultLockRedirect();
const firstPasskeyRef = useRef<HTMLDivElement>(null);
@@ -81,8 +82,9 @@ const PasskeyAuthenticate: React.FC = () => {
}
// Map to display format
- setAvailablePasskeys(filteredPasskeys.map((pk: { Id: string; DisplayName: string; ServiceName?: string | null; RpId: string; Username?: string | null }) => ({
+ setAvailablePasskeys(filteredPasskeys.map((pk: { Id: string; ItemId: string; DisplayName: string; ServiceName?: string | null; RpId: string; Username?: string | null }) => ({
id: pk.Id,
+ itemId: pk.ItemId,
displayName: pk.DisplayName,
serviceName: pk.ServiceName,
rpId: pk.RpId,
@@ -147,6 +149,14 @@ const PasskeyAuthenticate: React.FC = () => {
// Build the assertion from the selected passkey using the shared service.
const credential = await buildPasskeyAssertion(dbContext.sqliteClient, request, passkeyId);
+ /*
+ * Copy the linked TOTP code to the clipboard (if any).
+ */
+ const selected = availablePasskeys.find((pk) => pk.id === passkeyId);
+ if (selected) {
+ await copyTotpToClipboardIfEnabled(selected.itemId);
+ }
+
/*
* Send response back
* The background script will close the window (Safari-compatible)
diff --git a/apps/browser-extension/src/utils/TotpClipboard.ts b/apps/browser-extension/src/utils/TotpClipboard.ts
new file mode 100644
index 000000000..acc88094e
--- /dev/null
+++ b/apps/browser-extension/src/utils/TotpClipboard.ts
@@ -0,0 +1,32 @@
+import { LocalPreferencesService } from '@/utils/LocalPreferencesService';
+import { sendMessage } from '@/utils/messaging/ExtensionMessaging';
+
+/**
+ * Copy an item's current TOTP code to the clipboard (only when the user has the copy-on-fill
+ * setting enabled, which is the default).
+ *
+ * @param itemId - The ID of the item being filled.
+ */
+export async function copyTotpToClipboardIfEnabled(itemId: string): Promise<void> {
+ try {
+ if (!await LocalPreferencesService.getAutoCopyTotpOnAutofill()) {
+ return;
+ }
+
+ // Generate TOTP code via background
+ const response = await sendMessage('GENERATE_TOTP_CODE', { itemId });
+
+ if (!response.success || !response.code) {
+ return;
+ }
+
+ await navigator.clipboard.writeText(response.code);
+
+ // Notify background script that clipboard was copied to start countdown
+ sendMessage('CLIPBOARD_COPIED').catch(() => {
+ // Ignore errors as background script might not be ready
+ });
+ } catch {
+ // Silently fail if the TOTP code is not available.
+ }
+}