2022-11-16 19:12:31 +01:00
|
|
|
import {
|
|
|
|
DocumentType,
|
|
|
|
SEPARATOR,
|
|
|
|
ViewName,
|
|
|
|
getAllApps,
|
|
|
|
queryGlobalView,
|
2022-11-24 19:48:51 +01:00
|
|
|
} from "../db"
|
|
|
|
import { options } from "../middleware/passport/jwt"
|
|
|
|
import { Header, Cookie, MAX_VALID_DATE } from "../constants"
|
|
|
|
import env from "../environment"
|
|
|
|
import * as userCache from "../cache/user"
|
|
|
|
import { getSessionsForUser, invalidateSessions } from "../security/sessions"
|
|
|
|
import * as events from "../events"
|
|
|
|
import tenancy from "../tenancy"
|
2022-11-11 12:10:07 +01:00
|
|
|
import {
|
|
|
|
App,
|
|
|
|
BBContext,
|
|
|
|
PlatformLogoutOpts,
|
|
|
|
TenantResolutionStrategy,
|
|
|
|
} from "@budibase/types"
|
2022-11-09 17:35:16 +01:00
|
|
|
import { SetOption } from "cookies"
|
2021-04-11 12:35:55 +02:00
|
|
|
const jwt = require("jsonwebtoken")
|
2021-04-08 12:20:37 +02:00
|
|
|
|
2022-08-11 14:50:05 +02:00
|
|
|
const APP_PREFIX = DocumentType.APP + SEPARATOR
|
2022-03-23 17:45:06 +01:00
|
|
|
const PROD_APP_PREFIX = "/app/"
|
2021-04-08 12:20:37 +02:00
|
|
|
|
2022-11-09 17:35:16 +01:00
|
|
|
function confirmAppId(possibleAppId: string | undefined) {
|
2021-04-08 12:20:37 +02:00
|
|
|
return possibleAppId && possibleAppId.startsWith(APP_PREFIX)
|
|
|
|
? possibleAppId
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
|
2022-11-09 17:35:16 +01:00
|
|
|
async function resolveAppUrl(ctx: BBContext) {
|
2022-03-23 17:45:06 +01:00
|
|
|
const appUrl = ctx.path.split("/")[2]
|
|
|
|
let possibleAppUrl = `/${appUrl.toLowerCase()}`
|
|
|
|
|
2022-11-16 19:12:31 +01:00
|
|
|
let tenantId: string | null = tenancy.getTenantId()
|
2022-11-16 12:34:16 +01:00
|
|
|
if (env.MULTI_TENANCY) {
|
|
|
|
// always use the tenant id from the subdomain in multi tenancy
|
2022-11-09 17:35:16 +01:00
|
|
|
// this ensures the logged-in user tenant id doesn't overwrite
|
|
|
|
// e.g. in the case of viewing a public app while already logged-in to another tenant
|
|
|
|
tenantId = tenancy.getTenantIDFromCtx(ctx, {
|
|
|
|
includeStrategies: [TenantResolutionStrategy.SUBDOMAIN],
|
|
|
|
})
|
2022-03-23 17:45:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// search prod apps for a url that matches
|
2022-11-09 17:35:16 +01:00
|
|
|
const apps: App[] = await tenancy.doInTenant(tenantId, () =>
|
2022-03-23 17:45:06 +01:00
|
|
|
getAllApps({ dev: false })
|
|
|
|
)
|
|
|
|
const app = apps.filter(
|
|
|
|
a => a.url && a.url.toLowerCase() === possibleAppUrl
|
|
|
|
)[0]
|
|
|
|
|
|
|
|
return app && app.appId ? app.appId : undefined
|
|
|
|
}
|
2022-09-06 13:25:57 +02:00
|
|
|
|
2022-11-16 19:12:31 +01:00
|
|
|
export function isServingApp(ctx: BBContext) {
|
2022-09-06 13:25:57 +02:00
|
|
|
// dev app
|
|
|
|
if (ctx.path.startsWith(`/${APP_PREFIX}`)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// prod app
|
|
|
|
if (ctx.path.startsWith(PROD_APP_PREFIX)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-03-23 17:45:06 +01:00
|
|
|
|
2021-04-08 12:20:37 +02:00
|
|
|
/**
|
|
|
|
* Given a request tries to find the appId, which can be located in various places
|
|
|
|
* @param {object} ctx The main request body to look through.
|
|
|
|
* @returns {string|undefined} If an appId was found it will be returned.
|
|
|
|
*/
|
2022-11-16 19:12:31 +01:00
|
|
|
export async function getAppIdFromCtx(ctx: BBContext) {
|
2022-03-23 17:45:06 +01:00
|
|
|
// look in headers
|
2022-11-16 18:23:12 +01:00
|
|
|
const options = [ctx.headers[Header.APP_ID]]
|
2021-04-08 12:20:37 +02:00
|
|
|
let appId
|
|
|
|
for (let option of options) {
|
2022-11-09 17:35:16 +01:00
|
|
|
appId = confirmAppId(option as string)
|
2021-04-08 12:20:37 +02:00
|
|
|
if (appId) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 17:45:06 +01:00
|
|
|
// look in body
|
2021-04-08 12:20:37 +02:00
|
|
|
if (!appId && ctx.request.body && ctx.request.body.appId) {
|
|
|
|
appId = confirmAppId(ctx.request.body.appId)
|
|
|
|
}
|
2022-03-23 17:45:06 +01:00
|
|
|
|
|
|
|
// look in the url - dev app
|
2021-04-08 12:20:37 +02:00
|
|
|
let appPath =
|
|
|
|
ctx.request.headers.referrer ||
|
|
|
|
ctx.path.split("/").filter(subPath => subPath.startsWith(APP_PREFIX))
|
2022-03-23 17:45:06 +01:00
|
|
|
if (!appId && appPath.length) {
|
2021-04-08 12:20:37 +02:00
|
|
|
appId = confirmAppId(appPath[0])
|
|
|
|
}
|
2022-03-23 17:45:06 +01:00
|
|
|
|
|
|
|
// look in the url - prod app
|
|
|
|
if (!appId && ctx.path.startsWith(PROD_APP_PREFIX)) {
|
|
|
|
appId = confirmAppId(await resolveAppUrl(ctx))
|
|
|
|
}
|
|
|
|
|
2021-04-08 12:20:37 +02:00
|
|
|
return appId
|
|
|
|
}
|
|
|
|
|
2021-12-17 15:08:48 +01:00
|
|
|
/**
|
|
|
|
* opens the contents of the specified encrypted JWT.
|
|
|
|
* @return {object} the contents of the token.
|
|
|
|
*/
|
2022-11-16 19:12:31 +01:00
|
|
|
export function openJwt(token: string) {
|
2021-12-17 15:08:48 +01:00
|
|
|
if (!token) {
|
|
|
|
return token
|
|
|
|
}
|
|
|
|
return jwt.verify(token, options.secretOrKey)
|
|
|
|
}
|
|
|
|
|
2021-04-11 12:35:55 +02:00
|
|
|
/**
|
|
|
|
* Get a cookie from context, and decrypt if necessary.
|
|
|
|
* @param {object} ctx The request which is to be manipulated.
|
|
|
|
* @param {string} name The name of the cookie to get.
|
|
|
|
*/
|
2022-11-16 19:12:31 +01:00
|
|
|
export function getCookie(ctx: BBContext, name: string) {
|
2021-04-12 19:31:58 +02:00
|
|
|
const cookie = ctx.cookies.get(name)
|
2021-04-11 12:35:55 +02:00
|
|
|
|
2021-04-12 19:31:58 +02:00
|
|
|
if (!cookie) {
|
|
|
|
return cookie
|
|
|
|
}
|
2021-04-11 12:35:55 +02:00
|
|
|
|
2022-11-09 17:35:16 +01:00
|
|
|
return openJwt(cookie)
|
2021-04-11 12:35:55 +02:00
|
|
|
}
|
|
|
|
|
2021-04-08 12:20:37 +02:00
|
|
|
/**
|
2021-07-06 19:10:04 +02:00
|
|
|
* Store a cookie for the request - it will not expire.
|
2021-04-08 12:20:37 +02:00
|
|
|
* @param {object} ctx The request which is to be manipulated.
|
|
|
|
* @param {string} name The name of the cookie to set.
|
|
|
|
* @param {string|object} value The value of cookie which will be set.
|
2021-12-03 13:39:20 +01:00
|
|
|
* @param {object} opts options like whether to sign.
|
2021-04-08 12:20:37 +02:00
|
|
|
*/
|
2022-11-16 19:12:31 +01:00
|
|
|
export function setCookie(
|
2022-11-09 17:35:16 +01:00
|
|
|
ctx: BBContext,
|
|
|
|
value: any,
|
|
|
|
name = "builder",
|
|
|
|
opts = { sign: true }
|
2022-11-16 19:12:31 +01:00
|
|
|
) {
|
2021-12-03 13:39:20 +01:00
|
|
|
if (value && opts && opts.sign) {
|
2021-07-06 19:10:04 +02:00
|
|
|
value = jwt.sign(value, options.secretOrKey)
|
2021-09-29 14:51:33 +02:00
|
|
|
}
|
2021-09-28 17:35:31 +02:00
|
|
|
|
2022-11-09 17:35:16 +01:00
|
|
|
const config: SetOption = {
|
2021-12-03 13:39:20 +01:00
|
|
|
expires: MAX_VALID_DATE,
|
2021-09-29 14:51:33 +02:00
|
|
|
path: "/",
|
|
|
|
httpOnly: false,
|
|
|
|
overwrite: true,
|
|
|
|
}
|
2021-09-28 17:35:31 +02:00
|
|
|
|
2022-04-08 02:28:22 +02:00
|
|
|
if (env.COOKIE_DOMAIN) {
|
|
|
|
config.domain = env.COOKIE_DOMAIN
|
2021-04-08 12:20:37 +02:00
|
|
|
}
|
2021-09-29 14:51:33 +02:00
|
|
|
|
|
|
|
ctx.cookies.set(name, value, config)
|
2021-04-08 12:20:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility function, simply calls setCookie with an empty string for value
|
|
|
|
*/
|
2022-11-16 19:12:31 +01:00
|
|
|
export function clearCookie(ctx: BBContext, name: string) {
|
2022-11-09 17:35:16 +01:00
|
|
|
setCookie(ctx, null, name)
|
2021-04-08 12:20:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the API call being made (based on the provided ctx object) is from the client. If
|
|
|
|
* the call is not from a client app then it is from the builder.
|
|
|
|
* @param {object} ctx The koa context object to be tested.
|
|
|
|
* @return {boolean} returns true if the call is from the client lib (a built app rather than the builder).
|
|
|
|
*/
|
2022-11-16 19:12:31 +01:00
|
|
|
export function isClient(ctx: BBContext) {
|
2022-11-16 18:23:12 +01:00
|
|
|
return ctx.headers[Header.TYPE] === "client"
|
2021-04-08 12:20:37 +02:00
|
|
|
}
|
2021-04-19 18:31:47 +02:00
|
|
|
|
2022-11-16 19:12:31 +01:00
|
|
|
async function getBuilders() {
|
2022-08-11 14:50:05 +02:00
|
|
|
const builders = await queryGlobalView(ViewName.USER_BY_BUILDERS, {
|
2022-03-22 01:23:22 +01:00
|
|
|
include_docs: false,
|
|
|
|
})
|
2022-04-25 16:38:37 +02:00
|
|
|
|
|
|
|
if (!builders) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(builders)) {
|
|
|
|
return builders
|
|
|
|
} else {
|
|
|
|
return [builders]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 19:12:31 +01:00
|
|
|
export async function getBuildersCount() {
|
2022-04-25 16:38:37 +02:00
|
|
|
const builders = await getBuilders()
|
2022-03-25 17:08:12 +01:00
|
|
|
return builders.length
|
2022-03-04 14:42:50 +01:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:13:54 +02:00
|
|
|
/**
|
|
|
|
* Logs a user out from budibase. Re-used across account portal and builder.
|
|
|
|
*/
|
2022-11-16 19:12:31 +01:00
|
|
|
export async function platformLogout(opts: PlatformLogoutOpts) {
|
2022-11-09 17:35:16 +01:00
|
|
|
const ctx = opts.ctx
|
|
|
|
const userId = opts.userId
|
|
|
|
const keepActiveSession = opts.keepActiveSession
|
|
|
|
|
2021-10-13 13:26:26 +02:00
|
|
|
if (!ctx) throw new Error("Koa context must be supplied to logout.")
|
|
|
|
|
2022-11-16 19:12:31 +01:00
|
|
|
const currentSession = getCookie(ctx, Cookie.Auth)
|
2022-08-05 18:13:03 +02:00
|
|
|
let sessions = await getSessionsForUser(userId)
|
2021-10-12 17:13:54 +02:00
|
|
|
|
|
|
|
if (keepActiveSession) {
|
2021-10-13 13:26:26 +02:00
|
|
|
sessions = sessions.filter(
|
|
|
|
session => session.sessionId !== currentSession.sessionId
|
|
|
|
)
|
2021-10-12 20:49:34 +02:00
|
|
|
} else {
|
2021-10-13 13:26:26 +02:00
|
|
|
// clear cookies
|
2022-11-16 19:12:31 +01:00
|
|
|
clearCookie(ctx, Cookie.Auth)
|
|
|
|
clearCookie(ctx, Cookie.CurrentApp)
|
2021-10-12 17:13:54 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 18:13:03 +02:00
|
|
|
const sessionIds = sessions.map(({ sessionId }) => sessionId)
|
|
|
|
await invalidateSessions(userId, { sessionIds, reason: "logout" })
|
2022-05-23 23:14:44 +02:00
|
|
|
await events.auth.logout()
|
2022-03-03 08:20:30 +01:00
|
|
|
await userCache.invalidateUser(userId)
|
2021-10-12 17:13:54 +02:00
|
|
|
}
|
2022-06-23 21:22:51 +02:00
|
|
|
|
2022-11-16 19:12:31 +01:00
|
|
|
export function timeout(timeMs: number) {
|
2022-06-23 21:22:51 +02:00
|
|
|
return new Promise(resolve => setTimeout(resolve, timeMs))
|
|
|
|
}
|