openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
104 lines (103 loc) • 3.94 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { r as captureOpenClawStateWorkerContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs";
import { n as resolveMcpTransportConfig } from "./mcp-transport-config-r6XeT3o8.mjs";
import { i as renderOAuthPage } from "./oauth-page-UJbx3jkp.mjs";
import { t as OAUTH_PAGE_CSP } from "./oauth-page-csp-fg7Bc2uR.mjs";
import { o as requesterMcpOAuthStoreKeyPrefix } from "./mcp-oauth-identity-B4O1ccto.mjs";
import { c as readMcpOAuthPendingAuthorization, l as readMcpOAuthStore } from "./mcp-oauth-store-Cp55xn8F.mjs";
import { o as completeOAuthCallback } from "./mcp-oauth-C_WlADYD.mjs";
//#region src/gateway/mcp-oauth-callback.ts
const MCP_OAUTH_CALLBACK_PATH = "/oauth/mcp/callback";
const MCP_OAUTH_CALLBACK_MAX_URL_BYTES = 8192;
const CONNECTED_HTML = renderOAuthPage({
title: "Account connected",
heading: "You're connected.",
message: "Return to the chat."
});
const RETRY_HTML = renderOAuthPage({
title: "Sign-in incomplete",
heading: "Sign-in wasn't completed.",
message: "Ask the bot to connect again."
});
const EXPIRED_HTML = renderOAuthPage({
title: "Sign-in link expired",
heading: "This sign-in link expired or was already used.",
message: "Ask the bot to connect again."
});
function respondHtml(e, t, n) {
e.statusCode = t;
e.setHeader("Cache-Control", "no-store");
e.setHeader("Content-Type", "text/html; charset=utf-8");
e.setHeader("Content-Security-Policy", OAUTH_PAGE_CSP);
e.end(n);
}
function isPerRequesterServer(e) {
const t = isRecord(e.oauth) ? e.oauth : void 0;
return e.enabled !== false && e.auth === "oauth" && t?.identity === "per-requester";
}
/** Completes one requester MCP OAuth redirect using durable state correlation. */
async function handleMcpOAuthCallback(e, t, n) {
if (e.method !== "GET") return false;
const r = e.url ?? "/";
const i = new URL(r, "http://localhost");
if (i.pathname !== MCP_OAUTH_CALLBACK_PATH) return false;
const a = Object.entries(n.config.mcp?.servers ?? {}).toSorted(([e], [t]) => e.localeCompare(t)).flatMap(([e, t]) => {
if (!isPerRequesterServer(t)) return [];
const n = resolveMcpTransportConfig(e, t, { logWarnings: false });
return n?.kind === "http" && n.auth === "oauth" ? [{
serverName: e,
resolved: n
}] : [];
});
if (a.length === 0) return false;
if (Buffer.byteLength(r, "utf8") > MCP_OAUTH_CALLBACK_MAX_URL_BYTES) {
respondHtml(t, 400, RETRY_HTML);
return true;
}
const o = i.searchParams.get("state")?.trim();
const s = captureOpenClawStateWorkerContext();
const c = o ? await readMcpOAuthPendingAuthorization(o, s) : void 0;
const l = c ? await readMcpOAuthStore(c, s) : void 0;
if (!c || !o || (URL.parse(l?.lastAuthorizationUrl ?? "")?.searchParams.get("state")?.trim() || void 0) !== o) {
respondHtml(t, 404, EXPIRED_HTML);
return true;
}
const u = a.find(({ serverName: e, resolved: t }) => c.startsWith(requesterMcpOAuthStoreKeyPrefix(e, t.url)));
if (!u) {
respondHtml(t, 404, EXPIRED_HTML);
return true;
}
if (i.searchParams.has("error")) {
respondHtml(t, 400, RETRY_HTML);
return true;
}
const d = i.searchParams.get("code")?.trim();
if (!d || i.searchParams.getAll("iss").length > 1) {
respondHtml(t, 400, RETRY_HTML);
return true;
}
const f = i.searchParams.get("iss");
try {
if (await completeOAuthCallback({
storeKey: c,
principal: "requester",
serverName: u.serverName,
serverUrl: u.resolved.url
}, u.resolved, {
code: d,
state: o,
...f === null ? {} : { iss: f }
}, void 0, s) === "expired") {
respondHtml(t, 404, EXPIRED_HTML);
return true;
}
respondHtml(t, 200, CONNECTED_HTML);
} catch (e) {
n.log.warn(`MCP OAuth callback failed for server "${u.serverName}": ${formatErrorMessage(e)}`);
respondHtml(t, 400, RETRY_HTML);
}
return true;
}
//#endregion
export { handleMcpOAuthCallback };