Add api authorization logging
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
2020-10-18 21:07:42 -05:00
parent 7c8a05aa4f
commit fac3431375
3 changed files with 25 additions and 2 deletions

View File

@@ -6,14 +6,21 @@ class APIRouteMiddleware extends Middleware {
}
async test(req, res, next, { allow_token = true, allow_user = true }) {
if ( !req.additional_api_log_data ) req.additional_api_log_data = {}
// First, check if there is a user in the session.
if ( allow_user && req.user ) {
req.additional_api_log_data.authorized_by = 'user'
return next()
} else if ( allow_token ) {
if ( !req.oauth ) req.oauth = {}
req.additional_api_log_data.attempted_token_auth = true
return req.app.oauth2.authorise()(req, res, async e => {
if ( e ) return next(e)
req.additional_api_log_data.authorized_by = 'token'
// Look up the OAuth2 client an inject it into the route
if ( req.user && req.user.id ) {
const User = this.models.get('auth:User')
@@ -44,6 +51,9 @@ class APIRouteMiddleware extends Middleware {
.message('This OAuth2 client is no longer authorized.')
.api()
req.additional_api_log_data.token_client_id = client.uuid
req.additional_api_log_data.token = bearer
req.oauth.token = token
req.oauth.client = client
} else