2021-07-23 16:29:14 +02:00
|
|
|
const { Cookies, Headers } = require("../constants")
|
2021-04-23 19:07:39 +02:00
|
|
|
const { getCookie, clearCookie } = require("../utils")
|
2021-07-06 19:10:04 +02:00
|
|
|
const { getUser } = require("../cache/user")
|
|
|
|
const { getSession, updateSessionTTL } = require("../security/sessions")
|
2021-08-05 10:59:08 +02:00
|
|
|
const { buildMatcherRegex, matches } = require("./matchers")
|
2021-05-11 13:02:29 +02:00
|
|
|
const env = require("../environment")
|
2021-04-11 12:35:55 +02:00
|
|
|
|
2021-08-05 10:59:08 +02:00
|
|
|
function finalise(
|
|
|
|
ctx,
|
|
|
|
{ authenticated, user, internal, version, publicEndpoint } = {}
|
|
|
|
) {
|
|
|
|
ctx.publicEndpoint = publicEndpoint || false
|
2021-06-21 18:13:06 +02:00
|
|
|
ctx.isAuthenticated = authenticated || false
|
|
|
|
ctx.user = user
|
|
|
|
ctx.internal = internal || false
|
2021-07-23 16:29:14 +02:00
|
|
|
ctx.version = version
|
2021-06-21 18:13:06 +02:00
|
|
|
}
|
|
|
|
|
2021-08-05 10:59:08 +02:00
|
|
|
/**
|
|
|
|
* This middleware is tenancy aware, so that it does not depend on other middlewares being used.
|
|
|
|
* The tenancy modules should not be used here and it should be assumed that the tenancy context
|
|
|
|
* has not yet been populated.
|
|
|
|
*/
|
2021-09-13 18:38:12 +02:00
|
|
|
module.exports = (
|
|
|
|
noAuthPatterns = [],
|
|
|
|
opts = { publicAllowed: false, populateUser: null }
|
|
|
|
) => {
|
2021-08-05 10:59:08 +02:00
|
|
|
const noAuthOptions = noAuthPatterns ? buildMatcherRegex(noAuthPatterns) : []
|
2021-04-21 17:42:44 +02:00
|
|
|
return async (ctx, next) => {
|
2021-08-05 10:59:08 +02:00
|
|
|
let publicEndpoint = false
|
2021-07-23 16:29:14 +02:00
|
|
|
const version = ctx.request.headers[Headers.API_VER]
|
2021-04-21 17:42:44 +02:00
|
|
|
// the path is not authenticated
|
2021-08-05 10:59:08 +02:00
|
|
|
const found = matches(ctx, noAuthOptions)
|
|
|
|
if (found) {
|
|
|
|
publicEndpoint = true
|
2021-04-11 12:35:55 +02:00
|
|
|
}
|
2021-04-21 17:42:44 +02:00
|
|
|
try {
|
|
|
|
// check the actual user is authenticated first
|
|
|
|
const authCookie = getCookie(ctx, Cookies.Auth)
|
2021-06-21 18:13:06 +02:00
|
|
|
let authenticated = false,
|
|
|
|
user = null,
|
|
|
|
internal = false
|
|
|
|
if (authCookie) {
|
2021-07-06 19:10:04 +02:00
|
|
|
let error = null
|
2021-10-12 17:13:54 +02:00
|
|
|
const sessionId = authCookie.sessionId
|
|
|
|
const userId = authCookie.userId
|
|
|
|
|
2021-07-06 19:10:04 +02:00
|
|
|
const session = await getSession(userId, sessionId)
|
|
|
|
if (!session) {
|
|
|
|
error = "No session found"
|
|
|
|
} else {
|
|
|
|
try {
|
2021-09-13 18:38:12 +02:00
|
|
|
if (opts && opts.populateUser) {
|
|
|
|
user = await getUser(
|
|
|
|
userId,
|
|
|
|
session.tenantId,
|
|
|
|
opts.populateUser(ctx)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
user = await getUser(userId, session.tenantId)
|
|
|
|
}
|
2021-07-06 19:10:04 +02:00
|
|
|
delete user.password
|
|
|
|
authenticated = true
|
|
|
|
} catch (err) {
|
|
|
|
error = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error) {
|
2021-08-04 11:38:49 +02:00
|
|
|
console.error("Auth Error", error)
|
2021-07-06 19:10:04 +02:00
|
|
|
// remove the cookie as the user does not exist anymore
|
2021-04-23 19:07:39 +02:00
|
|
|
clearCookie(ctx, Cookies.Auth)
|
2021-07-06 19:10:04 +02:00
|
|
|
} else {
|
|
|
|
// make sure we denote that the session is still in use
|
2021-07-08 00:29:19 +02:00
|
|
|
await updateSessionTTL(session)
|
2021-04-23 19:07:39 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-23 16:29:14 +02:00
|
|
|
const apiKey = ctx.request.headers[Headers.API_KEY]
|
2021-08-05 10:59:08 +02:00
|
|
|
const tenantId = ctx.request.headers[Headers.TENANT_ID]
|
2021-06-21 18:13:06 +02:00
|
|
|
// this is an internal request, no user made it
|
|
|
|
if (!authenticated && apiKey && apiKey === env.INTERNAL_API_KEY) {
|
|
|
|
authenticated = true
|
|
|
|
internal = true
|
|
|
|
}
|
2021-08-05 10:59:08 +02:00
|
|
|
if (!user && tenantId) {
|
|
|
|
user = { tenantId }
|
|
|
|
}
|
2021-04-23 19:07:39 +02:00
|
|
|
// be explicit
|
2021-06-21 18:13:06 +02:00
|
|
|
if (authenticated !== true) {
|
|
|
|
authenticated = false
|
2021-04-21 17:42:44 +02:00
|
|
|
}
|
2021-06-21 18:13:06 +02:00
|
|
|
// isAuthenticated is a function, so use a variable to be able to check authed state
|
2021-08-05 10:59:08 +02:00
|
|
|
finalise(ctx, { authenticated, user, internal, version, publicEndpoint })
|
2021-04-21 17:42:44 +02:00
|
|
|
return next()
|
|
|
|
} catch (err) {
|
2021-11-16 21:56:24 +01:00
|
|
|
// invalid token, clear the cookie
|
|
|
|
if (err && err.name === "JsonWebTokenError") {
|
|
|
|
clearCookie(ctx, Cookies.Auth)
|
|
|
|
}
|
2021-04-28 19:13:21 +02:00
|
|
|
// allow configuring for public access
|
2021-08-05 10:59:08 +02:00
|
|
|
if ((opts && opts.publicAllowed) || publicEndpoint) {
|
|
|
|
finalise(ctx, { authenticated: false, version, publicEndpoint })
|
2021-04-28 19:13:21 +02:00
|
|
|
} else {
|
|
|
|
ctx.throw(err.status || 403, err)
|
|
|
|
}
|
2021-04-21 17:42:44 +02:00
|
|
|
}
|
2021-04-11 12:35:55 +02:00
|
|
|
}
|
|
|
|
}
|