openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
188 lines (187 loc) • 7.66 kB
JavaScript
import { n as notifyListeners, r as registerListener } from "./listeners-Cb2-CnP8.mjs";
//#region src/gateway/device-revocation.ts
const owners = /* @__PURE__ */ new WeakMap();
const captures = /* @__PURE__ */ new WeakMap();
const sourceIdentities = /* @__PURE__ */ new WeakMap();
function sameReconnectSource(e, t) {
return e && t ? e.device.deviceId === t.device.deviceId && e.device.key === t.device.key && e.profileId === t.profileId && e.role === t.role && e.scopes === t.scopes && e.clientId === t.clientId && e.clientMode === t.clientMode && e.pairedClientId === t.pairedClientId && e.transportPolicyGeneration === t.transportPolicyGeneration && e.verifiedIdentity === t.verifiedIdentity && e.origin === t.origin && e.requestHost === t.requestHost && e.isLocalClient === t.isLocalClient && e.accessOwner === t.accessOwner : e === t;
}
function retainSourceIdentity(e, { client: t, ...n }) {
let r = n.reconnect ? e.reconnectSources ??= /* @__PURE__ */ new Set() : sourceIdentities.get(t);
if (!r) {
r = /* @__PURE__ */ new Set();
sourceIdentities.set(t, r);
}
let i = [...r].find((e) => e.dependencies.context === n.context && e.dependencies.authPolicyGeneration === n.authPolicyGeneration && e.dependencies.sharedGenerationOwner === n.sharedGenerationOwner && e.dependencies.authorityOwner === n.authorityOwner && e.dependencies.sharedGeneration === n.sharedGeneration && sameReconnectSource(e.dependencies.reconnect, n.reconnect));
if (!i) {
i = {
dependencies: n,
token: Object.freeze({}),
references: 0
};
r.add(i);
}
const a = i;
const o = r;
a.references += 1;
return {
token: a.token,
release: () => {
if (--a.references === 0) o.delete(a);
}
};
}
function getOwner(e) {
let t = owners.get(e);
if (!t) {
t = {
closed: false,
devices: /* @__PURE__ */ new Map()
};
owners.set(e, t);
}
return t;
}
function releaseHold(e) {
const { owner: t, state: n } = e;
let r = false;
return () => {
if (r) return;
r = true;
n.references -= 1;
if (n.references !== 0) return;
e.releaseSourceIdentity?.();
e.releaseSourceIdentity = void 0;
e.releaseClientRevocation?.();
e.releaseClientRevocation = void 0;
n.listeners?.clear();
if (!n.deviceId) return;
const i = t.devices.get(n.deviceId);
i?.delete(n);
if (i?.size === 0) t.devices.delete(n.deviceId);
};
}
function revoke(e) {
if (e.revoked) return;
e.revoked = true;
notifyListeners(e.listeners ?? [], void 0);
}
/** Capture the attested identity before dispatch yields, without retaining credentials or sockets. */
function captureGatewayDeviceRevocation(e, t, n, r, i) {
const a = getOwner(e);
const o = {
deviceId: t.deviceId,
role: t.role,
references: 1,
revoked: false,
sourceAccepted: false
};
if (o.deviceId && !a.closed) {
let e = a.devices.get(o.deviceId);
if (!e) {
e = /* @__PURE__ */ new Set();
a.devices.set(o.deviceId, e);
}
e.add(o);
}
const isRevocationCurrent = () => !a.closed && !o.revoked && (o.references > 0 || !o.sourceAccepted && r?.aborted === false);
const isCurrent = () => isRevocationCurrent() && (o.sourceAccepted && i ? i.isCurrent() : n());
const s = {
owner: a,
state: o,
isCurrent,
isSourceCurrent: i ? () => isRevocationCurrent() && i.isCurrent() : isCurrent,
hasSourceAuthority: i !== void 0,
isRevocationCurrent,
sourceIdentity: Object.freeze({})
};
captures.set(isCurrent, s);
s.releaseClientRevocation = i?.subscribe(() => revoke(o));
if (i?.dependencies) {
const e = retainSourceIdentity(a, i.dependencies);
s.reconnectSourceClient = i.dependencies.reconnect ? i.dependencies.client : void 0;
s.sourceIdentity = e.token;
s.releaseSourceIdentity = e.release;
}
return {
isCurrent,
release: releaseHold(s)
};
}
/** Carry the original capture through a composed commit guard without changing its contract. */
function bindGatewayDeviceRevocation(e, t) {
const n = t ? captures.get(t) : void 0;
if (n) captures.set(e, n);
return e;
}
/** Retain only device revocation for detached work, not its completed request or transport. */
function retainGatewayDeviceSourceRevocation(e) {
const t = e ? captures.get(e) : void 0;
if (!t) return;
if (!t.isRevocationCurrent() || t.state.references === 0) throw new Error("Gateway device source is no longer active.");
t.state.references += 1;
return {
isCurrent: t.isRevocationCurrent,
subscribe: (e) => registerListener(t.state.listeners ??= /* @__PURE__ */ new Set(), e),
release: releaseHold(t)
};
}
/** Read only owner-held revocation/lifetime facts, without invoking the caller authority callback. */
function readGatewayDeviceRevocationGuard(e) {
return e ? captures.get(e)?.isRevocationCurrent : void 0;
}
/** Original committed source authority, excluding tentative transport and later request lifetimes. */
function readGatewayDeviceSourceAuthority(e) {
return e ? captures.get(e)?.isSourceCurrent : void 0;
}
/** A committed request may finish its accepted input under the retained source grant. */
function acceptGatewayDeviceSourceAuthority(e) {
const t = e ? captures.get(e) : void 0;
if (!t?.hasSourceAuthority || t.state.references === 0 || !t.isSourceCurrent()) return false;
t.state.sourceAccepted = true;
return true;
}
function readAcceptedGatewayDeviceSourceAuthority(e) {
const t = e ? captures.get(e) : void 0;
return t?.state.sourceAccepted ? t.isSourceCurrent : void 0;
}
/** Prepared grants consume owned facts; opaque transport callbacks keep their native fence. */
function hasPreparedGatewayDeviceAuthority(e, t) {
const n = readAcceptedGatewayDeviceSourceAuthority(t);
return (n ? n() : !e?.invalidated) && readGatewayDeviceRevocationGuard(t)?.() !== false;
}
/** Only a producer-proven dependency cohort can share queued-input custody. */
function readGatewayDeviceSourceIdentity(e) {
return e ? captures.get(e)?.sourceIdentity : void 0;
}
/** Only the exact authenticated client or a host-transferred principal can use this cohort. */
function readGatewayDeviceReconnectSourceIdentity(e, t) {
const n = e ? captures.get(e) : void 0;
return n?.reconnectSourceClient === t ? n.sourceIdentity : void 0;
}
/** Notify retained work of access revocation, independently of transport or Gateway shutdown. */
function onGatewayDeviceSourceRevoked(e, t) {
const n = e ? captures.get(e) : void 0;
if (!n) return;
const r = registerListener(n.state.listeners ??= /* @__PURE__ */ new Set(), t);
if (n.state.revoked) t();
return r;
}
/** Transfer a hold on the original captured state, never recapture a later device/session. */
function retainGatewayDeviceRevocation(e) {
const t = e ? captures.get(e) : void 0;
if (!t) return;
if (!t.isCurrent() || t.state.references === 0) throw new Error("Gateway caller authority is no longer active.");
t.state.references += 1;
return releaseHold(t);
}
function invalidateGatewayDeviceRevocation(e, t, n) {
for (const r of owners.get(e)?.devices.get(t) ?? []) if (!n || r.role === n) revoke(r);
}
function closeGatewayDeviceRevocation(e) {
const t = getOwner(e);
t.closed = true;
t.devices.clear();
}
//#endregion
export { hasPreparedGatewayDeviceAuthority as a, readAcceptedGatewayDeviceSourceAuthority as c, readGatewayDeviceSourceAuthority as d, readGatewayDeviceSourceIdentity as f, closeGatewayDeviceRevocation as i, readGatewayDeviceReconnectSourceIdentity as l, retainGatewayDeviceSourceRevocation as m, bindGatewayDeviceRevocation as n, invalidateGatewayDeviceRevocation as o, retainGatewayDeviceRevocation as p, captureGatewayDeviceRevocation as r, onGatewayDeviceSourceRevoked as s, acceptGatewayDeviceSourceAuthority as t, readGatewayDeviceRevocationGuard as u };