UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

187 lines (186 loc) • 7.46 kB
import { a as isWssUrl } from "./url-protocol-DXxSDuQZ.mjs"; import { o as requireTlsFingerprint } from "./client-address-utils-Clpzy8Fr.mjs"; import { t as resolveGatewayPublicOrigin } from "./gateway-public-origin-SLjx6A2a.mjs"; import { r as trimToUndefined } from "./credential-planner-DCQbkiQO.mjs"; import { r as resolveExplicitGatewayAuth } from "./credentials-UDpmcQCV.mjs"; import { t as normalizeControlUiBasePath } from "./control-ui-shared-_tUmz6W8.mjs"; import { a as resolveGatewayDeviceAuthRoute, t as buildGatewayConnectionDetailsWithResolvers } from "./connection-details-BWzGtdxz.mjs"; import { n as resolveGatewayProbeSurfaceAuth, t as resolveGatewayInteractiveSurfaceAuth } from "./auth-surface-resolution-CJMC_AjZ.mjs"; import { n as resolveGatewayCredentialsWithSecretInputs } from "./credentials-secret-inputs-BBRl7DaH.mjs"; //#region src/gateway/tls-fingerprint.ts /** Resolve the certificate pin for one already-selected Gateway target. */ async function resolveGatewayConnectionTlsFingerprint(e) { const t = e.explicitTlsFingerprint ? requireTlsFingerprint(e.explicitTlsFingerprint) : void 0; if (t) return t; const n = e.config.gateway?.mode === "remote" && (e.urlSource === "config gateway.remote.url" || e.urlSource === "env OPENCLAW_GATEWAY_URL") ? e.config.gateway.remote?.tlsFingerprint ? requireTlsFingerprint(e.config.gateway.remote.tlsFingerprint) : void 0 : void 0; if (n) return n; if (!isWssUrl(e.url)) return; if (!(e.urlSource === "local loopback" || e.urlSource === "missing gateway.remote.url (fallback local)") || e.config.gateway?.tls?.enabled !== true) return; const { inspectGatewayTlsCertificate: r } = await import("./gateway-6xEaCcr5.mjs"); const i = await r(e.config.gateway.tls); return i.ok ? i.value.fingerprintSha256 : void 0; } //#endregion //#region src/gateway/client-bootstrap.ts var GatewayExplicitAuthRequiredError = class extends Error { constructor(e) { super(e); this.name = "GatewayExplicitAuthRequiredError"; } }; async function ensureExplicitGatewayAuth(e) { if (!e.urlOverride || !e.urlOverrideSource) return; if (e.explicitAuth?.token || e.explicitAuth?.password) return; if (e.urlOverrideSource === "env" && (e.resolvedAuth?.token || e.resolvedAuth?.password)) return; if (e.deviceAuthScope && await e.allowStoredOriginAuth?.(e.deviceAuthScope) === true) return; const t = e.urlOverrideSource === "env" ? "Set OPENCLAW_GATEWAY_TOKEN or OPENCLAW_GATEWAY_PASSWORD alongside OPENCLAW_GATEWAY_URL; config credentials are intentionally not reused." : "For the default local or SSH-tunneled Gateway, remove --url to use the configured target."; throw new GatewayExplicitAuthRequiredError([ "gateway url override requires explicit credentials", e.errorHint, t, e.configPath ? `Config: ${e.configPath}` : void 0 ].filter(Boolean).join("\n")); } /** Resolve the only URL overrides allowed to displace configured Gateway targets. */ function resolveGatewayUrlOverride(e) { const t = trimToUndefined(e.gatewayUrl); if (t) return { url: t, source: "cli" }; if (e.ignoreEnvUrlOverride || e.localPortOverride !== void 0) return {}; const n = trimToUndefined((e.env ?? process.env).OPENCLAW_GATEWAY_URL); return n ? { url: n, source: "env" } : {}; } /** * Resolves the URL, auth material, and handshake tuning needed to start a GatewayClient. */ async function resolveGatewayClientBootstrap(e) { const t = e.env ?? process.env; const n = resolveExplicitGatewayAuth(e.explicitAuth); const r = resolveGatewayUrlOverride({ gatewayUrl: e.gatewayUrl, env: t, ignoreEnvUrlOverride: e.ignoreEnvUrlOverride, localPortOverride: e.localPortOverride }); const i = e.buildConnectionDetails ?? buildGatewayConnectionDetailsWithResolvers; const a = i({ config: e.config, url: r.url, ...e.configPath ? { configPath: e.configPath } : {}, ...r.source ? { urlSource: r.source } : {}, ignoreEnvUrlOverride: true, ...e.localPortOverride !== void 0 ? { localPortOverride: e.localPortOverride } : {}, ...e.serviceTargetUrl ? { serviceTargetUrl: e.serviceTargetUrl } : {} }); const o = a.urlSource === "cli --url" ? "cli" : a.urlSource === "env OPENCLAW_GATEWAY_URL" ? "env" : void 0; const s = r.source ?? o; let c; if (e.allowConfiguredAuthForExactTarget && s === "cli") { if (e.config.gateway?.mode === "remote") { const t = trimToUndefined(e.config.gateway.remote?.url); if (t && t === a.url) c = { authSurface: "remote", tlsSource: "config gateway.remote.url" }; } else { const t = { ...e.config.gateway, mode: "local" }; delete t.remote; const n = i({ config: { ...e.config, gateway: t }, ignoreEnvUrlOverride: true, ...e.localPortOverride !== void 0 ? { localPortOverride: e.localPortOverride } : {} }).url; const r = normalizeControlUiBasePath(e.config.gateway?.controlUi?.basePath ?? ""); if (`${n}${r}` === a.url) c = { authSurface: "local", tlsSource: "local loopback" }; else { const t = resolveGatewayPublicOrigin(e.config); if (t && `${t.replace(/^https:/u, "wss:").replace(/^http:/u, "ws:")}${r}` === a.url) c = { authSurface: "local" }; } } } const l = c?.tlsSource ?? a.urlSource; const u = await resolveGatewayConnectionTlsFingerprint({ config: e.config, url: a.url, urlSource: l, explicitTlsFingerprint: e.explicitTlsFingerprint }); const d = c?.authSurface ?? e.modeOverride ?? (e.localPortOverride !== void 0 ? "local" : e.config.gateway?.mode === "remote" ? "remote" : "local"); let f; if (e.skipImplicitAuth) f = n; else if (s && !c) f = e.suppressEnvAuthFallback ? n : await resolveGatewayCredentialsWithSecretInputs({ config: e.config, explicitAuth: n, env: t, urlOverride: a.url, urlOverrideSource: s, modeOverride: e.modeOverride }); else if (e.authPolicy === "probe") f = await resolveGatewayProbeSurfaceAuth({ config: e.config, env: t, surface: d }); else if (e.authPolicy === "interactive") f = await resolveGatewayInteractiveSurfaceAuth({ config: e.config, env: t, explicitAuth: n, suppressEnvAuthFallback: e.suppressEnvAuthFallback, surface: d }); else f = await resolveGatewayCredentialsWithSecretInputs({ config: e.config, explicitAuth: n, env: t, urlOverride: s ? a.url : void 0, urlOverrideSource: s, modeOverride: d }); const { deviceAuthScope: p, sshTunnel: m } = resolveGatewayDeviceAuthRoute({ config: e.config, url: a.url, remote: Boolean(s || a.urlSource === "config gateway.remote.url"), configuredRemote: !s && a.urlSource === "config gateway.remote.url" || c?.authSurface === "remote", tlsFingerprint: u }); if (e.overrideAuthErrorHint && !c) await ensureExplicitGatewayAuth({ urlOverride: s ? a.url : void 0, urlOverrideSource: s, explicitAuth: n, resolvedAuth: f, deviceAuthScope: p, allowStoredOriginAuth: e.allowStoredOriginAuth, errorHint: e.overrideAuthErrorHint, configPath: e.configPath }); return { url: a.url, urlSource: a.urlSource, connectionDetails: a, ...s ? { urlOverrideSource: s } : {}, ...p ? { deviceAuthScope: p } : {}, ...m ? { sshTunnel: m } : {}, ...f.failureReason ? { authFailureReason: f.failureReason } : {}, ...u ? { tlsFingerprint: u } : {}, auth: { token: f.token, password: f.password } }; } //#endregion export { resolveGatewayUrlOverride as i, ensureExplicitGatewayAuth as n, resolveGatewayClientBootstrap as r, GatewayExplicitAuthRequiredError as t };