1
0
mirror of https://github.com/moparisthebest/mail synced 2025-01-31 07:00:17 -05:00

Refactor public key service to use promises

This commit is contained in:
Tankred Hase 2014-11-29 16:28:32 +01:00
parent 99c2f24d7d
commit f9f086d622

View File

@ -11,95 +11,75 @@ function PublicKey(publicKeyRestDao) {
/** /**
* Verify the public key behind the given uuid * Verify the public key behind the given uuid
*/ */
PublicKey.prototype.verify = function(uuid, callback) { PublicKey.prototype.verify = function(uuid) {
var uri = '/verify/' + uuid; return this._restDao.get({
uri: '/verify/' + uuid,
this._restDao.get({
uri: uri,
type: 'text' type: 'text'
}, function(err, res, status) { }).catch(function(err) {
if (err && err.code === 400) { if (err.code === 400) {
// there was an attempt to verify a non-existing public key // there was an attempt to verify a non-existing public key
callback();
return; return;
} }
callback(err, res, status); throw err;
}); });
}; };
/** /**
* Find the user's corresponding public key * Find the user's corresponding public key
*/ */
PublicKey.prototype.get = function(keyId, callback) { PublicKey.prototype.get = function(keyId) {
var uri = '/publickey/key/' + keyId; return this._restDao.get({
uri: '/publickey/key/' + keyId
this._restDao.get({ }).catch(function(err) {
uri: uri if (err.code === 404) {
}, function(err, key) {
if (err && err.code === 404) {
callback();
return; return;
} }
if (err) { throw err;
callback(err);
return;
}
callback(null, (key && key._id) ? key : undefined);
}); });
}; };
/** /**
* Find the user's corresponding public key by email * Find the user's corresponding public key by email
*/ */
PublicKey.prototype.getByUserId = function(userId, callback) { PublicKey.prototype.getByUserId = function(userId) {
var uri = '/publickey/user/' + userId; return this._restDao.get({
uri: '/publickey/user/' + userId
this._restDao.get({ }).then(function(keys) {
uri: uri
}, function(err, keys) {
// not found
if (err && err.code === 404) {
callback();
return;
}
if (err) {
callback(err);
return;
}
if (!keys || keys.length < 1) { if (!keys || keys.length < 1) {
// 'No public key for that user!' // 'No public key for that user!'
callback();
return; return;
} }
if (keys.length > 1) { if (keys.length > 1) {
callback({ throw new Error('That user has multiple public keys!');
errMsg: 'That user has multiple public keys!' }
});
return keys[0];
}).catch(function(err) {
// not found
if (err.code === 404) {
return; return;
} }
callback(null, keys[0]); throw err;
}); });
}; };
/** /**
* Persist the user's publc key * Persist the user's publc key
*/ */
PublicKey.prototype.put = function(pubkey, callback) { PublicKey.prototype.put = function(pubkey) {
var uri = '/publickey/user/' + pubkey.userId + '/key/' + pubkey._id; var uri = '/publickey/user/' + pubkey.userId + '/key/' + pubkey._id;
this._restDao.put(pubkey, uri, callback); return this._restDao.put(pubkey, uri);
}; };
/** /**
* Delete the public key from the cloud storage service * Delete the public key from the cloud storage service
*/ */
PublicKey.prototype.remove = function(keyId, callback) { PublicKey.prototype.remove = function(keyId) {
var uri = '/publickey/key/' + keyId; var uri = '/publickey/key/' + keyId;
this._restDao.remove(uri, callback); return this._restDao.remove(uri);
}; };