| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009 |
- import { createHash } from "node:crypto";
- import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
- import { Buffer } from "node:buffer";
- import { mkdtemp, rm } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import type {
- OAuthClientInformationMixed,
- OAuthTokens,
- } from "@modelcontextprotocol/sdk/shared/auth.js";
- import { auth } from "@modelcontextprotocol/sdk/client/auth.js";
- import type { Logger } from "@zcode/contracts";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import {
- createSharedZCodeCredentialStore,
- type SharedZCodeCredentialStore,
- } from "../src/auth/shared-credentials.js";
- import { createMcpAdapter } from "../src/mcp/index.js";
- import { publishCanonicalCredentials } from "../src/mcp/oauth-credentials.js";
- import { MCP_OAUTH_INTERACTIVE_REQUIRED_ERROR_CODE } from "../src/mcp/oauth-errors.js";
- import {
- createCredentialKeyPrefix,
- createMcpAuthorizationCodeOAuthSession,
- type McpOAuthSession,
- } from "../src/mcp/oauth.js";
- import { createMcpOAuthTokenProvider } from "../src/mcp/oauth-provider.js";
- const OAUTH_ACCESS_TOKEN = "oauth-e2e-access-token";
- const OAUTH_CLIENT_ID = "zcode-client";
- const OAUTH_CLIENT_SECRET = "zcode-secret";
- const DYNAMIC_CLIENT_ID = "dynamic-zcode-client";
- const DYNAMIC_CLIENT_SECRET = "dynamic-zcode-secret";
- const MCP_SCOPE = "mcp:tools";
- const servers: Server[] = [];
- interface StrictAuthorizationServerState {
- authorizationCodes: Map<
- string,
- {
- clientId: string;
- codeChallenge: string;
- redirectUri: string;
- }
- >;
- clients: Map<
- string,
- {
- clientSecret: string;
- redirectUris: string[];
- }
- >;
- refreshTokens: Map<string, string>;
- }
- describe("MCP OAuth client credentials e2e", () => {
- afterEach(async () => {
- await Promise.all(servers.splice(0).map(closeServer));
- });
- it("authenticates a protected HTTP MCP server after a 401 challenge", async () => {
- const authTokenRequests: Array<Record<string, string>> = [];
- const mcpAuthorizations: string[] = [];
- let authBaseUrl = "";
- const authServer = await listen(
- createServer(async (req, res) => {
- await handleAuthServerRequest(req, res, {
- authBaseUrl,
- tokenRequests: authTokenRequests,
- });
- }),
- );
- authBaseUrl = serverBaseUrl(authServer);
- let mcpUrl = "";
- const resourceMetadataPath = "/.well-known/oauth-protected-resource/mcp";
- const mcpServer = await listen(
- createServer(async (req, res) => {
- await handleProtectedMcpRequest(req, res, {
- authBaseUrl,
- authorizations: mcpAuthorizations,
- mcpUrl,
- resourceMetadataPath,
- });
- }),
- );
- mcpUrl = `${serverBaseUrl(mcpServer)}/mcp`;
- const adapter = createMcpAdapter();
- const status = await adapter.connectServer("protected", {
- type: "http",
- url: mcpUrl,
- oauth: {
- type: "client_credentials",
- clientId: OAUTH_CLIENT_ID,
- clientSecret: OAUTH_CLIENT_SECRET,
- scope: MCP_SCOPE,
- },
- });
- expect(status).toMatchObject({
- status: "connected",
- toolCount: 1,
- transport: "http",
- });
- await expect(adapter.listTools()).resolves.toMatchObject([
- {
- serverName: "protected",
- toolName: "oauth_ping",
- },
- ]);
- expect(authTokenRequests).toEqual([
- expect.objectContaining({
- grant_type: "client_credentials",
- resource: mcpUrl,
- scope: MCP_SCOPE,
- }),
- ]);
- expect(mcpAuthorizations).toContain(`Bearer ${OAUTH_ACCESS_TOKEN}`);
- await adapter.close();
- });
- it("completes interactive authorization_code OAuth and reconnects the MCP server", async () => {
- const authorizationRequests: Array<Record<string, string>> = [];
- const authTokenRequests: Array<Record<string, string>> = [];
- const mcpAuthorizations: string[] = [];
- const registrationRequests: Array<Record<string, unknown>> = [];
- const pkceChallenges = new Map<string, string>();
- let authBaseUrl = "";
- const authServer = await listen(
- createServer(async (req, res) => {
- await handleAuthorizationCodeAuthServerRequest(req, res, {
- authBaseUrl,
- authorizationRequests,
- pkceChallenges,
- registrationRequests,
- tokenRequests: authTokenRequests,
- });
- }),
- );
- authBaseUrl = serverBaseUrl(authServer);
- let mcpUrl = "";
- const resourceMetadataPath = "/.well-known/oauth-protected-resource/mcp";
- const mcpServer = await listen(
- createServer(async (req, res) => {
- await handleProtectedMcpRequest(req, res, {
- authBaseUrl,
- authorizations: mcpAuthorizations,
- mcpUrl,
- resourceMetadataPath,
- });
- }),
- );
- mcpUrl = `${serverBaseUrl(mcpServer)}/mcp`;
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: {
- ZCODE_CREDENTIAL_SECRET: "mcp-oauth-e2e-secret",
- },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const adapter = createMcpAdapter({
- mcpOAuth: {
- authorizationTimeoutMs: 5_000,
- credentialStore,
- openAuthorizationUrl: async ({ authorizationUrl }) => {
- const authorizationResponse = await fetch(authorizationUrl, {
- redirect: "manual",
- });
- expect(authorizationResponse.status).toBe(302);
- const location = authorizationResponse.headers.get("location");
- if (!location) {
- throw new Error("Authorization server did not redirect to callback URL");
- }
- const callbackResponse = await fetch(location);
- expect(callbackResponse.status).toBe(200);
- },
- },
- });
- try {
- const status = await adapter.connectServer("figma", {
- type: "http",
- url: mcpUrl,
- oauth: {
- type: "authorization_code",
- clientName: "ZCode Test",
- scope: MCP_SCOPE,
- },
- });
- expect(status).toMatchObject({
- status: "connected",
- toolCount: 1,
- transport: "http",
- });
- await expect(adapter.listTools()).resolves.toMatchObject([
- {
- serverName: "figma",
- toolName: "oauth_ping",
- },
- ]);
- expect(registrationRequests).toHaveLength(1);
- expect(registrationRequests[0]).toMatchObject({
- client_name: "ZCode Test",
- grant_types: ["authorization_code", "refresh_token"],
- response_types: ["code"],
- scope: MCP_SCOPE,
- });
- expect(registrationRequests[0]?.redirect_uris).toEqual([
- expect.stringMatching(/^http:\/\/127\.0\.0\.1:\d+\/oauth\/callback\/mcp\/figma$/),
- ]);
- expect(authorizationRequests).toEqual([
- expect.objectContaining({
- client_id: DYNAMIC_CLIENT_ID,
- code_challenge_method: "S256",
- redirect_uri: expect.stringMatching(
- /^http:\/\/127\.0\.0\.1:\d+\/oauth\/callback\/mcp\/figma$/,
- ),
- resource: mcpUrl,
- response_type: "code",
- scope: MCP_SCOPE,
- }),
- ]);
- expect(authorizationRequests[0]?.code_challenge).toMatch(/^[A-Za-z0-9_-]{43}$/);
- expect(authorizationRequests[0]?.state).toMatch(/^[A-Za-z0-9_-]+$/);
- expect(authTokenRequests).toEqual([
- expect.objectContaining({
- code: "interactive-code-1",
- grant_type: "authorization_code",
- redirect_uri: expect.stringMatching(
- /^http:\/\/127\.0\.0\.1:\d+\/oauth\/callback\/mcp\/figma$/,
- ),
- resource: mcpUrl,
- }),
- ]);
- expect(authTokenRequests[0]?.code_verifier).toMatch(/^[A-Za-z0-9._~-]{43,128}$/);
- expect(pkceChallenges.size).toBe(0);
- expect(mcpAuthorizations).toContain(`Bearer ${OAUTH_ACCESS_TOKEN}`);
- } finally {
- await adapter.close();
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("runs a single interactive authorization transaction for concurrent adapters sharing one credential file", async () => {
- const authorizationRequests: Array<Record<string, string>> = [];
- const authTokenRequests: Array<Record<string, string>> = [];
- const mcpAuthorizations: string[] = [];
- const registrationRequests: Array<Record<string, unknown>> = [];
- const pkceChallenges = new Map<string, string>();
- const strictAuthorizationState = createStrictAuthorizationServerState();
- let authBaseUrl = "";
- const authServer = await listen(
- createServer(async (req, res) => {
- await handleAuthorizationCodeAuthServerRequest(req, res, {
- authBaseUrl,
- authorizationRequests,
- pkceChallenges,
- registrationRequests,
- strictAuthorizationState,
- tokenRequests: authTokenRequests,
- });
- }),
- );
- authBaseUrl = serverBaseUrl(authServer);
- let mcpUrl = "";
- const resourceMetadataPath = "/.well-known/oauth-protected-resource/mcp";
- const mcpServer = await listen(
- createServer(async (req, res) => {
- await handleProtectedMcpRequest(req, res, {
- authBaseUrl,
- authorizations: mcpAuthorizations,
- mcpUrl,
- resourceMetadataPath,
- });
- }),
- );
- mcpUrl = `${serverBaseUrl(mcpServer)}/mcp`;
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-race-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-race-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const authorizationUrls: string[] = [];
- const driveAuthorization = async ({ authorizationUrl }: { authorizationUrl: string }) => {
- authorizationUrls.push(authorizationUrl);
- await followAuthorizationRedirect(authorizationUrl);
- };
- const adapterA = createMcpAdapter({
- mcpOAuth: {
- authorizationTimeoutMs: 5_000,
- credentialStore,
- openAuthorizationUrl: driveAuthorization,
- },
- });
- const adapterB = createMcpAdapter({
- mcpOAuth: {
- authorizationTimeoutMs: 5_000,
- credentialStore,
- openAuthorizationUrl: driveAuthorization,
- },
- });
- const config = {
- type: "http" as const,
- url: mcpUrl,
- oauth: {
- type: "authorization_code" as const,
- clientName: "ZCode Concurrent Test",
- scope: MCP_SCOPE,
- },
- };
- try {
- const [statusA, statusB] = await Promise.all([
- adapterA.connectServer("exa", config),
- adapterB.connectServer("exa", config),
- ]);
- // 修复原因(根因 2):过去两个并发 adapter 各跑一套授权事务,产生两个 DCR client、两个
- // state、两个授权 URL;用户会点到已超时或非目标事务的链接,且每个 client 的 redirect_uris
- // 都锁死在各自的随机端口。现在授权由跨进程 lease 单飞,follower 观察 canonical 换代后
- // 复用同一份凭据。
- expect(statusA).toMatchObject({ status: "connected", toolCount: 1 });
- expect(statusB).toMatchObject({ status: "connected", toolCount: 1 });
- expect(authorizationUrls).toHaveLength(1);
- expect(authorizationRequests).toHaveLength(1);
- expect(registrationRequests).toHaveLength(1);
- expect(authTokenRequests).toHaveLength(1);
- expect(authTokenRequests[0]).toMatchObject({
- client_id: `${DYNAMIC_CLIENT_ID}-1`,
- code: "interactive-code-1",
- grant_type: "authorization_code",
- });
- // fresh DCR 的 redirect_uri 必须与本次授权请求实际使用的回调地址一致。
- expect(registrationRequests[0]?.redirect_uris).toEqual([
- authorizationRequests[0]?.redirect_uri,
- ]);
- expect(pkceChallenges.size).toBe(0);
- } finally {
- await Promise.all([adapterA.close(), adapterB.close()]);
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("re-registers a fresh client instead of reusing a stored client bound to a dead callback port", async () => {
- const authorizationRequests: Array<Record<string, string>> = [];
- const authTokenRequests: Array<Record<string, string>> = [];
- const mcpAuthorizations: string[] = [];
- const registrationRequests: Array<Record<string, unknown>> = [];
- const pkceChallenges = new Map<string, string>();
- const strictAuthorizationState = createStrictAuthorizationServerState();
- const STALE_CLIENT_ID = "stale-exa-client";
- const STALE_REDIRECT_URI = "http://127.0.0.1:54735/oauth/callback/mcp/exa";
- let authBaseUrl = "";
- const authServer = await listen(
- createServer(async (req, res) => {
- await handleAuthorizationCodeAuthServerRequest(req, res, {
- authBaseUrl,
- authorizationRequests,
- // 精确匹配 redirect_uri(含端口),模拟对 loopback 不做 RFC 8252 端口豁免的授权服务器。
- exactRedirectUriMatch: true,
- pkceChallenges,
- registrationRequests,
- strictAuthorizationState,
- tokenRequests: authTokenRequests,
- });
- }),
- );
- authBaseUrl = serverBaseUrl(authServer);
- let mcpUrl = "";
- const resourceMetadataPath = "/.well-known/oauth-protected-resource/mcp";
- const mcpServer = await listen(
- createServer(async (req, res) => {
- await handleProtectedMcpRequest(req, res, {
- authBaseUrl,
- authorizations: mcpAuthorizations,
- mcpUrl,
- resourceMetadataPath,
- });
- }),
- );
- mcpUrl = `${serverBaseUrl(mcpServer)}/mcp`;
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-stale-client-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-stale-client-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const oauthConfig = {
- type: "authorization_code" as const,
- clientName: "ZCode exa",
- scope: MCP_SCOPE,
- };
- const keyPrefix = createCredentialKeyPrefix("exa", mcpUrl, oauthConfig);
- // 现场复现:client 注册于历史端口 54735,之后每次授权都换新的随机端口。
- strictAuthorizationState.clients.set(STALE_CLIENT_ID, {
- clientSecret: "stale-exa-secret",
- redirectUris: [STALE_REDIRECT_URI],
- });
- await credentialStore.save(
- `${keyPrefix}:client_information`,
- JSON.stringify({
- client_id: STALE_CLIENT_ID,
- client_secret: "stale-exa-secret",
- token_endpoint_auth_method: "client_secret_basic",
- }),
- );
- const adapter = createMcpAdapter({
- mcpOAuth: {
- authorizationTimeoutMs: 5_000,
- credentialStore,
- openAuthorizationUrl: async ({ authorizationUrl }) => {
- await followAuthorizationRedirect(authorizationUrl);
- },
- },
- });
- try {
- const status = await adapter.connectServer("exa", {
- type: "http",
- url: mcpUrl,
- oauth: oauthConfig,
- });
- // 修复原因(根因 2):过去这条路径会发出「旧 client_id + 新 redirect_uri」的授权请求。
- // 授权服务器按 RFC 6749 §4.1.2.1 禁止回跳、就地渲染错误页(用户看到的 Exa
- // "Server Components render error"),回调永不到达,且重试永不自愈。
- expect(status).toMatchObject({ status: "connected", toolCount: 1 });
- expect(registrationRequests).toHaveLength(1);
- expect(authorizationRequests).toHaveLength(1);
- expect(authorizationRequests[0]?.client_id).toBe(`${DYNAMIC_CLIENT_ID}-1`);
- expect(authorizationRequests[0]?.client_id).not.toBe(STALE_CLIENT_ID);
- expect(authorizationRequests[0]?.redirect_uri).not.toBe(STALE_REDIRECT_URI);
- // fresh DCR 注册的 redirect_uri 必须正是本次授权实际使用的存活 listener 地址。
- expect(registrationRequests[0]?.redirect_uris).toEqual([
- authorizationRequests[0]?.redirect_uri,
- ]);
- expect(authTokenRequests).toHaveLength(1);
- expect(authTokenRequests[0]?.client_id).toBe(`${DYNAMIC_CLIENT_ID}-1`);
- // 发布后的 canonical pair 必须是本次授权的 client,而不是历史 client。
- const canonicalRaw = await credentialStore.load(`${keyPrefix}:authorization_credentials`);
- expect(canonicalRaw).toBeTruthy();
- const canonical = JSON.parse(canonicalRaw ?? "{}") as {
- client_information?: { client_id?: string };
- generation?: string;
- };
- expect(canonical.client_information?.client_id).toBe(`${DYNAMIC_CLIENT_ID}-1`);
- expect(canonical.generation).toMatch(/^[0-9a-f]{32}$/);
- } finally {
- await adapter.close();
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("performs zero discovery, zero DCR and opens zero listeners for a passive connect with valid credentials", async () => {
- const authorizationRequests: Array<Record<string, string>> = [];
- const authTokenRequests: Array<Record<string, string>> = [];
- const mcpAuthorizations: string[] = [];
- const registrationRequests: Array<Record<string, unknown>> = [];
- const pkceChallenges = new Map<string, string>();
- let authServerRequests = 0;
- let authBaseUrl = "";
- const authServer = await listen(
- createServer(async (req, res) => {
- authServerRequests += 1;
- await handleAuthorizationCodeAuthServerRequest(req, res, {
- authBaseUrl,
- authorizationRequests,
- pkceChallenges,
- registrationRequests,
- tokenRequests: authTokenRequests,
- });
- }),
- );
- authBaseUrl = serverBaseUrl(authServer);
- let mcpUrl = "";
- const resourceMetadataPath = "/.well-known/oauth-protected-resource/mcp";
- const mcpServer = await listen(
- createServer(async (req, res) => {
- await handleProtectedMcpRequest(req, res, {
- authBaseUrl,
- authorizations: mcpAuthorizations,
- mcpUrl,
- resourceMetadataPath,
- });
- }),
- );
- mcpUrl = `${serverBaseUrl(mcpServer)}/mcp`;
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-passive-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-passive-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const oauthConfig = { type: "authorization_code" as const, scope: MCP_SCOPE };
- const keyPrefix = createCredentialKeyPrefix("passive", mcpUrl, oauthConfig);
- // 现成可用的凭据:还没临期,因此连接期不需要任何 OAuth 动作。
- await publishCanonicalCredentials(credentialStore, keyPrefix, {
- clientInformation: { client_id: "passive-client" },
- publishedBy: "passive-seed",
- tokens: {
- access_token: OAUTH_ACCESS_TOKEN,
- expires_in: 3600,
- refresh_token: "passive-refresh-token",
- token_type: "Bearer",
- },
- });
- const adapter = createMcpAdapter({ mcpOAuth: { credentialStore } });
- const listenersBefore = countTcpServers();
- try {
- const status = await adapter.connectServer("passive", {
- type: "http",
- url: mcpUrl,
- oauth: oauthConfig,
- });
- expect(status).toMatchObject({ status: "connected", toolCount: 1 });
- expect(mcpAuthorizations).toContain(`Bearer ${OAUTH_ACCESS_TOKEN}`);
- // 修复原因:过去任何没有 Authorization header 的 HTTP/SSE MCP 在建连前就会创建完整 OAuth
- // session,而 session 返回前就 listen(0) 起了一个 callback server——即使凭据完全有效。
- // 纯 AuthProvider 让被动连接彻底不碰 discovery / DCR / listener。
- expect(authServerRequests).toBe(0);
- expect(registrationRequests).toHaveLength(0);
- expect(authorizationRequests).toHaveLength(0);
- expect(countTcpServers()).toBe(listenersBefore);
- } finally {
- await adapter.close();
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("recovers a tool call whose refresh token was revoked by running Phase 2 and reconnecting", async () => {
- const authorizationRequests: Array<Record<string, string>> = [];
- const authTokenRequests: Array<Record<string, string>> = [];
- const mcpAuthorizations: string[] = [];
- const registrationRequests: Array<Record<string, unknown>> = [];
- const pkceChallenges = new Map<string, string>();
- let authBaseUrl = "";
- const authServer = await listen(
- createServer(async (req, res) => {
- await handleAuthorizationCodeAuthServerRequest(req, res, {
- authBaseUrl,
- authorizationRequests,
- pkceChallenges,
- registrationRequests,
- tokenRequests: authTokenRequests,
- });
- }),
- );
- authBaseUrl = serverBaseUrl(authServer);
- // 前 N 次工具调用返回 401,模拟运行期 access token 失效。
- let rejectToolCalls = true;
- let mcpUrl = "";
- const resourceMetadataPath = "/.well-known/oauth-protected-resource/mcp";
- const mcpServer = await listen(
- createServer(async (req, res) => {
- if (rejectToolCalls && req.method === "POST") {
- const body = await readRequestBody(req);
- if (body.includes('"tools/call"')) {
- res.writeHead(401, {
- "WWW-Authenticate": [
- `Bearer resource_metadata="${new URL(resourceMetadataPath, mcpUrl).href}", scope="${MCP_SCOPE}"`,
- ],
- });
- res.end();
- return;
- }
- await handleProtectedMcpRequest(req, res, {
- authBaseUrl,
- authorizations: mcpAuthorizations,
- body,
- mcpUrl,
- resourceMetadataPath,
- });
- return;
- }
- await handleProtectedMcpRequest(req, res, {
- authBaseUrl,
- authorizations: mcpAuthorizations,
- mcpUrl,
- resourceMetadataPath,
- });
- }),
- );
- mcpUrl = `${serverBaseUrl(mcpServer)}/mcp`;
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-runtime-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-runtime-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const oauthConfig = { type: "authorization_code" as const, scope: MCP_SCOPE };
- const keyPrefix = createCredentialKeyPrefix("runtime", mcpUrl, oauthConfig);
- await publishCanonicalCredentials(credentialStore, keyPrefix, {
- clientInformation: { client_id: "revoked-client" },
- publishedBy: "runtime-seed",
- tokens: {
- access_token: OAUTH_ACCESS_TOKEN,
- expires_in: 3600,
- // 该 refresh token 在 fake AS 上不存在 → 刷新时收到 invalid_grant。
- refresh_token: "revoked-refresh-token",
- token_type: "Bearer",
- },
- });
- const adapter = createMcpAdapter({
- mcpOAuth: {
- credentialStore,
- openAuthorizationUrl: async ({ authorizationUrl }) => {
- rejectToolCalls = false;
- await followAuthorizationRedirect(authorizationUrl);
- },
- },
- });
- try {
- const status = await adapter.connectServer("runtime", {
- type: "http",
- url: mcpUrl,
- oauth: oauthConfig,
- });
- expect(status).toMatchObject({ status: "connected", toolCount: 1 });
- // Bugfix 回归:建连后 token 被撤销时,过去 callTool 的 401/403 原样冒泡,用户看到裸错误且
- // 永不自愈——OAuth 自愈只存在于 startup connect 路径。现在运行期共用 Phase 2 → Phase 1 编排。
- await expect(
- adapter.callTool({ serverName: "runtime", toolName: "oauth_ping" }),
- ).resolves.toMatchObject({ isError: false });
- expect(registrationRequests).toHaveLength(1);
- expect(authorizationRequests).toHaveLength(1);
- } finally {
- await adapter.close();
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("steps up authorization scope with the union of configured, granted, and challenged scopes", async () => {
- const authorizationRequests: Array<Record<string, string>> = [];
- const authTokenRequests: Array<Record<string, string>> = [];
- const mcpAuthorizations: string[] = [];
- const registrationRequests: Array<Record<string, unknown>> = [];
- const pkceChallenges = new Map<string, string>();
- const STEP_UP_SCOPE = "admin:tools";
- let authBaseUrl = "";
- const authServer = await listen(
- createServer(async (req, res) => {
- await handleAuthorizationCodeAuthServerRequest(req, res, {
- authBaseUrl,
- authorizationRequests,
- pkceChallenges,
- registrationRequests,
- tokenRequests: authTokenRequests,
- });
- }),
- );
- authBaseUrl = serverBaseUrl(authServer);
- let mcpUrl = "";
- let stepUpRequired = true;
- const resourceMetadataPath = "/.well-known/oauth-protected-resource/mcp";
- const mcpServer = await listen(
- createServer(async (req, res) => {
- await handleProtectedMcpRequest(req, res, {
- authBaseUrl,
- authorizations: mcpAuthorizations,
- mcpUrl,
- resourceMetadataPath,
- ...(stepUpRequired ? { stepUpScope: STEP_UP_SCOPE } : {}),
- });
- }),
- );
- mcpUrl = `${serverBaseUrl(mcpServer)}/mcp`;
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-stepup-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-stepup-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const oauthConfig = { type: "authorization_code" as const, scope: MCP_SCOPE };
- const keyPrefix = createCredentialKeyPrefix("stepup", mcpUrl, oauthConfig);
- // 现存凭据:token 有效、已授予 mcp:tools,但服务端现在要求 admin:tools。
- await publishCanonicalCredentials(credentialStore, keyPrefix, {
- clientInformation: { client_id: "stepup-client" },
- publishedBy: "stepup-seed",
- tokens: {
- access_token: OAUTH_ACCESS_TOKEN,
- expires_in: 3600,
- scope: MCP_SCOPE,
- token_type: "Bearer",
- },
- });
- const adapter = createMcpAdapter({
- mcpOAuth: {
- credentialStore,
- openAuthorizationUrl: async ({ authorizationUrl }) => {
- stepUpRequired = false;
- await followAuthorizationRedirect(authorizationUrl);
- },
- },
- });
- try {
- const status = await adapter.connectServer("stepup", {
- type: "http",
- url: mcpUrl,
- oauth: oauthConfig,
- });
- expect(status).toMatchObject({ status: "connected", toolCount: 1 });
- // 修复原因(spec §5.2,复核 P1):只带 challenge scope 重新授权时,授权服务器可能按新请求
- // 收回先前授予的 scope,下一个请求换个 challenge 又 403,形成重授权乒乓。重新授权请求
- // 必须携带 config ∪ token.scope ∪ challenge 的并集,且 DCR 注册同一个值。
- expect(authorizationRequests).toHaveLength(1);
- expect(authorizationRequests[0]?.scope).toBe(`${MCP_SCOPE} ${STEP_UP_SCOPE}`);
- expect(registrationRequests).toHaveLength(1);
- expect(registrationRequests[0]?.scope).toBe(`${MCP_SCOPE} ${STEP_UP_SCOPE}`);
- // challenge 携带的 resource_metadata 要传导为授权请求的 resource 受众参数。
- expect(authorizationRequests[0]?.resource).toBe(mcpUrl);
- expect(authTokenRequests).toHaveLength(1);
- } finally {
- await adapter.close();
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("binds dynamically registered client information to each OAuth transaction", async () => {
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-client-transaction-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-client-transaction-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const sessions: McpOAuthSession[] = [];
- try {
- const sessionA = await createTestOAuthSession(credentialStore, "transaction-a");
- const sessionB = await createTestOAuthSession(credentialStore, "transaction-b");
- sessions.push(sessionA, sessionB);
- const clientA = dynamicClientInformation("transaction-client-a");
- const clientB = dynamicClientInformation("transaction-client-b");
- await saveDynamicClientInformation(sessionA, clientA);
- await saveDynamicClientInformation(sessionB, clientB);
- await expect(sessionA.provider.clientInformation()).resolves.toEqual(clientA);
- await expect(sessionB.provider.clientInformation()).resolves.toEqual(clientB);
- } finally {
- await Promise.all(sessions.map((session) => session.close()));
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("restores dynamically registered client information after provider reconstruction", async () => {
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-client-recovery-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-client-recovery-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const sessions: McpOAuthSession[] = [];
- try {
- const registered = await createTestOAuthSession(credentialStore, "recovery-registered");
- sessions.push(registered);
- const client = dynamicClientInformation("recovery-client");
- await saveDynamicClientInformation(registered, client);
- await registered.close();
- sessions.splice(sessions.indexOf(registered), 1);
- const reconstructed = await createTestOAuthSession(credentialStore, "recovery-reconstructed");
- sessions.push(reconstructed);
- await expect(reconstructed.provider.clientInformation()).resolves.toEqual(client);
- } finally {
- await Promise.all(sessions.map((session) => session.close()));
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("retries credential snapshots after a transient load failure", async () => {
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-load-retry-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-load-retry-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const sessions: McpOAuthSession[] = [];
- try {
- const publisher = await createTestOAuthSession(credentialStore, "load-retry-publisher");
- sessions.push(publisher);
- const client = dynamicClientInformation("load-retry-client");
- const tokens = oauthTokens("load-retry-client");
- await saveDynamicClientInformation(publisher, client);
- await publisher.provider.saveTokens(tokens);
- let loadAttempts = 0;
- const transientFailure = new Error("injected transient credential load failure");
- const retryingStore: SharedZCodeCredentialStore = {
- ...credentialStore,
- async loadMany(keys: string[]): Promise<Record<string, string | null>> {
- loadAttempts += 1;
- if (loadAttempts === 1) throw transientFailure;
- return await credentialStore.loadMany(keys);
- },
- };
- const reader = await createTestOAuthSession(retryingStore, "load-retry-reader");
- sessions.push(reader);
- await expect(reader.provider.tokens()).rejects.toThrow(transientFailure.message);
- await expect(reader.provider.tokens()).resolves.toEqual(tokens);
- await expect(reader.provider.clientInformation()).resolves.toEqual(client);
- expect(loadAttempts).toBe(2);
- } finally {
- await Promise.all(sessions.map((session) => session.close()));
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("does not fail session close when verifier cleanup fails", async () => {
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-close-cleanup-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-close-cleanup-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const cleanupError = new Error("injected verifier cleanup failure");
- const failingStore: SharedZCodeCredentialStore = {
- ...credentialStore,
- async delete(key: string): Promise<void> {
- if (key.includes(":code_verifier:")) throw cleanupError;
- await credentialStore.delete(key);
- },
- };
- const { logger, warn } = createTestLogger();
- const session = await createMcpAuthorizationCodeOAuthSession({
- adapterInstanceId: "close-cleanup",
- config: testAuthorizationCodeConfig(),
- logger,
- options: { credentialStore: failingStore },
- serverName: "credential-pair-test",
- serverUrl: "https://mcp.example.test/mcp",
- });
- try {
- await session.provider.saveCodeVerifier("close-cleanup-verifier");
- await expect(session.close()).resolves.toBeUndefined();
- expect(warn).toHaveBeenCalledWith(
- "MCP OAuth verifier cleanup failed",
- expect.objectContaining({
- error: cleanupError.message,
- event: "mcp.oauth.verifier.cleanup_failed",
- mcpServerName: "credential-pair-test",
- }),
- );
- } finally {
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("publishes client information and tokens as one credential snapshot", async () => {
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-client-token-pair-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-client-token-pair-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const sessions: McpOAuthSession[] = [];
- try {
- const sessionA = await createTestOAuthSession(credentialStore, "pair-a");
- const sessionB = await createTestOAuthSession(credentialStore, "pair-b");
- sessions.push(sessionA, sessionB);
- const clientA = dynamicClientInformation("pair-client-a");
- const clientB = dynamicClientInformation("pair-client-b");
- const tokensA = oauthTokens("pair-client-a");
- const tokensB = oauthTokens("pair-client-b");
- await saveDynamicClientInformation(sessionA, clientA);
- await saveDynamicClientInformation(sessionB, clientB);
- await sessionB.provider.saveTokens(tokensB);
- await sessionA.provider.saveTokens(tokensA);
- const reader = await createTestOAuthSession(credentialStore, "pair-reader");
- sessions.push(reader);
- await expect(reader.provider.clientInformation()).resolves.toEqual(clientA);
- await expect(reader.provider.tokens()).resolves.toEqual(tokensA);
- } finally {
- await Promise.all(sessions.map((session) => session.close()));
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("preserves v1 canonical credentials across legacy migration states", async () => {
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-v1-migration-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-v1-migration-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const sessions: McpOAuthSession[] = [];
- const config = testAuthorizationCodeConfig();
- const serverUrl = "https://mcp.example.test/mcp";
- const keyPrefix = createCredentialKeyPrefix("credential-pair-test", serverUrl, config);
- const canonicalKey = `${keyPrefix}:authorization_credentials`;
- const legacyClientKey = `${keyPrefix}:client_information`;
- const legacyTokensKey = `${keyPrefix}:tokens`;
- const canonicalClient = dynamicClientInformation("v1-canonical-client");
- const canonicalTokens = oauthTokens("v1-canonical-client");
- const canonicalRaw = JSON.stringify({
- client_information: canonicalClient,
- expires_at: Date.now() + 3_600_000,
- published_by: "v1-publisher",
- tokens: canonicalTokens,
- version: 1,
- });
- try {
- // v1 发布时会删除 legacy 镜像;无镜像是正常稳态,不能因此丢弃 canonical token。
- await credentialStore.save(canonicalKey, canonicalRaw);
- const canonicalReader = await createTestOAuthSession(credentialStore, "v1-canonical-reader");
- sessions.push(canonicalReader);
- await expect(canonicalReader.provider.clientInformation()).resolves.toEqual(canonicalClient);
- await expect(canonicalReader.provider.tokens()).resolves.toEqual(canonicalTokens);
- const phaseOneProvider = createMcpOAuthTokenProvider({
- config,
- credentialStore,
- keyPrefix,
- serverName: "credential-pair-test",
- serverUrl,
- });
- await expect(phaseOneProvider.token()).resolves.toBe(canonicalTokens.access_token);
- // 旧 provider 可能只写回 refresh 后的 tokens;缺少 legacy client 时复用 canonical 身份。
- const tokenOnlyRefresh = {
- ...canonicalTokens,
- access_token: "v1-token-only-refreshed-access-token",
- };
- await credentialStore.save(legacyTokensKey, JSON.stringify(tokenOnlyRefresh));
- const tokenOnlyReader = await createTestOAuthSession(credentialStore, "v1-token-only-reader");
- sessions.push(tokenOnlyReader);
- await expect(tokenOnlyReader.provider.clientInformation()).resolves.toEqual(canonicalClient);
- await expect(tokenOnlyReader.provider.tokens()).resolves.toEqual(tokenOnlyRefresh);
- // legacy client 与 canonical 身份一致时,可以确认 tokens 属于同一 client 的更新。
- const matchingRefresh = {
- ...canonicalTokens,
- access_token: "v1-matching-refreshed-access-token",
- };
- await credentialStore.saveMany({
- [legacyClientKey]: JSON.stringify(canonicalClient),
- [legacyTokensKey]: JSON.stringify(matchingRefresh),
- });
- const matchingReader = await createTestOAuthSession(credentialStore, "v1-matching-reader");
- sessions.push(matchingReader);
- await expect(matchingReader.provider.clientInformation()).resolves.toEqual(canonicalClient);
- await expect(matchingReader.provider.tokens()).resolves.toEqual(matchingRefresh);
- // legacy client 身份不一致时无法确认 token 归属,必须保留完整 canonical pair。
- const mismatchedClient = dynamicClientInformation("v1-mismatched-client");
- const mismatchedTokens = oauthTokens("v1-mismatched-client");
- await credentialStore.saveMany({
- [legacyClientKey]: JSON.stringify(mismatchedClient),
- [legacyTokensKey]: JSON.stringify(mismatchedTokens),
- });
- const mismatchedReader = await createTestOAuthSession(
- credentialStore,
- "v1-mismatched-reader",
- );
- sessions.push(mismatchedReader);
- await expect(mismatchedReader.provider.clientInformation()).resolves.toEqual(canonicalClient);
- await expect(mismatchedReader.provider.tokens()).resolves.toEqual(canonicalTokens);
- } finally {
- await Promise.all(sessions.map((session) => session.close()));
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("keeps legacy readers and writers coherent during the canonical migration window", async () => {
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-mixed-version-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-mixed-version-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const sessions: McpOAuthSession[] = [];
- const config = testAuthorizationCodeConfig();
- const keyPrefix = createCredentialKeyPrefix(
- "credential-pair-test",
- "https://mcp.example.test/mcp",
- config,
- );
- const legacyClientKey = `${keyPrefix}:client_information`;
- const legacyTokensKey = `${keyPrefix}:tokens`;
- const client = dynamicClientInformation("mixed-version-client");
- const initialTokens = oauthTokens("mixed-version-client");
- const reauthorizedClient = dynamicClientInformation("mixed-version-reauthorized-client");
- const reauthorizedTokens = oauthTokens("mixed-version-reauthorized-client");
- const refreshedByLegacy = {
- ...reauthorizedTokens,
- access_token: "mixed-version-reauthorized-client-legacy-refreshed-access-token",
- };
- try {
- const publisher = await createTestOAuthSession(credentialStore, "mixed-version-publisher");
- sessions.push(publisher);
- await saveDynamicClientInformation(publisher, client);
- await publisher.provider.saveTokens(initialTokens);
- // 目标分支旧 provider 只认识这两个 legacy key,发布后必须仍能读取同一 pair。
- await expect(credentialStore.load(legacyClientKey)).resolves.toBe(JSON.stringify(client));
- await expect(credentialStore.load(legacyTokensKey)).resolves.toBe(
- JSON.stringify(initialTokens),
- );
- // 旧 provider 的 DCR 与 token exchange 不是同一次写入;只更新 client 时仍须读取旧 canonical pair。
- await credentialStore.save(legacyClientKey, JSON.stringify(reauthorizedClient));
- const pendingReader = await createTestOAuthSession(
- credentialStore,
- "mixed-version-pending-reader",
- );
- sessions.push(pendingReader);
- await expect(pendingReader.provider.clientInformation()).resolves.toEqual(client);
- await expect(pendingReader.provider.tokens()).resolves.toEqual(initialTokens);
- // legacy 没有 generation;client/token 都变化时无法区分已完成重授权与跨事务交错。
- // 新 provider 保留 client 但丢弃不可信 token,完成自己的 token exchange 后再发布 pair。
- await credentialStore.save(legacyTokensKey, JSON.stringify(reauthorizedTokens));
- const reauthorizedReader = await createTestOAuthSession(
- credentialStore,
- "mixed-version-reauthorized-reader",
- );
- sessions.push(reauthorizedReader);
- await expect(reauthorizedReader.provider.clientInformation()).resolves.toEqual(
- reauthorizedClient,
- );
- await expect(reauthorizedReader.provider.tokens()).resolves.toBeUndefined();
- await reauthorizedReader.provider.saveTokens(reauthorizedTokens);
- // 模拟旧 provider refresh:它只更新 legacy tokens,新 provider 不得继续读取 stale canonical。
- await credentialStore.save(legacyTokensKey, JSON.stringify(refreshedByLegacy));
- const refreshedReader = await createTestOAuthSession(
- credentialStore,
- "mixed-version-refreshed-reader",
- );
- sessions.push(refreshedReader);
- await expect(refreshedReader.provider.clientInformation()).resolves.toEqual(
- reauthorizedClient,
- );
- await expect(refreshedReader.provider.tokens()).resolves.toEqual(refreshedByLegacy);
- // 模拟旧 provider invalidate tokens:canonical 不能把已失效 token 复活。
- await credentialStore.delete(legacyTokensKey);
- const invalidatedReader = await createTestOAuthSession(
- credentialStore,
- "mixed-version-invalidated-reader",
- );
- sessions.push(invalidatedReader);
- await expect(invalidatedReader.provider.clientInformation()).resolves.toEqual(
- reauthorizedClient,
- );
- await expect(invalidatedReader.provider.tokens()).resolves.toBeUndefined();
- } finally {
- await Promise.all(sessions.map((session) => session.close()));
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("does not combine a pending legacy client with another legacy refresh", async () => {
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-legacy-race-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-legacy-race-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const sessions: McpOAuthSession[] = [];
- const config = testAuthorizationCodeConfig();
- const keyPrefix = createCredentialKeyPrefix(
- "credential-pair-test",
- "https://mcp.example.test/mcp",
- config,
- );
- const clientA = dynamicClientInformation("legacy-race-client-a");
- const tokensA = oauthTokens("legacy-race-client-a");
- const pendingClientB = dynamicClientInformation("legacy-race-client-b");
- const refreshedTokensA = {
- ...tokensA,
- access_token: "legacy-race-client-a-refreshed-access-token",
- };
- try {
- const publisher = await createTestOAuthSession(credentialStore, "legacy-race-publisher");
- sessions.push(publisher);
- await saveDynamicClientInformation(publisher, clientA);
- await publisher.provider.saveTokens(tokensA);
- // 旧 provider B 只完成动态注册;旧 provider A 随后只刷新 token。两次独立写入
- // 没有 generation 可证明归属,新 reader 不能把 client B 与 token A2 拼成一对。
- await credentialStore.save(`${keyPrefix}:client_information`, JSON.stringify(pendingClientB));
- await credentialStore.save(`${keyPrefix}:tokens`, JSON.stringify(refreshedTokensA));
- const reader = await createTestOAuthSession(credentialStore, "legacy-race-reader");
- sessions.push(reader);
- await expect(reader.provider.clientInformation()).resolves.toEqual(pendingClientB);
- await expect(reader.provider.tokens()).resolves.toBeUndefined();
- } finally {
- await Promise.all(sessions.map((session) => session.close()));
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("does not invalidate a credential snapshot replaced by another transaction", async () => {
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-compare-delete-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-compare-delete-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const sessions: McpOAuthSession[] = [];
- try {
- const publisherA = await createTestOAuthSession(credentialStore, "publisher-a");
- const staleReader = await createTestOAuthSession(credentialStore, "stale-reader");
- const publisherB = await createTestOAuthSession(credentialStore, "publisher-b");
- sessions.push(publisherA, staleReader, publisherB);
- const clientA = dynamicClientInformation("published-client-a");
- const tokensA = oauthTokens("published-client-a");
- const clientB = dynamicClientInformation("published-client-b");
- const tokensB = oauthTokens("published-client-b");
- await saveDynamicClientInformation(publisherA, clientA);
- await publisherA.provider.saveTokens(tokensA);
- await expect(staleReader.provider.clientInformation()).resolves.toEqual(clientA);
- await expect(staleReader.provider.tokens()).resolves.toEqual(tokensA);
- await saveDynamicClientInformation(publisherB, clientB);
- await publisherB.provider.saveTokens(tokensB);
- await staleReader.provider.invalidateCredentials?.("tokens");
- // 同一个失败 provider 的 SDK 重试也必须丢弃旧 client 缓存并读取获胜事务的 pair。
- await expect(staleReader.provider.clientInformation()).resolves.toEqual(clientB);
- await expect(staleReader.provider.tokens()).resolves.toEqual(tokensB);
- const verifier = await createTestOAuthSession(credentialStore, "compare-delete-verifier");
- sessions.push(verifier);
- await expect(verifier.provider.clientInformation()).resolves.toEqual(clientB);
- await expect(verifier.provider.tokens()).resolves.toEqual(tokensB);
- } finally {
- await Promise.all(sessions.map((session) => session.close()));
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("refreshes the last concurrently published canonical pair with one client identity", async () => {
- const authorizationRequests: Array<Record<string, string>> = [];
- const tokenRequests: Array<Record<string, string>> = [];
- const registrationRequests: Array<Record<string, unknown>> = [];
- const pkceChallenges = new Map<string, string>();
- const strictAuthorizationState = createStrictAuthorizationServerState();
- let authBaseUrl = "";
- const authServer = await listen(
- createServer(async (req, res) => {
- await handleAuthorizationCodeAuthServerRequest(req, res, {
- authBaseUrl,
- authorizationRequests,
- pkceChallenges,
- registrationRequests,
- strictAuthorizationState,
- tokenRequests,
- });
- }),
- );
- authBaseUrl = serverBaseUrl(authServer);
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-refresh-pair-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-refresh-pair-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const sessions: McpOAuthSession[] = [];
- const clientA = dynamicClientInformation("refresh-pair-client-a");
- const tokensA = oauthTokens("refresh-pair-client-a");
- const clientB = dynamicClientInformation("refresh-pair-client-b");
- const tokensB = oauthTokens("refresh-pair-client-b");
- for (const [client, tokens] of [
- [clientA, tokensA],
- [clientB, tokensB],
- ] as const) {
- strictAuthorizationState.clients.set(client.client_id, {
- clientSecret: client.client_secret ?? "",
- redirectUris: [],
- });
- strictAuthorizationState.refreshTokens.set(tokens.refresh_token ?? "", client.client_id);
- }
- try {
- const publisherA = await createTestOAuthSession(
- credentialStore,
- "refresh-publisher-a",
- `${authBaseUrl}/mcp`,
- );
- const publisherB = await createTestOAuthSession(
- credentialStore,
- "refresh-publisher-b",
- `${authBaseUrl}/mcp`,
- );
- sessions.push(publisherA, publisherB);
- await saveDynamicClientInformation(publisherA, clientA);
- await saveDynamicClientInformation(publisherB, clientB);
- await publisherB.provider.saveTokens(tokensB);
- await publisherA.provider.saveTokens(tokensA);
- const reader = await createTestOAuthSession(
- credentialStore,
- "refresh-reader",
- `${authBaseUrl}/mcp`,
- );
- sessions.push(reader);
- await expect(auth(reader.provider, { serverUrl: `${authBaseUrl}/mcp` })).resolves.toBe(
- "AUTHORIZED",
- );
- expect(authorizationRequests).toHaveLength(0);
- expect(registrationRequests).toHaveLength(0);
- expect(tokenRequests).toEqual([
- expect.objectContaining({
- client_id: clientA.client_id,
- grant_type: "refresh_token",
- refresh_token: tokensA.refresh_token,
- }),
- ]);
- await expect(reader.provider.clientInformation()).resolves.toEqual(clientA);
- await expect(reader.provider.tokens()).resolves.toMatchObject({
- access_token: `${clientA.client_id}-refreshed-access-token`,
- refresh_token: tokensA.refresh_token,
- });
- } finally {
- await Promise.all(sessions.map((session) => session.close()));
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("preserves a canonical credential snapshot written by a newer version", async () => {
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-future-credentials-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-future-credentials-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const sessions: McpOAuthSession[] = [];
- const keyPrefix = createCredentialKeyPrefix(
- "credential-pair-test",
- "https://mcp.example.test/mcp",
- testAuthorizationCodeConfig(),
- );
- const futureSnapshot = JSON.stringify({
- client_information: dynamicClientInformation("future-client"),
- published_by: "future-publisher",
- tokens: oauthTokens("future-client"),
- version: 3,
- });
- try {
- await credentialStore.save(`${keyPrefix}:authorization_credentials`, futureSnapshot);
- const session = await createTestOAuthSession(credentialStore, "future-reader");
- sessions.push(session);
- // 未知版本必须保留原值:CLI 与 desktop 独立升级、共享同一份凭据文件,旧版本不能把新版本
- // 写入的 canonical pair 当损坏数据删除。没有可用 client 时改为要求交互授权(PR2 过渡 seam),
- // 而不是返回 undefined 让 SDK 自行做一次动态注册。
- await expect(session.provider.clientInformation()).rejects.toMatchObject({
- code: MCP_OAUTH_INTERACTIVE_REQUIRED_ERROR_CODE,
- reason: "no_credentials",
- });
- await expect(session.provider.tokens()).resolves.toBeUndefined();
- await expect(credentialStore.load(`${keyPrefix}:authorization_credentials`)).resolves.toBe(
- futureSnapshot,
- );
- } finally {
- await Promise.all(sessions.map((session) => session.close()));
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("removes both legacy credential halves when the legacy client is invalid", async () => {
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-legacy-invalidation-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: { ZCODE_CREDENTIAL_SECRET: "mcp-oauth-legacy-invalidation-secret" },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const sessions: McpOAuthSession[] = [];
- const config = testAuthorizationCodeConfig();
- const keyPrefix = createCredentialKeyPrefix(
- "credential-pair-test",
- "https://mcp.example.test/mcp",
- config,
- );
- try {
- await credentialStore.save(
- `${keyPrefix}:client_information`,
- JSON.stringify(dynamicClientInformation("legacy-client")),
- );
- await credentialStore.save(
- `${keyPrefix}:tokens`,
- JSON.stringify(oauthTokens("legacy-client")),
- );
- const session = await createTestOAuthSession(credentialStore, "legacy-invalidation");
- sessions.push(session);
- await expect(session.provider.clientInformation()).resolves.toBeDefined();
- await expect(session.provider.tokens()).resolves.toBeDefined();
- await session.provider.invalidateCredentials?.("client");
- await expect(credentialStore.load(`${keyPrefix}:client_information`)).resolves.toBeNull();
- await expect(credentialStore.load(`${keyPrefix}:tokens`)).resolves.toBeNull();
- } finally {
- await Promise.all(sessions.map((session) => session.close()));
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("discovers authorization_code OAuth for a bare HTTP MCP config", async () => {
- const authorizationRequests: Array<Record<string, string>> = [];
- const authTokenRequests: Array<Record<string, string>> = [];
- const mcpAuthorizations: string[] = [];
- const registrationRequests: Array<Record<string, unknown>> = [];
- const pkceChallenges = new Map<string, string>();
- let authBaseUrl = "";
- const authServer = await listen(
- createServer(async (req, res) => {
- await handleAuthorizationCodeAuthServerRequest(req, res, {
- authBaseUrl,
- authorizationRequests,
- pkceChallenges,
- registrationRequests,
- tokenRequests: authTokenRequests,
- });
- }),
- );
- authBaseUrl = serverBaseUrl(authServer);
- let mcpUrl = "";
- const resourceMetadataPath = "/.well-known/oauth-protected-resource/mcp";
- const mcpServer = await listen(
- createServer(async (req, res) => {
- await handleProtectedMcpRequest(req, res, {
- authBaseUrl,
- authorizations: mcpAuthorizations,
- mcpUrl,
- resourceMetadataPath,
- });
- }),
- );
- mcpUrl = `${serverBaseUrl(mcpServer)}/mcp`;
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-auto-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: {
- ZCODE_CREDENTIAL_SECRET: "mcp-oauth-e2e-secret",
- },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const adapter = createMcpAdapter({
- mcpOAuth: {
- authorizationTimeoutMs: 5_000,
- credentialStore,
- openAuthorizationUrl: async ({ authorizationUrl }) => {
- const authorizationResponse = await fetch(authorizationUrl, {
- redirect: "manual",
- });
- expect(authorizationResponse.status).toBe(302);
- const location = authorizationResponse.headers.get("location");
- if (!location) {
- throw new Error("Authorization server did not redirect to callback URL");
- }
- const callbackResponse = await fetch(location);
- expect(callbackResponse.status).toBe(200);
- },
- },
- });
- try {
- const status = await adapter.connectServer("notion", {
- type: "http",
- url: mcpUrl,
- });
- expect(status).toMatchObject({
- status: "connected",
- toolCount: 1,
- transport: "http",
- });
- await expect(adapter.listTools()).resolves.toMatchObject([
- {
- serverName: "notion",
- toolName: "oauth_ping",
- },
- ]);
- expect(registrationRequests).toHaveLength(1);
- expect(registrationRequests[0]).toMatchObject({
- client_name: "ZCode notion",
- grant_types: ["authorization_code", "refresh_token"],
- response_types: ["code"],
- scope: MCP_SCOPE,
- });
- expect(authorizationRequests).toEqual([
- expect.objectContaining({
- client_id: DYNAMIC_CLIENT_ID,
- resource: mcpUrl,
- response_type: "code",
- scope: MCP_SCOPE,
- }),
- ]);
- expect(authTokenRequests).toEqual([
- expect.objectContaining({
- code: "interactive-code-1",
- grant_type: "authorization_code",
- resource: mcpUrl,
- }),
- ]);
- expect(mcpAuthorizations).toContain(`Bearer ${OAUTH_ACCESS_TOKEN}`);
- } finally {
- await adapter.close();
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- it("isolates persisted authorization_code credentials by authorization config", async () => {
- const credentialsDir = await mkdtemp(join(tmpdir(), "zcode-mcp-oauth-"));
- const credentialStore = createSharedZCodeCredentialStore({
- env: {
- ZCODE_CREDENTIAL_SECRET: "mcp-oauth-e2e-secret",
- },
- filePath: join(credentialsDir, "credentials.json"),
- });
- const serverName = "figma";
- const serverUrl = "https://mcp.example.test/mcp";
- const keyPrefixA = createCredentialKeyPrefix(serverName, serverUrl, {
- type: "authorization_code",
- clientName: "ZCode Test",
- scope: MCP_SCOPE,
- });
- const keyPrefixB = createCredentialKeyPrefix(serverName, serverUrl, {
- type: "authorization_code",
- clientName: "ZCode Test",
- scope: "mcp:tools:readonly",
- });
- try {
- await credentialStore.save(
- `${keyPrefixA}:client_information`,
- JSON.stringify({
- client_id: DYNAMIC_CLIENT_ID,
- client_secret: DYNAMIC_CLIENT_SECRET,
- }),
- );
- await credentialStore.save(
- `${keyPrefixA}:tokens`,
- JSON.stringify({
- access_token: OAUTH_ACCESS_TOKEN,
- refresh_token: "oauth-e2e-refresh-token",
- token_type: "Bearer",
- }),
- );
- expect(keyPrefixB).not.toBe(keyPrefixA);
- await expect(credentialStore.load(`${keyPrefixA}:client_information`)).resolves.toContain(
- DYNAMIC_CLIENT_ID,
- );
- await expect(credentialStore.load(`${keyPrefixA}:tokens`)).resolves.toContain(
- OAUTH_ACCESS_TOKEN,
- );
- await expect(credentialStore.load(`${keyPrefixB}:client_information`)).resolves.toBeNull();
- await expect(credentialStore.load(`${keyPrefixB}:tokens`)).resolves.toBeNull();
- } finally {
- await rm(credentialsDir, { force: true, recursive: true });
- }
- });
- });
- async function handleAuthServerRequest(
- req: IncomingMessage,
- res: ServerResponse,
- options: {
- authBaseUrl: string;
- tokenRequests: Array<Record<string, string>>;
- },
- ): Promise<void> {
- const url = new URL(req.url ?? "/", "http://localhost");
- if (req.method === "GET" && url.pathname === "/.well-known/oauth-authorization-server") {
- sendJson(res, 200, {
- issuer: options.authBaseUrl,
- authorization_endpoint: `${options.authBaseUrl}/authorize`,
- token_endpoint: `${options.authBaseUrl}/token`,
- response_types_supported: ["code"],
- grant_types_supported: ["client_credentials"],
- token_endpoint_auth_methods_supported: ["client_secret_basic"],
- });
- return;
- }
- if (req.method === "POST" && url.pathname === "/token") {
- if (!isValidClientSecretBasic(req.headers.authorization)) {
- sendJson(res, 401, {
- error: "invalid_client",
- });
- return;
- }
- const body = new URLSearchParams(await readRequestBody(req));
- options.tokenRequests.push(Object.fromEntries(body.entries()));
- sendJson(res, 200, {
- access_token: OAUTH_ACCESS_TOKEN,
- expires_in: 3600,
- scope: body.get("scope") ?? MCP_SCOPE,
- token_type: "Bearer",
- });
- return;
- }
- res.writeHead(404);
- res.end();
- }
- async function handleAuthorizationCodeAuthServerRequest(
- req: IncomingMessage,
- res: ServerResponse,
- options: {
- authBaseUrl: string;
- authorizationRequests: Array<Record<string, string>>;
- /**
- * 要求 redirect_uri 与注册值完全一致(含端口)。
- *
- * 默认的 loopback 比较按 RFC 8252 忽略端口,这恰好掩盖了根因 2:真实授权服务器(如
- * auth.exa.ai)可能做精确匹配,失配时按 RFC 6749 §4.1.2.1 禁止回跳、就地渲染错误页。
- */
- exactRedirectUriMatch?: boolean;
- pkceChallenges: Map<string, string>;
- registrationRequests: Array<Record<string, unknown>>;
- strictAuthorizationState?: StrictAuthorizationServerState;
- tokenRequests: Array<Record<string, string>>;
- },
- ): Promise<void> {
- const url = new URL(req.url ?? "/", "http://localhost");
- if (req.method === "GET" && url.pathname === "/.well-known/oauth-authorization-server") {
- sendJson(res, 200, {
- issuer: options.authBaseUrl,
- authorization_endpoint: `${options.authBaseUrl}/authorize`,
- token_endpoint: `${options.authBaseUrl}/token`,
- registration_endpoint: `${options.authBaseUrl}/register`,
- response_types_supported: ["code"],
- grant_types_supported: ["authorization_code", "refresh_token"],
- token_endpoint_auth_methods_supported: ["client_secret_basic", "client_secret_post"],
- code_challenge_methods_supported: ["S256"],
- scopes_supported: [MCP_SCOPE],
- });
- return;
- }
- if (req.method === "POST" && url.pathname === "/register") {
- const body = JSON.parse(await readRequestBody(req)) as Record<string, unknown>;
- options.registrationRequests.push(body);
- const registrationIndex = options.registrationRequests.length;
- const clientId = options.strictAuthorizationState
- ? `${DYNAMIC_CLIENT_ID}-${registrationIndex}`
- : DYNAMIC_CLIENT_ID;
- const clientSecret = options.strictAuthorizationState
- ? `${DYNAMIC_CLIENT_SECRET}-${registrationIndex}`
- : DYNAMIC_CLIENT_SECRET;
- if (options.strictAuthorizationState) {
- const redirectUris = Array.isArray(body.redirect_uris)
- ? body.redirect_uris.filter((entry): entry is string => typeof entry === "string")
- : [];
- options.strictAuthorizationState.clients.set(clientId, { clientSecret, redirectUris });
- }
- sendJson(res, 201, {
- ...body,
- client_id: clientId,
- client_secret: clientSecret,
- client_id_issued_at: 1,
- client_secret_expires_at: 0,
- token_endpoint_auth_method: "client_secret_basic",
- });
- return;
- }
- if (req.method === "GET" && url.pathname === "/authorize") {
- options.authorizationRequests.push(Object.fromEntries(url.searchParams.entries()));
- const clientId = url.searchParams.get("client_id") ?? "";
- const redirectUri = url.searchParams.get("redirect_uri");
- const codeChallenge = url.searchParams.get("code_challenge");
- const registeredClient = options.strictAuthorizationState?.clients.get(clientId);
- if (
- !redirectUri ||
- !codeChallenge ||
- url.searchParams.get("code_challenge_method") !== "S256" ||
- (options.strictAuthorizationState &&
- (!registeredClient ||
- !registeredClient.redirectUris.some((registeredUri) =>
- options.exactRedirectUriMatch
- ? redirectUri === registeredUri
- : matchesRegisteredRedirectUri(redirectUri, registeredUri),
- )))
- ) {
- sendJson(res, 400, {
- error: "invalid_request",
- });
- return;
- }
- const authorizationCode = `interactive-code-${options.authorizationRequests.length}`;
- options.pkceChallenges.set(authorizationCode, codeChallenge);
- options.strictAuthorizationState?.authorizationCodes.set(authorizationCode, {
- clientId,
- codeChallenge,
- redirectUri,
- });
- const callbackUrl = new URL(redirectUri);
- callbackUrl.searchParams.set("code", authorizationCode);
- const state = url.searchParams.get("state");
- if (state) callbackUrl.searchParams.set("state", state);
- res.writeHead(302, {
- Location: callbackUrl.toString(),
- });
- res.end();
- return;
- }
- if (req.method === "POST" && url.pathname === "/token") {
- const body = new URLSearchParams(await readRequestBody(req));
- const clientCredentials = parseClientSecretBasic(req.headers.authorization);
- options.tokenRequests.push({
- ...Object.fromEntries(body.entries()),
- ...(clientCredentials ? { client_id: clientCredentials.clientId } : {}),
- });
- if (body.get("grant_type") === "refresh_token") {
- const refreshToken = body.get("refresh_token") ?? "";
- const refreshClientId = options.strictAuthorizationState?.refreshTokens.get(refreshToken);
- const refreshClient = refreshClientId
- ? options.strictAuthorizationState?.clients.get(refreshClientId)
- : undefined;
- if (
- !refreshClientId ||
- !refreshClient ||
- clientCredentials?.clientId !== refreshClientId ||
- clientCredentials.clientSecret !== refreshClient.clientSecret
- ) {
- sendJson(res, 400, { error: "invalid_grant" });
- return;
- }
- sendJson(res, 200, {
- access_token: `${refreshClientId}-refreshed-access-token`,
- expires_in: 3600,
- refresh_token: refreshToken,
- scope: MCP_SCOPE,
- token_type: "Bearer",
- });
- return;
- }
- const authorizationCode = body.get("code") ?? "";
- const codeVerifier = body.get("code_verifier") ?? "";
- const expectedChallenge = options.pkceChallenges.get(authorizationCode);
- const actualChallenge = createHash("sha256").update(codeVerifier).digest("base64url");
- const strictAuthorization =
- options.strictAuthorizationState?.authorizationCodes.get(authorizationCode);
- const strictClient = strictAuthorization
- ? options.strictAuthorizationState?.clients.get(strictAuthorization.clientId)
- : undefined;
- const hasValidClient = options.strictAuthorizationState
- ? Boolean(
- strictAuthorization &&
- strictClient &&
- clientCredentials?.clientId === strictAuthorization.clientId &&
- clientCredentials.clientSecret === strictClient.clientSecret &&
- body.get("redirect_uri") === strictAuthorization.redirectUri,
- )
- : isValidClientSecretBasic(
- req.headers.authorization,
- DYNAMIC_CLIENT_ID,
- DYNAMIC_CLIENT_SECRET,
- );
- if (!hasValidClient) {
- sendJson(res, 400, { error: "invalid_grant" });
- return;
- }
- if (!expectedChallenge || actualChallenge !== expectedChallenge) {
- sendJson(res, 400, { error: "invalid_grant" });
- return;
- }
- options.pkceChallenges.delete(authorizationCode);
- options.strictAuthorizationState?.authorizationCodes.delete(authorizationCode);
- sendJson(res, 200, {
- access_token: OAUTH_ACCESS_TOKEN,
- expires_in: 3600,
- refresh_token: "oauth-e2e-refresh-token",
- scope: MCP_SCOPE,
- token_type: "Bearer",
- });
- return;
- }
- res.writeHead(404);
- res.end();
- }
- async function followAuthorizationRedirect(authorizationUrl: string): Promise<void> {
- const authorizationResponse = await fetch(authorizationUrl, { redirect: "manual" });
- expect(authorizationResponse.status).toBe(302);
- const location = authorizationResponse.headers.get("location");
- if (!location) throw new Error("Authorization server did not redirect to callback URL");
- const callbackResponse = await fetch(location);
- expect(callbackResponse.status).toBe(200);
- }
- async function handleProtectedMcpRequest(
- req: IncomingMessage,
- res: ServerResponse,
- options: {
- authBaseUrl: string;
- authorizations: string[];
- /** 调用方已经消费过请求体时传入,避免二次读取空流。 */
- body?: string;
- mcpUrl: string;
- resourceMetadataPath: string;
- /** 403 insufficient_scope challenge 的 scope;置位时对 tools/list 生效。 */
- stepUpScope?: string;
- },
- ): Promise<void> {
- const url = new URL(req.url ?? "/", "http://localhost");
- if (req.method === "GET" && url.pathname === options.resourceMetadataPath) {
- sendJson(res, 200, {
- authorization_servers: [options.authBaseUrl],
- resource: options.mcpUrl,
- resource_name: "ZCode OAuth E2E MCP",
- scopes_supported: [MCP_SCOPE],
- });
- return;
- }
- if (req.method === "GET" && url.pathname === "/mcp") {
- res.writeHead(405);
- res.end();
- return;
- }
- if (req.method !== "POST" || url.pathname !== "/mcp") {
- res.writeHead(404);
- res.end();
- return;
- }
- const authorization = req.headers.authorization;
- if (authorization) options.authorizations.push(authorization);
- if (authorization !== `Bearer ${OAUTH_ACCESS_TOKEN}`) {
- res.writeHead(401, {
- "WWW-Authenticate": [
- `Bearer resource_metadata="${new URL(options.resourceMetadataPath, options.mcpUrl).href}", scope="${MCP_SCOPE}"`,
- ],
- });
- res.end();
- return;
- }
- const message = JSON.parse(options.body ?? (await readRequestBody(req))) as {
- id?: string | number;
- method?: string;
- };
- // 403 insufficient_scope:Bearer 合法但 scope 不足。challenge 同时带 resource_metadata,
- // 验证编排层把 challenge 的发现线索传进 Phase 2(spec §5.2 / §8 item 15)。
- if (options.stepUpScope && message.method === "tools/list") {
- res.writeHead(403, {
- "WWW-Authenticate": [
- `Bearer error="insufficient_scope", scope="${options.stepUpScope}", resource_metadata="${new URL(options.resourceMetadataPath, options.mcpUrl).href}"`,
- ],
- });
- res.end();
- return;
- }
- if (message.method === "initialize") {
- sendJson(res, 200, {
- id: message.id,
- jsonrpc: "2.0",
- result: {
- capabilities: { tools: {} },
- protocolVersion: "2025-11-25",
- serverInfo: {
- name: "oauth-e2e-mcp",
- version: "1.0.0",
- },
- },
- });
- return;
- }
- if (message.method === "tools/list") {
- sendJson(res, 200, {
- id: message.id,
- jsonrpc: "2.0",
- result: {
- tools: [
- {
- description: "OAuth protected ping",
- inputSchema: {
- properties: {},
- type: "object",
- },
- name: "oauth_ping",
- },
- ],
- },
- });
- return;
- }
- if (message.method === "tools/call") {
- sendJson(res, 200, {
- id: message.id,
- jsonrpc: "2.0",
- result: {
- content: [{ type: "text", text: "pong" }],
- isError: false,
- },
- });
- return;
- }
- if (message.method === "server/discover") {
- sendJson(res, 200, {
- error: {
- code: -32601,
- message: "Method not found",
- },
- id: message.id,
- jsonrpc: "2.0",
- });
- return;
- }
- sendJson(res, 202, {});
- }
- function isValidClientSecretBasic(
- value: string | undefined,
- clientId = OAUTH_CLIENT_ID,
- clientSecret = OAUTH_CLIENT_SECRET,
- ): boolean {
- if (!value?.startsWith("Basic ")) return false;
- const decoded = Buffer.from(value.slice("Basic ".length), "base64").toString("utf8");
- return decoded === `${clientId}:${clientSecret}`;
- }
- function createStrictAuthorizationServerState(): StrictAuthorizationServerState {
- return {
- authorizationCodes: new Map(),
- clients: new Map(),
- refreshTokens: new Map(),
- };
- }
- async function createTestOAuthSession(
- credentialStore: SharedZCodeCredentialStore,
- adapterInstanceId: string,
- serverUrl = "https://mcp.example.test/mcp",
- ): Promise<McpOAuthSession> {
- return await createMcpAuthorizationCodeOAuthSession({
- adapterInstanceId,
- config: testAuthorizationCodeConfig(),
- options: { credentialStore },
- serverName: "credential-pair-test",
- serverUrl,
- });
- }
- function testAuthorizationCodeConfig() {
- return {
- type: "authorization_code" as const,
- clientName: "ZCode Credential Pair Test",
- scope: MCP_SCOPE,
- };
- }
- function dynamicClientInformation(clientId: string): OAuthClientInformationMixed {
- return {
- client_id: clientId,
- client_secret: `${clientId}-secret`,
- token_endpoint_auth_method: "client_secret_basic",
- };
- }
- function oauthTokens(clientId: string): OAuthTokens {
- return {
- access_token: `${clientId}-access-token`,
- refresh_token: `${clientId}-refresh-token`,
- token_type: "Bearer",
- };
- }
- function createTestLogger(): { logger: Logger; warn: ReturnType<typeof vi.fn> } {
- const warn = vi.fn();
- const logger: Logger = {
- child: () => logger,
- debug: vi.fn(),
- error: vi.fn(),
- info: vi.fn(),
- warn,
- };
- return { logger, warn };
- }
- async function saveDynamicClientInformation(
- session: McpOAuthSession,
- clientInformation: OAuthClientInformationMixed,
- ): Promise<void> {
- if (!session.provider.saveClientInformation) {
- throw new Error("OAuth provider does not support dynamic client registration");
- }
- await session.provider.saveClientInformation(clientInformation);
- }
- function parseClientSecretBasic(
- value: string | undefined,
- ): { clientId: string; clientSecret: string } | undefined {
- if (!value?.startsWith("Basic ")) return undefined;
- const decoded = Buffer.from(value.slice("Basic ".length), "base64").toString("utf8");
- const separatorIndex = decoded.indexOf(":");
- if (separatorIndex < 0) return undefined;
- return {
- clientId: decoded.slice(0, separatorIndex),
- clientSecret: decoded.slice(separatorIndex + 1),
- };
- }
- function matchesRegisteredRedirectUri(requestedValue: string, registeredValue: string): boolean {
- try {
- const requested = new URL(requestedValue);
- const registered = new URL(registeredValue);
- const isLoopback =
- requested.protocol === "http:" &&
- registered.protocol === "http:" &&
- requested.hostname === registered.hostname &&
- (requested.hostname === "127.0.0.1" || requested.hostname === "[::1]");
- if (!isLoopback) return requested.href === registered.href;
- return (
- requested.pathname === registered.pathname &&
- requested.search === registered.search &&
- requested.hash === registered.hash
- );
- } catch {
- return false;
- }
- }
- /** 统计当前活跃的 TCP server handle 数,用来断言连接期没有新开 callback listener。 */
- function countTcpServers(): number {
- return process.getActiveResourcesInfo().filter((resource) => resource === "TCPSERVERWRAP").length;
- }
- function sendJson(res: ServerResponse, statusCode: number, body: unknown): void {
- res.writeHead(statusCode, {
- "Content-Type": "application/json",
- });
- res.end(JSON.stringify(body));
- }
- async function readRequestBody(req: IncomingMessage): Promise<string> {
- const chunks: Buffer[] = [];
- for await (const chunk of req) {
- chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
- }
- return Buffer.concat(chunks).toString("utf8");
- }
- async function listen(server: Server): Promise<Server> {
- await new Promise<void>((resolve) => {
- server.listen(0, "127.0.0.1", resolve);
- });
- servers.push(server);
- return server;
- }
- async function closeServer(server: Server): Promise<void> {
- if (!server.listening) return;
- await new Promise<void>((resolve, reject) => {
- server.close((error) => {
- if (error) reject(error);
- else resolve();
- });
- });
- }
- function serverBaseUrl(server: Server): string {
- const address = server.address();
- if (!address || typeof address === "string") {
- throw new Error("Test server is not listening on a TCP port");
- }
- return `http://127.0.0.1:${address.port}`;
- }
|