bitwarden-estensione-browser/libs/importer/src/importers/padlock-csv-importer.ts

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

87 lines
2.7 KiB
TypeScript
Raw Normal View History

import { CollectionView } from "@bitwarden/common/vault/models/view/collection.view";
import { ImportResult } from "../models/import-result";
2022-02-22 15:39:11 +01:00
import { BaseImporter } from "./base-importer";
2022-02-22 15:39:11 +01:00
import { Importer } from "./importer";
export class PadlockCsvImporter extends BaseImporter implements Importer {
parse(data: string): Promise<ImportResult> {
const result = new ImportResult();
const results = this.parseCsv(data, false);
if (results == null) {
result.success = false;
return Promise.resolve(result);
}
let headers: string[] = null;
results.forEach((value) => {
if (headers == null) {
headers = value.map((v: string) => v);
return;
}
if (value.length < 2 || value.length !== headers.length) {
return;
2021-12-16 13:36:21 +01:00
}
if (!this.isNullOrWhitespace(value[1])) {
if (this.organization) {
const tags = (value[1] as string).split(",");
tags.forEach((tag) => {
tag = tag.trim();
let addCollection = true;
let collectionIndex = result.collections.length;
for (let i = 0; i < result.collections.length; i++) {
if (result.collections[i].name === tag) {
addCollection = false;
collectionIndex = i;
2021-12-16 13:36:21 +01:00
break;
}
}
if (addCollection) {
const collection = new CollectionView();
collection.name = tag;
result.collections.push(collection);
}
2018-07-10 23:51:47 +02:00
result.collectionRelationships.push([result.ciphers.length, collectionIndex]);
2021-12-16 13:36:21 +01:00
});
} else {
const tags = (value[1] as string).split(",");
const tag = tags.length > 0 ? tags[0].trim() : null;
this.processFolder(result, tag);
2021-12-16 13:36:21 +01:00
}
}
const cipher = this.initLoginCipher();
cipher.name = this.getValueOrDefault(value[0], "--");
for (let i = 2; i < value.length; i++) {
const header = headers[i].trim().toLowerCase();
if (this.isNullOrWhitespace(value[i]) || this.isNullOrWhitespace(header)) {
continue;
}
2018-07-10 23:51:47 +02:00
if (this.usernameFieldNames.indexOf(header) > -1) {
cipher.login.username = value[i];
} else if (this.passwordFieldNames.indexOf(header) > -1) {
cipher.login.password = value[i];
} else if (this.uriFieldNames.indexOf(header) > -1) {
cipher.login.uris = this.makeUriArray(value[i]);
2021-12-16 13:36:21 +01:00
} else {
2018-07-10 23:51:47 +02:00
this.processKvp(cipher, headers[i], value[i]);
}
2021-12-16 13:36:21 +01:00
}
this.cleanupCipher(cipher);
result.ciphers.push(cipher);
2021-12-16 13:36:21 +01:00
});
result.success = true;
return Promise.resolve(result);
}
}