2022-02-22 15:39:11 +01:00
|
|
|
import { CipherRepromptType } from "../enums/cipherRepromptType";
|
|
|
|
import { CipherType } from "../enums/cipherType";
|
|
|
|
import { FieldType } from "../enums/fieldType";
|
|
|
|
import { SecureNoteType } from "../enums/secureNoteType";
|
2018-06-23 15:27:30 +02:00
|
|
|
import { ImportResult } from "../models/domain/importResult";
|
|
|
|
import { CipherView } from "../models/view/cipherView";
|
2018-07-05 23:29:35 +02:00
|
|
|
import { CollectionView } from "../models/view/collectionView";
|
2018-06-23 15:27:30 +02:00
|
|
|
import { FieldView } from "../models/view/fieldView";
|
|
|
|
import { LoginView } from "../models/view/loginView";
|
|
|
|
import { SecureNoteView } from "../models/view/secureNoteView";
|
|
|
|
|
2022-02-22 15:39:11 +01:00
|
|
|
import { BaseImporter } from "./baseImporter";
|
|
|
|
import { Importer } from "./importer";
|
2018-06-23 15:27:30 +02:00
|
|
|
|
|
|
|
export class BitwardenCsvImporter extends BaseImporter implements Importer {
|
2020-12-05 03:05:11 +01:00
|
|
|
parse(data: string): Promise<ImportResult> {
|
2018-06-23 15:27:30 +02:00
|
|
|
const result = new ImportResult();
|
2018-07-05 23:29:35 +02:00
|
|
|
const results = this.parseCsv(data, true);
|
|
|
|
if (results == null) {
|
2018-07-06 01:00:00 +02:00
|
|
|
result.success = false;
|
2021-05-05 09:54:17 +02:00
|
|
|
return Promise.resolve(result);
|
|
|
|
}
|
2018-06-23 15:27:30 +02:00
|
|
|
|
|
|
|
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;
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-06-23 15:27:30 +02:00
|
|
|
for (let i = 0; i < result.collections.length; i++) {
|
|
|
|
if (result.collections[i].name === col) {
|
|
|
|
addCollection = false;
|
|
|
|
collectionIndex = i;
|
2021-12-16 13:36:21 +01:00
|
|
|
break;
|
2018-06-23 15:27:30 +02:00
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2018-06-23 15:27:30 +02:00
|
|
|
|
|
|
|
if (addCollection) {
|
2018-10-25 14:20:31 +02:00
|
|
|
const collection = new CollectionView();
|
2018-06-23 15:27:30 +02:00
|
|
|
collection.name = col;
|
|
|
|
result.collections.push(collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.collectionRelationships.push([result.ciphers.length, collectionIndex]);
|
|
|
|
});
|
2018-07-06 01:00:00 +02:00
|
|
|
} else if (!this.organization) {
|
2018-07-12 15:57:08 +02:00
|
|
|
this.processFolder(result, value.folder);
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-06-23 15:27:30 +02:00
|
|
|
const cipher = new CipherView();
|
2018-07-06 01:00:00 +02:00
|
|
|
cipher.favorite =
|
|
|
|
!this.organization && this.getValueOrDefault(value.favorite, "0") !== "0" ? true : false;
|
2018-06-23 15:27:30 +02:00
|
|
|
cipher.type = CipherType.Login;
|
|
|
|
cipher.notes = this.getValueOrDefault(value.notes);
|
|
|
|
cipher.name = this.getValueOrDefault(value.name, "--");
|
2021-12-16 13:36:21 +01:00
|
|
|
try {
|
2021-05-05 09:54:17 +02:00
|
|
|
cipher.reprompt = parseInt(
|
|
|
|
this.getValueOrDefault(value.reprompt, CipherRepromptType.None.toString()),
|
2021-12-16 13:36:21 +01:00
|
|
|
10
|
|
|
|
);
|
2021-05-05 09:54:17 +02:00
|
|
|
} catch (e) {
|
2022-02-22 15:39:11 +01:00
|
|
|
// eslint-disable-next-line
|
2021-05-05 09:54:17 +02:00
|
|
|
console.error("Unable to parse reprompt value", e);
|
|
|
|
cipher.reprompt = CipherRepromptType.None;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-07-06 01:00:00 +02:00
|
|
|
if (!this.isNullOrWhitespace(value.fields)) {
|
2018-06-23 15:27:30 +02:00
|
|
|
const fields = this.splitNewLine(value.fields);
|
2018-07-05 23:29:35 +02:00
|
|
|
for (let i = 0; i < fields.length; i++) {
|
2018-06-23 15:27:30 +02:00
|
|
|
if (this.isNullOrWhitespace(fields[i])) {
|
|
|
|
continue;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-06-23 15:27:30 +02:00
|
|
|
const delimPosition = fields[i].lastIndexOf(": ");
|
|
|
|
if (delimPosition === -1) {
|
|
|
|
continue;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-06-23 15:27:30 +02:00
|
|
|
if (cipher.fields == null) {
|
|
|
|
cipher.fields = [];
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-06-23 15:27:30 +02:00
|
|
|
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);
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2018-06-23 15:27:30 +02:00
|
|
|
cipher.fields.push(field);
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 15:27:30 +02:00
|
|
|
const valueType = value.type != null ? value.type.toLowerCase() : null;
|
|
|
|
switch (valueType) {
|
2021-12-16 13:36:21 +01:00
|
|
|
case "note":
|
2018-10-25 14:20:31 +02:00
|
|
|
cipher.type = CipherType.SecureNote;
|
|
|
|
cipher.secureNote = new SecureNoteView();
|
|
|
|
cipher.secureNote.type = SecureNoteType.Generic;
|
2021-12-16 13:36:21 +01:00
|
|
|
break;
|
2022-02-22 15:39:11 +01:00
|
|
|
default: {
|
2018-06-23 15:27:30 +02:00
|
|
|
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);
|
2021-12-16 13:36:21 +01:00
|
|
|
break;
|
2022-02-22 15:39:11 +01:00
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-07-10 22:38:43 +02:00
|
|
|
result.ciphers.push(cipher);
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
|
2018-06-23 20:46:23 +02:00
|
|
|
result.success = true;
|
2020-12-05 03:05:11 +01:00
|
|
|
return Promise.resolve(result);
|
2018-06-23 15:27:30 +02:00
|
|
|
}
|
|
|
|
}
|