diff --git a/src/import.ts b/src/import.ts index 21bebdabb..c6b8d7aff 100644 --- a/src/import.ts +++ b/src/import.ts @@ -6,6 +6,7 @@ import { loadI18nMessages } from "./store/i18n"; import { Encryption } from "./models/encryption"; import { EntryStorage } from "./models/storage"; import { getOTPAuthPerLineFromOPTAuthMigration } from "./models/migration"; +import { OTPAlgorithm } from "./models/otp"; import * as CryptoJS from "crypto-js"; async function init() { @@ -311,7 +312,12 @@ export async function getEntryDataFromOTPAuthPerLine(importCode: string) { exportData[hash].digits = digits; } if (algorithm) { - exportData[hash].algorithm = algorithm; + const algorithmValue = + OTPAlgorithm[algorithm.toUpperCase() as keyof typeof OTPAlgorithm]; + exportData[hash].algorithm = + typeof algorithmValue === "number" + ? String(algorithmValue) + : algorithm; } succeededCount++; diff --git a/src/test/import.test.ts b/src/test/import.test.ts new file mode 100644 index 000000000..2292e0502 --- /dev/null +++ b/src/test/import.test.ts @@ -0,0 +1,19 @@ +import "mocha"; +import { expect } from "chai"; +import { getEntryDataFromOTPAuthPerLine } from "../import"; +import { OTPAlgorithm } from "../models/otp"; + +mocha.setup("bdd"); + +describe("OTPAuth URI imports", () => { + for (const algorithm of ["SHA256", "SHA512"] as const) { + it(`preserves the ${algorithm} algorithm`, async () => { + const result = await getEntryDataFromOTPAuthPerLine( + `otpauth://totp/alice?secret=JBSWY3DPEHPK3PXP&algorithm=${algorithm}` + ); + const entry = Object.values(result.exportData)[0]; + + expect(entry.algorithm).to.equal(String(OTPAlgorithm[algorithm])); + }); + } +});