2022-10-14 18:25:50 +02:00
|
|
|
import { ImportResult } from "../models/domain/import-result";
|
|
|
|
import { CollectionView } from "../models/view/collection.view";
|
2022-02-22 15:39:11 +01:00
|
|
|
|
2022-11-11 16:20:03 +01:00
|
|
|
import { BaseImporter } from "./base-importer";
|
2022-02-22 15:39:11 +01:00
|
|
|
import { Importer } from "./importer";
|
2018-07-10 22:38:43 +02:00
|
|
|
|
|
|
|
export class PadlockCsvImporter extends BaseImporter implements Importer {
|
2020-12-05 03:05:11 +01:00
|
|
|
parse(data: string): Promise<ImportResult> {
|
2018-07-10 22:38:43 +02:00
|
|
|
const result = new ImportResult();
|
|
|
|
const results = this.parseCsv(data, false);
|
|
|
|
if (results == null) {
|
|
|
|
result.success = false;
|
2020-12-05 03:05:11 +01:00
|
|
|
return Promise.resolve(result);
|
2018-07-10 22:38:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2018-07-10 22:38:43 +02: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;
|
|
|
|
}
|
2018-07-10 22:38:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
});
|
2018-07-10 22:38:43 +02: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
|
|
|
}
|
|
|
|
}
|
2018-07-10 22:38:43 +02: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];
|
2018-07-10 22:38:43 +02:00
|
|
|
} 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]);
|
2018-07-10 22:38:43 +02:00
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2018-07-10 22:38:43 +02:00
|
|
|
|
|
|
|
this.cleanupCipher(cipher);
|
|
|
|
result.ciphers.push(cipher);
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
|
2018-07-10 22:38:43 +02:00
|
|
|
result.success = true;
|
2020-12-05 03:05:11 +01:00
|
|
|
return Promise.resolve(result);
|
2018-07-10 22:38:43 +02:00
|
|
|
}
|
|
|
|
}
|