1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-26 02:42:17 -05:00

use spaces in all daos and cleanup keystorage dao to use prototype style

This commit is contained in:
Tankred Hase 2013-08-16 21:21:24 +02:00
parent 66e0d5341b
commit 7f42722699
3 changed files with 431 additions and 420 deletions

View File

@ -8,22 +8,26 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
var KeychainDAO = function(cloudstorage) {
var self = this;
self._cloudstorage = cloudstorage;
};
/**
* Get an array of public keys by looking in local storage and
* fetching missing keys from the cloud service.
* @param ids [Array] the key ids as [{_id, userId}]
* @return [PublicKeyCollection] The requiested public keys
*/
self.getPublicKeys = function(ids, callback) {
var already, pubkeys = [];
KeychainDAO.prototype.getPublicKeys = function(ids, callback) {
var self = this,
after, already, pubkeys = [];
var after = _.after(ids.length, function() {
after = _.after(ids.length, function() {
callback(null, pubkeys);
});
_.each(ids, function(i) {
// lookup locally and in storage
lookupPublicKey(i._id, function(err, pubkey) {
self.lookupPublicKey(i._id, function(err, pubkey) {
if (err || !pubkey) {
callback({
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
* @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
jsonDao.list('publickey', 0, null, function(allPubkeys) {
var pubkey = _.findWhere(allPubkeys, {
@ -60,7 +66,7 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
if (!pubkey || !pubkey._id) {
// no public key by that user id in storage
// find from cloud by email address
cloudstorage.getPublicKeyByUserId(userId, function(err, cloudPubkey) {
self._cloudstorage.getPublicKeyByUserId(userId, function(err, cloudPubkey) {
if (err || !cloudPubkey) {
callback();
return;
@ -69,7 +75,7 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
if (cloudPubkey && cloudPubkey._id) {
// there is a public key for that user already in the cloud...
// save to local storage
saveLocalPublicKey(cloudPubkey, function(err) {
self.saveLocalPublicKey(cloudPubkey, function(err) {
if (err) {
callback(err);
return;
@ -97,7 +103,9 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
* If no key pair exists, null is returned.
* 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
jsonDao.list('publickey', 0, null, function(allPubkeys) {
var pubkey = _.findWhere(allPubkeys, {
@ -107,7 +115,7 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
if (!pubkey || !pubkey._id) {
// no public key by that user id in storage
// find from cloud by email address
cloudstorage.getPublicKeyByUserId(userId, function(err, cloudPubkey) {
self._cloudstorage.getPublicKeyByUserId(userId, function(err, cloudPubkey) {
if (err) {
callback(err);
return;
@ -133,14 +141,14 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
function syncKeypair(keypairId) {
// persist key pair in local storage
lookupPublicKey(keypairId, function(err, savedPubkey) {
self.lookupPublicKey(keypairId, function(err, savedPubkey) {
if (err) {
callback(err);
return;
}
// persist private key in local storage
lookupPrivateKey(keypairId, function(err, savedPrivkey) {
self.lookupPrivateKey(keypairId, function(err, savedPrivkey) {
if (err) {
callback(err);
return;
@ -169,7 +177,9 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
* locally and in the cloud and persist arccordingly
* @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
if (!keypair || !keypair.publicKey || !keypair.privateKey || !keypair.publicKey.userId || keypair.publicKey.userId !== keypair.privateKey.userId) {
callback({
@ -179,14 +189,14 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
}
// store public key locally
saveLocalPublicKey(keypair.publicKey, function(err) {
self.saveLocalPublicKey(keypair.publicKey, function(err) {
if (err) {
callback(err);
return;
}
// persist public key in cloud storage
cloudstorage.putPublicKey(keypair.publicKey, function(err) {
self._cloudstorage.putPublicKey(keypair.publicKey, function(err) {
// validate result
if (err) {
callback(err);
@ -194,14 +204,14 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
}
// store private key locally
saveLocalPrivateKey(keypair.privateKey, function(err) {
self.saveLocalPrivateKey(keypair.privateKey, function(err) {
if (err) {
callback(err);
return;
}
// persist private key in cloud storage
cloudstorage.putPrivateKey(keypair.privateKey, function(err) {
self._cloudstorage.putPrivateKey(keypair.privateKey, function(err) {
// validate result
if (err) {
callback(err);
@ -219,19 +229,21 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
// Helper functions
//
function lookupPublicKey(id, callback) {
KeychainDAO.prototype.lookupPublicKey = function(id, callback) {
var self = this;
// lookup in local storage
jsonDao.read('publickey_' + id, function(pubkey) {
if (!pubkey) {
// fetch from cloud storage
cloudstorage.getPublicKey(id, function(err, cloudPubkey) {
self._cloudstorage.getPublicKey(id, function(err, cloudPubkey) {
if (err) {
callback(err);
return;
}
// cache public key in cache
saveLocalPublicKey(cloudPubkey, function(err) {
self.saveLocalPublicKey(cloudPubkey, function(err) {
if (err) {
callback(err);
return;
@ -245,21 +257,23 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
callback(null, pubkey);
}
});
}
};
KeychainDAO.prototype.lookupPrivateKey = function(id, callback) {
var self = this;
function lookupPrivateKey(id, callback) {
// lookup in local storage
jsonDao.read('privatekey_' + id, function(privkey) {
if (!privkey) {
// fetch from cloud storage
cloudstorage.getPrivateKey(id, function(err, cloudPrivkey) {
self._cloudstorage.getPrivateKey(id, function(err, cloudPrivkey) {
if (err) {
callback(err);
return;
}
// cache private key in cache
saveLocalPrivateKey(cloudPrivkey, function(err) {
self.saveLocalPrivateKey(cloudPrivkey, function(err) {
if (err) {
callback(err);
return;
@ -274,9 +288,9 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
callback(null, privkey);
}
});
}
};
function saveLocalPublicKey(pubkey, callback) {
KeychainDAO.prototype.saveLocalPublicKey = function(pubkey, callback) {
// persist public key (email, _id)
var pkLookupKey = 'publickey_' + pubkey._id;
@ -291,9 +305,9 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
callback();
});
}
};
function saveLocalPrivateKey(privkey, callback) {
KeychainDAO.prototype.saveLocalPrivateKey = function(privkey, callback) {
// persist private key (email, _id)
var prkLookupKey = 'privatekey_' + privkey._id;
@ -308,8 +322,6 @@ define(['underscore', 'js/dao/lawnchair-dao'], function(_, jsonDao) {
callback();
});
}
};
return KeychainDAO;

View File

@ -4,9 +4,8 @@
define(['lawnchair', 'lawnchairSQL', 'lawnchairIDB'], function(Lawnchair) {
'use strict';
var self = {};
var db;
var self = {},
db;
self.init = function(dbName) {
if (!dbName) {