openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
74 lines (73 loc) • 2.67 kB
JavaScript
import { t as MAX_DATE_TIMESTAMP_MS } from "./number-coercion-D6u-4YD8.mjs";
import { g as parseSecretRef, p as normalizeSecretInputString } from "./types.secrets-BmNLLnHp.mjs";
import { i as isOAuthRefreshFence } from "./oauth-refresh-marker-Bm1Qb0ET.mjs";
//#region src/agents/auth-profiles/credential-state.ts
const DEFAULT_OAUTH_REFRESH_MARGIN_MS = 3e5;
function resolveTokenExpiryState(e, t = Date.now(), n) {
if (e === void 0) return "missing";
if (typeof e !== "number") return "invalid_expires";
if (!Number.isFinite(e) || e <= 0 || e > 864e13) return "invalid_expires";
const r = e - t;
if (r <= 0) return "expired";
const i = Math.max(0, n?.expiringWithinMs ?? 0);
if (i > 0 && r <= i) return "expiring";
return "valid";
}
function hasUsableOAuthCredential(e, t) {
if (!e || e.type !== "oauth" || isOAuthRefreshFence(e)) return false;
if (typeof e.access !== "string" || e.access.trim().length === 0) return false;
const n = t?.now ?? Date.now();
const r = Math.max(0, t?.refreshMarginMs ?? 3e5);
return resolveTokenExpiryState(e.expires, n, { expiringWithinMs: r }) === "valid";
}
function isMalformedApiKeyInput(e) {
const t = normalizeSecretInputString(e);
return t !== void 0 && /^openclaw\s+onboard(?:\s+.*)?\s+--auth-choice(?:\s|=|$)/i.test(t);
}
function evaluateStoredCredentialEligibility(e) {
const t = e.now ?? Date.now();
const n = e.credential;
if (n.type === "api_key" || n.type === "token") {
const e = normalizeSecretInputString(n.type === "api_key" ? n.key : n.token) !== void 0;
const r = parseSecretRef(n.type === "api_key" ? n.keyRef : n.tokenRef) !== null;
if (n.type === "api_key" && isMalformedApiKeyInput(n.key)) return {
eligible: false,
reasonCode: "malformed_api_key"
};
if (!e && !r) return {
eligible: false,
reasonCode: "missing_credential"
};
if (n.type === "token") {
const e = resolveTokenExpiryState(n.expires, t);
if (e === "invalid_expires" || e === "expired") return {
eligible: false,
reasonCode: e
};
}
return {
eligible: true,
reasonCode: "ok"
};
}
if (isOAuthRefreshFence(n)) return {
eligible: false,
reasonCode: "expired"
};
if (normalizeSecretInputString(n.access) === void 0 && normalizeSecretInputString(n.refresh) === void 0) {
if (n.oauthRef) return {
eligible: false,
reasonCode: "unresolved_ref"
};
return {
eligible: false,
reasonCode: "missing_credential"
};
}
return {
eligible: true,
reasonCode: "ok"
};
}
//#endregion
export { resolveTokenExpiryState as a, isMalformedApiKeyInput as i, evaluateStoredCredentialEligibility as n, hasUsableOAuthCredential as r, DEFAULT_OAUTH_REFRESH_MARGIN_MS as t };