mirror of
https://github.com/moparisthebest/mail
synced 2025-02-18 07:50:34 -05:00
use spaces in all daos and cleanup keystorage dao to use prototype style
This commit is contained in:
parent
66e0d5341b
commit
7f42722699
@ -8,22 +8,26 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
var KeychainDAO = function(cloudstorage) {
|
var KeychainDAO = function(cloudstorage) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
self._cloudstorage = cloudstorage;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an array of public keys by looking in local storage and
|
* Get an array of public keys by looking in local storage and
|
||||||
* fetching missing keys from the cloud service.
|
* fetching missing keys from the cloud service.
|
||||||
* @param ids [Array] the key ids as [{_id, userId}]
|
* @param ids [Array] the key ids as [{_id, userId}]
|
||||||
* @return [PublicKeyCollection] The requiested public keys
|
* @return [PublicKeyCollection] The requiested public keys
|
||||||
*/
|
*/
|
||||||
self.getPublicKeys = function(ids, callback) {
|
KeychainDAO.prototype.getPublicKeys = function(ids, callback) {
|
||||||
var already, pubkeys = [];
|
var self = this,
|
||||||
|
after, already, pubkeys = [];
|
||||||
|
|
||||||
var after = _.after(ids.length, function() {
|
after = _.after(ids.length, function() {
|
||||||
callback(null, pubkeys);
|
callback(null, pubkeys);
|
||||||
});
|
});
|
||||||
|
|
||||||
_.each(ids, function(i) {
|
_.each(ids, function(i) {
|
||||||
// lookup locally and in storage
|
// lookup locally and in storage
|
||||||
lookupPublicKey(i._id, function(err, pubkey) {
|
self.lookupPublicKey(i._id, function(err, pubkey) {
|
||||||
if (err || !pubkey) {
|
if (err || !pubkey) {
|
||||||
callback({
|
callback({
|
||||||
errMsg: 'Error looking up public key!',
|
errMsg: 'Error looking up public key!',
|
||||||
@ -50,7 +54,9 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
* Look up a reveiver's public key by user id
|
* Look up a reveiver's public key by user id
|
||||||
* @param userId [String] the receiver's email address
|
* @param userId [String] the receiver's email address
|
||||||
*/
|
*/
|
||||||
self.getReveiverPublicKey = function(userId, callback) {
|
KeychainDAO.prototype.getReveiverPublicKey = function(userId, callback) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
// search local keyring for public key
|
// search local keyring for public key
|
||||||
jsonDao.list('publickey', 0, null, function(allPubkeys) {
|
jsonDao.list('publickey', 0, null, function(allPubkeys) {
|
||||||
var pubkey = _.findWhere(allPubkeys, {
|
var pubkey = _.findWhere(allPubkeys, {
|
||||||
@ -60,7 +66,7 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
if (!pubkey || !pubkey._id) {
|
if (!pubkey || !pubkey._id) {
|
||||||
// no public key by that user id in storage
|
// no public key by that user id in storage
|
||||||
// find from cloud by email address
|
// find from cloud by email address
|
||||||
cloudstorage.getPublicKeyByUserId(userId, function(err, cloudPubkey) {
|
self._cloudstorage.getPublicKeyByUserId(userId, function(err, cloudPubkey) {
|
||||||
if (err || !cloudPubkey) {
|
if (err || !cloudPubkey) {
|
||||||
callback();
|
callback();
|
||||||
return;
|
return;
|
||||||
@ -69,7 +75,7 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
if (cloudPubkey && cloudPubkey._id) {
|
if (cloudPubkey && cloudPubkey._id) {
|
||||||
// there is a public key for that user already in the cloud...
|
// there is a public key for that user already in the cloud...
|
||||||
// save to local storage
|
// save to local storage
|
||||||
saveLocalPublicKey(cloudPubkey, function(err) {
|
self.saveLocalPublicKey(cloudPubkey, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
return;
|
return;
|
||||||
@ -97,7 +103,9 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
* If no key pair exists, null is returned.
|
* If no key pair exists, null is returned.
|
||||||
* return [Object] The user's key pair {publicKey, privateKey}
|
* return [Object] The user's key pair {publicKey, privateKey}
|
||||||
*/
|
*/
|
||||||
self.getUserKeyPair = function(userId, callback) {
|
KeychainDAO.prototype.getUserKeyPair = function(userId, callback) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
// search for user's public key locally
|
// search for user's public key locally
|
||||||
jsonDao.list('publickey', 0, null, function(allPubkeys) {
|
jsonDao.list('publickey', 0, null, function(allPubkeys) {
|
||||||
var pubkey = _.findWhere(allPubkeys, {
|
var pubkey = _.findWhere(allPubkeys, {
|
||||||
@ -107,7 +115,7 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
if (!pubkey || !pubkey._id) {
|
if (!pubkey || !pubkey._id) {
|
||||||
// no public key by that user id in storage
|
// no public key by that user id in storage
|
||||||
// find from cloud by email address
|
// find from cloud by email address
|
||||||
cloudstorage.getPublicKeyByUserId(userId, function(err, cloudPubkey) {
|
self._cloudstorage.getPublicKeyByUserId(userId, function(err, cloudPubkey) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
return;
|
return;
|
||||||
@ -133,14 +141,14 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
|
|
||||||
function syncKeypair(keypairId) {
|
function syncKeypair(keypairId) {
|
||||||
// persist key pair in local storage
|
// persist key pair in local storage
|
||||||
lookupPublicKey(keypairId, function(err, savedPubkey) {
|
self.lookupPublicKey(keypairId, function(err, savedPubkey) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// persist private key in local storage
|
// persist private key in local storage
|
||||||
lookupPrivateKey(keypairId, function(err, savedPrivkey) {
|
self.lookupPrivateKey(keypairId, function(err, savedPrivkey) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
return;
|
return;
|
||||||
@ -169,7 +177,9 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
* locally and in the cloud and persist arccordingly
|
* locally and in the cloud and persist arccordingly
|
||||||
* @param [Object] The user's key pair {publicKey, privateKey}
|
* @param [Object] The user's key pair {publicKey, privateKey}
|
||||||
*/
|
*/
|
||||||
self.putUserKeyPair = function(keypair, callback) {
|
KeychainDAO.prototype.putUserKeyPair = function(keypair, callback) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
// validate input
|
// validate input
|
||||||
if (!keypair || !keypair.publicKey || !keypair.privateKey || !keypair.publicKey.userId || keypair.publicKey.userId !== keypair.privateKey.userId) {
|
if (!keypair || !keypair.publicKey || !keypair.privateKey || !keypair.publicKey.userId || keypair.publicKey.userId !== keypair.privateKey.userId) {
|
||||||
callback({
|
callback({
|
||||||
@ -179,14 +189,14 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// store public key locally
|
// store public key locally
|
||||||
saveLocalPublicKey(keypair.publicKey, function(err) {
|
self.saveLocalPublicKey(keypair.publicKey, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// persist public key in cloud storage
|
// persist public key in cloud storage
|
||||||
cloudstorage.putPublicKey(keypair.publicKey, function(err) {
|
self._cloudstorage.putPublicKey(keypair.publicKey, function(err) {
|
||||||
// validate result
|
// validate result
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
@ -194,14 +204,14 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// store private key locally
|
// store private key locally
|
||||||
saveLocalPrivateKey(keypair.privateKey, function(err) {
|
self.saveLocalPrivateKey(keypair.privateKey, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// persist private key in cloud storage
|
// persist private key in cloud storage
|
||||||
cloudstorage.putPrivateKey(keypair.privateKey, function(err) {
|
self._cloudstorage.putPrivateKey(keypair.privateKey, function(err) {
|
||||||
// validate result
|
// validate result
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
@ -219,19 +229,21 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
// Helper functions
|
// Helper functions
|
||||||
//
|
//
|
||||||
|
|
||||||
function lookupPublicKey(id, callback) {
|
KeychainDAO.prototype.lookupPublicKey = function(id, callback) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
// lookup in local storage
|
// lookup in local storage
|
||||||
jsonDao.read('publickey_' + id, function(pubkey) {
|
jsonDao.read('publickey_' + id, function(pubkey) {
|
||||||
if (!pubkey) {
|
if (!pubkey) {
|
||||||
// fetch from cloud storage
|
// fetch from cloud storage
|
||||||
cloudstorage.getPublicKey(id, function(err, cloudPubkey) {
|
self._cloudstorage.getPublicKey(id, function(err, cloudPubkey) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// cache public key in cache
|
// cache public key in cache
|
||||||
saveLocalPublicKey(cloudPubkey, function(err) {
|
self.saveLocalPublicKey(cloudPubkey, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
return;
|
return;
|
||||||
@ -245,21 +257,23 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
callback(null, pubkey);
|
callback(null, pubkey);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
|
KeychainDAO.prototype.lookupPrivateKey = function(id, callback) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
function lookupPrivateKey(id, callback) {
|
|
||||||
// lookup in local storage
|
// lookup in local storage
|
||||||
jsonDao.read('privatekey_' + id, function(privkey) {
|
jsonDao.read('privatekey_' + id, function(privkey) {
|
||||||
if (!privkey) {
|
if (!privkey) {
|
||||||
// fetch from cloud storage
|
// fetch from cloud storage
|
||||||
cloudstorage.getPrivateKey(id, function(err, cloudPrivkey) {
|
self._cloudstorage.getPrivateKey(id, function(err, cloudPrivkey) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// cache private key in cache
|
// cache private key in cache
|
||||||
saveLocalPrivateKey(cloudPrivkey, function(err) {
|
self.saveLocalPrivateKey(cloudPrivkey, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
return;
|
return;
|
||||||
@ -274,9 +288,9 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
callback(null, privkey);
|
callback(null, privkey);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
function saveLocalPublicKey(pubkey, callback) {
|
KeychainDAO.prototype.saveLocalPublicKey = function(pubkey, callback) {
|
||||||
// persist public key (email, _id)
|
// persist public key (email, _id)
|
||||||
var pkLookupKey = 'publickey_' + pubkey._id;
|
var pkLookupKey = 'publickey_' + pubkey._id;
|
||||||
|
|
||||||
@ -291,9 +305,9 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
|
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
function saveLocalPrivateKey(privkey, callback) {
|
KeychainDAO.prototype.saveLocalPrivateKey = function(privkey, callback) {
|
||||||
// persist private key (email, _id)
|
// persist private key (email, _id)
|
||||||
var prkLookupKey = 'privatekey_' + privkey._id;
|
var prkLookupKey = 'privatekey_' + privkey._id;
|
||||||
|
|
||||||
@ -308,8 +322,6 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
|
|||||||
|
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return KeychainDAO;
|
return KeychainDAO;
|
||||||
|
@ -4,9 +4,8 @@
|
|||||||
define(['lawnchair', 'lawnchairSQL', 'lawnchairIDB'], function(Lawnchair) {
|
define(['lawnchair', 'lawnchairSQL', 'lawnchairIDB'], function(Lawnchair) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var self = {};
|
var self = {},
|
||||||
|
db;
|
||||||
var db;
|
|
||||||
|
|
||||||
self.init = function(dbName) {
|
self.init = function(dbName) {
|
||||||
if (!dbName) {
|
if (!dbName) {
|
||||||
|
Loading…
Reference in New Issue
Block a user