Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/routes/oauth.authorize.tsx
T
2026-07-08 16:15:37 +02:00

88 lines
4.2 KiB
TypeScript

import { Form, useLoaderData } from "react-router";
import type { LoaderFunctionArgs } from "react-router";
import { requireUserId, getUserInfo } from "~/utils/session.server";
import { McpOAuthError, resolveAuthorizeRequest, type McpOAuthAuthorizeRequest } from "~/services/mcp/oauth.server";
import { getTranslations } from "~/locale/i18next.server";
import { loadRequiredWebConfig } from "@hivegrid/config";
type AuthorizeLabels = {
errorTitle: string;
title: string;
description: string;
approve: string;
deny: string;
};
export async function loader({ request }: LoaderFunctionArgs) {
const requestUrl = new URL(request.url);
const userId = await requireUserId(request, `${requestUrl.pathname}${requestUrl.search}`);
const { t } = await getTranslations(request);
const appName = loadRequiredWebConfig().appName;
try {
const resolved = await resolveAuthorizeRequest(requestUrl, userId);
const userInfo = await getUserInfo(request);
const labels: AuthorizeLabels = {
errorTitle: t("mcp.oauth.authorize.errorTitle"),
title: t("mcp.oauth.authorize.title", { appName }),
description: t("mcp.oauth.authorize.description", { clientName: resolved.client.clientName, tenantName: resolved.tenant.name }),
approve: t("mcp.oauth.authorize.approve"),
deny: t("mcp.oauth.authorize.deny"),
};
return Response.json({ ...resolved, csrf: userInfo.csrf ?? "", labels });
} catch (error) {
if (error instanceof McpOAuthError) {
return Response.json(
{ error: error.error, errorDescription: error.description, labels: { errorTitle: t("mcp.oauth.authorize.errorTitle") } },
{ status: error.status },
);
}
throw error;
}
}
export default function McpOAuthAuthorizeRoute() {
const data = useLoaderData<(McpOAuthAuthorizeRequest & { csrf: string; labels: AuthorizeLabels }) | { error: string; errorDescription: string; labels: { errorTitle: string } }>();
if ("error" in data) {
return (
<main className="flex min-h-screen items-center justify-center bg-gray-50 px-6 py-12">
<section className="w-full max-w-lg rounded-lg border border-red-200 bg-white p-6 shadow-sm">
<h1 className="text-xl font-semibold text-gray-950">{data.labels.errorTitle}</h1>
<p className="mt-3 text-sm text-gray-700">{data.errorDescription}</p>
</section>
</main>
);
}
return (
<main className="flex min-h-screen items-center justify-center bg-gray-50 px-6 py-12">
<section className="w-full max-w-lg rounded-lg border border-gray-200 bg-white p-6 shadow-sm">
<h1 className="text-xl font-semibold text-gray-950">{data.labels.title}</h1>
<p className="mt-3 text-sm text-gray-700">{data.labels.description}</p>
<ul className="mt-4 list-disc space-y-1 pl-5 text-sm text-gray-700">
{data.scopes.map((scope) => (
<li key={scope}>{scope}</li>
))}
</ul>
<Form method="post" action="/oauth/authorize/approve" className="mt-6 flex gap-3">
<input type="hidden" name="csrf" value={data.csrf} />
<input type="hidden" name="response_type" value="code" />
<input type="hidden" name="client_id" value={data.client.clientId} />
<input type="hidden" name="redirect_uri" value={data.redirectUri} />
<input type="hidden" name="scope" value={data.scopes.join(" ")} />
<input type="hidden" name="code_challenge" value={data.codeChallenge} />
<input type="hidden" name="code_challenge_method" value={data.codeChallengeMethod} />
<input type="hidden" name="resource" value={data.resource} />
<input type="hidden" name="tenant" value={data.tenant.id} />
{data.state ? <input type="hidden" name="state" value={data.state} /> : null}
<button name="decision" value="approve" className="rounded-md bg-gray-950 px-4 py-2 text-sm font-semibold text-white">
{data.labels.approve}
</button>
<button name="decision" value="deny" className="rounded-md border border-gray-300 px-4 py-2 text-sm font-semibold text-gray-800">
{data.labels.deny}
</button>
</Form>
</section>
</main>
);
}