151 lines
7.4 KiB
TypeScript
151 lines
7.4 KiB
TypeScript
import { Form, useLoaderData } from "react-router";
|
|
import type { LoaderFunctionArgs } from "react-router";
|
|
import { FeatherAlertTriangle } from "@subframe/core";
|
|
import { FeatherEye, FeatherPencil } from "@subframe/core";
|
|
import { describeMcpScope, resolveMcpClientIcon } from "@hivegrid/view-models";
|
|
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";
|
|
import { AuthShell, AuthStatus } from "~/components/auth/AuthShell";
|
|
import { ClientIcon } from "~/components/mcp/ClientIcon";
|
|
import { Button } from "~/ui/components/Button";
|
|
|
|
type AuthorizeLabels = {
|
|
errorTitle: string;
|
|
title: string;
|
|
description: string;
|
|
scopesHeading: string;
|
|
revokeHint: 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 }),
|
|
scopesHeading: t("mcp.oauth.authorize.scopesHeading"),
|
|
revokeHint: t("mcp.oauth.authorize.revokeHint", { appName }),
|
|
approve: t("mcp.oauth.authorize.approve"),
|
|
deny: t("mcp.oauth.authorize.deny"),
|
|
};
|
|
// Same brand-mark derivation the connect dialog uses, so the client the
|
|
// user is about to approve looks the same here as it does afterwards.
|
|
const clientIcon = resolveMcpClientIcon(resolved.client.clientName);
|
|
// Every requested scope still gets its own row — the consent has to stay
|
|
// complete. Each carries a plain-language label where we have one; the raw
|
|
// scope stays visible underneath so nothing is paraphrased away.
|
|
const scopeRows = resolved.scopes.map((scope) => {
|
|
const described = describeMcpScope(scope);
|
|
return { ...described, label: described.labelKey ? t(described.labelKey) : null };
|
|
});
|
|
return Response.json({ ...resolved, csrf: userInfo.csrf ?? "", clientIcon, scopeRows, 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;
|
|
}
|
|
}
|
|
|
|
type ScopeRow = { scope: string; labelKey: string | null; label: string | null; write: boolean };
|
|
type AuthorizeData = McpOAuthAuthorizeRequest & { csrf: string; clientIcon: string; scopeRows: ScopeRow[]; labels: AuthorizeLabels };
|
|
type AuthorizeError = { error: string; errorDescription: string; labels: { errorTitle: string } };
|
|
|
|
export default function McpOAuthAuthorizeRoute() {
|
|
const data = useLoaderData<AuthorizeData | AuthorizeError>();
|
|
|
|
if ("error" in data) {
|
|
return (
|
|
<AuthStatus
|
|
icon={<FeatherAlertTriangle />}
|
|
tone="negative"
|
|
title={data.labels.errorTitle}
|
|
message={data.errorDescription}
|
|
dataUiId="ui.mcp.oauth.authorize.error"
|
|
/>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<AuthShell
|
|
title={data.labels.title}
|
|
subtitle={data.labels.description}
|
|
titleAlign="start"
|
|
cardSize="large"
|
|
showLogo={true}
|
|
dataUiId="ui.mcp.oauth.authorize.page"
|
|
>
|
|
<div className="border-border-default bg-surface-neutral-secondary flex w-full items-center gap-3 rounded-md border border-solid px-3 py-2.5">
|
|
<ClientIcon icon={data.clientIcon} className="h-5 w-5" />
|
|
<div className="flex min-w-0 grow flex-col">
|
|
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary truncate">{data.client.clientName}</span>
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary truncate">{data.tenant.name}</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex w-full flex-col gap-2">
|
|
<span className="text-body-small-semibold-0 font-body-small-semibold-0 text-text-neutral-primary">{data.labels.scopesHeading}</span>
|
|
<ul className="flex w-full list-none flex-col gap-1.5 p-0">
|
|
{data.scopeRows.map((row) => (
|
|
<li
|
|
key={row.scope}
|
|
className="border-border-default flex w-full items-start gap-2.5 rounded-md border border-solid px-3 py-2"
|
|
data-ui-id={`ui.mcp.oauth.authorize.scope.${row.scope}`}
|
|
>
|
|
<span className={`mt-0.5 flex h-4 w-4 shrink-0 [&>svg]:h-full [&>svg]:w-full ${row.write ? "text-icon-feedback-warning" : "text-icon-neutral-secondary"}`}>
|
|
{row.write ? <FeatherPencil /> : <FeatherEye />}
|
|
</span>
|
|
<div className="flex min-w-0 grow flex-col">
|
|
{row.label ? (
|
|
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-primary">{row.label}</span>
|
|
) : null}
|
|
{/* The raw scope stays on screen even when a label exists: it is
|
|
what actually gets granted, and an unlabelled scope must
|
|
never look like it was silently dropped. */}
|
|
<code className="font-mono text-body-extra-small-regular-0 text-text-neutral-tertiary break-all">{row.scope}</code>
|
|
</div>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</div>
|
|
|
|
<Form method="post" action="/oauth/authorize/approve" className="flex w-full flex-col 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 type="submit" name="decision" value="approve" variant="primary" size="large" className="w-full">
|
|
{data.labels.approve}
|
|
</Button>
|
|
{/* `neutral` is remapped to the same solid black as `primary` in
|
|
globals.css, so the decline action needs the outlined variant to
|
|
read as the secondary choice. */}
|
|
<Button type="submit" name="decision" value="deny" variant="secondary" size="large" className="w-full">
|
|
{data.labels.deny}
|
|
</Button>
|
|
</Form>
|
|
|
|
<p className="text-body-extra-small-regular-150 font-body-extra-small-regular-150 text-text-neutral-tertiary">{data.labels.revokeHint}</p>
|
|
</AuthShell>
|
|
);
|
|
}
|