Merge branch 'pr/170'

This commit is contained in:
Thomas Oberndörfer 2014-02-06 12:23:47 +01:00
commit 191eca353e
3 changed files with 23 additions and 4 deletions

View File

@ -67,7 +67,7 @@ Keyring.prototype.clear = function() {
function emailCheck(email, key) {
email = email.toLowerCase();
var keyEmails = key.getUserIds();
for (var i; i < keyEmails.length; i++) {
for (var i = 0; i < keyEmails.length; i++) {
//we need to get just the email from the userid key
keyEmail = keyEmails[i].split('<')[1].split('>')[0].trim().toLowerCase();
if (keyEmail == email) {

View File

@ -19,25 +19,34 @@
* The class that deals with storage of the keyring. Currently the only option is to use HTML5 local storage.
* @requires openpgp
* @module keyring/localstore
* @param {String} item itemname in localstore
*/
module.exports = LocalStore;
var openpgp = require('../');
function LocalStore() {
function LocalStore(item) {
if (typeof window != 'undefined' && window.localStorage) {
this.storage = window.localStorage;
} else {
this.storage = new (require('node-localstorage').LocalStorage)(openpgp.config.node_store);
}
if(typeof item == 'string') {
this.item = item;
}
}
/*
* Declare the localstore itemname
*/
LocalStore.prototype.item = 'armoredKeys';
/**
* Load the keyring from HTML5 local storage and initializes this instance.
* @return {Array<module:key~Key>} array of keys retrieved from localstore
*/
LocalStore.prototype.load = function () {
var armoredKeys = JSON.parse(this.storage.getItem('armoredKeys'));
var armoredKeys = JSON.parse(this.storage.getItem(this.item));
var keys = [];
if (armoredKeys !== null && armoredKeys.length !== 0) {
var key;
@ -59,5 +68,5 @@ LocalStore.prototype.store = function (keys) {
for (var i = 0; i < keys.length; i++) {
armoredKeys.push(keys[i].armor());
}
this.storage.setItem('armoredKeys', JSON.stringify(armoredKeys));
this.storage.setItem(this.item, JSON.stringify(armoredKeys));
};

View File

@ -87,5 +87,15 @@ describe("Keyring", function() {
expect(keys).to.exist.and.have.length(1);
done();
});
it('customize localstorage itemname', function() {
var localstore1 = new openpgp.Keyring.localstore('my-custom-name');
var localstore2 = new openpgp.Keyring.localstore('my-custom-name');
var localstore3 = new openpgp.Keyring.localstore();
localstore3.store([]);
var key = openpgp.key.readArmored(pubkey).keys[0];
localstore1.store([key]);
expect(localstore2.load()[0].primaryKey.getKeyId().equals(key.primaryKey.getKeyId())).to.be.true;
expect(localstore3.load()).to.have.length(0);
});
});