2011-12-27 02:16:41 +08:00
|
|
|
/**
|
|
|
|
* this module allows to auth user using an pregenerated api key
|
|
|
|
*/
|
2018-02-20 02:16:33 +08:00
|
|
|
function ApikeyAuth(req, apikey) {
|
2014-08-05 22:20:06 +08:00
|
|
|
this.req = req;
|
2018-02-20 02:16:33 +08:00
|
|
|
this.apikey = apikey;
|
2013-12-18 18:57:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ApikeyAuth;
|
|
|
|
|
2018-02-16 00:23:35 +08:00
|
|
|
ApikeyAuth.prototype.verifyCredentials = function (options, callback) {
|
2018-02-20 02:16:33 +08:00
|
|
|
callback(null, verifyRequest(this.apikey, options.apiKey));
|
2014-08-05 22:20:06 +08:00
|
|
|
};
|
|
|
|
|
2018-02-16 00:23:35 +08:00
|
|
|
ApikeyAuth.prototype.hasCredentials = function () {
|
2018-02-20 02:16:33 +08:00
|
|
|
return !!this.apikey;
|
2014-08-05 22:20:06 +08:00
|
|
|
};
|
|
|
|
|
2018-02-16 00:23:35 +08:00
|
|
|
ApikeyAuth.prototype.getCredentials = function () {
|
2018-02-20 02:16:33 +08:00
|
|
|
return this.apikey;
|
2017-11-25 00:57:54 +08:00
|
|
|
};
|
|
|
|
|
2013-12-18 18:57:46 +08:00
|
|
|
/**
|
|
|
|
* Get id of authorized user
|
|
|
|
*
|
2014-08-05 08:29:07 +08:00
|
|
|
* @param {Object} req - standard req object. Importantly contains table and host information
|
|
|
|
* @param {String} requiredApi - the API associated to the user, req must contain it
|
|
|
|
* @param {Function} callback - err, boolean (whether the request is authenticated or not)
|
2013-12-18 18:57:46 +08:00
|
|
|
*/
|
2018-02-16 00:23:35 +08:00
|
|
|
function verifyRequest(apikey, requiredApikey) {
|
|
|
|
return (apikey === requiredApikey);
|
2014-08-05 22:20:06 +08:00
|
|
|
}
|