bitwarden-estensione-browser/libs/common/src/importers/enpassCsvImporter.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

134 lines
4.6 KiB
TypeScript
Raw Normal View History

2018-07-12 06:11:09 +02:00
import { CipherType } from "../enums/cipherType";
import { SecureNoteType } from "../enums/secureNoteType";
2022-02-22 15:39:11 +01:00
import { ImportResult } from "../models/domain/importResult";
2018-07-12 06:11:09 +02:00
import { CardView } from "../models/view/cardView";
import { SecureNoteView } from "../models/view/secureNoteView";
2022-02-22 15:39:11 +01:00
import { BaseImporter } from "./baseImporter";
import { Importer } from "./importer";
2018-07-12 06:11:09 +02:00
export class EnpassCsvImporter extends BaseImporter implements Importer {
parse(data: string): Promise<ImportResult> {
2018-07-12 06:11:09 +02:00
const result = new ImportResult();
const results = this.parseCsv(data, false);
if (results == null) {
result.success = false;
return Promise.resolve(result);
2018-07-12 06:11:09 +02:00
}
let firstRow = true;
results.forEach((value) => {
if (value.length < 2 || (firstRow && (value[0] === "Title" || value[0] === "title"))) {
firstRow = false;
return;
2021-12-16 13:36:21 +01:00
}
2018-07-12 06:11:09 +02:00
2018-09-10 16:38:21 +02:00
const cipher = this.initLoginCipher();
cipher.notes = this.getValueOrDefault(value[value.length - 1]);
cipher.name = this.getValueOrDefault(value[0], "--");
2018-07-12 06:11:09 +02:00
2018-09-10 16:38:21 +02:00
if (
value.length === 2 ||
(!this.containsField(value, "username") &&
!this.containsField(value, "password") &&
!this.containsField(value, "email") &&
!this.containsField(value, "url"))
) {
2018-07-12 06:11:09 +02:00
cipher.type = CipherType.SecureNote;
cipher.secureNote = new SecureNoteView();
cipher.secureNote.type = SecureNoteType.Generic;
}
if (
this.containsField(value, "cardholder") &&
this.containsField(value, "number") &&
this.containsField(value, "expiry date")
) {
cipher.type = CipherType.Card;
cipher.card = new CardView();
}
if (value.length > 2 && value.length % 2 === 0) {
for (let i = 0; i < value.length - 2; i += 2) {
const fieldValue: string = value[i + 2];
if (this.isNullOrWhitespace(fieldValue)) {
continue;
2021-12-16 13:36:21 +01:00
}
2018-07-12 06:11:09 +02:00
const fieldName: string = value[i + 1];
const fieldNameLower = fieldName.toLowerCase();
if (cipher.type === CipherType.Login) {
2021-12-16 13:36:21 +01:00
if (
2018-07-12 06:11:09 +02:00
fieldNameLower === "url" &&
(cipher.login.uris == null || cipher.login.uris.length === 0)
2021-12-16 13:36:21 +01:00
) {
2018-07-12 06:11:09 +02:00
cipher.login.uris = this.makeUriArray(fieldValue);
continue;
} else if (
(fieldNameLower === "username" || fieldNameLower === "email") &&
this.isNullOrWhitespace(cipher.login.username)
2021-12-16 13:36:21 +01:00
) {
2018-07-12 06:11:09 +02:00
cipher.login.username = fieldValue;
continue;
} else if (
fieldNameLower === "password" &&
this.isNullOrWhitespace(cipher.login.password)
2021-12-16 13:36:21 +01:00
) {
2018-07-12 06:11:09 +02:00
cipher.login.password = fieldValue;
continue;
} else if (fieldNameLower === "totp" && this.isNullOrWhitespace(cipher.login.totp)) {
cipher.login.totp = fieldValue;
continue;
2018-07-12 06:11:09 +02:00
}
} else if (cipher.type === CipherType.Card) {
2021-12-16 13:36:21 +01:00
if (
2018-07-12 06:11:09 +02:00
fieldNameLower === "cardholder" &&
this.isNullOrWhitespace(cipher.card.cardholderName)
2021-12-16 13:36:21 +01:00
) {
2018-07-12 06:11:09 +02:00
cipher.card.cardholderName = fieldValue;
continue;
} else if (fieldNameLower === "number" && this.isNullOrWhitespace(cipher.card.number)) {
cipher.card.number = fieldValue;
cipher.card.brand = this.getCardBrand(fieldValue);
continue;
} else if (fieldNameLower === "cvc" && this.isNullOrWhitespace(cipher.card.code)) {
cipher.card.code = fieldValue;
continue;
} else if (
fieldNameLower === "expiry date" &&
this.isNullOrWhitespace(cipher.card.expMonth) &&
this.isNullOrWhitespace(cipher.card.expYear)
2021-12-16 13:36:21 +01:00
) {
if (this.setCardExpiration(cipher, fieldValue)) {
2018-07-12 06:11:09 +02:00
continue;
2021-12-16 13:36:21 +01:00
}
2018-07-12 06:11:09 +02:00
} else if (fieldNameLower === "type") {
// Skip since brand was determined from number above
continue;
2021-12-16 13:36:21 +01:00
}
}
2018-07-12 06:11:09 +02:00
this.processKvp(cipher, fieldName, fieldValue);
2021-12-16 13:36:21 +01:00
}
2018-07-12 06:11:09 +02:00
}
this.cleanupCipher(cipher);
result.ciphers.push(cipher);
2018-07-12 06:11:09 +02:00
});
2018-09-10 16:38:21 +02:00
2018-07-12 06:11:09 +02:00
result.success = true;
return Promise.resolve(result);
2021-12-16 13:36:21 +01:00
}
2018-09-10 16:38:21 +02:00
private containsField(fields: any[], name: string) {
if (fields == null || name == null) {
return false;
}
2021-12-16 13:36:21 +01:00
return (
2018-07-12 06:11:09 +02:00
fields.filter((f) => !this.isNullOrWhitespace(f) && f.toLowerCase() === name.toLowerCase())
2018-09-10 16:38:21 +02:00
.length > 0
2021-12-16 13:36:21 +01:00
);
}
2018-07-12 06:11:09 +02:00
}