2020-05-12 01:26:09 +00:00
|
|
|
const { Middleware } = require('libflitter')
|
|
|
|
|
|
|
|
class PermissionMiddleware extends Middleware {
|
2020-05-21 02:17:07 +00:00
|
|
|
static get services() {
|
2020-07-13 14:35:11 +00:00
|
|
|
return [...super.services, 'models', 'activity']
|
2020-05-21 02:17:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 01:26:09 +00:00
|
|
|
async test(req, res, next, { check }) {
|
2020-05-21 02:17:07 +00:00
|
|
|
const Policy = this.models.get('iam:Policy')
|
|
|
|
|
2020-10-19 15:19:01 +00:00
|
|
|
if ( !req.additional_api_log_data ) req.additional_api_log_data = {}
|
2020-10-19 02:07:42 +00:00
|
|
|
req.additional_api_log_data.permission_check = check
|
|
|
|
|
2020-05-17 04:55:08 +00:00
|
|
|
// If the request was authorized using an OAuth2 bearer token,
|
|
|
|
// make sure the associated client has permission to access this endpoint.
|
|
|
|
if ( req?.oauth?.client ) {
|
2020-07-13 14:35:11 +00:00
|
|
|
if ( !req.oauth.client.can(check) ) {
|
|
|
|
const reason = 'oauth-permission-fail'
|
2020-10-19 02:07:42 +00:00
|
|
|
const fail_activity = await this.activity.api_access_denial({
|
2020-07-13 14:35:11 +00:00
|
|
|
req,
|
|
|
|
reason,
|
|
|
|
check,
|
2020-10-18 23:55:21 +00:00
|
|
|
oauth_client_id: req.oauth.client.uuid,
|
2020-07-13 14:35:11 +00:00
|
|
|
})
|
|
|
|
|
2020-10-19 02:07:42 +00:00
|
|
|
req.additional_api_log_data.permission_check_succeeded = false
|
|
|
|
req.additional_api_log_data.permission_check_activity_id = fail_activity.id
|
|
|
|
|
2020-05-17 04:55:08 +00:00
|
|
|
return res.status(401)
|
|
|
|
.message('Insufficient permissions (OAuth2 Client).')
|
|
|
|
.api()
|
2020-07-13 14:35:11 +00:00
|
|
|
}
|
2020-10-19 01:22:10 +00:00
|
|
|
|
2020-10-19 02:07:42 +00:00
|
|
|
req.additional_api_log_data.permission_check_succeeded = true
|
|
|
|
|
2020-10-19 01:22:10 +00:00
|
|
|
// If the oauth2 client has this permission, then allow the request to continue,
|
|
|
|
// even if the user does not.
|
|
|
|
// OAuth2Clients need to be able to query users via the API.
|
|
|
|
return next()
|
2020-05-17 04:55:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 02:17:07 +00:00
|
|
|
const policy_denied = await Policy.check_user_denied(req.user, check)
|
|
|
|
const policy_access = await Policy.check_user_access(req.user, check)
|
|
|
|
|
2020-05-17 04:55:08 +00:00
|
|
|
// Make sure the user has permission
|
2020-07-13 14:35:11 +00:00
|
|
|
if ( policy_denied || (!req.user.can(check) && !policy_access) ) {
|
|
|
|
// Record the failed API access
|
|
|
|
const reason = policy_denied ? 'iam-denial' : (!req.user.can(check) ? 'user-permission-fail' : 'iam-not-granted')
|
2020-10-19 02:07:42 +00:00
|
|
|
const fail_activity = await this.activity.api_access_denial({ req, reason, check })
|
|
|
|
|
|
|
|
req.additional_api_log_data.permission_check_succeeded = false
|
|
|
|
req.additional_api_log_data.permission_check_reason = reason
|
|
|
|
req.additional_api_log_data.permission_check_activity_id = fail_activity.id
|
2020-07-13 14:35:11 +00:00
|
|
|
|
2020-05-12 01:26:09 +00:00
|
|
|
return res.status(401)
|
|
|
|
.message('Insufficient permissions.')
|
|
|
|
.api()
|
2020-07-13 14:35:11 +00:00
|
|
|
}
|
2020-05-12 01:26:09 +00:00
|
|
|
|
2020-10-19 02:07:42 +00:00
|
|
|
req.additional_api_log_data.permission_check_succeeded = true
|
2020-05-12 01:26:09 +00:00
|
|
|
return next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = PermissionMiddleware
|