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

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

104 lines
3.5 KiB
TypeScript
Raw Normal View History

2018-09-21 19:54:06 +02:00
import { ImportResult } from "../models/domain/importResult";
import { CollectionView } from "../models/view/collectionView";
2022-02-22 15:39:11 +01:00
import { BaseImporter } from "./baseImporter";
import { Importer } from "./importer";
2018-09-21 19:54:06 +02:00
export class PasspackCsvImporter extends BaseImporter implements Importer {
parse(data: string): Promise<ImportResult> {
2018-09-21 19:54:06 +02:00
const result = new ImportResult();
const results = this.parseCsv(data, true);
if (results == null) {
result.success = false;
return Promise.resolve(result);
}
2018-09-21 19:54:06 +02:00
results.forEach((value) => {
const tagsJson = !this.isNullOrWhitespace(value.Tags) ? JSON.parse(value.Tags) : null;
const tags: string[] =
tagsJson != null && tagsJson.tags != null && tagsJson.tags.length > 0
? tagsJson.tags
.map((tagJson: string) => {
2021-12-16 13:36:21 +01:00
try {
2018-09-21 19:54:06 +02:00
const t = JSON.parse(tagJson);
return this.getValueOrDefault(t.tag);
} catch {
2018-09-21 19:54:06 +02:00
// Ignore error
2021-12-16 13:36:21 +01:00
}
2018-09-21 19:54:06 +02:00
return null;
})
.filter((t: string) => !this.isNullOrWhitespace(t))
2021-12-16 13:36:21 +01:00
: null;
2018-09-21 19:54:06 +02:00
if (this.organization && tags != null && tags.length > 0) {
tags.forEach((tag) => {
2018-09-21 19:54:06 +02:00
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;
break;
}
2021-12-16 13:36:21 +01:00
}
2018-09-21 19:54:06 +02:00
if (addCollection) {
const collection = new CollectionView();
collection.name = tag;
result.collections.push(collection);
2021-12-16 13:36:21 +01:00
}
2018-09-21 19:54:06 +02:00
result.collectionRelationships.push([result.ciphers.length, collectionIndex]);
2021-12-16 13:36:21 +01:00
});
2018-09-21 19:54:06 +02:00
} else if (!this.organization && tags != null && tags.length > 0) {
this.processFolder(result, tags[0]);
}
const cipher = this.initLoginCipher();
cipher.notes = this.getValueOrDefault(value.Notes, "");
cipher.notes += "\n\n" + this.getValueOrDefault(value["Shared Notes"], "") + "\n";
cipher.name = this.getValueOrDefault(value["Entry Name"], "--");
cipher.login.username = this.getValueOrDefault(value["User ID"]);
cipher.login.password = this.getValueOrDefault(value.Password);
cipher.login.uris = this.makeUriArray(value.URL);
if (value.__parsed_extra != null && value.__parsed_extra.length > 0) {
value.__parsed_extra.forEach((extra: string) => {
if (!this.isNullOrWhitespace(extra)) {
cipher.notes += "\n" + extra;
2021-12-16 13:36:21 +01:00
}
2018-09-21 19:54:06 +02:00
});
2021-12-16 13:36:21 +01:00
}
2018-09-21 19:54:06 +02:00
const fieldsJson = !this.isNullOrWhitespace(value["Extra Fields"])
? JSON.parse(value["Extra Fields"])
2021-12-16 13:36:21 +01:00
: null;
2018-09-21 19:54:06 +02:00
const fields =
fieldsJson != null && fieldsJson.extraFields != null && fieldsJson.extraFields.length > 0
? fieldsJson.extraFields.map((fieldJson: string) => {
2021-12-16 13:36:21 +01:00
try {
2018-09-21 19:54:06 +02:00
return JSON.parse(fieldJson);
} catch {
// Ignore error
2021-12-16 13:36:21 +01:00
}
2018-09-21 19:54:06 +02:00
return null;
2021-12-16 13:36:21 +01:00
})
: null;
2018-09-21 19:54:06 +02:00
if (fields != null) {
fields.forEach((f: any) => {
if (f != null) {
this.processKvp(cipher, f.name, f.data);
2021-12-16 13:36:21 +01:00
}
});
}
2018-09-21 19:54:06 +02:00
this.cleanupCipher(cipher);
result.ciphers.push(cipher);
2021-12-16 13:36:21 +01:00
});
2018-09-21 19:54:06 +02:00
result.success = true;
return Promise.resolve(result);
2018-09-21 19:54:06 +02:00
}
}