60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
import { CatalogError } from "@hivegrid/foundation";
|
|
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
|
|
|
import { loadMcpConnectContext, loadMcpConnectVM, revokeMcpConnection } from "~/services/mcp/connect.server";
|
|
import { validateCSRFToken } from "~/utils/session.server";
|
|
|
|
/**
|
|
* Resource route behind the app-shell "Mit KI verbinden" dialog. The dialog is
|
|
* reachable from every tenant page, so its data must not be tied to one page's
|
|
* loader — the shell fetches this route the first time the dialog opens.
|
|
*/
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const ctx = await loadMcpConnectContext({ request, params });
|
|
return Response.json(await loadMcpConnectVM(ctx));
|
|
};
|
|
|
|
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
|
const ctx = await loadMcpConnectContext({ request, params });
|
|
if (!hasFormContentType(request)) {
|
|
return Response.json({ error: "unsupported_media_type" }, { status: 415 });
|
|
}
|
|
|
|
let formData: FormData;
|
|
try {
|
|
formData = await request.formData();
|
|
} catch {
|
|
return Response.json({ error: "invalid_form_data" }, { status: 400 });
|
|
}
|
|
await validateCSRFToken(request, { formData });
|
|
|
|
if (formData.get("intent") !== "revoke") {
|
|
return Response.json({ error: "invalid_intent" }, { status: 400 });
|
|
}
|
|
|
|
const clientId = formData.get("clientId");
|
|
if (typeof clientId !== "string") {
|
|
return Response.json({ error: "invalid_client" }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
await revokeMcpConnection(ctx, clientId);
|
|
} catch (error) {
|
|
// An unknown client id is indistinguishable from someone else's connection
|
|
// on purpose; both surface as "not found" rather than confirming existence.
|
|
if (error instanceof CatalogError) {
|
|
return Response.json({ error: "not_found" }, { status: 404 });
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
return Response.json(await loadMcpConnectVM(ctx));
|
|
};
|
|
|
|
function hasFormContentType(request: Request): boolean {
|
|
const contentType = request.headers.get("content-type")?.toLowerCase() ?? "";
|
|
const mediaType = contentType.split(";", 1)[0]?.trim();
|
|
return mediaType === "application/x-www-form-urlencoded" || mediaType === "multipart/form-data";
|
|
}
|