2020-05-07 11:53:34 +02:00
|
|
|
const jwt = require("jsonwebtoken")
|
2020-05-14 16:12:30 +02:00
|
|
|
const STATUS_CODES = require("../utilities/statusCodes")
|
2021-02-12 21:34:54 +01:00
|
|
|
const { getRole, getBuiltinRoles } = require("../utilities/security/roles")
|
2020-10-13 22:33:56 +02:00
|
|
|
const { AuthTypes } = require("../constants")
|
2021-01-28 19:30:59 +01:00
|
|
|
const {
|
|
|
|
getAppId,
|
|
|
|
getCookieName,
|
|
|
|
clearCookie,
|
|
|
|
setCookie,
|
|
|
|
isClient,
|
|
|
|
} = require("../utilities")
|
2020-04-23 15:37:08 +02:00
|
|
|
|
|
|
|
module.exports = async (ctx, next) => {
|
2020-05-18 12:53:04 +02:00
|
|
|
if (ctx.path === "/_builder") {
|
2020-05-14 16:12:30 +02:00
|
|
|
await next()
|
2020-05-07 15:04:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-03 14:45:49 +01:00
|
|
|
// do everything we can to make sure the appId is held correctly
|
|
|
|
// we hold it in state as a
|
|
|
|
let appId = getAppId(ctx)
|
2020-11-03 16:00:39 +01:00
|
|
|
const cookieAppId = ctx.cookies.get(getCookieName("currentapp"))
|
2021-02-12 21:34:54 +01:00
|
|
|
const builtinRoles = getBuiltinRoles()
|
2020-11-03 16:00:39 +01:00
|
|
|
if (appId && cookieAppId !== appId) {
|
2021-01-28 19:30:59 +01:00
|
|
|
setCookie(ctx, appId, "currentapp")
|
2020-11-03 16:00:39 +01:00
|
|
|
} else if (cookieAppId) {
|
|
|
|
appId = cookieAppId
|
2020-11-03 14:45:49 +01:00
|
|
|
}
|
2021-01-28 19:30:59 +01:00
|
|
|
let token, authType
|
|
|
|
if (!isClient(ctx)) {
|
2020-11-19 21:16:37 +01:00
|
|
|
token = ctx.cookies.get(getCookieName())
|
2021-01-28 19:30:59 +01:00
|
|
|
authType = AuthTypes.BUILDER
|
2021-01-29 14:14:36 +01:00
|
|
|
}
|
|
|
|
if (!token && appId) {
|
2021-01-28 19:30:59 +01:00
|
|
|
token = ctx.cookies.get(getCookieName(appId))
|
|
|
|
authType = AuthTypes.APP
|
2020-05-18 07:40:29 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 22:33:56 +02:00
|
|
|
if (!token) {
|
2020-10-12 14:32:52 +02:00
|
|
|
ctx.auth.authenticated = false
|
2020-11-09 10:42:35 +01:00
|
|
|
ctx.appId = appId
|
2020-10-13 22:33:56 +02:00
|
|
|
ctx.user = {
|
2020-10-14 22:43:36 +02:00
|
|
|
appId,
|
2021-02-12 21:34:54 +01:00
|
|
|
role: builtinRoles.PUBLIC,
|
2020-10-13 22:33:56 +02:00
|
|
|
}
|
2020-05-07 11:53:34 +02:00
|
|
|
await next()
|
|
|
|
return
|
|
|
|
}
|
2020-04-23 15:37:08 +02:00
|
|
|
|
|
|
|
try {
|
2020-11-19 21:42:49 +01:00
|
|
|
ctx.auth.authenticated = authType
|
2020-10-13 22:33:56 +02:00
|
|
|
const jwtPayload = jwt.verify(token, ctx.config.jwtSecret)
|
2020-11-03 14:45:49 +01:00
|
|
|
ctx.appId = appId
|
2020-10-13 22:33:56 +02:00
|
|
|
ctx.auth.apiKey = jwtPayload.apiKey
|
2020-05-27 18:23:01 +02:00
|
|
|
ctx.user = {
|
|
|
|
...jwtPayload,
|
2020-11-03 14:45:49 +01:00
|
|
|
appId: appId,
|
2020-12-02 14:20:56 +01:00
|
|
|
role: await getRole(appId, jwtPayload.roleId),
|
2020-05-27 18:23:01 +02:00
|
|
|
}
|
2020-04-23 15:37:08 +02:00
|
|
|
} catch (err) {
|
2021-01-28 19:30:59 +01:00
|
|
|
if (authType === AuthTypes.BUILDER) {
|
|
|
|
clearCookie(ctx)
|
|
|
|
ctx.status = 200
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
ctx.throw(err.status || STATUS_CODES.FORBIDDEN, err.text)
|
|
|
|
}
|
2020-04-23 15:37:08 +02:00
|
|
|
}
|
|
|
|
|
2020-05-07 11:53:34 +02:00
|
|
|
await next()
|
|
|
|
}
|