1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-23 11:56:00 +01:00

password safe importer (#1)

This commit is contained in:
Kyle Spearrin 2016-12-21 23:30:47 -05:00
parent 11787193ed
commit 6cbd618fb8
2 changed files with 106 additions and 0 deletions

View File

@ -50,6 +50,9 @@
case 'enpasscsv':
importEnpassCsv(file, success, error);
break;
case 'pwsafexml':
importPasswordSafeXml(file, success, error);
break;
default:
error();
break;
@ -1049,5 +1052,107 @@
});
}
function importPasswordSafeXml(file, success, error) {
var folders = [],
sites = [],
folderRelationships = [],
foldersIndex = [],
j = 0;
var reader = new FileReader();
reader.readAsText(file, 'utf-8');
reader.onload = function (evt) {
var xmlDoc = $.parseXML(evt.target.result),
xml = $(xmlDoc);
var pwsafe = xml.find('passwordsafe');
if (pwsafe.length) {
var notesDelimiter = pwsafe.attr('delimiter');
var entries = pwsafe.find('> entry');
if (entries.length) {
for (var i = 0; i < entries.length; i++) {
var entry = $(entries[i]);
var titleNode = entry.find('> title'),
title = titleNode.length ? $(titleNode) : null,
usernameNode = entry.find('> username'),
username = usernameNode.length ? $(usernameNode) : null,
emailNode = entry.find('> email'),
email = emailNode.length ? $(emailNode) : null,
emailText = email ? email.text() : null,
passwordNode = entry.find('> password'),
password = passwordNode.length ? $(passwordNode) : null,
urlNode = entry.find('> url'),
url = urlNode.length ? $(urlNode) : null,
notesNode = entry.find('> notes'),
notes = notesNode.length ? $(notesNode) : null,
notesText = notes ? notes.text().split(notesDelimiter).join('\n') : null,
groupNode = entry.find('> group'),
group = groupNode.length ? $(groupNode) : null,
groupText = group ? group.text().split('.').join(' > ') : null;
var folderIndex = folders.length,
siteIndex = sites.length,
hasFolder = groupText && groupText !== '',
addFolder = hasFolder;
if (hasFolder) {
for (j = 0; j < folders.length; j++) {
if (folders[j].name === groupText) {
addFolder = false;
folderIndex = j;
break;
}
}
}
var site = {
favorite: false,
uri: url && url.text() !== '' ? trimUri(url.text()) : null,
username: username && username.text() !== '' ? username.text() : null,
password: password && password.text() !== '' ? password.text() : null,
notes: notes && notesText !== '' ? notesText : null,
name: title && title.text() !== '' ? title.text() : '--',
};
if (!site.username && emailText && emailText !== '') {
site.username = emailText;
}
else if (emailText && emailText !== '') {
site.notes = site.notes === null ? 'Email: ' + emailText
: site.notes + '\n' + 'Email: ' + emailText;
}
sites.push(site);
if (addFolder) {
folders.push({
name: groupText
});
}
if (hasFolder) {
var relationship = {
key: siteIndex,
value: folderIndex
};
folderRelationships.push(relationship);
}
}
}
success(folders, sites, folderRelationships);
}
else {
error();
}
};
reader.onerror = function (evt) {
error();
};
}
return _service;
});

View File

@ -20,6 +20,7 @@
<option value="keepercsv">Keeper (csv)</option>
<option value="passworddragonxml">Password Dragon (xml)</option>
<option value="enpasscsv">Enpass (csv)</option>
<option value="pwsafexml">Password Safe (xml)</option>
</select>
</div>
<div class="form-group">