Skip to content

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

Tweak TOTP display in web app and creation (#2439)

Leendert de Borstc4f9e27132f5
diff --git a/apps/browser-extension/src/entrypoints/popup/components/Items/Details/TotpEditor.tsx b/apps/browser-extension/src/entrypoints/popup/components/Items/Details/TotpEditor.tsx
index 683aab2d6..052731925 100644
--- a/apps/browser-extension/src/entrypoints/popup/components/Items/Details/TotpEditor.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/components/Items/Details/TotpEditor.tsx
@@ -1,4 +1,4 @@
-import { buildOtpAuthUri, parseOtpAuthUri } from '@aliasvault/client/items/OtpAuthUri';
+import { buildOtpAuthUri, otpAuthDisplayName, parseOtpAuthUri } from '@aliasvault/client/items/OtpAuthUri';
import { TOTP_DEFAULT_ALGORITHM, TOTP_DEFAULT_DIGITS, TOTP_DEFAULT_PERIOD } from '@aliasvault/models/vault';
import QRCode from 'qrcode';
import React, { useState } from 'react';
@@ -79,9 +79,9 @@ const TotpEditor: React.FC<TotpEditorProps> = ({
algorithm = parsed.algorithm;
digits = parsed.digits;
period = parsed.period;
- // If name is empty, use the account from the URI label
- if (!name && parsed.account) {
- name = parsed.account;
+ // If name is empty, use the issuer and account from the URI
+ if (!name) {
+ name = otpAuthDisplayName(parsed);
}
}
diff --git a/apps/mobile-app/components/items/details/TotpEditor.tsx b/apps/mobile-app/components/items/details/TotpEditor.tsx
index d540ee06e..6981aa138 100644
--- a/apps/mobile-app/components/items/details/TotpEditor.tsx
+++ b/apps/mobile-app/components/items/details/TotpEditor.tsx
@@ -13,7 +13,7 @@ import { useColors } from '@/hooks/useColorScheme';
import NativeVaultManager from '@/specs/NativeVaultManager';
import type { TotpCode } from '@aliasvault/models/vault';
import { TOTP_DEFAULT_ALGORITHM, TOTP_DEFAULT_DIGITS, TOTP_DEFAULT_PERIOD } from '@aliasvault/models/vault';
-import { buildOtpAuthUri, parseOtpAuthUri } from '@aliasvault/client/items/OtpAuthUri';
+import { buildOtpAuthUri, otpAuthDisplayName, parseOtpAuthUri } from '@aliasvault/client/items/OtpAuthUri';
type TotpFormData = {
name: string;
@@ -112,7 +112,7 @@ export const TotpEditor: React.FC<TotpEditorProps> = ({
const parsed = parseOtpAuthUri(scannedData);
if (parsed) {
const secretKey = parsed.secret.replace(/\s/g, '').replace(/=+$/, '');
- const name = parsed.label || '';
+ const name = otpAuthDisplayName(parsed);
const newTotpCode: TotpCode = {
Id: crypto.randomUUID(),
@@ -172,9 +172,9 @@ export const TotpEditor: React.FC<TotpEditorProps> = ({
algorithm = parsed.algorithm;
digits = parsed.digits;
period = parsed.period;
- // If name is empty, use the label from the URI
- if (!name && parsed.label) {
- name = parsed.label;
+ // If name is empty, use the issuer and account from the URI
+ if (!name) {
+ name = otpAuthDisplayName(parsed);
}
}
diff --git a/apps/web/src/components/totp/TotpCodes.tsx b/apps/web/src/components/totp/TotpCodes.tsx
index 18b641c9a..0f644e3c2 100644
--- a/apps/web/src/components/totp/TotpCodes.tsx
+++ b/apps/web/src/components/totp/TotpCodes.tsx
@@ -1,4 +1,4 @@
-import { buildOtpAuthUri, parseOtpAuthUri } from '@aliasvault/client/items/OtpAuthUri';
+import { buildOtpAuthUri, otpAuthDisplayName, parseOtpAuthUri } from '@aliasvault/client/items/OtpAuthUri';
import { TOTP_DEFAULT_ALGORITHM, TOTP_DEFAULT_DIGITS, TOTP_DEFAULT_PERIOD, type TotpCode } from '@aliasvault/models/vault';
import QRCode from 'qrcode';
import React, { useEffect, useRef, useState } from 'react';
@@ -42,8 +42,8 @@ const sanitizeSecretKey = (secretInput: string, nameInput: string, invalidMessag
algorithm = parsed.algorithm;
digits = parsed.digits;
period = parsed.period;
- if (name.length === 0 && parsed.account) {
- name = parsed.account;
+ if (name.length === 0) {
+ name = otpAuthDisplayName(parsed);
}
}
@@ -95,7 +95,7 @@ const TotpCodes: React.FC<TotpCodesProps> = ({ totpCodes, onTotpCodesChange, can
return;
}
try {
- const sanitized = sanitizeSecretKey(newSecret, newName, t('validation.secretKeyRequired'));
+ const sanitized = sanitizeSecretKey(newSecret, newName, t('totp.errors.invalidSecretKey'));
const code: TotpCode = { Id: crypto.randomUUID(), Name: sanitized.name, SecretKey: sanitized.secretKey, Algorithm: sanitized.algorithm, Digits: sanitized.digits, Period: sanitized.period, ItemId: '' };
onTotpCodesChange([...totpCodes, code]);
setIsAddFormVisible(false);
diff --git a/apps/web/src/components/totp/TotpViewer.tsx b/apps/web/src/components/totp/TotpViewer.tsx
index 384b5652b..909957be9 100644
--- a/apps/web/src/components/totp/TotpViewer.tsx
+++ b/apps/web/src/components/totp/TotpViewer.tsx
@@ -19,6 +19,11 @@ type TotpViewerProps = {
*/
const codeOf = (totpCode: TotpCode): string => generateTotpCode(totpCode.SecretKey, totpCode) ?? '';
+/**
+ * A code split in two halves for readability ("123 456"); an odd length is shown as-is.
+ */
+const formatCode = (code: string): string => (code.length % 2 === 0 ? `${code.slice(0, code.length / 2)} ${code.slice(code.length / 2)}` : code);
+
/**
* A single TOTP row with its countdown.
*/
@@ -40,7 +45,7 @@ const TotpRow: React.FC<{ totpCode: TotpCode; item: ItemRef; tick: number }> = (
<div className="flex items-center gap-2">
<div className="flex flex-col items-end">
<div className="totp-code text-lg font-bold text-gray-900 dark:text-white group-hover:text-primary-600 dark:group-hover:text-primary-400 transition-colors">
- {code}
+ {formatCode(code)}
</div>
<div className="text-xs">
{copied
diff --git a/core/client/src/items/OtpAuthUri.ts b/core/client/src/items/OtpAuthUri.ts
index ff6e656f3..f90ec612a 100644
--- a/core/client/src/items/OtpAuthUri.ts
+++ b/core/client/src/items/OtpAuthUri.ts
@@ -66,6 +66,18 @@ export function parseOtpAuthUri(uri: string): OtpAuthUri | null {
};
}
+/**
+ * The name for a code imported from an otpauth URI: "Issuer: account" when the URI names an issuer, else the account.
+ * @param parsed - The parsed URI
+ */
+export function otpAuthDisplayName(parsed: OtpAuthUri): string {
+ const issuer = parsed.issuer?.trim() ?? '';
+ if (issuer.length === 0) {
+ return parsed.account;
+ }
+ return parsed.account.length > 0 ? `${issuer}: ${parsed.account}` : issuer;
+}
+
/**
* Serialize a TOTP code back to an `otpauth://` URI. Non-default parameters are written out so a
* scanned QR reproduces the same codes.