feat(server): authentication middleware should log auth context creation status (#1508)

* feat(server): authentication middleware should log auth context creation status
- this uses the pino http logger provided via prior express middleware, ensuring a request ID is associated with the log messages
- userID, scopes and roles will be logged
* Appends the authContext to the req.log, which makes it available on all subsequent calls
This commit is contained in:
Iain Sproat
2023-04-11 18:43:46 +01:00
committed by GitHub
parent 6c66049248
commit 4c723781b5
@@ -89,12 +89,14 @@ export async function authContextMiddleware(
) {
const token = getTokenFromRequest(req)
const authContext = await createAuthContextFromToken(token)
req.log = req.log.child({ authContext })
if (!authContext.auth && authContext.err) {
let message = 'Unknown Auth context error'
let status = 500
message = authContext.err?.message || message
if (authContext.err instanceof UnauthorizedError) status = 401
if (authContext.err instanceof ForbiddenError) status = 403
req.log.warn('Auth context creation failed.')
return res.status(status).json({ error: message })
}
req.context = authContext