openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
86 lines (85 loc) • 2.82 kB
JavaScript
import { r as captureOpenClawStateWorkerContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs";
import { t as AuthProfileAuthorizationError } from "./authorization-contract-CQ0hvliU.mjs";
import { n as withOpenClawStateLeaseAsync } from "./openclaw-state-lease-Cn13CM0c.mjs";
import { r as projectMcpOAuthAuthorization, t as hasMcpOAuthAuthorization } from "./mcp-oauth-authorization-facts-BN0BPuN8.mjs";
import { h as retainMcpOAuthAuthorizationState, s as mutateMcpOAuthStore, u as readMcpOAuthStoreReadOnly } from "./mcp-oauth-store-Cp55xn8F.mjs";
//#region src/agents/mcp-oauth-authorization.ts
/** Capture native credential custody without refreshing or holding a flow lease over a source. */
async function captureMcpOAuthAuthorization(e) {
const t = e.identity.storeKey;
const n = e.assertCurrent;
const r = captureOpenClawStateWorkerContext();
const assertSource = () => {
n();
try {
r.maintenanceScope?.assertAdmission();
r.admission.assertCurrent();
} catch {
throw new AuthProfileAuthorizationError("retired");
}
};
assertSource();
const i = retainMcpOAuthAuthorizationState(r.admission, t);
const readCanonical = async () => {
assertSource();
const e = i.prepareRead();
const n = await readMcpOAuthStoreReadOnly(t, r).catch(() => {
assertSource();
throw new AuthProfileAuthorizationError("unavailable");
});
assertSource();
e(projectMcpOAuthAuthorization(n));
return n;
};
try {
let e = await readCanonical();
if (!hasMcpOAuthAuthorization(e) || !e.tokens?.refresh_token && e.tokenExpiresAt !== void 0 && e.tokenExpiresAt <= Date.now()) throw new AuthProfileAuthorizationError("retired");
if (!e.authorizationId) {
await withOpenClawStateLeaseAsync({
scope: "core:mcp-oauth",
key: t,
leaseMs: 6e4,
waitMs: 3e4
}, r, (e) => mutateMcpOAuthStore({
storeKey: t,
lease: e,
context: r
}, { kind: "ensureAuthorization" }, { assertCurrent: assertSource }));
e = await readCanonical();
}
const n = e.authorizationId;
if (!n) throw new AuthProfileAuthorizationError("retired");
let a = "ready";
const assertCurrent = () => {
assertSource();
i.assertAuthorization(n);
if (a !== "ready") throw new AuthProfileAuthorizationError("unavailable");
};
assertCurrent();
return {
authorizationId: n,
assertCurrent,
async revalidate() {
assertSource();
i.assertAuthorization(n);
if (a === "pending") throw new AuthProfileAuthorizationError("unavailable");
a = "pending";
try {
await readCanonical();
assertSource();
i.assertAuthorization(n);
a = "ready";
} catch (e) {
a = "unavailable";
throw e;
}
},
dispose: i.release
};
} catch (e) {
i.release();
throw e;
}
}
//#endregion
export { captureMcpOAuthAuthorization };