UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

133 lines (132 loc) • 6.66 kB
import { o as getPluginRuntimeGatewayRequestScope } from "./gateway-request-scope-BvvDhs5D.mjs"; import { i as getGatewayPluginMetadataSnapshot } from "./current-plugin-metadata-state-BmYo9Q7A.mjs"; import { zi as validateMcpAuthLoginParams } from "./src-BX8OAe1J.mjs"; import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs"; import { p as errorShape } from "./error-codes-BzF7aRQ0.mjs"; import { n as isTransientNetworkError } from "./retryable-network-errors-D2gJmBqw.mjs"; import { t as WizardSession } from "./session-beMMG7GW.mjs"; import { i as createProviderBrowserAuthSession, n as ProviderBrowserSignInError, r as ProviderBrowserSignInUnavailableError } from "./provider-browser-auth-CczEsRoa.mjs"; import { t as assertValidParams } from "./validation-DwNk6A-B.mjs"; import { i as operatorMcpOAuthIdentity, t as mcpOAuthCliLoginSteps } from "./mcp-oauth-identity-B4O1ccto.mjs"; import { f as startMcpOAuthAuthorization, o as completeOAuthCallback, p as McpOAuthCredentialsRetainedError, t as cancelMcpOAuthAuthorization } from "./mcp-oauth-C_WlADYD.mjs"; import { n as resolveOperatorMcpOAuthConnection } from "./mcp-auth-status-wwhN90-8.mjs"; import { a as startGatewayWizard } from "./setup-admission-CcyIocc3.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/gateway/server-methods/mcp-auth-login.ts const mcpAuthLoginHandlers = { "mcp.authLogin": async (e) => { const { params: t, respond: n, context: r, client: i } = e; if (!assertValidParams(t, validateMcpAuthLoginParams, "mcp.authLogin", n)) return; const reject = (e) => n(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, e)); if (!i || !i.connect.scopes?.includes("operator.admin")) { reject("Sign-in requires an administrator connection."); return; } const a = getGatewayPluginMetadataSnapshot(); const resolveConnection = () => resolveOperatorMcpOAuthConnection({ config: r.getRuntimeConfig(), metadata: a, serverName: t.serverName }); const o = resolveConnection(); const s = r.getGatewayMethodRegistry?.(); const isOperatorOwned = () => getGatewayPluginMetadataSnapshot() === a && getPluginRuntimeGatewayRequestScope()?.pluginRegistry === s?.pluginRegistry; if (!o || !isOperatorOwned()) { reject("This MCP server cannot use operator browser sign-in. Check its existing account settings."); return; } const { config: c } = o; const l = operatorMcpOAuthIdentity(t.serverName, c.url, r.getRuntimeConfig()); const u = mcpOAuthCliLoginSteps(l); const d = u ? "MCP server" : "plugin"; if (!i.browserOrigin) { reject(u ? `Open Settings on this Gateway to sign in, or sign in from its terminal: ${u}.` : "Open Settings on this Gateway to sign in."); return; } const f = structuredClone(o.server); const assertServerCurrent = () => { if (i.invalidated || !i.connect.scopes?.includes("operator.admin")) throw new Error("Sign-in authority is no longer active."); if (r.getGatewayMethodRegistry?.() !== s || !isDeepStrictEqual(f, resolveConnection()?.server) || !isOperatorOwned()) throw new Error("This MCP server changed. Close this dialog and review its settings before signing in again."); }; await startGatewayWizard({ request: e, sessionId: t.sessionId, connectionBound: true, assertCurrent: assertServerCurrent, createSession: (e) => new WizardSession(async (n, a, o) => { let s; let f = false; const p = createProviderBrowserAuthSession({ signal: AbortSignal.any([a, ...i.connectionSignal ? [i.connectionSignal] : []]), browserOrigin: i.browserOrigin, openUrl: async (e) => { m.assertCurrent(); await n.openUrl?.(e); m.assertCurrent(); } }); const m = { signal: p.signal, assertCurrent: () => { a.throwIfAborted(); e(); p.assertCurrent(); }, onAuthorizationPublished: (e) => { s = e; }, beforeTokensSaved: () => o.lockCancellation(), onTokensSaved: () => { f = true; } }; const failure = () => /* @__PURE__ */ new Error(f ? `Authentication saved, but sign-in cleanup did not finish. Close this dialog and check the ${d} before trying again.` : u ? `Sign-in did not finish. Check the MCP server settings and try again, or sign in from this Gateway's terminal: ${u}.` : "Sign-in did not finish. Check the plugin settings, then select Connect to try again."); try { try { const e = await p.authorizePrepared({ timeoutMs: 6e5, prepare: async (e) => { const t = await startMcpOAuthAuthorization(l, c, { redirectUrl: e, login: m }); return t.status === "authorized" ? { result: "authorized" } : t; } }); if (e !== "authorized" && await completeOAuthCallback(l, c, e, m) !== "authorized") throw new ProviderBrowserSignInError("expired"); } finally { p.close(); if (s) await cancelMcpOAuthAuthorization(l, s); } } catch (e) { if (f) throw failure(); if (e instanceof ProviderBrowserSignInUnavailableError) throw e; const { AuthorizationServerMismatchError: n, IssuerMismatchError: i, OAuthError: a, RegistrationRejectedError: o } = await import("@modelcontextprotocol/client"); let s = "unexpected"; let c; if (e instanceof i || e instanceof n) { s = "provider-mismatch"; c = "OpenClaw could not verify the sign-in provider. Close this dialog, review the connection settings, and try again."; } else if (e instanceof ProviderBrowserSignInError) { s = e.reason; c = e.reason === "expired" ? "This sign-in attempt expired. Close this dialog and start sign-in again." : "The provider did not approve sign-in. Close this dialog and try again."; } else if (e instanceof McpOAuthCredentialsRetainedError) { s = "credentials-retained"; c = "The sign-in service rejected this attempt. Existing credentials were not changed. Close this dialog, review the connection settings, and try again."; } else if (e instanceof o) { s = "registration-rejected"; c = "The sign-in service rejected this connection's settings. Close this dialog and review the connection settings before trying again."; } else if (e instanceof a) { s = "provider-rejected"; c = "The sign-in service rejected this attempt. Close this dialog and try again."; } else if (isTransientNetworkError(e)) { s = "network"; c = "OpenClaw could not reach the sign-in service. Check this Gateway's network connection, then close this dialog and try again."; } r.logGateway.warn(`MCP sign-in failed for server "${t.serverName}": ${s}`); throw c ? new Error(c) : failure(); } }) }); } }; //#endregion export { mcpAuthLoginHandlers };