2018-10-05 13:45:17 +03:00
|
|
|
const labs = require('../labs');
|
|
|
|
const session = require('./session');
|
|
|
|
const common = require('../../lib/common');
|
2016-09-30 14:45:59 +03:00
|
|
|
|
2018-10-05 13:45:17 +03:00
|
|
|
const authorize = {
|
2016-09-30 14:45:59 +03:00
|
|
|
// Workaround for missing permissions
|
|
|
|
// TODO: rework when https://github.com/TryGhost/Ghost/issues/3911 is done
|
|
|
|
requiresAuthorizedUser: function requiresAuthorizedUser(req, res, next) {
|
|
|
|
if (req.user && req.user.id) {
|
|
|
|
return next();
|
|
|
|
} else {
|
2017-12-12 00:47:46 +03:00
|
|
|
return next(new common.errors.NoPermissionError({message: common.i18n.t('errors.middleware.auth.pleaseSignIn')}));
|
2016-09-30 14:45:59 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// ### Require user depending on public API being activated.
|
|
|
|
requiresAuthorizedUserPublicAPI: function requiresAuthorizedUserPublicAPI(req, res, next) {
|
|
|
|
if (labs.isSet('publicAPI') === true) {
|
|
|
|
return next();
|
|
|
|
} else {
|
|
|
|
if (req.user && req.user.id) {
|
|
|
|
return next();
|
|
|
|
} else {
|
2017-12-12 00:47:46 +03:00
|
|
|
return next(new common.errors.NoPermissionError({message: common.i18n.t('errors.middleware.auth.pleaseSignIn')}));
|
2016-09-30 14:45:59 +03:00
|
|
|
}
|
|
|
|
}
|
2017-08-22 13:15:40 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// Requires the authenticated client to match specific client
|
|
|
|
requiresAuthorizedClient: function requiresAuthorizedClient(client) {
|
|
|
|
return function doAuthorizedClient(req, res, next) {
|
2017-12-15 12:35:48 +03:00
|
|
|
if (client && (!req.client || !req.client.name || req.client.name !== client)) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return next(new common.errors.NoPermissionError({message: common.i18n.t('errors.permissions.noPermissionToAction')}));
|
2017-08-22 13:15:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
|
|
|
};
|
2018-10-05 13:45:17 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
authorizeAdminAPI: [session.ensureUser]
|
2016-09-30 14:45:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = authorize;
|