2022-11-10 18:38:26 +01:00
|
|
|
import {
|
|
|
|
utils,
|
|
|
|
constants,
|
|
|
|
roles,
|
|
|
|
tenancy,
|
|
|
|
context,
|
2023-07-18 17:57:48 +02:00
|
|
|
users,
|
2023-12-04 17:47:41 +01:00
|
|
|
auth,
|
2022-11-10 18:38:26 +01:00
|
|
|
} from "@budibase/backend-core"
|
|
|
|
import { generateUserMetadataID, isDevAppID } from "../db/utils"
|
|
|
|
import { getCachedSelf } from "../utilities/global"
|
|
|
|
import env from "../environment"
|
2024-10-24 17:54:08 +02:00
|
|
|
import { isWebhookEndpoint, isBrowser, isApiKey } from "./utils"
|
2023-07-18 17:57:48 +02:00
|
|
|
import { UserCtx, ContextUser } from "@budibase/types"
|
2024-01-02 12:36:32 +01:00
|
|
|
import tracer from "dd-trace"
|
2024-12-05 14:37:20 +01:00
|
|
|
import type { Middleware, Next } from "koa"
|
2021-10-06 23:16:50 +02:00
|
|
|
|
2024-12-05 12:36:56 +01:00
|
|
|
const middleware = (async (ctx: UserCtx, next: Next) => {
|
2023-12-15 12:10:23 +01:00
|
|
|
// try to get the appID from the request
|
|
|
|
let requestAppId = await utils.getAppIdFromCtx(ctx)
|
|
|
|
if (!requestAppId) {
|
|
|
|
return next()
|
|
|
|
}
|
2023-01-12 16:38:22 +01:00
|
|
|
|
2024-01-02 12:36:32 +01:00
|
|
|
if (requestAppId) {
|
|
|
|
const span = tracer.scope().active()
|
|
|
|
span?.setTag("appId", requestAppId)
|
|
|
|
}
|
|
|
|
|
2023-12-15 12:10:23 +01:00
|
|
|
// deny access to application preview
|
2024-10-24 17:54:08 +02:00
|
|
|
if (isBrowser(ctx) && !isApiKey(ctx)) {
|
2023-12-15 12:10:23 +01:00
|
|
|
if (
|
|
|
|
isDevAppID(requestAppId) &&
|
|
|
|
!isWebhookEndpoint(ctx) &&
|
|
|
|
!users.isBuilder(ctx.user, requestAppId)
|
|
|
|
) {
|
|
|
|
return ctx.redirect("/")
|
2022-01-28 16:43:51 +01:00
|
|
|
}
|
2023-12-15 12:10:23 +01:00
|
|
|
}
|
2021-10-25 17:59:09 +02:00
|
|
|
|
2023-12-15 12:10:23 +01:00
|
|
|
let appId: string | undefined,
|
|
|
|
roleId = roles.BUILTIN_ROLE_IDS.PUBLIC
|
|
|
|
if (!ctx.user?._id) {
|
|
|
|
// not logged in, try to set a cookie for public apps
|
|
|
|
appId = requestAppId
|
|
|
|
} else if (requestAppId != null) {
|
|
|
|
// Different App ID means cookie needs reset, or if the same public user has logged in
|
|
|
|
const globalUser = await getCachedSelf(ctx, requestAppId)
|
|
|
|
appId = requestAppId
|
|
|
|
// retrieving global user gets the right role
|
|
|
|
roleId = globalUser.roleId || roleId
|
2022-02-24 15:03:29 +01:00
|
|
|
|
2023-12-15 12:10:23 +01:00
|
|
|
// Allow builders to specify their role via a header
|
|
|
|
const isBuilder = users.isBuilder(globalUser, appId)
|
|
|
|
const isDevApp = appId && isDevAppID(appId)
|
|
|
|
const roleHeader =
|
|
|
|
ctx.request &&
|
|
|
|
(ctx.request.headers[constants.Header.PREVIEW_ROLE] as string)
|
|
|
|
if (isBuilder && isDevApp && roleHeader) {
|
2024-10-22 16:03:13 +02:00
|
|
|
roleId = roleHeader
|
|
|
|
// Delete admin and builder flags so that the specified role is honoured
|
|
|
|
ctx.user = users.removePortalUserPermissions(ctx.user) as ContextUser
|
2022-02-24 15:03:29 +01:00
|
|
|
}
|
2023-12-15 12:10:23 +01:00
|
|
|
}
|
2021-10-06 23:16:50 +02:00
|
|
|
|
2023-12-15 12:10:23 +01:00
|
|
|
// nothing more to do
|
|
|
|
if (!appId) {
|
|
|
|
return next()
|
|
|
|
}
|
2021-11-26 14:25:02 +01:00
|
|
|
|
2024-01-02 12:36:32 +01:00
|
|
|
if (ctx.user) {
|
|
|
|
const span = tracer.scope().active()
|
|
|
|
if (ctx.user._id) {
|
|
|
|
span?.setTag("userId", ctx.user._id)
|
|
|
|
}
|
|
|
|
span?.setTag("tenantId", ctx.user.tenantId)
|
|
|
|
}
|
|
|
|
|
2023-12-15 12:10:23 +01:00
|
|
|
const userId = ctx.user ? generateUserMetadataID(ctx.user._id!) : undefined
|
2023-12-04 17:47:41 +01:00
|
|
|
|
2023-12-15 12:10:23 +01:00
|
|
|
// if the user is not in the right tenant then make sure to wipe their cookie
|
|
|
|
// also cleanse any information about them that has been allocated
|
|
|
|
// this avoids apps making calls to say the worker which are cross tenant,
|
|
|
|
// we simply remove the authentication
|
|
|
|
if (
|
|
|
|
env.MULTI_TENANCY &&
|
|
|
|
userId &&
|
|
|
|
requestAppId &&
|
|
|
|
!tenancy.isUserInAppTenant(requestAppId, ctx.user)
|
|
|
|
) {
|
|
|
|
// clear out the user
|
|
|
|
ctx.user = users.cleanseUserObject(ctx.user) as ContextUser
|
|
|
|
ctx.isAuthenticated = false
|
|
|
|
roleId = roles.BUILTIN_ROLE_IDS.PUBLIC
|
|
|
|
// remove the cookie, so future calls are public
|
|
|
|
await auth.platformLogout({
|
|
|
|
ctx,
|
|
|
|
userId,
|
|
|
|
})
|
|
|
|
}
|
2021-08-05 10:59:08 +02:00
|
|
|
|
2023-12-15 12:10:23 +01:00
|
|
|
return context.doInAppContext(appId, async () => {
|
|
|
|
ctx.appId = appId
|
|
|
|
if (roleId) {
|
|
|
|
ctx.roleId = roleId
|
|
|
|
const globalId = ctx.user ? ctx.user._id : undefined
|
|
|
|
ctx.user = {
|
|
|
|
...ctx.user!,
|
|
|
|
// override userID with metadata one
|
|
|
|
_id: userId,
|
|
|
|
userId,
|
|
|
|
globalId,
|
|
|
|
roleId,
|
|
|
|
role: await roles.getRole(roleId, { defaultPublic: true }),
|
2023-12-14 18:26:48 +01:00
|
|
|
}
|
2023-12-15 12:10:23 +01:00
|
|
|
}
|
2023-12-14 18:26:48 +01:00
|
|
|
|
2023-12-15 12:10:23 +01:00
|
|
|
return next()
|
2022-01-28 16:43:51 +01:00
|
|
|
})
|
2024-12-05 12:36:56 +01:00
|
|
|
}) as Middleware
|
|
|
|
|
|
|
|
export default middleware
|