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