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

cleanup keychain dao

This commit is contained in:
Tankred Hase 2013-06-03 04:12:53 +02:00
parent 6224d01afd
commit cb8df440f2
3 changed files with 21 additions and 54 deletions

View File

@ -16,8 +16,8 @@
<script src="js/jqm-config.js"></script> <script src="js/jqm-config.js"></script>
<script src="lib/jquery.mobile-1.2.0.min.js"></script> <script src="lib/jquery.mobile-1.2.0.min.js"></script>
<script src="lib/lawnchair/lawnchair-git.min.js"></script> <script src="lib/lawnchair/lawnchair-git.min.js"></script>
<script src="lib/lawnchair/lawnchair-adapter-indexed-db-git.js"></script>
<script src="lib/lawnchair/lawnchair-adapter-webkit-sqlite-git.js"></script> <script src="lib/lawnchair/lawnchair-adapter-webkit-sqlite-git.js"></script>
<script src="lib/lawnchair/lawnchair-adapter-indexed-db-git.js"></script>
<script src="lib/forge/forge.rsa.bundle.js"></script> <script src="lib/forge/forge.rsa.bundle.js"></script>
<script src="lib/uuid.js"></script> <script src="lib/uuid.js"></script>

View File

@ -50,30 +50,23 @@ app.dao.KeychainDAO = function(jsonDao, cloudstorage) {
* return [Object] The user's key pair {publicKey, privateKey} * return [Object] The user's key pair {publicKey, privateKey}
*/ */
this.getUserKeyPair = function(userId, callback) { this.getUserKeyPair = function(userId, callback) {
// lookup public key id // search for user's public key
jsonDao.read('publickey_' + userId, function(pubkeyId) { jsonDao.list('publickey', 0, null, function(allPubkeys) {
if (!pubkeyId || !pubkeyId._id) { var pubkey = _.findWhere(allPubkeys, {
// no public key by that id in storage userId: userId
});
if (!pubkey) {
// no public key by that user id in storage
// TODO: find from cloud // TODO: find from cloud
// TODO: persist in local storage // TODO: persist in local storage
callback(); callback();
return; return;
} }
// lookup public key in storage and cloud // public key found
lookupPublicKey(pubkeyId._id, function(err, pubkey) { // get corresponding private key
if (err || !pubkey) { fetchEncryptedPrivateKey(pubkey);
callback({
errMsg: 'Error looking up public key!',
err: err
});
return;
}
// public key found
// get corresponding private key
fetchEncryptedPrivateKey(pubkey);
});
}); });
function fetchEncryptedPrivateKey(publicKey) { function fetchEncryptedPrivateKey(publicKey) {
@ -210,10 +203,9 @@ app.dao.KeychainDAO = function(jsonDao, cloudstorage) {
function saveLocalPublicKey(pubkey, callback) { function saveLocalPublicKey(pubkey, callback) {
// persist public key (email, _id) // persist public key (email, _id)
var pkLookupKey = 'publickey_' + pubkey.userId; var pkLookupKey = 'publickey_' + pubkey._id;
jsonDao.persist(pkLookupKey, {
_id: pubkey._id jsonDao.persist(pkLookupKey, pubkey, function(res1) {
}, function(res1) {
// validate result // validate result
if (res1.key !== pkLookupKey) { if (res1.key !== pkLookupKey) {
callback({ callback({
@ -222,28 +214,15 @@ app.dao.KeychainDAO = function(jsonDao, cloudstorage) {
return; return;
} }
// persist public key in local storage callback();
var pkKey = 'publickey_' + pubkey._id;
jsonDao.persist(pkKey, pubkey, function(res2) {
// validate result
if (res2.key !== pkKey) {
callback({
errMsg: 'Persisting public key in local storage went wrong!'
});
return;
}
callback();
});
}); });
} }
function saveLocalPrivateKey(privkey, callback) { function saveLocalPrivateKey(privkey, callback) {
// persist private key (email, _id) // persist private key (email, _id)
var prkLookupKey = 'privatekey_' + privkey.userId; var prkLookupKey = 'privatekey_' + privkey._id;
jsonDao.persist(prkLookupKey, {
_id: privkey._id jsonDao.persist(prkLookupKey, privkey, function(res1) {
}, function(res1) {
// validate result // validate result
if (res1.key !== prkLookupKey) { if (res1.key !== prkLookupKey) {
callback({ callback({
@ -252,19 +231,7 @@ app.dao.KeychainDAO = function(jsonDao, cloudstorage) {
return; return;
} }
// persist private key callback();
var prkKey = 'privatekey_' + privkey._id;
jsonDao.persist(prkKey, privkey, function(res2) {
// validate result
if (res2.key !== prkKey) {
callback({
errMsg: 'Persisting private key in local storage went wrong!'
});
return;
}
callback();
});
}); });
} }

View File

@ -21,8 +21,8 @@
<script src="../../src/lib/underscore-1.4.4.min.js"></script> <script src="../../src/lib/underscore-1.4.4.min.js"></script>
<script src="../../src/lib/backbone-1.0.0.min.js"></script> <script src="../../src/lib/backbone-1.0.0.min.js"></script>
<script src="../../src/lib/lawnchair/lawnchair-git.min.js"></script> <script src="../../src/lib/lawnchair/lawnchair-git.min.js"></script>
<script src="../../src/lib/lawnchair/lawnchair-adapter-indexed-db-git.js"></script>
<script src="../../src/lib/lawnchair/lawnchair-adapter-webkit-sqlite-git.js"></script> <script src="../../src/lib/lawnchair/lawnchair-adapter-webkit-sqlite-git.js"></script>
<script src="../../src/lib/lawnchair/lawnchair-adapter-indexed-db-git.js"></script>
<script src="../../src/lib/forge/forge.rsa.bundle.js"></script> <script src="../../src/lib/forge/forge.rsa.bundle.js"></script>
<script src="../../src/lib/uuid.js"></script> <script src="../../src/lib/uuid.js"></script>