UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

134 lines (133 loc) • 4.77 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { t as stableStringify } from "./stable-stringify-vqde_3tT.mjs"; import { t as sha256Base64Url } from "./crypto-digest-B2m8wIBj.mjs"; import { h as readRuntimeConfig } from "./runtime-snapshot-WbwbY5b7.mjs"; import { f as isOperatorScope } from "./operator-scopes-CULmYfyY.mjs"; import "./io-4QsrsWTJ.mjs"; import { o as roleScopesAllow } from "./operator-scope-compat-CpFHu-FC.mjs"; import { n as checkGatewayWsBrowserOrigin } from "./origin-check-C_9X1y_u.mjs"; import { i as resolveGatewayAuthForConfig } from "./auth-resolve-C6_i-P4T.mjs"; import { t as resolveIdentityOperatorScopes } from "./operator-identity-scopes-CltjA6id.mjs"; //#region src/gateway/auth-policy.ts const policies = /* @__PURE__ */ new WeakMap(); /** Capture the grant principal together with its generation; null means no identity-derived scopes. */ function captureGatewayAuthPolicy(e, t) { let n = policies.get(e); const r = t && n?.captured.get(t); if (r) return r; const i = normalizeOptionalString(t?.verifiedIdentity); const a = t?.role; const o = t?.authMethod; const s = t?.authModeOverride; const c = t?.browserOrigin; const l = c?.remoteControlUiIngress !== void 0; if (!n) { const t = e.gateway; const r = t?.auth?.trustedProxy; n = { global: stableStringify({ trustedProxies: t?.trustedProxies?.toSorted(), allowRealIpFallback: t?.allowRealIpFallback, allowTailscale: t?.auth?.allowTailscale, trustedProxy: r && { ...r, requiredHeaders: (r.requiredHeaders ?? []).toSorted(), allowUsers: (r.allowUsers ?? []).toSorted() } }), identities: /* @__PURE__ */ new Map(), captured: /* @__PURE__ */ new WeakMap() }; policies.set(e, n); } const u = l ? void 0 : stableStringify([ a, i, o, s, c ]); let d = u === void 0 ? void 0 : n.identities.get(u); if (!d) { const t = a === "operator" && i ? [...new Set(resolveIdentityOperatorScopes(i, e.gateway?.auth?.identityScopes))].toSorted() : void 0; let r; let l; let f; if (o === "trusted-proxy" || o === "tailscale" || o === "none" || o === "password") { const t = resolveGatewayAuthForConfig({ config: e, authOverride: s ? { mode: s } : void 0, tailscaleMode: e.gateway?.tailscale?.mode }); if (o === "trusted-proxy") { r = t.mode === "trusted-proxy" && t.trustedProxy !== void 0; const e = t.trustedProxy?.allowUsers; l = Boolean(i && (!e?.length || e.includes(i))); } else if (o === "tailscale") r = t.allowTailscale && t.mode !== "trusted-proxy" && t.mode !== "none"; else if (o === "none") r = t.mode === "none"; else if (t.mode === "trusted-proxy") { r = Boolean(t.password); f = t.password ? sha256Base64Url(t.password) : void 0; } } d = Object.freeze({ generation: n.global, grantGeneration: stableStringify({ grant: t, methodEnabled: r, proxyUserAllowed: l, fallbackCredentialGeneration: f, browserOriginAllowed: c ? checkGatewayWsBrowserOrigin(c, e).ok : void 0 }), role: a, authMethod: o, authModeOverride: s, verifiedIdentity: i, browserOrigin: c && Object.freeze({ ...c }) }); if (u !== void 0) { n.identities.set(u, d); n.captured.set(d, d); } } return d; } /** Preserve persisted fingerprints while accepting equivalent identity-scope spellings. */ function areGatewayAuthGrantsEquivalent(e, t) { if (e === t) return true; const n = /^(.*"grant":)(\[[^\]]*\])(,.*)$/u; const r = n.exec(e); const i = n.exec(t); if (!r || !i || r[1] !== i[1] || r[3] !== i[3]) return false; try { const e = JSON.parse(r[2]); const t = JSON.parse(i[2]); if (!Array.isArray(e) || !e.every(isOperatorScope) || !Array.isArray(t) || !t.every(isOperatorScope)) return false; return roleScopesAllow({ role: "operator", requestedScopes: e, allowedScopes: t }) && roleScopesAllow({ role: "operator", requestedScopes: t, allowedScopes: e }); } catch { return false; } } function isGatewayAuthPolicyCurrent(e, t) { if (!e) return true; const n = t === void 0 ? readRuntimeConfig() : t; if (n === null) return false; const r = captureGatewayAuthPolicy(n, e); return r.generation === e.generation && areGatewayAuthGrantsEquivalent(e.grantGeneration, r.grantGeneration); } /** Accepted work follows committed access grants, not a transport's need to reauthenticate. */ function isGatewayAuthGrantCurrent(e, t) { if (!e) return true; const n = t === void 0 ? readRuntimeConfig() : t; return n !== null && areGatewayAuthGrantsEquivalent(e.grantGeneration, captureGatewayAuthPolicy(n, e).grantGeneration); } //#endregion export { isGatewayAuthGrantCurrent as n, isGatewayAuthPolicyCurrent as r, captureGatewayAuthPolicy as t };