1
0
Fork 0
mirror of synced 2024-09-27 06:42:03 +12:00
budibase/packages/worker/src/api/index.ts

186 lines
4 KiB
TypeScript
Raw Normal View History

import Router from "@koa/router"
2023-11-21 09:52:29 +13:00
const compress = require("koa-compress")
2023-11-21 09:52:29 +13:00
2023-04-17 21:27:37 +12:00
import zlib from "zlib"
import { routes } from "./routes"
import { middleware as pro, sdk } from "@budibase/pro"
2024-07-09 01:21:07 +12:00
import { auth, middleware, env } from "@budibase/backend-core"
if (env.SQS_SEARCH_ENABLE) {
sdk.auditLogs.useSQLSearch()
}
const PUBLIC_ENDPOINTS = [
// deprecated single tenant sso callback
{
route: "/api/admin/auth/google/callback",
method: "GET",
},
// deprecated single tenant sso callback
{
route: "/api/admin/auth/oidc/callback",
method: "GET",
},
{
// this covers all of the POST auth routes
route: "/api/global/auth/:tenantId",
method: "POST",
},
{
// this covers all of the GET auth routes
route: "/api/global/auth/:tenantId",
2021-06-28 02:46:04 +12:00
method: "GET",
},
{
// this covers all of the public config routes
route: "/api/global/configs/public",
method: "GET",
},
{
route: "/api/global/configs/checklist",
2021-08-04 21:02:24 +12:00
method: "GET",
},
2021-05-06 22:56:53 +12:00
{
route: "/api/global/users/init",
method: "POST",
2021-05-06 22:56:53 +12:00
},
{
route: "/api/global/users/sso",
method: "POST",
},
{
route: "/api/global/users/invite/accept",
method: "POST",
},
{
2022-11-17 02:06:30 +13:00
route: "/api/system/environment",
2021-08-04 21:02:24 +12:00
method: "GET",
},
{
2022-11-17 02:06:30 +13:00
route: "/api/system/status",
method: "GET",
},
// TODO: This should be an internal api
2021-09-07 22:22:11 +12:00
{
route: "/api/global/users/tenant/:id",
method: "GET",
},
// TODO: This should be an internal api
{
route: "/api/system/restored",
method: "POST",
},
2023-01-28 02:44:57 +13:00
{
route: "/api/global/users/invite",
method: "GET",
},
{
route: "/api/global/tenant",
method: "POST",
},
]
const NO_TENANCY_ENDPOINTS = [
// system endpoints are not specific to any tenant
2021-08-04 21:02:24 +12:00
{
route: "/api/system",
method: "ALL",
},
// tenant is determined in request body
// used for creating the tenant
{
route: "/api/global/users/init",
method: "POST",
},
// tenant is retrieved from the user found by the requested email
{
route: "/api/global/users/sso",
method: "POST",
},
// deprecated single tenant sso callback
{
route: "/api/admin/auth/google/callback",
method: "GET",
},
// deprecated single tenant sso callback
{
route: "/api/admin/auth/oidc/callback",
method: "GET",
},
// global user search - no tenancy
// :id is user id
// TODO: this should really be `/api/system/users/:id`
{
route: "/api/global/users/tenant/:id",
method: "GET",
},
// tenant is determined from code in redis
{
route: "/api/global/users/invite/accept",
method: "POST",
},
{
route: "/api/global/users/invite/:code",
method: "GET",
},
{
route: "/api/global/tenant",
method: "POST",
},
{
route: "/api/global/tenant/:id",
method: "GET",
},
2021-04-27 02:44:28 +12:00
]
2022-01-26 11:54:50 +13:00
// most public endpoints are gets, but some are posts
// add them all to be safe
const NO_CSRF_ENDPOINTS = [...PUBLIC_ENDPOINTS]
const router: Router = new Router()
router
.use(middleware.errorHandling)
.use(
compress({
threshold: 2048,
gzip: {
2021-03-30 03:06:00 +13:00
flush: zlib.constants.Z_SYNC_FLUSH,
},
deflate: {
2021-03-30 03:06:00 +13:00
flush: zlib.constants.Z_SYNC_FLUSH,
},
br: false,
})
)
2021-05-04 22:32:22 +12:00
.use("/health", ctx => (ctx.status = 200))
2022-08-26 09:56:58 +12:00
.use(auth.buildAuthMiddleware(PUBLIC_ENDPOINTS))
.use(auth.buildTenancyMiddleware(PUBLIC_ENDPOINTS, NO_TENANCY_ENDPOINTS))
.use(auth.buildCsrfMiddleware({ noCsrfPatterns: NO_CSRF_ENDPOINTS }))
.use(pro.licensing())
// for now no public access is allowed to worker (bar health check)
.use((ctx, next) => {
if (ctx.publicEndpoint) {
return next()
}
if (
(!ctx.isAuthenticated || (ctx.user && !ctx.user.budibaseAccess)) &&
!ctx.internal
) {
ctx.throw(403, "Unauthorized")
2021-05-12 23:38:49 +12:00
}
return next()
})
2022-08-26 09:56:58 +12:00
.use(middleware.auditLog)
2021-05-04 22:32:22 +12:00
router.get("/health", ctx => (ctx.status = 200))
// authenticated routes
for (let route of routes) {
router.use(route.routes())
router.use(route.allowedMethods())
}
export default router