From 5bf0890c02848a9b12668db6d8f8242f58a32fce Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Tue, 10 Feb 2015 15:21:04 +0100 Subject: [PATCH] Ignore keys from HKP server in keychain.getUserKeyPair Remove unused getPublicKeys function --- src/js/service/keychain.js | 50 ++++-------------- test/unit/service/keychain-dao-test.js | 73 ++++++++++++-------------- 2 files changed, 43 insertions(+), 80 deletions(-) diff --git a/src/js/service/keychain.js b/src/js/service/keychain.js index 162a556..4c9912d 100644 --- a/src/js/service/keychain.js +++ b/src/js/service/keychain.js @@ -57,40 +57,6 @@ Keychain.prototype.verifyPublicKey = function(uuid) { return this._publicKeyDao.verify(uuid); }; -/** - * 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 - */ -Keychain.prototype.getPublicKeys = function(ids) { - var self = this, - jobs = [], - pubkeys = []; - - ids.forEach(function(i) { - // lookup locally and in storage - var promise = self.lookupPublicKey(i._id).then(function(pubkey) { - if (!pubkey) { - throw new Error('Error looking up public key!'); - } - - // check if public key with that id has already been fetched - var already = _.findWhere(pubkeys, { - _id: i._id - }); - if (!already) { - pubkeys.push(pubkey); - } - }); - jobs.push(promise); - }); - - return Promise.all(jobs).then(function() { - return pubkeys; - }); -}; - /** * Checks for public key updates of a given user id * @param {String} options.userId The user id (email address) for which to check the key @@ -191,7 +157,7 @@ Keychain.prototype.getReceiverPublicKey = function(userId) { var pubkey = _.findWhere(allPubkeys, { userId: userId }); - // query mutliple userIds (for imported public keys) + // query mutliple userIds if (!pubkey) { for (var i = 0, match; i < allPubkeys.length; i++) { userIds = self._pgp.getKeyParams(allPubkeys[i].publicKey).userIds; @@ -636,7 +602,7 @@ Keychain.prototype.getUserKeyPair = function(userId) { userId: userId }); - if (pubkey && pubkey._id) { + if (pubkey && pubkey._id && !pubkey.source) { // that user's public key is already in local storage... // sync keypair to the cloud return syncKeypair(pubkey._id); @@ -645,13 +611,13 @@ Keychain.prototype.getUserKeyPair = function(userId) { // no public key by that user id in storage // find from cloud by email address return self._publicKeyDao.getByUserId(userId).then(function(cloudPubkey) { - if (cloudPubkey && cloudPubkey._id) { + if (cloudPubkey && cloudPubkey._id && !cloudPubkey.source) { // there is a public key for that user already in the cloud... // sync keypair to local storage return syncKeypair(cloudPubkey._id); } - // continue without keypair... generate in crypto.js + // continue without keypair... generate or import new keypair }); }); @@ -660,10 +626,12 @@ Keychain.prototype.getUserKeyPair = function(userId) { // persist key pair in local storage return self.lookupPublicKey(keypairId).then(function(pub) { savedPubkey = pub; + // persist private key in local storage - return self.lookupPrivateKey(keypairId).then(function(priv) { - savedPrivkey = priv; - }); + return self.lookupPrivateKey(keypairId); + + }).then(function(priv) { + savedPrivkey = priv; }).then(function() { var keys = {}; diff --git a/test/unit/service/keychain-dao-test.js b/test/unit/service/keychain-dao-test.js index ee4bb1d..165b941 100644 --- a/test/unit/service/keychain-dao-test.js +++ b/test/unit/service/keychain-dao-test.js @@ -417,45 +417,6 @@ describe('Keychain DAO unit tests', function() { }); }); - describe('get public keys by id', function() { - it('should fail', function(done) { - keychainDao.getPublicKeys([]).then(function(keys) { - expect(keys.length).to.equal(0); - done(); - }); - }); - - it('should fail', function(done) { - lawnchairDaoStub.read.returns(rejects(42)); - - var ids = [{ - _id: '12345' - }]; - keychainDao.getPublicKeys(ids).catch(function(err) { - expect(err).to.exist; - expect(lawnchairDaoStub.read.calledOnce).to.be.true; - done(); - }); - }); - - it('should work from local storage', function(done) { - lawnchairDaoStub.read.returns(resolves({ - _id: '12345', - publicKey: 'asdf' - })); - - var ids = [{ - _id: '12345' - }]; - keychainDao.getPublicKeys(ids).then(function(keys) { - expect(keys.length).to.equal(1); - expect(keys[0]._id).to.equal('12345'); - expect(lawnchairDaoStub.read.calledOnce).to.be.true; - done(); - }); - }); - }); - describe('get receiver public key', function() { it('should fail due to error in lawnchair list', function(done) { lawnchairDaoStub.list.returns(rejects(42)); @@ -598,6 +559,40 @@ describe('Keychain DAO unit tests', function() { expect(keys.privateKey).to.exist; expect(lawnchairDaoStub.list.calledOnce).to.be.true; expect(lawnchairDaoStub.read.calledTwice).to.be.true; + expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true; + done(); + }); + }); + + it('should work if local key is from a source other than the whiteout key server', function(done) { + lawnchairDaoStub.list.returns(resolves([{ + _id: '12345', + userId: testUser, + publicKey: 'asdf', + source: 'pgp.mit.edu' + }])); + pubkeyDaoStub.getByUserId.returns(resolves()); + + keychainDao.getUserKeyPair(testUser).then(function(keys) { + expect(keys).to.not.exist; + expect(lawnchairDaoStub.list.calledOnce).to.be.true; + expect(lawnchairDaoStub.read.called).to.be.false; + expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true; + done(); + }); + }); + + it('should work if cloud public key is from a source other than the whiteout key server', function(done) { + lawnchairDaoStub.list.returns(resolves()); + pubkeyDaoStub.getByUserId.returns(resolves({ + _id: '12345', + publicKey: 'asdf', + source: 'pgp.mit.edu' + })); + + keychainDao.getUserKeyPair(testUser).then(function(keys) { + expect(keys).to.not.exist; + expect(pubkeyDaoStub.getByUserId.calledOnce).to.be.true; done(); }); });