From aa04d84c11f8092077586352f4b2ca91b69c29e7 Mon Sep 17 00:00:00 2001 From: SHASHI KUMAR KASTURI <59004150+kshashikumar@users.noreply.github.com> Date: Tue, 12 Nov 2024 12:47:25 -0500 Subject: [PATCH] [PM-14627] Import TOTP with ZohoVault CSV importer (#11912) * totp secret is assigned to cipher object in zohovalut-csv-importer to populate when importing keys from zoho vault fixes #11872 closes #11872 * fixed issue#11872 * assigned full totp url to cipher object and also implemented unit tests for zohovault importer * Add test to when no data is passed to the importer * Fix import of folders - Replace "Chambername" with "Folder Name" - Add tests for importing folders and collections --------- Co-authored-by: Daniel James Smith Co-authored-by: Daniel James Smith <2670567+djsmith85@users.noreply.github.com> --- .../zohovault/sample-zohovault-data.csv.ts | 5 ++ .../spec/zohovault-csv-importer.spec.ts | 88 +++++++++++++++++++ .../src/importers/zohovault-csv-importer.ts | 4 +- 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 libs/importer/spec/test-data/zohovault/sample-zohovault-data.csv.ts create mode 100644 libs/importer/spec/zohovault-csv-importer.spec.ts diff --git a/libs/importer/spec/test-data/zohovault/sample-zohovault-data.csv.ts b/libs/importer/spec/test-data/zohovault/sample-zohovault-data.csv.ts new file mode 100644 index 0000000000..a95178ffba --- /dev/null +++ b/libs/importer/spec/test-data/zohovault/sample-zohovault-data.csv.ts @@ -0,0 +1,5 @@ +export const data = `"Password Name","Description","Password URL","SecretData","Notes","CustomData","Tags","Classification","Favorite","login_totp","Folder Name" +XYZ Test,,https://abc.xyz.de:5001/#/login,"SecretType:Web Account +User Name:email@domain.de +Password:PcY_IQEXIjKGj8YW +",,"",,E,0,otpauth://totp?secret=PI2XO5TW0DF0SHTYOVZXOOBVHFEWM6JU&algorithm=SHA1&period=30&digits=6,folderName`; diff --git a/libs/importer/spec/zohovault-csv-importer.spec.ts b/libs/importer/spec/zohovault-csv-importer.spec.ts new file mode 100644 index 0000000000..2831894529 --- /dev/null +++ b/libs/importer/spec/zohovault-csv-importer.spec.ts @@ -0,0 +1,88 @@ +import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; +import { LoginUriView } from "@bitwarden/common/vault/models/view/login-uri.view"; +import { LoginView } from "@bitwarden/common/vault/models/view/login.view"; + +import { ZohoVaultCsvImporter } from "../src/importers"; + +import { data as samplezohovaultcsvdata } from "./test-data/zohovault/sample-zohovault-data.csv"; + +const CipherData = [ + { + title: "should parse Zoho Vault CSV format", + csv: samplezohovaultcsvdata, + expected: Object.assign(new CipherView(), { + id: null, + organizationId: null, + folderId: null, + name: "XYZ Test", + login: Object.assign(new LoginView(), { + username: "email@domain.de", + password: "PcY_IQEXIjKGj8YW", + uris: [ + Object.assign(new LoginUriView(), { + uri: "https://abc.xyz.de:5001/#/login", + }), + ], + totp: "otpauth://totp?secret=PI2XO5TW0DF0SHTYOVZXOOBVHFEWM6JU&algorithm=SHA1&period=30&digits=6", + }), + type: 1, + favorite: false, + }), + }, +]; + +describe("Zoho Vault CSV Importer", () => { + it("should not succeed given no data", async () => { + const importer = new ZohoVaultCsvImporter(); + const result = await importer.parse(""); + expect(result != null).toBe(true); + expect(result.success).toBe(false); + }); + + CipherData.forEach((data) => { + it(data.title, async () => { + const importer = new ZohoVaultCsvImporter(); + const result = await importer.parse(data.csv); + expect(result != null).toBe(true); + expect(result.ciphers.length).toBeGreaterThan(0); + + const cipher = result.ciphers.shift(); + let property: keyof typeof data.expected; + for (property in data.expected) { + // eslint-disable-next-line + if (data.expected.hasOwnProperty(property)) { + // eslint-disable-next-line + expect(cipher.hasOwnProperty(property)).toBe(true); + expect(cipher[property]).toEqual(data.expected[property]); + } + } + }); + }); + + it("should create folder and assign ciphers", async () => { + const importer = new ZohoVaultCsvImporter(); + const result = await importer.parse(samplezohovaultcsvdata); + expect(result != null).toBe(true); + expect(result.success).toBe(true); + expect(result.ciphers.length).toBeGreaterThan(0); + + const folder = result.folders.shift(); + expect(folder.name).toBe("folderName"); + + expect(result.folderRelationships[0]).toEqual([0, 0]); + }); + + it("should create collection and assign ciphers when importing into an organization", async () => { + const importer = new ZohoVaultCsvImporter(); + importer.organizationId = "someOrgId"; + const result = await importer.parse(samplezohovaultcsvdata); + expect(result != null).toBe(true); + expect(result.success).toBe(true); + expect(result.ciphers.length).toBeGreaterThan(0); + + const collection = result.collections.shift(); + expect(collection.name).toBe("folderName"); + + expect(result.collectionRelationships[0]).toEqual([0, 0]); + }); +}); diff --git a/libs/importer/src/importers/zohovault-csv-importer.ts b/libs/importer/src/importers/zohovault-csv-importer.ts index 6ae4b6a88a..6a34179e86 100644 --- a/libs/importer/src/importers/zohovault-csv-importer.ts +++ b/libs/importer/src/importers/zohovault-csv-importer.ts @@ -13,7 +13,6 @@ export class ZohoVaultCsvImporter extends BaseImporter implements Importer { result.success = false; return Promise.resolve(result); } - results.forEach((value) => { if ( this.isNullOrWhitespace(value["Password Name"]) && @@ -21,7 +20,7 @@ export class ZohoVaultCsvImporter extends BaseImporter implements Importer { ) { return; } - this.processFolder(result, this.getValueOrDefault(value.ChamberName)); + this.processFolder(result, this.getValueOrDefault(value["Folder Name"])); const cipher = this.initLoginCipher(); cipher.favorite = this.getValueOrDefault(value.Favorite, "0") === "1"; cipher.notes = this.getValueOrDefault(value.Notes); @@ -32,6 +31,7 @@ export class ZohoVaultCsvImporter extends BaseImporter implements Importer { cipher.login.uris = this.makeUriArray( this.getValueOrDefault(value["Password URL"], this.getValueOrDefault(value["Secret URL"])), ); + cipher.login.totp = this.getValueOrDefault(value["login_totp"]); this.parseData(cipher, value.SecretData); this.parseData(cipher, value.CustomData); this.convertToNoteIfNeeded(cipher);