1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-29 20:32:15 -05:00

Merge pull request #112 from whiteout-io/dev/WO-531

[WO-531] introduce option to query key w/o starting recovery
This commit is contained in:
Tankred Hase 2014-09-17 15:05:26 +02:00
commit 0aff411c3f
6 changed files with 103 additions and 21 deletions

View File

@ -60,9 +60,9 @@ define(function(require) {
$scope.checkServerForKey = function(callback) { $scope.checkServerForKey = function(callback) {
var keyParams = pgp.getKeyParams(); var keyParams = pgp.getKeyParams();
keychain.requestPrivateKeyDownload({ keychain.hasPrivateKey({
userId: keyParams.userId, userId: keyParams.userId,
keyId: keyParams._id, keyId: keyParams._id
}, function(err, privateKeySynced) { }, function(err, privateKeySynced) {
if (err) { if (err) {
$scope.onError(err); $scope.onError(err);

View File

@ -641,6 +641,16 @@ define(function(require) {
this._privateKeyDao.requestDownload(options, callback); 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. * Download the encrypted private PGP key from the server using the recovery token.
* @param {String} options.userId The user's email address * @param {String} options.userId The user's email address

View File

@ -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.userId The user's email address
* @param {String} options.keyId The private PGP key id * @param {String} options.keyId The private PGP key id
* @param {Function} callback(error, found) * @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) { PrivateKeyDAO.prototype.hasPrivateKey = function(options, callback) {
var uri;
if (!options.userId || !options.keyId) { if (!options.userId || !options.keyId) {
callback(new Error('Incomplete arguments!')); callback(new Error('Incomplete arguments!'));
return; return;
} }
uri = '/privatekey/user/' + options.userId + '/key/' + options.keyId;
this._restDao.get({ 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) { }, function(err) {
// 404: there is no encrypted private key on the server // 404: there is no encrypted private key on the server
if (err && err.code !== 200) { if (err && err.code !== 200) {

View File

@ -1202,7 +1202,8 @@ define(function(require) {
describe('requestPrivateKeyDownload', function() { describe('requestPrivateKeyDownload', function() {
it('should work', function(done) { it('should work', function(done) {
var options = { var options = {
userId: testUser userId: testUser,
keyId: 'someId'
}; };
privkeyDaoStub.requestDownload.withArgs(options).yields(); 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() { describe('downloadPrivateKey', function() {
it('should work', function(done) { it('should work', function(done) {
var options = { var options = {

View File

@ -150,19 +150,44 @@ define(function(require) {
}); });
it('should work', function(done) { it('should work', function(done) {
var key = { var keyId = '12345';
_id: '12345'
};
restDaoStub.get.withArgs({ restDaoStub.get.withArgs({
uri: '/privatekey/user/' + emailAddress + '/key/' + key._id uri: '/privatekey/user/' + emailAddress + '/key/' + keyId
}).yields(); }).yields();
privkeyDao.requestDownload({ privkeyDao.requestDownload({
userId: emailAddress, userId: emailAddress,
keyId: key._id keyId: keyId
}, function(err) { }, function(err, found) {
expect(err).to.not.exist; 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(); done();
}); });
}); });

View File

@ -50,16 +50,16 @@ define(function(require) {
describe('checkServerForKey', function() { describe('checkServerForKey', function() {
var keyParams = { var keyParams = {
userId: emailAddress, userId: emailAddress,
_id: 'keyId' _id: 'keyId',
}; };
it('should fail', function(done) { it('should fail', function(done) {
pgpStub.getKeyParams.returns(keyParams); pgpStub.getKeyParams.returns(keyParams);
keychainMock.requestPrivateKeyDownload.yields(42); keychainMock.hasPrivateKey.yields(42);
scope.onError = function(err) { scope.onError = function(err) {
expect(err).to.exist; expect(err).to.exist;
expect(keychainMock.requestPrivateKeyDownload.calledOnce).to.be.true; expect(keychainMock.hasPrivateKey.calledOnce).to.be.true;
done(); done();
}; };
@ -68,7 +68,10 @@ define(function(require) {
it('should return true', function(done) { it('should return true', function(done) {
pgpStub.getKeyParams.returns(keyParams); 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) { scope.checkServerForKey(function(privateKeySynced) {
expect(privateKeySynced).to.be.true; expect(privateKeySynced).to.be.true;
@ -78,7 +81,10 @@ define(function(require) {
it('should return undefined', function(done) { it('should return undefined', function(done) {
pgpStub.getKeyParams.returns(keyParams); 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) { scope.checkServerForKey(function(privateKeySynced) {
expect(privateKeySynced).to.be.undefined; expect(privateKeySynced).to.be.undefined;