openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
158 lines (157 loc) • 5.14 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { t as hasMcpOAuthAuthorization } from "./mcp-oauth-authorization-facts-BN0BPuN8.mjs";
import { randomUUID } from "node:crypto";
//#region src/agents/mcp-oauth-store.mutations.ts
const MCP_OAUTH_DEFAULT_REDIRECT_URL = "http://127.0.0.1:8989/oauth/callback";
function beginMcpOAuthAuthorization(e) {
const t = { ...e };
if (t.credentialState === "uninitialized") delete t.credentialState;
return t;
}
function bindMcpOAuthTokensIssuer(e) {
const t = e.discoveryState?.authorizationServerUrl;
if (!e.tokens?.refresh_token || e.tokensAuthorizationServerUrl !== void 0 || t === void 0) return e;
return {
...e,
tokensAuthorizationServerUrl: t
};
}
function applyMcpOAuthAuthorizationChallenge(e, t) {
const n = {
...e,
pendingAuthorizationChallenge: {
...e.pendingAuthorizationChallenge,
...t.resourceMetadataUrl ? { resourceMetadataUrl: t.resourceMetadataUrl } : {},
...t.scope ? { scope: t.scope } : {},
...t.requiresAuthorization ? { requiresAuthorization: true } : {}
}
};
if (t.requiresAuthorization) delete n.authorizationId;
if (e.credentialState === void 0 && e.tokens === void 0 && e.clientInformation === void 0 && e.codeVerifier === void 0 && e.discoveryState === void 0 && e.lastAuthorizationUrl === void 0 && e.redirectUrl === void 0) n.credentialState = "uninitialized";
if (t.resourceMetadataUrl && e.discoveryState?.resourceMetadataUrl !== t.resourceMetadataUrl) {
const e = bindMcpOAuthTokensIssuer(n);
delete e.discoveryState;
return e;
}
return n;
}
/** Apply an SDK or flow operation to the authoritative transaction snapshot. */
function applyMcpOAuthMutation(e, t) {
switch (t.kind) {
case "beginRefresh":
if (e.refreshClaim || e.tokens?.refresh_token !== t.refreshToken) throw new Error("MCP OAuth refresh ownership changed; sign in again.");
return {
store: {
...e,
refreshClaim: t.claim
},
applied: true
};
case "restoreRefresh": {
if (e.refreshClaim !== t.claim) return {
store: e,
applied: false
};
const n = { ...e };
delete n.refreshClaim;
return {
store: n,
applied: true
};
}
case "ensureAuthorization": return !e.authorizationId && hasMcpOAuthAuthorization(e) ? {
store: {
...e,
authorizationId: randomUUID()
},
applied: true
} : {
store: e,
applied: false
};
case "clientInformation":
case "discoveryState": {
const n = beginMcpOAuthAuthorization(e);
if (t.kind === "clientInformation") n.clientInformation = t.clientInformation;
else n.discoveryState = t.discoveryState;
return {
store: n,
applied: true
};
}
case "authorizationRedirect": return {
store: {
...beginMcpOAuthAuthorization(e),
...t.codeVerifier ? { codeVerifier: t.codeVerifier } : {},
lastAuthorizationUrl: t.authorizationUrl,
redirectUrl: t.redirectUrl ?? normalizeOptionalString(e.redirectUrl) ?? "http://127.0.0.1:8989/oauth/callback"
},
applied: true
};
case "bindTokensIssuer": return {
store: bindMcpOAuthTokensIssuer(e),
applied: true
};
case "authorizationChallenge": return t.rejectedAccessToken !== void 0 && e.tokens?.access_token !== t.rejectedAccessToken ? {
store: e,
applied: false
} : {
store: applyMcpOAuthAuthorizationChallenge(e, t),
applied: true
};
case "completeAuthorization": {
const t = { ...e };
delete t.codeVerifier;
delete t.lastAuthorizationUrl;
delete t.redirectUrl;
return {
store: t,
applied: true
};
}
case "tokens": {
if (t.refreshClaim !== void 0 && e.refreshClaim !== t.refreshClaim) throw new Error("MCP OAuth refresh ownership changed; sign in again.");
const n = t.tokens;
const r = {
...e,
tokens: n
};
r.authorizationId = !t.replaceAuthorization && hasMcpOAuthAuthorization(e) && e.authorizationId ? e.authorizationId : randomUUID();
if (t.refreshClaim !== void 0 || t.replaceAuthorization) delete r.refreshClaim;
delete r.credentialState;
delete r.pendingAuthorizationChallenge;
const i = e.discoveryState?.authorizationServerUrl;
if (i === void 0) delete r.tokensAuthorizationServerUrl;
else r.tokensAuthorizationServerUrl = i;
const a = t.tokenExpiresAt;
if (a === void 0) delete r.tokenExpiresAt;
else r.tokenExpiresAt = a;
return {
store: r,
applied: true
};
}
case "invalidate": {
const { scope: n, suppressStoredTokens: r } = t;
const i = { ...e };
if (n === "all" || n === "client") delete i.clientInformation;
if ((n === "all" || n === "tokens") && !r) {
delete i.tokens;
delete i.refreshClaim;
delete i.authorizationId;
delete i.tokenExpiresAt;
delete i.tokensAuthorizationServerUrl;
i.credentialState = "cleared";
}
if (n === "all" || n === "verifier") delete i.codeVerifier;
if (n === "all" || n === "discovery") delete i.discoveryState;
return {
store: i,
applied: true
};
}
}
throw new Error("Unknown MCP OAuth mutation");
}
//#endregion
export { applyMcpOAuthMutation as n, MCP_OAUTH_DEFAULT_REDIRECT_URL as t };