Merge pull request #35 from eordano/fix/various

Adds various small fixes to Encrypted local storage
This commit is contained in:
Matias Alejo Garcia 2014-10-27 12:11:22 -03:00
commit 5f7f60f4b9
5 changed files with 41 additions and 5 deletions

View File

@ -55,9 +55,10 @@ var defaultConfig = {
plugins: { plugins: {
//LocalStorage: true, //LocalStorage: true,
EncryptedLocalStorage: true,
//GoogleDrive: true, //GoogleDrive: true,
//InsightStorage: true //InsightStorage: true
EncryptedInsightStorage: true //EncryptedInsightStorage: true
}, },
EncryptedInsightStorage: { EncryptedInsightStorage: {

View File

@ -209,14 +209,14 @@ Identity.isAvailable = function(email, opts, cb) {
* @param {Function} cb * @param {Function} cb
*/ */
Identity.prototype.storeWallet = function(wallet, cb) { Identity.prototype.storeWallet = function(wallet, cb) {
preconditions.checkArgument(w && _.isObject(wallet)); preconditions.checkArgument(wallet && _.isObject(wallet));
var val = wallet.toObj(); var val = wallet.toObj();
var key = wallet.getStorageKey(); var key = wallet.getStorageKey();
this.storage.setItem(key, val, function(err) { this.storage.setItem(key, val, function(err) {
if (err) { if (err) {
log.debug('Wallet:' + w.getName() + ' couldnt be stored'); log.debug('Wallet:' + wallet.getName() + ' couldnt be stored');
return cb(err); return cb(err);
} }
return cb(); return cb();

View File

@ -0,0 +1,30 @@
var cryptoUtil = require('../util/crypto');
var LocalStorage = require('./LocalStorage');
var inherits = require('inherits');
function EncryptedLocalStorage(config) {
LocalStorage.apply(this, [config]);
}
inherits(EncryptedLocalStorage, LocalStorage);
EncryptedLocalStorage.prototype.getItem = function(name, callback) {
var key = cryptoUtil.kdf(this.password, this.email);
LocalStorage.prototype.getItem.apply(this, [name, function(err, body) {
var decryptedJson = cryptoUtil.decrypt(key, body);
if (!decryptedJson) {
return callback('Internal Error');
}
return callback(null, decryptedJson);
}]);
};
EncryptedLocalStorage.prototype.setItem = function(name, value, callback) {
var key = cryptoUtil.kdf(this.password, this.email);
if (!_.isString(value)) {
value = JSON.stringify(value);
}
var record = cryptoUtil.encrypt(key, value);
LocalStorage.prototype.setItem.apply(this, [name, record, callback]);
};
module.exports = EncryptedLocalStorage;

View File

@ -8,10 +8,12 @@ LocalStorage.prototype.init = function() {
}; };
LocalStorage.prototype.setCredentials = function(email, password, opts) { LocalStorage.prototype.setCredentials = function(email, password, opts) {
this.email = email;
this.password = password;
}; };
LocalStorage.prototype.getItem = function(k,cb) { LocalStorage.prototype.getItem = function(k,cb) {
return cb(localStorage.getItem(k)); return cb(null, localStorage.getItem(k));
}; };
LocalStorage.prototype.setItem = function(k,v,cb) { LocalStorage.prototype.setItem = function(k,v,cb) {
@ -36,7 +38,7 @@ LocalStorage.prototype.allKeys = function(cb) {
for(var i=0; i<l; i++) for(var i=0; i<l; i++)
ret.push(localStorage.key(i)); ret.push(localStorage.key(i));
return cb(ret); return cb(null, ret);
}; };
module.exports = LocalStorage; module.exports = LocalStorage;

View File

@ -101,6 +101,9 @@ var createBundle = function(opts) {
b.require('./js/plugins/EncryptedInsightStorage', { b.require('./js/plugins/EncryptedInsightStorage', {
expose: '../plugins/EncryptedInsightStorage' expose: '../plugins/EncryptedInsightStorage'
}); });
b.require('./js/plugins/EncryptedLocalStorage', {
expose: '../plugins/EncryptedLocalStorage'
});
} }
b.require('./config', { b.require('./config', {