diff --git a/src/js/controller/privatekey-upload.js b/src/js/controller/privatekey-upload.js index 7ba31fa..fe7cc14 100644 --- a/src/js/controller/privatekey-upload.js +++ b/src/js/controller/privatekey-upload.js @@ -60,9 +60,9 @@ define(function(require) { $scope.checkServerForKey = function(callback) { var keyParams = pgp.getKeyParams(); - keychain.requestPrivateKeyDownload({ + keychain.hasPrivateKey({ userId: keyParams.userId, - keyId: keyParams._id, + keyId: keyParams._id }, function(err, privateKeySynced) { if (err) { $scope.onError(err); diff --git a/src/js/dao/keychain-dao.js b/src/js/dao/keychain-dao.js index 92bc4bf..7b070c1 100644 --- a/src/js/dao/keychain-dao.js +++ b/src/js/dao/keychain-dao.js @@ -641,6 +641,16 @@ define(function(require) { this._privateKeyDao.requestDownload(options, callback); }; + /** + * Query if an encrypted private PGP key exists on the server without initializing the recovery procedure + * @param {String} options.userId The user's email address + * @param {String} options.keyId The private PGP key id + * @param {Function} callback(error) + */ + KeychainDAO.prototype.hasPrivateKey = function(options, callback) { + this._privateKeyDao.hasPrivateKey(options, callback); + }; + /** * Download the encrypted private PGP key from the server using the recovery token. * @param {String} options.userId The user's email address diff --git a/src/js/dao/privatekey-dao.js b/src/js/dao/privatekey-dao.js index 782965c..5d8ba49 100644 --- a/src/js/dao/privatekey-dao.js +++ b/src/js/dao/privatekey-dao.js @@ -111,23 +111,51 @@ define(function() { }; /** - * Request download for the encrypted private PGP key. + * Query if an encrypted private PGP key exists on the server without initializing the recovery procedure. * @param {String} options.userId The user's email address * @param {String} options.keyId The private PGP key id * @param {Function} callback(error, found) - * @return {Boolean} weather the key was found on the server or not. + * @return {Boolean} whether the key was found on the server or not. */ - PrivateKeyDAO.prototype.requestDownload = function(options, callback) { - var uri; - + PrivateKeyDAO.prototype.hasPrivateKey = function(options, callback) { if (!options.userId || !options.keyId) { callback(new Error('Incomplete arguments!')); return; } - uri = '/privatekey/user/' + options.userId + '/key/' + options.keyId; this._restDao.get({ - uri: uri + uri: '/privatekey/user/' + options.userId + '/key/' + options.keyId + '?ignoreRecovery=true', + }, function(err) { + // 404: there is no encrypted private key on the server + if (err && err.code !== 200) { + callback(null, false); + return; + } + + if (err) { + callback(err); + return; + } + + callback(null, true); + }); + }; + + /** + * Request download for the encrypted private PGP key. + * @param {String} options.userId The user's email address + * @param {String} options.keyId The private PGP key id + * @param {Function} callback(error, found) + * @return {Boolean} whether the key was found on the server or not. + */ + PrivateKeyDAO.prototype.requestDownload = function(options, callback) { + if (!options.userId || !options.keyId) { + callback(new Error('Incomplete arguments!')); + return; + } + + this._restDao.get({ + uri: '/privatekey/user/' + options.userId + '/key/' + options.keyId }, function(err) { // 404: there is no encrypted private key on the server if (err && err.code !== 200) { diff --git a/test/unit/keychain-dao-test.js b/test/unit/keychain-dao-test.js index 716264c..c05cef1 100644 --- a/test/unit/keychain-dao-test.js +++ b/test/unit/keychain-dao-test.js @@ -1202,7 +1202,8 @@ define(function(require) { describe('requestPrivateKeyDownload', function() { it('should work', function(done) { var options = { - userId: testUser + userId: testUser, + keyId: 'someId' }; privkeyDaoStub.requestDownload.withArgs(options).yields(); @@ -1210,6 +1211,18 @@ define(function(require) { }); }); + describe('hasPrivateKey', function() { + it('should work', function(done) { + var options = { + userId: testUser, + keyId: 'someId' + }; + + privkeyDaoStub.hasPrivateKey.withArgs(options).yields(); + keychainDao.hasPrivateKey(options, done); + }); + }); + describe('downloadPrivateKey', function() { it('should work', function(done) { var options = { diff --git a/test/unit/privatekey-dao-test.js b/test/unit/privatekey-dao-test.js index 8234a03..7092cfa 100644 --- a/test/unit/privatekey-dao-test.js +++ b/test/unit/privatekey-dao-test.js @@ -150,19 +150,44 @@ define(function(require) { }); it('should work', function(done) { - var key = { - _id: '12345' - }; + var keyId = '12345'; restDaoStub.get.withArgs({ - uri: '/privatekey/user/' + emailAddress + '/key/' + key._id + uri: '/privatekey/user/' + emailAddress + '/key/' + keyId }).yields(); privkeyDao.requestDownload({ userId: emailAddress, - keyId: key._id - }, function(err) { + keyId: keyId + }, function(err, found) { expect(err).to.not.exist; + expect(found).to.be.true; + done(); + }); + }); + }); + + describe('hasPrivateKey', function() { + it('should fail due to invalid args', function(done) { + privkeyDao.hasPrivateKey({}, function(err) { + expect(err).to.exist; + done(); + }); + }); + + it('should work', function(done) { + var keyId = '12345'; + + restDaoStub.get.withArgs({ + uri: '/privatekey/user/' + emailAddress + '/key/' + keyId + '?ignoreRecovery=true' + }).yields(); + + privkeyDao.hasPrivateKey({ + userId: emailAddress, + keyId: keyId + }, function(err, found) { + expect(err).to.not.exist; + expect(found).to.be.true; done(); }); }); diff --git a/test/unit/privatekey-upload-ctrl-test.js b/test/unit/privatekey-upload-ctrl-test.js index 4dc7375..da7051f 100644 --- a/test/unit/privatekey-upload-ctrl-test.js +++ b/test/unit/privatekey-upload-ctrl-test.js @@ -50,16 +50,16 @@ define(function(require) { describe('checkServerForKey', function() { var keyParams = { userId: emailAddress, - _id: 'keyId' + _id: 'keyId', }; it('should fail', function(done) { pgpStub.getKeyParams.returns(keyParams); - keychainMock.requestPrivateKeyDownload.yields(42); + keychainMock.hasPrivateKey.yields(42); scope.onError = function(err) { expect(err).to.exist; - expect(keychainMock.requestPrivateKeyDownload.calledOnce).to.be.true; + expect(keychainMock.hasPrivateKey.calledOnce).to.be.true; done(); }; @@ -68,7 +68,10 @@ define(function(require) { it('should return true', function(done) { pgpStub.getKeyParams.returns(keyParams); - keychainMock.requestPrivateKeyDownload.yields(null, true); + keychainMock.hasPrivateKey.withArgs({ + userId: keyParams.userId, + keyId: keyParams._id + }).yields(null, true); scope.checkServerForKey(function(privateKeySynced) { expect(privateKeySynced).to.be.true; @@ -78,7 +81,10 @@ define(function(require) { it('should return undefined', function(done) { pgpStub.getKeyParams.returns(keyParams); - keychainMock.requestPrivateKeyDownload.yields(null, false); + keychainMock.hasPrivateKey.withArgs({ + userId: keyParams.userId, + keyId: keyParams._id + }).yields(null, false); scope.checkServerForKey(function(privateKeySynced) { expect(privateKeySynced).to.be.undefined;