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")
|
2020-05-27 18:23:01 +02:00
|
|
|
const accessLevelController = require("../api/controllers/accesslevel")
|
2020-11-13 16:35:20 +01:00
|
|
|
const { BUILTIN_LEVEL_ID_ARRAY } = require("../utilities/security/accessLevels")
|
2020-10-28 21:35:06 +01:00
|
|
|
const env = require("../environment")
|
2020-10-13 22:33:56 +02:00
|
|
|
const { AuthTypes } = require("../constants")
|
2020-11-03 14:45:49 +01:00
|
|
|
const { getAppId, getCookieName, setCookie } = 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"))
|
|
|
|
if (appId && cookieAppId !== appId) {
|
2020-11-03 14:45:49 +01:00
|
|
|
setCookie(ctx, "currentapp", appId)
|
2020-11-03 16:00:39 +01:00
|
|
|
} else if (cookieAppId) {
|
|
|
|
appId = cookieAppId
|
2020-11-03 14:45:49 +01:00
|
|
|
}
|
2020-11-02 21:14:10 +01:00
|
|
|
|
|
|
|
const appToken = ctx.cookies.get(getCookieName(appId))
|
|
|
|
const builderToken = ctx.cookies.get(getCookieName())
|
2020-06-03 18:05:36 +02:00
|
|
|
|
2020-10-13 22:33:56 +02:00
|
|
|
let token
|
|
|
|
// if running locally in the builder itself
|
2020-10-28 21:35:06 +01:00
|
|
|
if (!env.CLOUD && !appToken) {
|
2020-10-13 22:33:56 +02:00
|
|
|
token = builderToken
|
|
|
|
ctx.auth.authenticated = AuthTypes.BUILDER
|
|
|
|
} else {
|
|
|
|
token = appToken
|
|
|
|
ctx.auth.authenticated = 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,
|
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-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,
|
|
|
|
accessLevel: await getAccessLevel(appId, jwtPayload.accessLevelId),
|
2020-05-27 18:23:01 +02:00
|
|
|
}
|
2020-04-23 15:37:08 +02:00
|
|
|
} catch (err) {
|
2020-05-07 15:04:32 +02:00
|
|
|
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()
|
|
|
|
}
|
2020-05-27 18:23:01 +02:00
|
|
|
|
2020-06-29 19:57:17 +02:00
|
|
|
/**
|
2020-07-07 22:29:20 +02:00
|
|
|
* Return the full access level object either from constants
|
2020-06-29 19:57:17 +02:00
|
|
|
* or the database based on the access level ID passed.
|
2020-07-07 22:29:20 +02:00
|
|
|
*
|
2020-10-29 11:28:27 +01:00
|
|
|
* @param {*} appId - appId of the user
|
2020-07-07 22:29:20 +02:00
|
|
|
* @param {*} accessLevelId - the id of the users access level
|
2020-06-29 19:57:17 +02:00
|
|
|
*/
|
2020-10-29 11:28:27 +01:00
|
|
|
const getAccessLevel = async (appId, accessLevelId) => {
|
2020-11-13 16:35:20 +01:00
|
|
|
if (BUILTIN_LEVEL_ID_ARRAY.indexOf(accessLevelId) !== -1) {
|
2020-05-27 18:23:01 +02:00
|
|
|
return {
|
|
|
|
_id: accessLevelId,
|
|
|
|
name: accessLevelId,
|
|
|
|
permissions: [],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const findAccessContext = {
|
|
|
|
params: {
|
|
|
|
levelId: accessLevelId,
|
2020-06-18 21:41:37 +02:00
|
|
|
},
|
|
|
|
user: {
|
2020-10-29 11:28:27 +01:00
|
|
|
appId,
|
2020-05-27 18:23:01 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
await accessLevelController.find(findAccessContext)
|
|
|
|
return findAccessContext.body
|
|
|
|
}
|