openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
92 lines (91 loc) • 3.51 kB
JavaScript
import { hash, randomBytes } from "node:crypto";
//#region src/agents/auth-profiles/oauth-refresh-marker.ts
const OAUTH_REFRESH_FENCE_PREFIX = "openclaw-oauth-refresh-fence:v1:";
const OAUTH_REFRESH_ACCESS_PATTERN = /^openclaw-oauth-refresh-fence:v1:([a-f0-9]{32}):(failed:)?access:([a-f0-9]{64})$/;
const OAUTH_REFRESH_TOKEN_PATTERN = /^openclaw-oauth-refresh-fence:v1:([a-f0-9]{32}):(failed:)?refresh:([a-f0-9]{64})$/;
function buildOAuthRefreshSecretDigest(e) {
const t = JSON.stringify([
"openclaw.oauth-refresh-generation",
1,
e.profileId,
e.provider,
e.kind,
e.secret
]);
return hash("sha256", t, "hex");
}
function parseOAuthRefreshFence(e) {
if (!e || e.type !== "oauth" || e.expires !== 1) return;
const t = OAUTH_REFRESH_ACCESS_PATTERN.exec(e.access);
const n = OAUTH_REFRESH_TOKEN_PATTERN.exec(e.refresh);
if (!t || !n || t[1] !== n[1] || t[2] !== n[2]) return;
return {
accessDigest: t[3],
claimId: t[1],
refreshDigest: n[3],
state: t[2] ? "failed" : "pending"
};
}
/** Replace one claimed OAuth generation with an inert, schema-valid durable marker. */
function createOAuthRefreshFence(e) {
const { access: t, refresh: n, expires: r, idToken: i, oauthRef: a, copyToAgents: o, ...s } = e.credential;
const c = randomBytes(16).toString("hex");
const marker = (t, n) => `${OAUTH_REFRESH_FENCE_PREFIX}${c}:${t}:${buildOAuthRefreshSecretDigest({
profileId: e.profileId,
provider: e.credential.provider,
kind: t,
secret: n
})}`;
return {
...s,
type: "oauth",
provider: e.credential.provider,
access: marker("access", t),
refresh: marker("refresh", n),
expires: 1
};
}
/** True only for the exact v1 inert marker shape written by the refresh owner. */
function isOAuthRefreshFence(e) {
return parseOAuthRefreshFence(e) !== void 0;
}
/** True once the refresh owner settled the claim as failed: the sign-in must be redone. */
function isFailedOAuthRefreshFence(e) {
return parseOAuthRefreshFence(e)?.state === "failed";
}
/** True only while the durable refresh owner may still settle its claim. */
function isPendingOAuthRefreshFence(e) {
return parseOAuthRefreshFence(e)?.state === "pending";
}
/** Mark a failed owner terminal without restoring its single-use refresh generation. */
function createFailedOAuthRefreshFence(e) {
const t = parseOAuthRefreshFence(e);
if (!t || t.state === "failed") return e;
return {
...e,
access: `${OAUTH_REFRESH_FENCE_PREFIX}${t.claimId}:failed:access:${t.accessDigest}`,
refresh: `${OAUTH_REFRESH_FENCE_PREFIX}${t.claimId}:failed:refresh:${t.refreshDigest}`
};
}
/** True when two persisted values share the same single-use refresh generation. */
function isSameOAuthRefreshGeneration(e) {
if (e.left.provider !== e.right.provider) return false;
return readOAuthRefreshGenerationDigest({
profileId: e.profileId,
credential: e.left
}) === readOAuthRefreshGenerationDigest({
profileId: e.profileId,
credential: e.right
});
}
/** The same secret-free generation identity for a credential and its durable fence. */
function readOAuthRefreshGenerationDigest(e) {
return parseOAuthRefreshFence(e.credential)?.refreshDigest ?? buildOAuthRefreshSecretDigest({
profileId: e.profileId,
provider: e.credential.provider,
kind: "refresh",
secret: e.credential.refresh
});
}
//#endregion
export { isPendingOAuthRefreshFence as a, isOAuthRefreshFence as i, createOAuthRefreshFence as n, isSameOAuthRefreshGeneration as o, isFailedOAuthRefreshFence as r, createFailedOAuthRefreshFence as t };