UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

124 lines (123 loc) • 5.11 kB
import { t as captureResidentUserProfileAccess } from "./user-profile-list-D7DDbqt5.mjs"; import { S as parseEnterpriseGitHubPrincipal } from "./user-profile-identity.read-DZIwS_8H.mjs"; import { n as resolveGitHubHost, t as resolveGitHubApiBaseUrl } from "./github-host-runtime-CMm44VF1.mjs"; import { randomBytes } from "node:crypto"; //#region src/gateway/factory-github-proof.ts const AUDIENCE = "lobster.github.system-token"; const PROOF_LIFETIME_MS = 6e4; const MAX_PENDING_PROOFS = 128; const FACTORY_GITHUB_PROOF_ENV = "OPENCLAW_FACTORY_GITHUB_PROOF"; /** Capture only handshake-authenticated Factory identity, never a request field. */ function factoryGitHubClientProof(e) { const t = e.client; const n = t?.authenticatedFactoryGitHubAccountId; const r = t?.authenticatedUserProfile?.profileId; const i = `github:${resolveGitHubHost()}:${n}`; const assertCurrent = () => { e.assertCurrent(); if (t?.invalidated || t?.connect.role !== "operator" || !t.connect.scopes?.includes("operator.write") && !t.connect.scopes?.includes("operator.admin") && !t.connect.scopes?.includes("operator.read") || !t.connId || t.connectionSignal?.aborted || t.internal?.authenticatedOperator !== true || t.internal.syntheticClient || t.internal.agentRuntimeIdentity || t.internal.agentToolCaller || t.authenticatedUserId !== i || t.authenticatedFactoryGitHubAccountId !== n || t.authenticatedUserProfile?.profileId !== r) throw new Error("Factory GitHub caller authority changed."); }; assertCurrent(); if (!n || !r) throw new Error("Factory GitHub requires a verified actor profile."); return { actorId: n, profileId: r, claim: e.claim, assertCurrent }; } const pending = /* @__PURE__ */ new Map(); /** The issuer-bound immutable account comes from current catalog facts, never a login label. */ function factoryGitHubActorIdForProfile(e, t) { const n = e.map(parseEnterpriseGitHubPrincipal).filter((e) => e?.host === t); if (n.length !== 1 || !n[0]) throw new Error("GitHub actor binding is unavailable or ambiguous."); return n[0].accountId; } function pruneExpired(e) { for (const [t, n] of pending) if (n.expiresAtMs <= e) pending.delete(t); } function issueFactoryGitHubProof(e) { e.assertCurrent(); if (!Number.isSafeInteger(e.actorId) || e.actorId <= 0 || !e.profileId) throw new Error("GitHub proof actor is invalid."); const t = Date.now(); pruneExpired(t); if (pending.size >= MAX_PENDING_PROOFS) throw new Error("GitHub proof admission is busy; retry publication shortly."); const n = randomBytes(32).toString("base64url"); const r = Object.freeze(e.claim); Object.freeze(r.binding); const i = Object.freeze({ ...r, version: 1, audience: AUDIENCE, actorId: e.actorId, actorPrincipal: `github:${resolveGitHubHost()}:${e.actorId}`, profileId: e.profileId }); e.assertCurrent(); const a = { verdict: i, expiresAtMs: t + PROOF_LIFETIME_MS, assertCurrent: e.assertCurrent, redeemed: false }; pending.set(n, a); return { proof: n, release: () => pending.delete(n), wasRedeemed: () => a.redeemed }; } /** Consume before checking authority so even a failed or racing redemption cannot replay. */ function redeemFactoryGitHubProof(e) { if (!/^[A-Za-z0-9_-]{43}$/.test(e)) throw new Error("GitHub proof is invalid."); const t = pending.get(e); pending.delete(e); if (!t || t.expiresAtMs <= Date.now()) throw new Error("GitHub proof is unavailable or expired."); t.assertCurrent(); t.redeemed = true; return t.verdict; } /** Retain original caller, profile bindings and issuer through one broker-owned operation. */ async function withFactoryGitHubProof(e, t) { e.assertCurrent(); const n = resolveGitHubHost(); const r = resolveGitHubApiBaseUrl(); const i = captureResidentUserProfileAccess(e.profileId); let a; try { e.assertCurrent(); const o = factoryGitHubActorIdForProfile(i.readCurrentFacts().emails, n); if (o !== e.actorId) throw new Error("Factory GitHub actor profile changed."); const assertCurrent = () => { e.assertCurrent(); if (factoryGitHubActorIdForProfile(i.readCurrentFacts().emails, n) !== o || resolveGitHubHost() !== n || resolveGitHubApiBaseUrl() !== r) throw new Error("Factory GitHub actor or issuer changed."); }; const s = e.claim.binding.sessionKey; if (!s) throw new Error("Factory GitHub session binding is unavailable."); a = issueFactoryGitHubProof({ actorId: o, profileId: e.profileId, claim: e.claim, assertCurrent }); const c = { ...process.env, GH_TOKEN: void 0, GH_ENTERPRISE_TOKEN: void 0, GITHUB_TOKEN: void 0, GITHUB_ENTERPRISE_TOKEN: void 0, OPENCLAW_FACTORY_ACTOR_ID: String(o), OPENCLAW_FACTORY_SESSION_KEY: s, [FACTORY_GITHUB_PROOF_ENV]: a.proof }; assertCurrent(); const l = await t(c, assertCurrent); assertCurrent(); if (!a.wasRedeemed()) throw new Error("Factory GitHub broker did not authorize this credential lookup."); return l; } finally { a?.release(); } } //#endregion export { withFactoryGitHubProof as i, factoryGitHubClientProof as n, redeemFactoryGitHubProof as r, FACTORY_GITHUB_PROOF_ENV as t };