1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-24 12:06:15 +01:00

folder helps and pwsafe xml importer

This commit is contained in:
Kyle Spearrin 2018-07-12 09:48:39 -04:00
parent 768b41153d
commit 7ca2a40478
3 changed files with 89 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import { LoginUriView } from '../models/view/loginUriView';
import { Utils } from '../misc/utils';
import { FieldView } from '../models/view/fieldView';
import { FolderView } from '../models/view/folderView';
import { LoginView } from '../models/view/loginView';
import { CipherType } from '../enums/cipherType';
@ -287,4 +288,29 @@ export abstract class BaseImporter {
cipher.fields.push(field);
}
}
protected processFolder(result: ImportResult, folderName: string) {
let folderIndex = result.folders.length;
const hasFolder = !this.isNullOrWhitespace(folderName);
let addFolder = hasFolder;
if (hasFolder) {
for (let i = 0; i < result.folders.length; i++) {
if (result.folders[i].name === folderName) {
addFolder = false;
folderIndex = i;
break;
}
}
}
if (addFolder) {
const f = new FolderView();
f.name = folderName;
result.folders.push(f);
}
if (hasFolder) {
result.folderRelationships.push([result.ciphers.length, folderIndex]);
}
}
}

View File

@ -0,0 +1,62 @@
import { BaseImporter } from './baseImporter';
import { Importer } from './importer';
import { ImportResult } from '../models/domain/importResult';
export class PasswordSafeXmlImporter extends BaseImporter implements Importer {
parse(data: string): ImportResult {
const result = new ImportResult();
const doc = this.parseXml(data);
if (doc == null) {
result.success = false;
return result;
}
const passwordSafe = doc.querySelector('passwordsafe');
if (passwordSafe == null) {
result.errorMessage = 'Missing `passwordsafe` node.';
result.success = false;
return result;
}
const notesDelimiter = passwordSafe.getAttribute('delimiter');
const entries = doc.querySelectorAll('passwordsafe > entry');
Array.from(entries).forEach((entry) => {
const group = this.querySelectorDirectChild(entry, 'group');
const groupText = group != null && !this.isNullOrWhitespace(group.textContent) ?
group.textContent.split('.').join(' > ') : null;
this.processFolder(result, groupText);
const title = this.querySelectorDirectChild(entry, 'title');
const username = this.querySelectorDirectChild(entry, 'username');
const email = this.querySelectorDirectChild(entry, 'email');
const password = this.querySelectorDirectChild(entry, 'password');
const url = this.querySelectorDirectChild(entry, 'url');
const notes = this.querySelectorDirectChild(entry, 'notes');
const cipher = this.initLoginCipher();
cipher.name = title != null ? this.getValueOrDefault(title.textContent, '--') : '--';
cipher.notes = notes != null ?
this.getValueOrDefault(notes.textContent, '').split(notesDelimiter).join('\n') : null;
cipher.login.username = username != null ? this.getValueOrDefault(username.textContent) : null;
cipher.login.password = password != null ? this.getValueOrDefault(password.textContent) : null;
cipher.login.uris = url != null ? this.makeUriArray(url.textContent) : null;
if (this.isNullOrWhitespace(cipher.login.username) && email != null) {
cipher.login.username = this.getValueOrDefault(email.textContent);
} else if (email != null && !this.isNullOrWhitespace(email.textContent)) {
cipher.notes = this.isNullOrWhitespace(cipher.notes) ? 'Email: ' + email.textContent
: (cipher.notes + '\n' + 'Email: ' + email.textContent);
}
this.cleanupCipher(cipher);
result.ciphers.push(cipher);
});
if (this.organization) {
this.moveFoldersToCollections(result);
}
result.success = true;
return result;
}
}

View File

@ -1,4 +1,4 @@
import { CipherString } from '../domain/cipherString';
import { CipherString } from './cipherString';
import { View } from '../view/view';