1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-30 13:03:53 +01:00

padlock importer. move relationships to arrays

This commit is contained in:
Kyle Spearrin 2018-07-10 16:38:43 -04:00
parent a6a0673af8
commit 4004449aa8
7 changed files with 148 additions and 11 deletions

View File

@ -10,6 +10,8 @@ import { Utils } from '../misc/utils';
export abstract class BaseImporter {
organization = false;
protected newLineRegex = /(?:\r\n|\r|\n)/;
protected passwordFieldNames = [
'password', 'pass word', 'passphrase', 'pass phrase',
'pass', 'code', 'code word', 'codeword',
@ -145,7 +147,7 @@ export abstract class BaseImporter {
}
protected splitNewLine(str: string): string[] {
return str.split(/(?:\r\n|\r|\n)/);
return str.split(this.newLineRegex);
}
// ref https://stackoverflow.com/a/5911300
@ -207,13 +209,13 @@ export abstract class BaseImporter {
}
protected moveFoldersToCollections(result: ImportResult) {
result.folderRelationships.forEach((value, key) => result.collectionRelationships.set(key, value));
result.folderRelationships.forEach((r) => result.collectionRelationships.push(r));
result.collections = result.folders.map((f) => {
const collection = new CollectionView();
collection.name = f.name;
return collection;
});
result.folderRelationships = new Map<number, number>();
result.folderRelationships = [];
result.folders = [];
}

View File

@ -44,11 +44,11 @@ export class BitwardenCsvImporter extends BaseImporter implements Importer {
result.collections.push(collection);
}
result.collectionRelationships.set(result.ciphers.length, collectionIndex);
result.collectionRelationships.push([result.ciphers.length, collectionIndex]);
});
} else if (!this.organization) {
let folderIndex = result.folders.length;
const hasFolder = !this.organization && !this.isNullOrWhitespace(value.folder);
const hasFolder = !this.isNullOrWhitespace(value.folder);
let addFolder = hasFolder;
if (hasFolder) {
@ -68,7 +68,7 @@ export class BitwardenCsvImporter extends BaseImporter implements Importer {
}
if (hasFolder) {
result.folderRelationships.set(result.ciphers.length, folderIndex);
result.folderRelationships.push([result.ciphers.length, folderIndex]);
}
}

View File

@ -47,7 +47,7 @@ export class KeePassXCsvImporter extends BaseImporter implements Importer {
result.folders.push(f);
}
if (hasFolder) {
result.folderRelationships.set(result.ciphers.length, folderIndex);
result.folderRelationships.push([result.ciphers.length, folderIndex]);
}
const cipher = new CipherView();

View File

@ -76,7 +76,7 @@ export class LastPassCsvImporter extends BaseImporter implements Importer {
result.folders.push(f);
}
if (hasFolder) {
result.folderRelationships.set(cipherIndex, folderIndex);
result.folderRelationships.push([cipherIndex, folderIndex]);
}
});

View File

@ -0,0 +1,135 @@
import { BaseImporter } from './baseImporter';
import { Importer } from './importer';
import { ImportResult } from '../models/domain/importResult';
import { CipherView } from '../models/view/cipherView';
import { CollectionView } from '../models/view/collectionView';
import { FieldView } from '../models/view/fieldView';
import { FolderView } from '../models/view/folderView';
import { LoginView } from '../models/view/loginView';
import { CipherType } from '../enums/cipherType';
import { FieldType } from '../enums/fieldType';
export class PadlockCsvImporter extends BaseImporter implements Importer {
parse(data: string): ImportResult {
const result = new ImportResult();
const results = this.parseCsv(data, false);
if (results == null) {
result.success = false;
return 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;
}
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;
break;
}
}
if (addCollection) {
const collection = new CollectionView();
collection.name = tag;
result.collections.push(collection);
}
result.collectionRelationships.push([result.ciphers.length, collectionIndex]);
});
} else {
const tags = (value[1] as string).split(',');
let folderIndex = result.folders.length;
const hasFolder = tags.length > 0 && !this.isNullOrWhitespace(tags[0].trim());
let addFolder = hasFolder;
const tag = tags[0].trim();
if (hasFolder) {
for (let i = 0; i < result.folders.length; i++) {
if (result.folders[i].name === tag) {
addFolder = false;
folderIndex = i;
break;
}
}
}
if (addFolder) {
const f = new FolderView();
f.name = tag;
result.folders.push(f);
}
if (hasFolder) {
result.folderRelationships.push([result.ciphers.length, folderIndex]);
}
}
}
const cipher = new CipherView();
cipher.type = CipherType.Login;
cipher.favorite = false;
cipher.notes = '';
cipher.fields = [];
cipher.name = this.getValueOrDefault(value[0], '--');
cipher.login = new LoginView();
for (let i = 2; i < value.length; i++) {
const header = headers[i].trim().toLowerCase();
if (this.isNullOrWhitespace(value[i]) || this.isNullOrWhitespace(header)) {
continue;
}
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]);
} else {
if (value[i].length > 200 || value[i].search(this.newLineRegex) > -1) {
cipher.notes += (headers[i] + ': ' + value[i] + '\n');
} else {
const field = new FieldView();
field.type = FieldType.Text;
field.name = headers[i];
field.value = value[i];
cipher.fields.push(field);
}
}
}
if (this.isNullOrWhitespace(cipher.notes)) {
cipher.notes = null;
} else {
cipher.notes = cipher.notes.trim();
}
if (cipher.fields.length === 0) {
cipher.fields = null;
}
result.ciphers.push(cipher);
});
result.success = true;
return result;
}
}

View File

@ -51,7 +51,7 @@ export class SafeInCloudXmlImporter extends BaseImporter implements Importer {
if (labelIdEl != null) {
const labelId = labelIdEl.textContent;
if (!this.isNullOrWhitespace(labelId) && foldersMap.has(labelId)) {
result.folderRelationships.set(result.ciphers.length, foldersMap.get(labelId));
result.folderRelationships.push([result.ciphers.length, foldersMap.get(labelId)]);
}
}

View File

@ -7,7 +7,7 @@ export class ImportResult {
errorMessage: string;
ciphers: CipherView[] = [];
folders: FolderView[] = [];
folderRelationships: Map<number, number> = new Map<number, number>();
folderRelationships: Array<[number, number]> = [];
collections: CollectionView[] = [];
collectionRelationships: Map<number, number> = new Map<number, number>();
collectionRelationships: Array<[number, number]> = [];
}