2021-06-15 20:39:40 +02:00
|
|
|
const { getAppId, setCookie, getCookie, clearCookie } =
|
|
|
|
require("@budibase/auth").utils
|
2021-04-21 17:42:44 +02:00
|
|
|
const { Cookies } = require("@budibase/auth").constants
|
2021-05-14 16:43:41 +02:00
|
|
|
const { getRole } = require("@budibase/auth/roles")
|
2021-05-27 15:55:48 +02:00
|
|
|
const { getGlobalSelf } = require("../utilities/workerRequests")
|
2021-05-14 16:43:41 +02:00
|
|
|
const { BUILTIN_ROLE_IDS } = require("@budibase/auth/roles")
|
2021-04-23 19:07:39 +02:00
|
|
|
const { generateUserMetadataID } = require("../db/utils")
|
2021-06-08 19:06:16 +02:00
|
|
|
const { dbExists } = require("@budibase/auth/db")
|
|
|
|
const CouchDB = require("../db")
|
2021-04-12 19:31:58 +02:00
|
|
|
|
|
|
|
module.exports = async (ctx, next) => {
|
|
|
|
// try to get the appID from the request
|
|
|
|
const requestAppId = getAppId(ctx)
|
|
|
|
// get app cookie if it exists
|
|
|
|
const appCookie = getCookie(ctx, Cookies.CurrentApp)
|
|
|
|
if (!appCookie && !requestAppId) {
|
|
|
|
return next()
|
|
|
|
}
|
2021-06-08 19:06:16 +02:00
|
|
|
// check the app exists referenced in cookie
|
|
|
|
if (appCookie) {
|
|
|
|
const appId = appCookie.appId
|
|
|
|
const exists = await dbExists(CouchDB, appId)
|
|
|
|
if (!exists) {
|
|
|
|
clearCookie(ctx, Cookies.CurrentApp)
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
}
|
2021-04-12 19:31:58 +02:00
|
|
|
|
|
|
|
let updateCookie = false,
|
|
|
|
appId,
|
2021-04-13 19:12:35 +02:00
|
|
|
roleId = BUILTIN_ROLE_IDS.PUBLIC
|
2021-04-12 19:31:58 +02:00
|
|
|
if (!ctx.user) {
|
|
|
|
// not logged in, try to set a cookie for public apps
|
|
|
|
updateCookie = true
|
|
|
|
appId = requestAppId
|
|
|
|
} else if (
|
|
|
|
requestAppId != null &&
|
2021-04-13 17:56:45 +02:00
|
|
|
(appCookie == null ||
|
|
|
|
requestAppId !== appCookie.appId ||
|
2021-05-27 15:53:41 +02:00
|
|
|
appCookie.roleId === BUILTIN_ROLE_IDS.PUBLIC ||
|
|
|
|
!appCookie.roleId)
|
2021-04-12 19:31:58 +02:00
|
|
|
) {
|
2021-04-13 17:56:45 +02:00
|
|
|
// Different App ID means cookie needs reset, or if the same public user has logged in
|
2021-05-27 15:53:41 +02:00
|
|
|
const globalUser = await getGlobalSelf(ctx, requestAppId)
|
2021-04-12 19:31:58 +02:00
|
|
|
updateCookie = true
|
|
|
|
appId = requestAppId
|
2021-05-13 19:10:09 +02:00
|
|
|
// retrieving global user gets the right role
|
2021-07-06 19:43:04 +02:00
|
|
|
roleId = globalUser.roleId || BUILTIN_ROLE_IDS.BASIC
|
2021-04-13 14:32:09 +02:00
|
|
|
} else if (appCookie != null) {
|
2021-04-12 19:31:58 +02:00
|
|
|
appId = appCookie.appId
|
2021-07-06 19:43:04 +02:00
|
|
|
roleId = appCookie.roleId || BUILTIN_ROLE_IDS.BASIC
|
2021-04-12 19:31:58 +02:00
|
|
|
}
|
2021-04-20 18:17:44 +02:00
|
|
|
// nothing more to do
|
|
|
|
if (!appId) {
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.appId = appId
|
|
|
|
if (roleId) {
|
|
|
|
ctx.roleId = roleId
|
2021-04-22 12:45:22 +02:00
|
|
|
const userId = ctx.user ? generateUserMetadataID(ctx.user._id) : null
|
2021-04-20 18:17:44 +02:00
|
|
|
ctx.user = {
|
|
|
|
...ctx.user,
|
|
|
|
// override userID with metadata one
|
|
|
|
_id: userId,
|
|
|
|
userId,
|
2021-05-27 15:53:41 +02:00
|
|
|
roleId,
|
2021-04-20 18:17:44 +02:00
|
|
|
role: await getRole(appId, roleId),
|
2021-04-13 15:35:00 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-20 18:17:44 +02:00
|
|
|
if (updateCookie) {
|
2021-04-13 15:35:00 +02:00
|
|
|
setCookie(ctx, { appId, roleId }, Cookies.CurrentApp)
|
|
|
|
}
|
|
|
|
return next()
|
2021-04-12 19:31:58 +02:00
|
|
|
}
|