UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

284 lines (283 loc) • 10.8 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { t as GATEWAY_OWNER_PROFILE_ID } from "./user-profile-constants-Bj3FwpfE.mjs"; import { t as UserProfileMutationUnsettledError } from "./user-profile-events-BhLFevt9.mjs"; import { o as roleScopesAllow } from "./operator-scope-compat-CpFHu-FC.mjs"; import { l as prepareUserProfileIdentity } from "./user-profile-list-D7DDbqt5.mjs"; import { f as resolveGatewayOperatorRoleActor, h as resolveOperatorRolePolicyForAssignment, y as prepareSessionCreatorProfile } from "./operator-role-policy-DhcqVpmG.mjs"; import { i as authorizePreparedSessionMutation, n as authorizeIncognitoSessionTarget } from "./session-sharing-policy-3TAaxC-R.mjs"; import { t as prepareSessionMutationFacts } from "./session-sharing-preparation-vuYznHON.mjs"; import { t as createSyntheticPluginRuntimeClient } from "./server-plugin-runtime-client-DE2ydcEw.mjs"; import { c as GitHubPublicationRequesterUnavailableError, t as GitHubPublicationRecoveryPendingError } from "./github-publication-git-index-DZcaQ_xd.mjs"; import { n as captureGatewayOperatorRunAuthority } from "./operator-run-authority-3Pm3MQDA.mjs"; import { n as GatewayOperatorAccessDeniedError, s as resumeGatewayOperatorAccessGrant } from "./operator-access-policy-BWvd6twL.mjs"; import { t as decodeGitHubPublicationRequester } from "./github-publication-requester-CE4BURqL.mjs"; import { r as prepareGitHubPublicationSource } from "./github-publication-source-C9xaX8hX.mjs"; //#region src/gateway/github-publication-requester.ts function assertPublicationIncognitoAccess(e, t, n) { const r = createSyntheticPluginRuntimeClient({ operatorRoleActor: { kind: "operator", profileId: e }, scopes: [...t] }); if (authorizeIncognitoSessionTarget({ client: r, sessionKey: n, target: null })) throw new GitHubPublicationRequesterUnavailableError(); } async function preparePublicationSession(e, t) { try { return await prepareSessionMutationFacts({ cfg: t, ...e }); } catch (e) { throw new GitHubPublicationRecoveryPendingError("GitHub publication session authorization is unavailable; retry after session storage is ready.", { cause: e }); } } const workerRequesters = resolveGlobalSingleton(Symbol.for("openclaw.githubPublicationWorkerRequesters"), () => /* @__PURE__ */ new WeakSet()); /** Classify before invoking any released assertion; a failed worker command never selects legacy. */ function isGitHubPublicationRequesterV2(e) { return workerRequesters.has(e); } function prepareRequesterPolicy(e, t, n, r, i, a) { const o = createSyntheticPluginRuntimeClient({ operatorRoleActor: e.actor, scopes: [...e.scopes] }); const assertIdentity = () => { if (e.actor.kind !== "operator") return; try { if (!r) throw new GitHubPublicationRequesterUnavailableError(); return r.readCurrentFacts(e.grant?.aliasBindingIds); } catch (e) { if (e instanceof UserProfileMutationUnsettledError) throw new GitHubPublicationRecoveryPendingError("GitHub publication identity authorization is unavailable; retry after the profile mutation settles.", { cause: e }); throw new GitHubPublicationRequesterUnavailableError(); } }; const assertRole = (t, n) => { const r = t ? resolveOperatorRolePolicyForAssignment(t.profileId, t.assignedRole, n, t.githubLogin ?? null) : void 0; if (!roleScopesAllow({ role: "operator", requestedScopes: ["operator.sessions.write"], allowedScopes: e.scopes }) || r && (!roleScopesAllow({ role: "operator", requestedScopes: e.scopes, allowedScopes: r.scopes }) || r.accessPolicyPlugin && r.accessPolicyPlugin !== e.grant?.pluginId)) throw new GitHubPublicationRequesterUnavailableError(); return r; }; const assertPrepared = (n) => { const r = assertIdentity(); const a = assertRole(r?.profile, n); if (r) { let s; try { if (!i) throw new Error("Prepared publication session is missing"); s = i.readCurrent(n); } catch (e) { throw new GitHubPublicationRecoveryPendingError("GitHub publication session authorization is unavailable; retry after session storage is ready.", { cause: e }); } if (e.actor.kind === "operator" && !roleScopesAllow({ role: "operator", requestedScopes: ["operator.write"], allowedScopes: e.scopes }) && !prepareSessionCreatorProfile(e.actor.profileId, r.aliases)(s.target.entry.createdActor)) throw new GitHubPublicationRequesterUnavailableError(); if (authorizePreparedSessionMutation({ cfg: n, client: o, ...t }, s, { policy: a, aliases: r.aliases, authorizesAgentRun: false })) throw new GitHubPublicationRequesterUnavailableError(); } return r?.profile; }; return () => { const t = n(); const r = assertPrepared(t); if (r) { try { const n = resumeGatewayOperatorAccessGrant(r, t, e.grant); a?.(n); } catch (e) { if (e instanceof GatewayOperatorAccessDeniedError) throw new GitHubPublicationRequesterUnavailableError(); throw e; } assertPrepared(n()); } }; } /** Capture from the admitted caller, never request arguments, publisher, or session attribution. */ async function prepareGitHubPublicationRequesterV2(e, t) { e.signal?.throwIfAborted(); e.sessionMutationAuthorization?.assertCurrent(); const n = await captureGatewayOperatorRunAuthority(e); let r; let i; const a = new AbortController(); const release = () => { a.abort(new GitHubPublicationRequesterUnavailableError()); i?.release(); r?.release(); n?.release(); }; try { const o = (n ? { kind: "operator", profileId: n.authority.profileId } : resolveGatewayOperatorRoleActor(e.client))?.kind === "system" || e.client?.authenticatedUserProfile?.profileId === "gateway-owner"; if (!n && !o || e.client?.connect.role !== "operator" || n && n.authority.gatewayAccessGrant === void 0) throw new GitHubPublicationRequesterUnavailableError(); let s = null; if (n) { assertPublicationIncognitoAccess(n.authority.profileId, n.authority.scopes, t.sessionKey); r = await prepareUserProfileIdentity(n.authority.profileId); i = await preparePublicationSession(t, (e.context.getCommittedRuntimeConfig ?? e.context.getRuntimeConfig)()); if (n.authority.gatewayAccessGrant) s = Object.freeze({ ...n.authority.gatewayAccessGrant, aliasBindingIds: r.emailBindingIds }); } const c = Object.freeze({ version: 1, actor: Object.freeze(n ? { kind: "operator", profileId: n.authority.profileId } : { kind: "system" }), scopes: Object.freeze([...n?.authority.scopes ?? e.client.connect.scopes ?? []]), grant: s }); let l; const u = prepareRequesterPolicy(c, t, e.context.getCommittedRuntimeConfig ?? e.context.getRuntimeConfig, r, i, (e) => { l = e; }); const d = AbortSignal.any([ a.signal, ...e.signal ? [e.signal] : [], ...n?.authority.signal ? [n.authority.signal] : [] ]); const assertInvocationCurrent = () => { try { d.throwIfAborted(); if (e.hasCurrentClientAuthority?.() === false) throw new GitHubPublicationRequesterUnavailableError(); e.sessionMutationAuthorization?.assertCurrent(); n?.authority.assertCurrent(); } catch { throw new GitHubPublicationRequesterUnavailableError(); } }; const f = Object.freeze({ version: 2, signal: d, snapshot: c, assertInvocationCurrent, assertCurrent: () => { assertInvocationCurrent(); u(); assertInvocationCurrent(); }, async prepareSource(n) { f.assertCurrent(); if (n.agentId !== t.agentId || n.sessionKey !== t.sessionKey) throw new GitHubPublicationRequesterUnavailableError(); i ??= await preparePublicationSession(t, (e.context.getCommittedRuntimeConfig ?? e.context.getRuntimeConfig)()); f.assertCurrent(); const { sourcePath: r } = i.readCurrent((e.context.getCommittedRuntimeConfig ?? e.context.getRuntimeConfig)()); if (!r) throw new GitHubPublicationRecoveryPendingError("GitHub publication source is unavailable; retry after session storage is ready."); return await prepareGitHubPublicationSource({ sourcePath: r, selector: { ...n, ...c.actor.kind === "operator" ? { profileId: c.actor.profileId, aliasBindingIds: c.grant?.aliasBindingIds } : {} }, signal: AbortSignal.any([d, ...l ? [l.signal] : []]), assertCurrent: f.assertCurrent }); } }); workerRequesters.add(f); f.assertCurrent(); return { requester: f, release }; } catch (e) { release(); throw e; } } /** Restoration rechecks the original immutable basis; a new role or invitation cannot replace it. */ async function restoreGitHubPublicationRequester(e, t, n) { const r = decodeGitHubPublicationRequester(e); if (!r) throw new GitHubPublicationRequesterUnavailableError(); let i; let a; const o = new AbortController(); if (r.actor.kind === "operator") { assertPublicationIncognitoAccess(r.actor.profileId, r.scopes, t.sessionKey); try { i = await prepareUserProfileIdentity(r.actor.profileId); a = await preparePublicationSession(t, n()); } catch (e) { i?.release(); if (e instanceof GitHubPublicationRecoveryPendingError) throw e; throw new GitHubPublicationRecoveryPendingError("GitHub publication requester identity is unavailable; retry recovery after profile storage is ready.", { cause: e }); } } const release = () => { o.abort(new GitHubPublicationRequesterUnavailableError()); a?.release(); i?.release(); }; try { let e; const s = prepareRequesterPolicy(r, t, n, i, a, (t) => { e = t; }); const c = Object.freeze({ version: 2, signal: o.signal, snapshot: r, assertCurrent() { o.signal.throwIfAborted(); s(); }, async prepareSource(i) { c.assertCurrent(); if (i.agentId !== t.agentId || i.sessionKey !== t.sessionKey) throw new GitHubPublicationRequesterUnavailableError(); a ??= await preparePublicationSession(t, n()); c.assertCurrent(); const { sourcePath: s } = a.readCurrent(n()); if (!s) throw new GitHubPublicationRecoveryPendingError("GitHub publication source is unavailable; retry after session storage is ready."); return await prepareGitHubPublicationSource({ sourcePath: s, selector: { ...i, ...r.actor.kind === "operator" ? { profileId: r.actor.profileId, aliasBindingIds: r.grant?.aliasBindingIds } : {} }, signal: AbortSignal.any([o.signal, ...e ? [e.signal] : []]), assertCurrent: c.assertCurrent }); }, release }); workerRequesters.add(c); c.assertCurrent(); return c; } catch (e) { release(); throw e; } } //#endregion export { prepareGitHubPublicationRequesterV2 as n, restoreGitHubPublicationRequester as r, isGitHubPublicationRequesterV2 as t };