UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

87 lines (86 loc) • 6.47 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { a as executeSqliteQueryTakeFirstSync, i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs"; import "./openclaw-state-db-schema-helpers-nImZNMq_.mjs"; import { g as McpOAuthStoreCorruptionError } from "./openclaw-state-worker-error-C8y1JGQN.mjs"; import { toUSVString } from "node:util"; import { OAuthClientInformationSchema, OAuthMetadataSchema, OAuthProtectedResourceMetadataSchema, OAuthTokensSchema, OpenIdProviderDiscoveryMetadataSchema } from "@modelcontextprotocol/core"; //#region src/agents/mcp-oauth-store.kernel.ts const MCP_OAUTH_STORE_FORMAT_VERSION = 1; const UNINITIALIZED_STORE_FIELDS = /* @__PURE__ */ new Set(["credentialState", "pendingAuthorizationChallenge"]); function assertOptionalString(e, t, n) { const r = t[n]; if (r !== void 0 && (typeof r !== "string" || r.length === 0)) throw new McpOAuthStoreCorruptionError(e, `${n} must be a non-empty string`); } function assertDiscoveryState(e, t) { if (t === void 0) return; if (!isRecord(t) || typeof t.authorizationServerUrl !== "string") throw new McpOAuthStoreCorruptionError(e, "discoveryState is invalid"); if (!URL.canParse(t.authorizationServerUrl)) throw new McpOAuthStoreCorruptionError(e, "discoveryState URLs are invalid"); if (t.resourceMetadataUrl !== void 0 && (typeof t.resourceMetadataUrl !== "string" || !URL.canParse(t.resourceMetadataUrl))) throw new McpOAuthStoreCorruptionError(e, "discoveryState URLs are invalid"); if (t.resourceMetadata !== void 0 && !OAuthProtectedResourceMetadataSchema.safeParse(t.resourceMetadata).success) throw new McpOAuthStoreCorruptionError(e, "discoveryState resource metadata is invalid"); if (t.authorizationServerMetadata !== void 0 && !OAuthMetadataSchema.safeParse(t.authorizationServerMetadata).success && !OpenIdProviderDiscoveryMetadataSchema.safeParse(t.authorizationServerMetadata).success) throw new McpOAuthStoreCorruptionError(e, "discoveryState authorization server metadata is invalid"); } function assertAuthorizationChallenge(e, t) { if (t === void 0) return; if (!isRecord(t)) throw new McpOAuthStoreCorruptionError(e, "pendingAuthorizationChallenge is invalid"); const n = t.resourceMetadataUrl; if (n !== void 0 && (typeof n !== "string" || !URL.canParse(n))) throw new McpOAuthStoreCorruptionError(e, "pendingAuthorizationChallenge URL is invalid"); const r = t.scope; if (r !== void 0 && (typeof r !== "string" || r.length === 0)) throw new McpOAuthStoreCorruptionError(e, "pendingAuthorizationChallenge scope is invalid"); if (t.requiresAuthorization !== void 0 && t.requiresAuthorization !== true) throw new McpOAuthStoreCorruptionError(e, "pendingAuthorizationChallenge requiresAuthorization must be true"); } /** Parse a canonical row without discarding SDK extension fields. */ function parseMcpOAuthStoreJson(e, t) { let n; try { n = JSON.parse(t); } catch (t) { throw new McpOAuthStoreCorruptionError(e, "store_json is not valid JSON", { cause: t }); } if (!isRecord(n)) throw new McpOAuthStoreCorruptionError(e, "store_json must contain an object"); if (n.clientInformation !== void 0 && !OAuthClientInformationSchema.safeParse(n.clientInformation).success) throw new McpOAuthStoreCorruptionError(e, "clientInformation is invalid"); if (n.tokens !== void 0 && !OAuthTokensSchema.safeParse(n.tokens).success) throw new McpOAuthStoreCorruptionError(e, "tokens are invalid"); if (n.credentialState !== void 0 && n.credentialState !== "uninitialized" && n.credentialState !== "cleared") throw new McpOAuthStoreCorruptionError(e, "credentialState is invalid"); if (n.credentialState !== void 0 && n.tokens !== void 0) throw new McpOAuthStoreCorruptionError(e, "credentialState cannot coexist with tokens"); if (n.credentialState === "uninitialized" && Object.keys(n).some((e) => !UNINITIALIZED_STORE_FIELDS.has(e))) throw new McpOAuthStoreCorruptionError(e, "uninitialized credential state contains authoritative OAuth fields"); if (n.tokenExpiresAt !== void 0 && (typeof n.tokenExpiresAt !== "number" || !Number.isFinite(n.tokenExpiresAt) || n.tokenExpiresAt < 0)) throw new McpOAuthStoreCorruptionError(e, "tokenExpiresAt is invalid"); if (n.tokenExpiresAt !== void 0 && n.tokens === void 0) throw new McpOAuthStoreCorruptionError(e, "tokenExpiresAt requires tokens"); if (n.tokensAuthorizationServerUrl !== void 0 && (typeof n.tokensAuthorizationServerUrl !== "string" || !URL.canParse(n.tokensAuthorizationServerUrl))) throw new McpOAuthStoreCorruptionError(e, "tokensAuthorizationServerUrl is invalid"); if (n.tokensAuthorizationServerUrl !== void 0 && n.tokens === void 0) throw new McpOAuthStoreCorruptionError(e, "tokensAuthorizationServerUrl requires tokens"); assertOptionalString(e, n, "codeVerifier"); assertOptionalString(e, n, "lastAuthorizationUrl"); assertOptionalString(e, n, "redirectUrl"); assertOptionalString(e, n, "authorizationId"); assertOptionalString(e, n, "refreshClaim"); if (n.refreshClaim !== void 0 && (!isRecord(n.tokens) || !n.tokens.refresh_token)) throw new McpOAuthStoreCorruptionError(e, "refreshClaim requires a refresh grant"); assertDiscoveryState(e, n.discoveryState); assertAuthorizationChallenge(e, n.pendingAuthorizationChallenge); return n; } function storeFromRow(e, t) { if (!t) return {}; if (t.format_version !== MCP_OAUTH_STORE_FORMAT_VERSION) throw new McpOAuthStoreCorruptionError(e, `unsupported format version ${t.format_version}`); return parseMcpOAuthStoreJson(e, t.store_json); } function readMcpOAuthStoreInDatabase(e, t) { return storeFromRow(t, executeSqliteQueryTakeFirstSync(e, getNodeSqliteKysely(e).selectFrom("mcp_oauth_stores").select(["format_version", "store_json"]).where("store_key", "=", t))); } const MCP_OAUTH_PENDING_STATE_TTL_MS = 6e5; function replaceMcpOAuthStoreInDatabase(e, t, n, r) { const i = JSON.stringify(n); parseMcpOAuthStoreJson(t, i); r?.(e); const a = Date.now(); executeSqliteQuerySync(e, getNodeSqliteKysely(e).insertInto("mcp_oauth_stores").values({ store_key: t, format_version: MCP_OAUTH_STORE_FORMAT_VERSION, store_json: i, updated_at: a }).onConflict((e) => e.column("store_key").doUpdateSet({ format_version: MCP_OAUTH_STORE_FORMAT_VERSION, store_json: i, updated_at: a }))); return n; } //#endregion export { replaceMcpOAuthStoreInDatabase as i, parseMcpOAuthStoreJson as n, readMcpOAuthStoreInDatabase as r, MCP_OAUTH_PENDING_STATE_TTL_MS as t };