From cf2615bb88a6dc0e0af286a638b41e95b8e09b1d Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Mon, 27 Oct 2014 10:36:17 -0300 Subject: [PATCH] Encrypted local storage plugin --- config.js | 3 ++- js/plugins/EncryptedLocalStorage.js | 30 +++++++++++++++++++++++++++++ js/plugins/LocalStorage.js | 2 ++ util/build.js | 3 +++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 js/plugins/EncryptedLocalStorage.js diff --git a/config.js b/config.js index 72e9c6d30..3bcf7b924 100644 --- a/config.js +++ b/config.js @@ -55,9 +55,10 @@ var defaultConfig = { plugins: { //LocalStorage: true, + EncryptedLocalStorage: true, //GoogleDrive: true, //InsightStorage: true - EncryptedInsightStorage: true + //EncryptedInsightStorage: true }, EncryptedInsightStorage: { diff --git a/js/plugins/EncryptedLocalStorage.js b/js/plugins/EncryptedLocalStorage.js new file mode 100644 index 000000000..88aec147e --- /dev/null +++ b/js/plugins/EncryptedLocalStorage.js @@ -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; diff --git a/js/plugins/LocalStorage.js b/js/plugins/LocalStorage.js index b8af2ccd9..5de9aa735 100644 --- a/js/plugins/LocalStorage.js +++ b/js/plugins/LocalStorage.js @@ -8,6 +8,8 @@ LocalStorage.prototype.init = function() { }; LocalStorage.prototype.setCredentials = function(email, password, opts) { + this.email = email; + this.password = password; }; LocalStorage.prototype.getItem = function(k,cb) { diff --git a/util/build.js b/util/build.js index f89dffed0..86638335e 100644 --- a/util/build.js +++ b/util/build.js @@ -101,6 +101,9 @@ var createBundle = function(opts) { b.require('./js/plugins/EncryptedInsightStorage', { expose: '../plugins/EncryptedInsightStorage' }); + b.require('./js/plugins/EncryptedLocalStorage', { + expose: '../plugins/EncryptedLocalStorage' + }); } b.require('./config', {