Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions apps/api/src/lib/integrations/requireOrgMember.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, test } from "bun:test";
import { auth } from "@superset/auth/server";
import { stub } from "../../../test/stub";
import { requireOrgMember } from "./requireOrgMember";

stub(auth.api, { getSession: async () => null });

async function signInRedirect(connect: string): Promise<URL> {
const response = await requireOrgMember(new Request(connect));
if (!(response instanceof Response)) throw new Error("expected a response");
expect(response.status).toBe(302);
return new URL(response.headers.get("location") ?? "");
}

describe("requireOrgMember", () => {
test("a browser with no session is sent to sign in and back to the connect link", async () => {
const connect =
"https://api.superset.sh/api/connectors/ahrefs_mcp/connect?organizationId=org-1&method=oauth2&plugin=ahrefs";

const location = await signInRedirect(connect);

expect(location.origin).toBe("https://app.superset.sh");
expect(location.pathname).toBe("/sign-in");
expect(location.searchParams.get("redirect")).toBe(connect);
});

test("sign-in happens on the connect link's own root domain, where its cookie will be sent", async () => {
const connect =
"https://api.paradev.com/api/connectors/ahrefs_mcp/connect?organizationId=org-1&method=oauth2";

const location = await signInRedirect(connect);

expect(location.origin).toBe("https://app.paradev.com");
expect(location.searchParams.get("redirect")).toBe(connect);
});
});
29 changes: 26 additions & 3 deletions apps/api/src/lib/integrations/requireOrgMember.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import { auth } from "@superset/auth/server";
import { findOrgMembership } from "@superset/db/utils";
import {
OWN_ROOT_DOMAINS,
withOwnDomainTwins,
} from "@superset/shared/own-domains";
import { env } from "@/env";

export type OrgMember = {
organizationId: string;
userId: string;
role: string;
};

function ownRootOf(hostname: string): string | undefined {
return OWN_ROOT_DOMAINS.find(
(root) => hostname === root || hostname.endsWith(`.${root}`),
);
}

/** The session cookie is scoped to one root domain, so sign in on the request's. */
function signInUrl(request: Request): URL {
const root = ownRootOf(new URL(request.url).hostname);
const web =
withOwnDomainTwins([env.NEXT_PUBLIC_WEB_URL]).find(
(url) => ownRootOf(new URL(url).hostname) === root,
) ?? env.NEXT_PUBLIC_WEB_URL;
const signIn = new URL("/sign-in", web);
signIn.searchParams.set("redirect", request.url);
return signIn;
}

/**
* The connect-route preamble: who is asking, for which organization, and
* whether they belong to it. Returns the error response to send when a check
* fails.
* whether they belong to it. Returns the response to send when a check fails:
* a sign-in redirect that comes back here, or an error.
*
* It deliberately hands back no state token: a state is only ever minted by
* `beginOAuthFlow`, which binds it to the asking browser in the same step.
Expand All @@ -20,7 +43,7 @@ export async function requireOrgMember(
): Promise<OrgMember | Response> {
const session = await auth.api.getSession({ headers: request.headers });
if (!session?.user) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
return Response.redirect(signInUrl(request).toString(), 302);
}

const organizationId = new URL(request.url).searchParams.get(
Expand Down
Loading