2013-10-29 07:19:27 -04:00
|
|
|
define(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var PublicKeyDAO = function(restDao) {
|
|
|
|
this._restDao = restDao;
|
|
|
|
};
|
|
|
|
|
2013-11-08 03:29:04 -05:00
|
|
|
/**
|
|
|
|
* Verify the public key behind the given uuid
|
|
|
|
*/
|
|
|
|
PublicKeyDAO.prototype.verify = function(uuid, callback) {
|
|
|
|
var uri = '/verify/' + uuid;
|
|
|
|
|
|
|
|
this._restDao.get({
|
|
|
|
uri: uri,
|
|
|
|
type: 'text'
|
|
|
|
}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-10-29 07:19:27 -04:00
|
|
|
/**
|
|
|
|
* Find the user's corresponding public key
|
|
|
|
*/
|
|
|
|
PublicKeyDAO.prototype.get = function(keyId, callback) {
|
|
|
|
var uri = '/publickey/key/' + keyId;
|
|
|
|
|
2013-11-08 03:29:04 -05:00
|
|
|
this._restDao.get({
|
|
|
|
uri: uri
|
|
|
|
}, function(err, key) {
|
2013-10-29 07:19:27 -04:00
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key || !key._id) {
|
|
|
|
callback({
|
|
|
|
errMsg: 'No public key for that user!'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, key);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the user's corresponding public key by email
|
|
|
|
*/
|
|
|
|
PublicKeyDAO.prototype.getByUserId = function(userId, callback) {
|
|
|
|
var uri = '/publickey/user/' + userId;
|
|
|
|
|
2013-11-08 03:29:04 -05:00
|
|
|
this._restDao.get({
|
|
|
|
uri: uri
|
|
|
|
}, function(err, keys) {
|
2013-10-29 07:19:27 -04:00
|
|
|
// not found
|
|
|
|
if (err && err.code === 404) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!keys || keys.length < 1) {
|
|
|
|
// 'No public key for that user!'
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keys.length > 1) {
|
|
|
|
callback({
|
|
|
|
errMsg: 'That user has multiple public keys!'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, keys[0]);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persist the user's publc key
|
|
|
|
*/
|
|
|
|
PublicKeyDAO.prototype.put = function(pubkey, callback) {
|
|
|
|
var uri = '/publickey/user/' + pubkey.userId + '/key/' + pubkey._id;
|
|
|
|
this._restDao.put(pubkey, uri, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the public key from the cloud storage service
|
|
|
|
*/
|
|
|
|
PublicKeyDAO.prototype.remove = function(keyId, callback) {
|
|
|
|
var uri = '/publickey/key/' + keyId;
|
|
|
|
this._restDao.remove(uri, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
return PublicKeyDAO;
|
|
|
|
});
|