2018-06-23 15:27:30 +02:00
|
|
|
import { BaseImporter } from './baseImporter';
|
|
|
|
import { Importer } from './importer';
|
|
|
|
|
|
|
|
import { ImportResult } from '../models/domain/importResult';
|
|
|
|
|
|
|
|
import { CipherView } from '../models/view/cipherView';
|
2018-07-05 23:29:35 +02:00
|
|
|
import { CollectionView } from '../models/view/collectionView';
|
2018-06-23 15:27:30 +02:00
|
|
|
import { FieldView } from '../models/view/fieldView';
|
|
|
|
import { FolderView } from '../models/view/folderView';
|
|
|
|
import { LoginView } from '../models/view/loginView';
|
|
|
|
import { SecureNoteView } from '../models/view/secureNoteView';
|
|
|
|
|
|
|
|
import { CipherType } from '../enums/cipherType';
|
|
|
|
import { FieldType } from '../enums/fieldType';
|
|
|
|
import { SecureNoteType } from '../enums/secureNoteType';
|
|
|
|
|
|
|
|
export class BitwardenCsvImporter extends BaseImporter implements Importer {
|
2018-07-06 01:00:00 +02:00
|
|
|
parse(data: string): ImportResult {
|
2018-06-23 15:27:30 +02:00
|
|
|
const result = new ImportResult();
|
|
|
|
const results = this.parseCsv(data, true);
|
|
|
|
if (results == null) {
|
|
|
|
result.success = false;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.forEach((value) => {
|
2018-07-06 01:00:00 +02:00
|
|
|
if (this.organization && !this.isNullOrWhitespace(value.collections)) {
|
2018-07-05 23:29:35 +02:00
|
|
|
const collections = (value.collections as string).split(',');
|
|
|
|
collections.forEach((col) => {
|
|
|
|
let addCollection = true;
|
|
|
|
let collectionIndex = result.collections.length;
|
|
|
|
|
|
|
|
for (let i = 0; i < result.collections.length; i++) {
|
|
|
|
if (result.collections[i].name === col) {
|
|
|
|
addCollection = false;
|
|
|
|
collectionIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addCollection) {
|
|
|
|
const collection = new CollectionView();
|
|
|
|
collection.name = col;
|
|
|
|
result.collections.push(collection);
|
|
|
|
}
|
|
|
|
|
2018-07-10 22:38:43 +02:00
|
|
|
result.collectionRelationships.push([result.ciphers.length, collectionIndex]);
|
2018-07-05 23:29:35 +02:00
|
|
|
});
|
2018-07-06 01:00:00 +02:00
|
|
|
} else if (!this.organization) {
|
2018-07-05 23:29:35 +02:00
|
|
|
let folderIndex = result.folders.length;
|
2018-07-10 22:38:43 +02:00
|
|
|
const hasFolder = !this.isNullOrWhitespace(value.folder);
|
2018-07-05 23:29:35 +02:00
|
|
|
let addFolder = hasFolder;
|
|
|
|
|
|
|
|
if (hasFolder) {
|
|
|
|
for (let i = 0; i < result.folders.length; i++) {
|
|
|
|
if (result.folders[i].name === value.folder) {
|
|
|
|
addFolder = false;
|
|
|
|
folderIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
2018-06-23 15:27:30 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-05 23:29:35 +02:00
|
|
|
|
|
|
|
if (addFolder) {
|
|
|
|
const f = new FolderView();
|
|
|
|
f.name = value.folder;
|
|
|
|
result.folders.push(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasFolder) {
|
2018-07-10 22:38:43 +02:00
|
|
|
result.folderRelationships.push([result.ciphers.length, folderIndex]);
|
2018-07-05 23:29:35 +02:00
|
|
|
}
|
2018-06-23 15:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const cipher = new CipherView();
|
2018-07-06 01:00:00 +02:00
|
|
|
cipher.favorite = !this.organization && this.getValueOrDefault(value.favorite, '0') !== '0' ? true : false;
|
2018-06-23 15:27:30 +02:00
|
|
|
cipher.type = CipherType.Login;
|
|
|
|
cipher.notes = this.getValueOrDefault(value.notes);
|
|
|
|
cipher.name = this.getValueOrDefault(value.name, '--');
|
|
|
|
|
|
|
|
if (!this.isNullOrWhitespace(value.fields)) {
|
|
|
|
const fields = this.splitNewLine(value.fields);
|
|
|
|
for (let i = 0; i < fields.length; i++) {
|
|
|
|
if (this.isNullOrWhitespace(fields[i])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const delimPosition = fields[i].lastIndexOf(': ');
|
|
|
|
if (delimPosition === -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cipher.fields == null) {
|
|
|
|
cipher.fields = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const field = new FieldView();
|
|
|
|
field.name = fields[i].substr(0, delimPosition);
|
|
|
|
field.value = null;
|
|
|
|
field.type = FieldType.Text;
|
|
|
|
if (fields[i].length > (delimPosition + 2)) {
|
|
|
|
field.value = fields[i].substr(delimPosition + 2);
|
|
|
|
}
|
|
|
|
cipher.fields.push(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const valueType = value.type != null ? value.type.toLowerCase() : null;
|
|
|
|
switch (valueType) {
|
|
|
|
case 'login':
|
|
|
|
case null:
|
|
|
|
cipher.type = CipherType.Login;
|
|
|
|
cipher.login = new LoginView();
|
|
|
|
cipher.login.totp = this.getValueOrDefault(value.login_totp || value.totp);
|
|
|
|
cipher.login.username = this.getValueOrDefault(value.login_username || value.username);
|
|
|
|
cipher.login.password = this.getValueOrDefault(value.login_password || value.password);
|
|
|
|
const uris = this.parseSingleRowCsv(value.login_uri || value.uri);
|
|
|
|
cipher.login.uris = this.makeUriArray(uris);
|
|
|
|
break;
|
|
|
|
case 'note':
|
|
|
|
cipher.type = CipherType.SecureNote;
|
|
|
|
cipher.secureNote = new SecureNoteView();
|
|
|
|
cipher.secureNote.type = SecureNoteType.Generic;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.ciphers.push(cipher);
|
|
|
|
});
|
|
|
|
|
2018-06-23 20:46:23 +02:00
|
|
|
result.success = true;
|
2018-06-23 15:27:30 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|