UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

209 lines (208 loc) • 9.33 kB
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { o as asDateTimestampMs } from "./number-coercion-D6u-4YD8.mjs"; import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { i as copyAuthProfileAuthorizationIntent, r as copyAuthProfileAuthorizationInheritance } from "./authorization-lifetime-qBbc4Xfj.mjs"; import { i as loadBundledPluginPublicSurfaceModuleSyncCore, t as MissingPublicSurfaceError } from "./facade-loader-08DBXZOS.mjs"; import { t as cloneAuthProfileJsonValue } from "./clone-value-xvoXDr9c.mjs"; import { r as hasUsableOAuthCredential } from "./credential-state-BiAE74F8.mjs"; import { AsyncLocalStorage } from "node:async_hooks"; //#region src/agents/auth-profiles/credential-observation.ts const state = resolveGlobalSingleton(Symbol.for("openclaw.canonicalAuthCredentialObservation"), () => ({ scope: new AsyncLocalStorage(), origins: /* @__PURE__ */ new WeakMap() })); function matchesCanonicalCredential(e, t) { const equal = (e, t) => { if (e === t) return true; if (!e || !t || typeof e !== "object" || typeof t !== "object") return false; if (Array.isArray(e) !== Array.isArray(t)) return false; const n = Object.keys(e); const r = Object.keys(t); return n.length === r.length && n.every((n, i) => n === r[i] && equal(Reflect.get(e, n), Reflect.get(t, n))); }; return equal(e.credential, cloneAuthProfileJsonValue(t)); } async function withCanonicalAuthProfileCredentialObserver(e, t) { const n = { observer: e, parent: state.scope.getStore() }; try { return await state.scope.run(n, t); } finally { n.observer = void 0; } } function publish(e, t) { let n = state.scope.getStore(); if (!n?.observer || Object.keys(t).length === 0) return; while (n) { n.observer?.({ databasePath: e, profiles: cloneAuthProfileJsonValue(t) }); n = n.parent; } } /** Observe an existing canonical read or committed credential, never a runtime overlay. */ function observeCanonicalAuthProfileCredentials(e, t) { for (const [n, r] of Object.entries(t)) { const t = state.origins.get(r) ?? []; state.origins.set(r, [...t.filter((t) => t.databasePath !== e || t.profileId !== n), { databasePath: e, profileId: n, credential: cloneAuthProfileJsonValue(r) }]); } publish(e, t); } /** Cloning preserves evidence only for the exact credential bytes the owner observed. */ function copyCanonicalAuthProfileCredentialObservations(e, t) { for (const [n, r] of Object.entries(t)) { const t = e[n]; if (!t) continue; const i = state.origins.get(t)?.filter((e) => e.profileId === n && matchesCanonicalCredential(e, r)); if (i?.length) state.origins.set(r, i); } } /** Cached reads replay producer facts, not profile IDs or inferred directory ownership. */ function observeCachedCanonicalAuthProfileCredentials(e) { if (!state.scope.getStore()?.observer) return; const t = /* @__PURE__ */ new Map(); for (const [n, r] of Object.entries(e)) for (const e of state.origins.get(r) ?? []) { if (e.profileId !== n || !matchesCanonicalCredential(e, r)) continue; const i = t.get(e.databasePath) ?? /* @__PURE__ */ new Map(); i.set(n, r); t.set(e.databasePath, i); } for (const [e, n] of t) publish(e, Object.fromEntries(n)); } //#endregion //#region src/agents/auth-profiles/oauth-identity.ts function isSafeToCopyOAuthRoutingScope(e, t) { if (e.provider !== t.provider) return false; if (e.provider !== "github-copilot") return true; let n; try { n = loadBundledPluginPublicSurfaceModuleSyncCore({ dirName: "github-copilot", artifactBasename: "api.js", env: {} }); } catch (e) { if (e instanceof MissingPublicSurfaceError) return false; throw e; } const { normalizeGithubCopilotOAuthScope: r } = n; const i = r(e.enterpriseUrl); const a = r(t.enterpriseUrl); return i !== void 0 && i === a; } function hasOidcRegistration(e) { return e.issuer !== void 0 && e.clientId !== void 0; } /** Identity evidence includes OIDC registration, even when its subject is unavailable. */ function hasOAuthIdentity(e) { return hasOidcRegistration(e) || normalizeOptionalString(e.accountId) !== void 0 || normalizeOptionalLowercaseString(e.email) !== void 0; } /** * One-sided copy gate for both directions: * - mirror: sub-agent refresh -> main-agent store * - adopt: main-agent store -> sub-agent store */ function isSafeToCopyOAuthIdentity(e, t) { if (e.provider !== void 0 && t.provider !== void 0 && !isSafeToCopyOAuthRoutingScope({ provider: e.provider, enterpriseUrl: e.enterpriseUrl }, { provider: t.provider, enterpriseUrl: t.enterpriseUrl })) return false; const n = normalizeOptionalString(e.accountId); const r = normalizeOptionalString(t.accountId); if (hasOidcRegistration(e) || hasOidcRegistration(t)) return e.issuer === t.issuer && e.clientId === t.clientId && n !== void 0 && n === r; const i = normalizeOptionalLowercaseString(e.email); const a = normalizeOptionalLowercaseString(t.email); if (n !== void 0 && r !== void 0) return n === r; if (i !== void 0 && a !== void 0) return i === a; if (n !== void 0 || i !== void 0) return false; return true; } function resolveOAuthMirrorDecision(e) { const { existing: t, refreshed: n } = e; if (!t) return "mirror"; if (t.type !== "oauth") return "keep"; if (t.provider !== n.provider) return "keep"; if (!isSafeToCopyOAuthIdentity(t, n)) return "identity-mismatch-or-regression"; const r = asDateTimestampMs(n.expires); if (r === void 0) return "incoming-not-fresher"; const i = asDateTimestampMs(t.expires); if (i !== void 0 && i >= r) return "incoming-not-fresher"; return "mirror"; } //#endregion //#region src/agents/auth-profiles/clone.ts /** * Auth profile store cloning helpers. * Keeps store snapshots JSON-serializable before callers mutate or persist * profile state. */ /** Deep-clones an auth profile store and rejects non-JSON values. */ function cloneAuthProfileStore(e) { const t = cloneAuthProfileJsonValue(e); for (const [n, r] of Object.entries(e.profiles)) { const e = t.profiles[n]; if (e) copyAuthProfileAuthorizationIntent(r, e); } copyAuthProfileAuthorizationInheritance(e.profiles, t.profiles); copyCanonicalAuthProfileCredentialObservations(e.profiles, t.profiles); return t; } //#endregion //#region src/agents/auth-profiles/oauth-shared.ts function areOAuthCredentialsEquivalent(e, t) { if (!e || e.type !== "oauth") return false; return e.provider === t.provider && e.access === t.access && e.refresh === t.refresh && e.expires === t.expires && e.email === t.email && e.enterpriseUrl === t.enterpriseUrl && e.projectId === t.projectId && e.accountId === t.accountId && e.idToken === t.idToken; } function hasMatchingOAuthIdentity(e, t) { return hasOAuthIdentity(e) && isSafeToCopyOAuthIdentity(e, t); } function isSafeOAuthOwnerRefreshResult(e, t) { return e.provider === t.provider && isSafeToCopyOAuthIdentity(e, t); } function isSafeOAuthPostClaimSettlement(e, t) { return t?.type === "oauth" && t.provider === e.provider && hasUsableOAuthCredential(t) && hasMatchingOAuthIdentity(e, t); } function isSafeToAdoptBootstrapOAuthIdentity(e, t) { return !e || e.type !== "oauth" || isSafeOAuthOwnerRefreshResult(e, t); } function isSafeToAdoptMainStoreOAuthIdentity(e, t) { return e?.type === "oauth" && isSafeOAuthOwnerRefreshResult(e, t); } /** Overlays runtime external OAuth profiles on a cloned store. */ function overlayRuntimeExternalOAuthProfiles(e, t, n) { const r = Array.from(t); const i = cloneAuthProfileStore(e); const a = new Set(r.map((e) => e.profileId)); for (const e of r) { i.profiles[e.profileId] = e.credential; delete i.runtimeCredentialSources?.[e.profileId]; } i.runtimePersistedProfileIds = e.runtimePersistedProfileIds?.filter((e) => i.profiles[e] && !a.has(e)).toSorted(); if (i.runtimePersistedProfileIds?.length === 0) i.runtimePersistedProfileIds = void 0; const o = new Set(r.filter((e) => e.persistence !== "persisted").map((e) => e.profileId)); for (const t of e.runtimeExternalProfileIds ?? []) if (i.profiles[t]) o.add(t); i.runtimeExternalProfileIds = o.size > 0 || n?.runtimeExternalProfileIdsAuthoritative === true ? [...o].toSorted() : void 0; i.runtimeExternalProfileIdsAuthoritative = n?.runtimeExternalProfileIdsAuthoritative === true ? true : void 0; return i; } function shouldPersistRuntimeExternalOAuthProfile(e) { for (const t of e.profiles) { if (t.profileId !== e.profileId) continue; if (t.persistence === "persisted") return true; return !areOAuthCredentialsEquivalent(t.credential, e.credential); } return true; } //#endregion export { observeCanonicalAuthProfileCredentials as _, isSafeToAdoptBootstrapOAuthIdentity as a, shouldPersistRuntimeExternalOAuthProfile as c, hasOidcRegistration as d, isSafeToCopyOAuthIdentity as f, observeCachedCanonicalAuthProfileCredentials as g, copyCanonicalAuthProfileCredentialObservations as h, isSafeOAuthPostClaimSettlement as i, cloneAuthProfileStore as l, resolveOAuthMirrorDecision as m, hasMatchingOAuthIdentity as n, isSafeToAdoptMainStoreOAuthIdentity as o, isSafeToCopyOAuthRoutingScope as p, isSafeOAuthOwnerRefreshResult as r, overlayRuntimeExternalOAuthProfiles as s, areOAuthCredentialsEquivalent as t, hasOAuthIdentity as u, withCanonicalAuthProfileCredentialObserver as v };