2020-04-30 20:26:12 +01:00
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const {i18n} = require('../../lib/common');
|
2016-09-30 13:45:59 +02:00
|
|
|
|
2018-10-05 17:45:17 +07:00
|
|
|
const authorize = {
|
2018-11-07 17:29:40 +07:00
|
|
|
authorizeContentApi(req, res, next) {
|
|
|
|
const hasApiKey = req.api_key && req.api_key.id;
|
2018-11-07 17:41:49 +07:00
|
|
|
const hasMember = req.member;
|
2018-11-07 17:29:40 +07:00
|
|
|
if (hasApiKey) {
|
|
|
|
return next();
|
|
|
|
}
|
2021-01-28 18:07:45 +00:00
|
|
|
if (hasMember) {
|
2018-11-07 17:41:49 +07:00
|
|
|
return next();
|
|
|
|
}
|
2020-04-30 20:26:12 +01:00
|
|
|
return next(new errors.NoPermissionError({
|
|
|
|
message: i18n.t('errors.middleware.auth.authorizationFailed'),
|
|
|
|
context: i18n.t('errors.middleware.auth.missingContentMemberOrIntegration')
|
2019-01-18 17:33:36 +01:00
|
|
|
}));
|
2018-11-07 17:29:40 +07:00
|
|
|
},
|
|
|
|
|
2019-01-18 17:41:52 +01:00
|
|
|
authorizeAdminApi(req, res, next) {
|
2018-10-15 16:23:34 +07:00
|
|
|
const hasUser = req.user && req.user.id;
|
|
|
|
const hasApiKey = req.api_key && req.api_key.id;
|
2019-01-18 17:33:36 +01:00
|
|
|
|
2018-10-15 16:23:34 +07:00
|
|
|
if (hasUser || hasApiKey) {
|
|
|
|
return next();
|
|
|
|
} else {
|
2020-04-30 20:26:12 +01:00
|
|
|
return next(new errors.NoPermissionError({
|
|
|
|
message: i18n.t('errors.middleware.auth.authorizationFailed'),
|
|
|
|
context: i18n.t('errors.middleware.auth.missingAdminUserOrIntegration')
|
2019-01-18 17:33:36 +01:00
|
|
|
}));
|
2018-10-15 16:23:34 +07:00
|
|
|
}
|
|
|
|
}
|
2016-09-30 13:45:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = authorize;
|