2021-07-23 16:29:14 +02:00
|
|
|
const { Cookies, Headers } = require("../constants")
|
2021-12-17 15:08:48 +01:00
|
|
|
const { getCookie, clearCookie, openJwt } = 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")
|
2022-02-11 23:24:48 +01:00
|
|
|
const { SEPARATOR, ViewNames, queryGlobalView } = require("../../db")
|
2022-03-14 20:05:02 +01:00
|
|
|
const { getGlobalDB, doInTenant } = require("../tenancy")
|
2022-02-14 19:32:09 +01:00
|
|
|
const { decrypt } = require("../security/encryption")
|
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
|
|
|
}
|
|
|
|
|
2022-02-11 23:24:48 +01:00
|
|
|
async function checkApiKey(apiKey, populateUser) {
|
|
|
|
if (apiKey === env.INTERNAL_API_KEY) {
|
|
|
|
return { valid: true }
|
|
|
|
}
|
2022-02-14 22:37:40 +01:00
|
|
|
const decrypted = decrypt(apiKey)
|
|
|
|
const tenantId = decrypted.split(SEPARATOR)[0]
|
2022-03-14 20:05:02 +01:00
|
|
|
return doInTenant(tenantId, async () => {
|
|
|
|
const db = getGlobalDB()
|
|
|
|
// api key is encrypted in the database
|
|
|
|
const userId = await queryGlobalView(
|
|
|
|
ViewNames.BY_API_KEY,
|
|
|
|
{
|
|
|
|
key: apiKey,
|
|
|
|
},
|
|
|
|
db
|
|
|
|
)
|
|
|
|
if (userId) {
|
|
|
|
return {
|
|
|
|
valid: true,
|
|
|
|
user: await getUser(userId, tenantId, populateUser),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw "Invalid API key"
|
|
|
|
}
|
|
|
|
})
|
2022-02-11 23:24:48 +01: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 {
|
2021-12-17 15:08:48 +01:00
|
|
|
// check the actual user is authenticated first, try header or cookie
|
|
|
|
const headerToken = ctx.request.headers[Headers.TOKEN]
|
|
|
|
const authCookie = getCookie(ctx, Cookies.Auth) || openJwt(headerToken)
|
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)
|
|
|
|
}
|
2022-01-25 23:54:50 +01:00
|
|
|
user.csrfToken = session.csrfToken
|
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
|
2022-02-11 23:24:48 +01:00
|
|
|
if (!authenticated && apiKey) {
|
|
|
|
const populateUser = opts.populateUser ? opts.populateUser(ctx) : null
|
|
|
|
const { valid, user: foundUser } = await checkApiKey(
|
|
|
|
apiKey,
|
|
|
|
populateUser
|
|
|
|
)
|
|
|
|
if (valid && foundUser) {
|
|
|
|
authenticated = true
|
|
|
|
user = foundUser
|
|
|
|
} else if (valid) {
|
|
|
|
authenticated = true
|
|
|
|
internal = true
|
|
|
|
}
|
2021-06-21 18:13:06 +02:00
|
|
|
}
|
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 })
|
2022-02-25 16:55:19 +01:00
|
|
|
return next()
|
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
|
|
|
}
|
|
|
|
}
|