Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add support for TOTP format custom properties in import flow (#2413)
Leendert de Borst327d9455f8e5
diff --git a/core/client/src/transfer/export/AliasVaultCsvExportService.ts b/core/client/src/transfer/export/AliasVaultCsvExportService.tsindex efcda3a39..8edf7b3ed 100644--- a/core/client/src/transfer/export/AliasVaultCsvExportService.ts+++ b/core/client/src/transfer/export/AliasVaultCsvExportService.ts@@ -1,6 +1,7 @@import { FieldKey, normalizeTotpAlgorithm, normalizeTotpDigits, normalizeTotpPeriod, TOTP_DEFAULT_ALGORITHM, TOTP_DEFAULT_DIGITS, TOTP_DEFAULT_PERIOD } from '@aliasvault/models/vault';import { parseDateExact } from '../shared/DateTimeUtils';+import { formatTotpUri } from '../shared/TotpUri';import { writeCsv } from './CsvWriter';@@ -124,9 +125,7 @@ export class AliasVaultCsvExportService {return totpCode.SecretKey;}- const name = totpCode.Name ?? '';- const label = encodeURIComponent(name.trim().length === 0 ? 'AliasVault' : name);- return `otpauth://totp/${label}?secret=${totpCode.SecretKey}&algorithm=${algorithm}&digits=${digits}&period=${period}`;+ return formatTotpUri(totpCode, 'AliasVault');}/**diff --git a/core/client/src/transfer/export/__tests__/AliasVaultCsvExportService.test.ts b/core/client/src/transfer/export/__tests__/AliasVaultCsvExportService.test.tsindex 28b8994ac..9ed2b6ab7 100644--- a/core/client/src/transfer/export/__tests__/AliasVaultCsvExportService.test.ts+++ b/core/client/src/transfer/export/__tests__/AliasVaultCsvExportService.test.ts@@ -1,7 +1,7 @@import { FieldKey, ItemTypes } from '@aliasvault/models/vault';import { describe, expect, it } from 'vitest';-import { byName, utc } from '../../import/__tests__/testHelpers';+import { byName, itemByName, utc } from '../../import/__tests__/testHelpers';import { AliasVaultCsvImportService } from '../../import/importers/aliasvault/AliasVaultCsvImportService';import { convertToItems } from '../../import/writers/ItemConverter';import { AliasVaultCsvExportService } from '../AliasVaultCsvExportService';@@ -111,4 +111,22 @@ describe('AliasVaultCsvExportService', () => {expect(imported[0].FolderPath).toBe('Work/Projects');expect(imported[0].TwoFactorSecret).toBe('JBSWY3DPEHPK3PXP');});++ it('writes a non-default TOTP code as an otpauth URI that re-imports with its name and parameters', () => {+ const sha512 = createTestItem('Sha512', ItemTypes.Login, {});+ addTotpCode(sha512, 'JBSWY3DPEHPK3PXP', { Name: 'GitHub: [email protected]', Algorithm: 'SHA512', Digits: 8, Period: 60 });+ const sha256 = createTestItem('Sha256', ItemTypes.Login, {});+ addTotpCode(sha256, 'JBSWY3DPEHPK3PXP', { Name: 'Work account', Algorithm: 'SHA256', Digits: 6, Period: 30 });+ const sha1 = createTestItem('Sha1', ItemTypes.Login, {});+ addTotpCode(sha1, 'JBSWY3DPEHPK3PXP');++ const imported = AliasVaultCsvImportService.importItemsFromCsv(csvText(AliasVaultCsvExportService.exportItemsToCsv([sha512, sha256, sha1])));+ const items = convertToItems(imported);++ expect(byName(imported, 'Sha1').TwoFactorSecret).toBe('JBSWY3DPEHPK3PXP');+ expect(byName(imported, 'Sha512').TwoFactorSecret).toMatch(/^otpauth:\/\/totp\//);+ expect(itemByName(items, 'Sha512').TotpCodes[0]).toMatchObject({ Name: 'GitHub: [email protected]', SecretKey: 'JBSWY3DPEHPK3PXP', Algorithm: 'SHA512', Digits: 8, Period: 60 });+ expect(itemByName(items, 'Sha256').TotpCodes[0]).toMatchObject({ Name: 'Work account', SecretKey: 'JBSWY3DPEHPK3PXP', Algorithm: 'SHA256', Digits: 6, Period: 30 });+ expect(itemByName(items, 'Sha1').TotpCodes[0]).toMatchObject({ SecretKey: 'JBSWY3DPEHPK3PXP', Algorithm: 'SHA1', Digits: 6, Period: 30 });+ });});diff --git a/core/client/src/transfer/export/__tests__/AvuxExportService.test.ts b/core/client/src/transfer/export/__tests__/AvuxExportService.test.tsindex 54cc0b823..c5a2a5fa2 100644--- a/core/client/src/transfer/export/__tests__/AvuxExportService.test.ts+++ b/core/client/src/transfer/export/__tests__/AvuxExportService.test.ts@@ -1,7 +1,7 @@import { FieldKey, FieldTypes, ItemTypes } from '@aliasvault/models/vault';import { describe, expect, it } from 'vitest';-import { byName } from '../../import/__tests__/testHelpers';+import { byName, itemByName } from '../../import/__tests__/testHelpers';import { AvuxImportService } from '../../import/importers/aliasvault/AvuxImportService';import { convertToItems } from '../../import/writers/ItemConverter';import { ZipArchive } from '../../shared/ZipArchive';@@ -61,7 +61,7 @@ describe('AvuxExportService', () => {const twoFa = byName(imported, 'Import 2FA');expect(twoFa.Username).toBe('user2fa');expect(twoFa.Password).toBe('pass2fa');- expect(twoFa.TwoFactorSecret).toBe('JBSWY3DPEHPK3PXP');+ expect(convertToItems([twoFa])[0].TotpCodes[0]).toMatchObject({ Name: 'Test TOTP', SecretKey: 'JBSWY3DPEHPK3PXP' });const creditCard = imported.find(c => c.ItemType === ItemTypes.CreditCard)!;expect(creditCard.Creditcard).toBeDefined();@@ -91,10 +91,22 @@ describe('AvuxExportService', () => {expect(credential.Notes).toBe('Reimport notes');expect(credential.Alias?.FirstName).toBe('John');expect(credential.Alias?.LastName).toBe('Doe');- expect(credential.TwoFactorSecret).toBe('JBSWY3DPEHPK3PXP');+ expect(convertToItems([credential])[0].TotpCodes[0]).toMatchObject({ Name: 'Test TOTP', SecretKey: 'JBSWY3DPEHPK3PXP' });expect(credential.CreatedAt?.getTime()).toBe(original.CreatedAt.getTime());});+ it('preserves the TOTP name and parameters when re-importing an .avux export', () => {+ const sha512 = createTestItem('Sha512', ItemTypes.Login, {});+ addTotpCode(sha512, 'JBSWY3DPEHPK3PXP', { Name: 'GitHub: [email protected]', Algorithm: 'SHA512', Digits: 8, Period: 60 });+ const sha256 = createTestItem('Sha256', ItemTypes.Login, {});+ addTotpCode(sha256, 'JBSWY3DPEHPK3PXP', { Name: '', Algorithm: 'SHA256', Digits: 7, Period: 45 });++ const items = convertToItems(AvuxImportService.importFromAvux(exportItems([sha512, sha256])));++ expect(itemByName(items, 'Sha512').TotpCodes[0]).toMatchObject({ Name: 'GitHub: [email protected]', SecretKey: 'JBSWY3DPEHPK3PXP', Algorithm: 'SHA512', Digits: 8, Period: 60 });+ expect(itemByName(items, 'Sha256').TotpCodes[0]).toMatchObject({ Name: '', SecretKey: 'JBSWY3DPEHPK3PXP', Algorithm: 'SHA256', Digits: 7, Period: 45 });+ });+it('preserves custom fields when re-importing an .avux export', () => {const item = createTestItem('Custom Field Item', ItemTypes.Login, { [FieldKey.LoginUsername]: 'customuser' });const textFieldDef: FieldDefinitionEntity = { Id: crypto.randomUUID(), Label: 'Security Question', FieldType: 'Text', IsMultiValue: false, IsHidden: false, EnableHistory: false, Weight: 3, ApplicableToTypes: null, CreatedAt: new Date(), UpdatedAt: new Date(), IsDeleted: false };diff --git a/core/client/src/transfer/export/__tests__/testHelpers.ts b/core/client/src/transfer/export/__tests__/testHelpers.tsindex 54cf153b6..9f056bc74 100644--- a/core/client/src/transfer/export/__tests__/testHelpers.ts+++ b/core/client/src/transfer/export/__tests__/testHelpers.ts@@ -1,6 +1,6 @@import { AvuxExportService } from '../AvuxExportService';-import type { FieldDefinitionEntity, FieldValueEntity, ItemEntity, LogoEntity } from '../../shared/VaultEntities';+import type { FieldDefinitionEntity, FieldValueEntity, ItemEntity, LogoEntity, TotpCodeEntity } from '../../shared/VaultEntities';import type { ItemType } from '@aliasvault/models/vault';/*@@ -92,9 +92,10 @@ export function addCustomFieldValue(item: ItemEntity, definition: FieldDefinitio* Add a TOTP code to an item.* @param item - The item* @param secretKey - The Base32 secret+ * @param options - Name and RFC 6238 parameters, defaulting to a named SHA1/6/30 code*/-export function addTotpCode(item: ItemEntity, secretKey: string): void {- item.TotpCodes.push({ Id: crypto.randomUUID(), ItemId: item.Id, Name: 'Test TOTP', SecretKey: secretKey, Algorithm: 'SHA1', Digits: 6, Period: 30, CreatedAt: item.CreatedAt, UpdatedAt: item.UpdatedAt, IsDeleted: false });+export function addTotpCode(item: ItemEntity, secretKey: string, options: Partial<Pick<TotpCodeEntity, 'Name' | 'Algorithm' | 'Digits' | 'Period'>> = {}): void {+ item.TotpCodes.push({ Id: crypto.randomUUID(), ItemId: item.Id, Name: 'Test TOTP', SecretKey: secretKey, Algorithm: 'SHA1', Digits: 6, Period: 30, ...options, CreatedAt: item.CreatedAt, UpdatedAt: item.UpdatedAt, IsDeleted: false });}/**diff --git a/core/client/src/transfer/import/importers/aliasvault/AvuxImportService.ts b/core/client/src/transfer/import/importers/aliasvault/AvuxImportService.tsindex f79772dab..4961313c3 100644--- a/core/client/src/transfer/import/importers/aliasvault/AvuxImportService.ts+++ b/core/client/src/transfer/import/importers/aliasvault/AvuxImportService.ts@@ -3,6 +3,7 @@ import { FieldTypes, isItemType, ItemTypes, type FieldType } from '@aliasvault/mimport { base64ToBytes } from '../../../../utilities/Base64';import { parseDateTime } from '../../../shared/DateTimeUtils';import { buildFolderPath } from '../../../shared/FolderPaths';+import { formatTotpUri } from '../../../shared/TotpUri';import { ZipArchive } from '../../../shared/ZipArchive';import { parseJson } from '../../readers/JsonReader';@@ -81,9 +82,10 @@ export class AvuxImportService {AvuxImportService.extractFieldValues(credential, item.fieldValues, fieldDefinitionsById);- // The import format carries one TOTP per item.+ // The import format carries one TOTP per item, as a URI so its name and parameters survive.if (item.totpCodes.length > 0) {- credential.TwoFactorSecret = item.totpCodes[0].secretKey;+ const totpCode = item.totpCodes[0];+ credential.TwoFactorSecret = formatTotpUri({ Name: totpCode.name, SecretKey: totpCode.secretKey, Algorithm: totpCode.algorithm, Digits: totpCode.digits, Period: totpCode.period });}if (item.passkeys.length > 0) {diff --git a/core/client/src/transfer/shared/TotpUri.ts b/core/client/src/transfer/shared/TotpUri.tsnew file mode 100644index 000000000..ce6a9e329--- /dev/null+++ b/core/client/src/transfer/shared/TotpUri.ts@@ -0,0 +1,26 @@+import { normalizeTotpAlgorithm, normalizeTotpDigits, normalizeTotpPeriod } from '@aliasvault/models/vault';++import type { TotpCodeEntity } from './VaultEntities';++/**+ * Format a TOTP code as an otpauth:// URI that the importer's `sanitizeTotpSecretKey` reads back with the same+ * name and parameters.+ *+ * @param totpCode - The TOTP code+ * @param blankLabel - The label to write when the code has no name+ * @returns The otpauth:// URI+ */+export function formatTotpUri(totpCode: Pick<TotpCodeEntity, 'Name' | 'SecretKey' | 'Algorithm' | 'Digits' | 'Period'>, blankLabel: string = ''): string {+ const name = (totpCode.Name ?? '').trim();+ const colonIndex = name.indexOf(':');+ const issuer = colonIndex > 0 ? name.substring(0, colonIndex).trim() : '';+ const label = issuer.length > 0 ? name.substring(colonIndex + 1).trim() : (name.length > 0 ? name : blankLabel);+ const params = new URLSearchParams({ secret: totpCode.SecretKey });+ if (issuer.length > 0) {+ params.set('issuer', issuer);+ }+ params.set('algorithm', normalizeTotpAlgorithm(totpCode.Algorithm));+ params.set('digits', String(normalizeTotpDigits(totpCode.Digits)));+ params.set('period', String(normalizeTotpPeriod(totpCode.Period)));+ return `otpauth://totp/${encodeURIComponent(label)}?${params.toString()}`;+}