diff --git a/packages/backend-core/src/auth/auth.ts b/packages/backend-core/src/auth/auth.ts index 8de8904f3a..1951c7986c 100644 --- a/packages/backend-core/src/auth/auth.ts +++ b/packages/backend-core/src/auth/auth.ts @@ -19,7 +19,7 @@ import { GoogleInnerConfig, OIDCInnerConfig, PlatformLogoutOpts, - SessionInfo, + SessionCookie, SSOProviderType, } from "@budibase/types" import * as events from "../events" @@ -191,7 +191,7 @@ export async function platformLogout(opts: PlatformLogoutOpts) { if (!ctx) throw new Error("Koa context must be supplied to logout.") - const currentSession = getCookie(ctx, Cookie.Auth) + const currentSession = getCookie(ctx, Cookie.Auth) let sessions = await getSessionsForUser(userId) if (currentSession && keepActiveSession) { diff --git a/packages/backend-core/src/middleware/authenticated.ts b/packages/backend-core/src/middleware/authenticated.ts index 3e82498bdd..16f658b90a 100644 --- a/packages/backend-core/src/middleware/authenticated.ts +++ b/packages/backend-core/src/middleware/authenticated.ts @@ -13,7 +13,7 @@ import { getGlobalDB, doInTenant } from "../context" import { decrypt } from "../security/encryption" import * as identity from "../context/identity" import env from "../environment" -import { Ctx, EndpointMatcher, SessionInfo } from "@budibase/types" +import { Ctx, EndpointMatcher, SessionCookie } from "@budibase/types" import { InvalidAPIKeyError, ErrorCode } from "../errors" const ONE_MINUTE = env.SESSION_UPDATE_PERIOD @@ -99,8 +99,8 @@ export default function ( let headerToken = ctx.request.headers[Header.TOKEN] const authCookie = - getCookie(ctx, Cookie.Auth) || - openJwt(headerToken) + getCookie(ctx, Cookie.Auth) || + openJwt(headerToken) let apiKey = ctx.request.headers[Header.API_KEY] if (!apiKey && ctx.request.headers[Header.AUTHORIZATION]) { diff --git a/packages/backend-core/src/middleware/passport/datasource/google.ts b/packages/backend-core/src/middleware/passport/datasource/google.ts index 7f768f1623..ab4ffee9d2 100644 --- a/packages/backend-core/src/middleware/passport/datasource/google.ts +++ b/packages/backend-core/src/middleware/passport/datasource/google.ts @@ -3,7 +3,7 @@ import { Cookie } from "../../../constants" import * as configs from "../../../configs" import * as cache from "../../../cache" import * as utils from "../../../utils" -import { UserCtx, SSOProfile } from "@budibase/types" +import { UserCtx, SSOProfile, DatasourceAuthCookie } from "@budibase/types" import { ssoSaveUserNoOp } from "../sso/sso" const GoogleStrategy = require("passport-google-oauth").OAuth2Strategy diff --git a/packages/server/src/api/controllers/query/index.ts b/packages/server/src/api/controllers/query/index.ts index 472c0d6272..4d307e9593 100644 --- a/packages/server/src/api/controllers/query/index.ts +++ b/packages/server/src/api/controllers/query/index.ts @@ -9,7 +9,7 @@ import { quotas } from "@budibase/pro" import { events, context, utils, constants } from "@budibase/backend-core" import sdk from "../../../sdk" import { QueryEvent } from "../../../threads/definitions" -import { ConfigType, Query, UserCtx } from "@budibase/types" +import { ConfigType, Query, UserCtx, SessionCookie } from "@budibase/types" import { ValidQueryNameRegex } from "@budibase/shared-core" const Runner = new Thread(ThreadType.QUERY, { @@ -113,7 +113,7 @@ function getOAuthConfigCookieId(ctx: UserCtx) { } function getAuthConfig(ctx: UserCtx) { - const authCookie = utils.getCookie(ctx, constants.Cookie.Auth) + const authCookie = utils.getCookie(ctx, constants.Cookie.Auth) let authConfigCtx: any = {} authConfigCtx["configId"] = getOAuthConfigCookieId(ctx) authConfigCtx["sessionId"] = authCookie ? authCookie.sessionId : null diff --git a/packages/types/src/api/web/cookies.ts b/packages/types/src/api/web/cookies.ts new file mode 100644 index 0000000000..27954a36a1 --- /dev/null +++ b/packages/types/src/api/web/cookies.ts @@ -0,0 +1,9 @@ +export interface DatasourceAuthCookie { + appId: string + provider: string +} + +export interface SessionCookie { + sessionId: string + userId: string +} diff --git a/packages/types/src/api/web/global/index.ts b/packages/types/src/api/web/global/index.ts index e6e2a78feb..efcb6dc39c 100644 --- a/packages/types/src/api/web/global/index.ts +++ b/packages/types/src/api/web/global/index.ts @@ -4,4 +4,3 @@ export * from "./events" export * from "./configs" export * from "./scim" export * from "./license" -export * from "./sessions" diff --git a/packages/types/src/api/web/global/sessions.ts b/packages/types/src/api/web/global/sessions.ts deleted file mode 100644 index a6b94a3d24..0000000000 --- a/packages/types/src/api/web/global/sessions.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface SessionInfo { - sessionId: string - userId: string -} diff --git a/packages/types/src/api/web/index.ts b/packages/types/src/api/web/index.ts index cba1e04f9a..75c246ab9b 100644 --- a/packages/types/src/api/web/index.ts +++ b/packages/types/src/api/web/index.ts @@ -9,3 +9,4 @@ export * from "./app" export * from "./global" export * from "./pagination" export * from "./searchFilter" +export * from "./cookies" diff --git a/packages/worker/src/api/controllers/global/auth.ts b/packages/worker/src/api/controllers/global/auth.ts index 279162fb08..a94ed082f7 100644 --- a/packages/worker/src/api/controllers/global/auth.ts +++ b/packages/worker/src/api/controllers/global/auth.ts @@ -15,6 +15,7 @@ import { PasswordResetRequest, PasswordResetUpdateRequest, GoogleInnerConfig, + DatasourceAuthCookie, } from "@budibase/types" import env from "../../../environment" @@ -148,7 +149,13 @@ export const datasourcePreAuth = async (ctx: any, next: any) => { } export const datasourceAuth = async (ctx: any, next: any) => { - const authStateCookie = getCookie(ctx, Cookie.DatasourceAuth) + const authStateCookie = getCookie( + ctx, + Cookie.DatasourceAuth + ) + if (!authStateCookie) { + throw new Error("Unable to retrieve datasource authentication cookie") + } const provider = authStateCookie.provider const { middleware } = require(`@budibase/backend-core`) const handler = middleware.datasource[provider]