2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-04-10 00:08:56 +08:00
|
|
|
module.exports = function authorize (authBackend) {
|
2018-03-01 02:21:44 +08:00
|
|
|
return function authorizeMiddleware (req, res, next) {
|
2018-04-10 00:08:56 +08:00
|
|
|
authBackend.authorize(req, res, (err, authorized) => {
|
2017-09-25 19:40:22 +08:00
|
|
|
req.profiler.done('authorize');
|
2018-03-01 02:25:50 +08:00
|
|
|
|
2017-09-25 19:40:22 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
if (!authorized) {
|
|
|
|
err = new Error('Sorry, you are unauthorized (permission denied)');
|
2017-09-25 19:40:22 +08:00
|
|
|
err.http_status = 403;
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|