1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-25 20:53:22 +00:00
Files
browser/libs/importer/src/importers/bitwarden/bitwarden-csv-importer.ts
Vincent Salucci bacb8828de [AC-1266] Enums filename conventions (#5140)
* refactor: update clientType enum

* refactor: update deviceType filename

* refactor: update encryptedExportType filename

* refactor: update encryptionType filename

* refactor: update eventType filename

* refactor: update fieldType filename

* refactor: update fileUploadType filename

* refactor: update hashPurpose filename

* refactor: update htmlStorageLocation filename

* refactor: update kdfType filename

* refactor: update keySuffixOptions filename

* refactor: update linkedIdType filename

* refactor: update logLevelType filename

* refactor: update nativeMessagingVersion filename

* refactor: update notificationType filename

* refactor: update productType filename

* refactor: update secureNoteType filename

* refactor: update stateVersion filename

* refactor: update storageLocation filename

* refactor: update themeType filename

* refactor: update uriMatchType filename

* fix: update kdfType classes missed in initial pass, refs AC-1266

* fix: missing import update for device-type

* refactor: add barrel file for enums and update pathed import statements, refs AC-1266

* fix: incorrect import statements for web, refs AC-1266

* fix: missed import statement updates (browser), refs AC-1266

* fix: missed import statement changes (cli), refs AC-1266

* fix: missed import statement changes (desktop), refs AC-1266

* fix: prettier, refs AC-1266

* refactor: (libs) update relative paths to use barrel file, refs AC-1266

* fix: missed find/replace import statements for SecureNoteType, refs AC-1266

* refactor: apply .enum suffix to enums folder and modify leftover relative paths, refs AC-1266

* fix: find/replace errors for native-messaging-version, refs AC-1266
2023-04-04 22:42:21 -05:00

120 lines
4.4 KiB
TypeScript

import { CollectionView } from "@bitwarden/common/admin-console/models/view/collection.view";
import { FieldType, SecureNoteType } from "@bitwarden/common/enums";
import { CipherRepromptType } from "@bitwarden/common/vault/enums/cipher-reprompt-type";
import { CipherType } from "@bitwarden/common/vault/enums/cipher-type";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { FieldView } from "@bitwarden/common/vault/models/view/field.view";
import { LoginView } from "@bitwarden/common/vault/models/view/login.view";
import { SecureNoteView } from "@bitwarden/common/vault/models/view/secure-note.view";
import { ImportResult } from "../../models/import-result";
import { BaseImporter } from "../base-importer";
import { Importer } from "../importer";
export class BitwardenCsvImporter extends BaseImporter implements Importer {
parse(data: string): Promise<ImportResult> {
const result = new ImportResult();
const results = this.parseCsv(data, true);
if (results == null) {
result.success = false;
return Promise.resolve(result);
}
results.forEach((value) => {
if (this.organization && !this.isNullOrWhitespace(value.collections)) {
const collections = (value.collections as string).split(",");
collections.forEach((col) => {
let addCollection = true;
let collectionIndex = result.collections.length;
for (let i = 0; i < result.collections.length; i++) {
if (result.collections[i].name === col) {
addCollection = false;
collectionIndex = i;
break;
}
}
if (addCollection) {
const collection = new CollectionView();
collection.name = col;
result.collections.push(collection);
}
result.collectionRelationships.push([result.ciphers.length, collectionIndex]);
});
} else if (!this.organization) {
this.processFolder(result, value.folder);
}
const cipher = new CipherView();
cipher.favorite =
!this.organization && this.getValueOrDefault(value.favorite, "0") !== "0" ? true : false;
cipher.type = CipherType.Login;
cipher.notes = this.getValueOrDefault(value.notes);
cipher.name = this.getValueOrDefault(value.name, "--");
try {
cipher.reprompt = parseInt(
this.getValueOrDefault(value.reprompt, CipherRepromptType.None.toString()),
10
);
} catch (e) {
// eslint-disable-next-line
console.error("Unable to parse reprompt value", e);
cipher.reprompt = CipherRepromptType.None;
}
if (!this.isNullOrWhitespace(value.fields)) {
const fields = this.splitNewLine(value.fields);
for (let i = 0; i < fields.length; i++) {
if (this.isNullOrWhitespace(fields[i])) {
continue;
}
const delimPosition = fields[i].lastIndexOf(": ");
if (delimPosition === -1) {
continue;
}
if (cipher.fields == null) {
cipher.fields = [];
}
const field = new FieldView();
field.name = fields[i].substr(0, delimPosition);
field.value = null;
field.type = FieldType.Text;
if (fields[i].length > delimPosition + 2) {
field.value = fields[i].substr(delimPosition + 2);
}
cipher.fields.push(field);
}
}
const valueType = value.type != null ? value.type.toLowerCase() : null;
switch (valueType) {
case "note":
cipher.type = CipherType.SecureNote;
cipher.secureNote = new SecureNoteView();
cipher.secureNote.type = SecureNoteType.Generic;
break;
default: {
cipher.type = CipherType.Login;
cipher.login = new LoginView();
cipher.login.totp = this.getValueOrDefault(value.login_totp || value.totp);
cipher.login.username = this.getValueOrDefault(value.login_username || value.username);
cipher.login.password = this.getValueOrDefault(value.login_password || value.password);
const uris = this.parseSingleRowCsv(value.login_uri || value.uri);
cipher.login.uris = this.makeUriArray(uris);
break;
}
}
result.ciphers.push(cipher);
});
result.success = true;
return Promise.resolve(result);
}
}