openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
132 lines (131 loc) • 5 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { randomUUID } from "node:crypto";
import { isDeepStrictEqual } from "node:util";
//#region src/agents/auth-profiles/authorization-lifetime.ts
const intents = /* @__PURE__ */ new WeakMap();
const prepared = /* @__PURE__ */ new WeakMap();
const inheritedContinuations = /* @__PURE__ */ new WeakMap();
/** Only the peer-settlement owner may retire a verified shared-credential mirror without reauthorizing. */
function continueAuthProfileAuthorizationInheritance(e, t, n) {
let r = inheritedContinuations.get(e);
if (!r) {
r = /* @__PURE__ */ new Map();
inheritedContinuations.set(e, r);
}
r.set(t, {
kind: "refresh",
previous: structuredClone(n),
used: false
});
}
function copyAuthProfileAuthorizationInheritance(e, t) {
const n = inheritedContinuations.get(e);
if (n) inheritedContinuations.set(t, n);
}
/** Only refresh-owner CAS producers carry continuity; a supplied JSON field cannot grant it. */
function continueAuthProfileAuthorization(e, t) {
intents.set(t, {
kind: "refresh",
previous: structuredClone(e),
used: false
});
return t;
}
/** Explicit login/upsert is replacement even when the operator supplies identical bytes. */
function replaceAuthProfileAuthorization(e) {
intents.set(e, {
kind: "replace",
used: false
});
return e;
}
function copyAuthProfileAuthorizationIntent(e, t) {
const n = intents.get(e);
if (n) intents.set(t, n);
}
/** Transfer native callback intent separately from caller-controlled credential JSON. */
function takeAuthProfileAuthorizationWriteIntents(e) {
const t = [];
const take = (e, n, r) => {
if (r && !r.used) {
const { used: i, ...a } = r;
t.push({
profileId: e,
inherited: n,
intent: a
});
r.used = true;
}
};
for (const [t, n] of Object.entries(e)) take(t, false, intents.get(n));
for (const [t, n] of inheritedContinuations.get(e) ?? []) take(t, true, n);
return t;
}
/** Only the private host/worker exchange may reattach transferred write intent. */
function restoreAuthProfileAuthorizationWriteIntents(e, t) {
for (const { profileId: n, inherited: r, intent: i } of t) if (r && i.kind === "refresh") continueAuthProfileAuthorizationInheritance(e, n, i.previous);
else if (!r && Object.hasOwn(e, n)) intents.set(e[n], {
...i,
used: false
});
}
function readAuthProfileAuthorizationLifetimes(e) {
if (!isRecord(e) || e.authorizationLifetimes === void 0) return {};
if (!isRecord(e.authorizationLifetimes)) throw new Error("Invalid auth profile authorization metadata");
const t = [];
for (const [n, r] of Object.entries(e.authorizationLifetimes)) {
if (typeof r !== "string" || !/^[a-f0-9-]{36}$/.test(r)) throw new Error("Invalid auth profile authorization metadata");
t.push([n, r]);
}
return Object.fromEntries(t);
}
function readAuthProfileAuthorizationLifetime(e, t) {
const n = readAuthProfileAuthorizationLifetimes(e);
return Object.hasOwn(n, t) ? n[t] : void 0;
}
function prepareAuthProfileAuthorizationWrite(e, t) {
const n = readAuthProfileAuthorizationLifetimes(e);
if (!isRecord(t) || !isRecord(t.profiles)) {
if (Object.keys(n).length) throw new Error("Invalid auth profile credential payload");
return t;
}
if (prepared.has(t) && isDeepStrictEqual(prepared.get(t), e)) return t;
if (Object.keys(n).length === 0) {
for (const e of Object.values(t.profiles)) {
const t = isRecord(e) ? intents.get(e) : void 0;
if (t) t.used = true;
}
const { authorizationLifetimes: e, ...n } = t;
return n;
}
const r = isRecord(e) && isRecord(e.profiles) ? e.profiles : {};
for (const e of Object.keys(n)) {
const i = r[e];
const a = t.profiles[e];
const o = isRecord(a) ? intents.get(a) : inheritedContinuations.get(t.profiles)?.get(e);
const s = o && !o.used && o.kind === "refresh" && isDeepStrictEqual(o.previous, i);
if (o && !o.used && o.kind === "replace" || !isDeepStrictEqual(i, a) && !s) n[e] = randomUUID();
if (o) o.used = true;
}
const i = {
...t,
authorizationLifetimes: n
};
prepared.set(i, e);
return i;
}
/** Enrollment changes no credential bytes and must run in the canonical writer transaction. */
function enrollAuthProfileAuthorization(e, t) {
if (!isRecord(e) || !isRecord(e.profiles)) throw new Error("Invalid auth profile credential payload");
const n = readAuthProfileAuthorizationLifetimes(e);
if (Object.hasOwn(n, t)) return e;
return {
...e,
authorizationLifetimes: {
...n,
[t]: randomUUID()
}
};
}
//#endregion
export { enrollAuthProfileAuthorization as a, readAuthProfileAuthorizationLifetimes as c, takeAuthProfileAuthorizationWriteIntents as d, copyAuthProfileAuthorizationIntent as i, replaceAuthProfileAuthorization as l, continueAuthProfileAuthorizationInheritance as n, prepareAuthProfileAuthorizationWrite as o, copyAuthProfileAuthorizationInheritance as r, readAuthProfileAuthorizationLifetime as s, continueAuthProfileAuthorization as t, restoreAuthProfileAuthorizationWriteIntents as u };