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")
|
|
|
|
const {
|
|
|
|
ADMIN_LEVEL_ID,
|
|
|
|
POWERUSER_LEVEL_ID,
|
2020-06-18 17:59:31 +02:00
|
|
|
BUILDER_LEVEL_ID,
|
|
|
|
ANON_LEVEL_ID,
|
2020-05-27 18:23:01 +02:00
|
|
|
} = require("../utilities/accessLevels")
|
2020-10-13 22:33:56 +02:00
|
|
|
const environment = require("../environment")
|
|
|
|
const { AuthTypes } = require("../constants")
|
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-06-03 18:05:36 +02:00
|
|
|
const appToken = ctx.cookies.get("budibase:token")
|
|
|
|
const builderToken = ctx.cookies.get("builder:token")
|
|
|
|
|
2020-10-13 22:33:56 +02:00
|
|
|
let token
|
|
|
|
// if running locally in the builder itself
|
|
|
|
if (!environment.CLOUD && !appToken) {
|
|
|
|
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-10-14 17:47:53 +02:00
|
|
|
|
2020-10-14 18:30:00 +02:00
|
|
|
let appId = process.env.CLOUD ? ctx.subdomains[1] : ctx.params.appId
|
|
|
|
|
|
|
|
if (!appId) {
|
|
|
|
appId = ctx.referer && ctx.referer.split("/").pop()
|
|
|
|
}
|
2020-10-14 17:47:53 +02:00
|
|
|
|
2020-10-13 22:33:56 +02:00
|
|
|
ctx.user = {
|
2020-10-14 17:47:53 +02:00
|
|
|
// if appId can't be determined from path param or subdomain
|
2020-10-14 18:30:00 +02:00
|
|
|
appId: 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)
|
|
|
|
ctx.auth.apiKey = jwtPayload.apiKey
|
2020-05-27 18:23:01 +02:00
|
|
|
ctx.user = {
|
|
|
|
...jwtPayload,
|
|
|
|
accessLevel: await getAccessLevel(
|
|
|
|
jwtPayload.instanceId,
|
|
|
|
jwtPayload.accessLevelId
|
|
|
|
),
|
|
|
|
}
|
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-06-29 19:57:17 +02:00
|
|
|
* @param {*} instanceId - instanceId 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-05-27 18:23:01 +02:00
|
|
|
const getAccessLevel = async (instanceId, accessLevelId) => {
|
|
|
|
if (
|
|
|
|
accessLevelId === POWERUSER_LEVEL_ID ||
|
2020-06-18 17:59:31 +02:00
|
|
|
accessLevelId === ADMIN_LEVEL_ID ||
|
|
|
|
accessLevelId === BUILDER_LEVEL_ID ||
|
|
|
|
accessLevelId === ANON_LEVEL_ID
|
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-05-27 18:23:01 +02:00
|
|
|
instanceId,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
await accessLevelController.find(findAccessContext)
|
|
|
|
return findAccessContext.body
|
|
|
|
}
|