openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
115 lines (114 loc) • 4.71 kB
JavaScript
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
import { i as collectNestedErrorCandidates } from "./error-coercion-hDrHOSGy.mjs";
//#region src/shared/oauth-refresh-outcome.ts
const terminalReasons = /* @__PURE__ */ new Set([
"invalid_grant",
"revoked",
"reused",
"expired",
"invalid_client",
"invalid_scope",
"unauthorized_client",
"unsupported_grant_type",
"refresh_token_reused",
"invalid_refresh_token",
"token_invalidated",
"expired_token",
"sign_in_again",
"refresh_token_expired",
"refresh_token_invalidated",
"bad_refresh_token",
"incorrect_client_credentials"
]);
function terminalReason(e) {
return typeof e === "string" && terminalReasons.has(e);
}
/** Read protocol fields, never prose or a transport code, for rejection precedence. */
function readOAuthRefreshErrorReason(e) {
const t = asOptionalRecord(e);
const n = asOptionalRecord(t?.error);
const r = [
t?.reason,
n?.code,
n?.reason,
t?.error,
t?.code
].find(terminalReason);
if (typeof r !== "string") return t?.error ? "provider_error" : void 0;
switch (r) {
case "refresh_token_reused": return "reused";
case "expired_token":
case "refresh_token_expired": return "expired";
case "incorrect_client_credentials": return "invalid_client";
case "invalid_refresh_token":
case "bad_refresh_token":
case "refresh_token_invalidated":
case "token_invalidated":
case "sign_in_again": return "invalid_grant";
case "invalid_grant":
case "revoked":
case "reused":
case "expired":
case "invalid_client":
case "invalid_scope":
case "unauthorized_client":
case "unsupported_grant_type": return r;
default: return;
}
}
/** Attach bounded, credential-free protocol facts while retaining the original cause. */
function createOAuthRefreshError(e, t, n) {
return Object.assign(new Error(e, n), {
oauthRefreshOutcome: t,
oauthRefreshFailure: {
status: t.status,
reason: t.reason
}
});
}
/**
* Reuse evidence must come from the provider owner, never from status alone.
* `owner-accepted-provider-retry` is a provider-specific product decision, not a
* documented reuse guarantee; only the named provider adapter may claim it.
*/
function createOAuthRefreshResponseError(e, t, n, r) {
const i = readOAuthRefreshErrorReason(n);
const a = asOptionalRecord(n);
const o = r && (!i || i === "provider_error") && (t === 429 || t >= 500);
return createOAuthRefreshError(`${e} failed (${t})${i ? `: ${i}` : ""}`, {
version: 1,
phase: "token-response",
...o ? {
outcome: "unchanged",
recovery: {
kind: "later",
evidence: r
}
} : { outcome: i && i !== "provider_error" && !a?.access_token ? "rejected" : "unknown" },
status: t,
...i ? { reason: i } : {}
});
}
/** Only positive provider evidence can admit a later attempt with the old refresh grant. */
function isRecoverableOAuthRefreshFailure(e) {
let t = false;
for (const n of collectNestedErrorCandidates(e)) {
const e = asOptionalRecord(n);
const r = asOptionalRecord(e?.oauthRefreshFailure);
const i = readOAuthRefreshErrorReason(e);
const a = asOptionalRecord(e?.oauthRefreshOutcome);
const o = a?.version === 1 && a.phase === "token-response" && a.outcome === "unchanged" && a.status === 403 && asOptionalRecord(a.recovery)?.kind === "later" && asOptionalRecord(a.recovery)?.evidence === "verified-intermediary-challenge";
if (i !== void 0 && i !== "provider_error" || terminalReason(r?.reason) || r?.retryable === false || [e?.status, r?.status].some((e) => typeof e === "number" && e >= 400 && e < 500 && e !== 429 && !(e === 403 && o))) return false;
if (e?.oauthRefreshOutcome === void 0) continue;
const s = asOptionalRecord(e.oauthRefreshOutcome);
if (!s || s.version !== 1 || terminalReason(s.reason) || s.reason !== void 0 && s.reason !== "provider_error" || s.status !== void 0 && (typeof s.status !== "number" || !Number.isInteger(s.status) || s.status < 100 || s.status > 599)) return false;
const c = asOptionalRecord(s.recovery);
const l = s.phase === "before-dispatch" && s.outcome === "not-attempted" && c?.evidence === "dispatch-not-started";
const u = s.phase === "token-response" && s.outcome === "unchanged" && (s.status === void 0 || s.status === 429 || typeof s.status === "number" && s.status >= 500) && (c?.evidence === "documented-nonconsuming-rejection" || c?.evidence === "documented-reusable-grant" || c?.evidence === "owner-accepted-provider-retry") || o && s === a;
if (c?.kind !== "later" || !l && !u) return false;
t = true;
}
return t;
}
//#endregion
export { readOAuthRefreshErrorReason as i, createOAuthRefreshResponseError as n, isRecoverableOAuthRefreshFailure as r, createOAuthRefreshError as t };