Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

44.0 KB
Raw Blame
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { createServer } from "node:http";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";

import { cardOwner, McpOAuthError, McpOAuthManager, mcpCallbackOrigin, mcpOAuthRedirectUri, type McpSignInPhase, type McpSignInStatus, withMcpSignIn, withoutPendingSignIn } from "./mcp-oauth.ts";
import type { McpOAuthClientConfig } from "./mcp-registry.ts";
import { startFakeHttpMcp, type FakeHttpMcp } from "./testing/fake-http-mcp-server.ts";
import { startFakeOAuth, type FakeOAuth, type FakeOAuthOptions } from "./testing/fake-oauth-server.ts";
import { withFreeSignInPort } from "./testing/ports.ts";

let dir: string;
let oauth: FakeOAuth;
let mcp: FakeHttpMcp;
let manager: McpOAuthManager;
let registered: McpOAuthClientConfig | undefined;

async function setup(options: FakeOAuthOptions = {}, lifetimeMs?: number) {
  oauth = await startFakeOAuth(options);
  mcp = await startFakeHttpMcp({ acceptBearer: oauth.isValid, wwwAuthenticate: oauth.challenge });
  // a pre-registered app returns to the port derived from the URL: make it a free one
  if (options.preRegistered) mcp = { ...mcp, url: await withFreeSignInPort(mcp.url) };
  manager = new McpOAuthManager({
    file: join(dir, "mcp-oauth.json"),
    ...(lifetimeMs ? { lifetimeMs } : {}),
    clients: (name, url) => (name === "docs" && url === mcp.url ? registered : undefined),
  });
}

async function approve(status: McpSignInStatus) {
  return fetch(status.authorizationUrl!, { redirect: "follow" });
}

async function settled(name: string, flowId: string): Promise<McpSignInStatus> {
  for (let i = 0; i < 100; i++) {
    const status = manager.status(name, flowId);
    if (status && status.phase !== "waiting") return status;
    await new Promise((resolve) => setTimeout(resolve, 10));
  }
  throw new Error("sign-in never settled");
}

beforeEach(() => {
  dir = mkdtempSync(join(tmpdir(), "mcp-oauth-"));
  registered = undefined;
});
afterEach(async () => {
  manager?.dispose();
  await mcp?.close();
  await oauth?.close();
  rmSync(dir, { recursive: true, force: true });
});

describe("McpOAuthManager sign-in", () => {
  it("signs in through the browser and returns a working token", async () => {
    await setup();
    expect(manager.authState("docs", mcp.url)).toBe("none");
    const started = await manager.start("docs", mcp.url);
    expect(started.phase).toBe("waiting");
    const link = new URL(started.authorizationUrl!);
    expect(link.origin + link.pathname).toBe(`${oauth.issuer}/authorize`);
    expect(link.searchParams.get("code_challenge_method")).toBe("S256");
    expect(link.searchParams.get("resource")).toBe(mcp.url);
    expect(link.searchParams.get("redirect_uri")).toMatch(/^http:\/\/127\.0\.0\.1:\d+\/mcp-oauth\/callback$/);

    const page = await approve(started);
    expect(page.status).toBe(200);
    expect(await page.text()).toMatch(/Signed in/);
    expect((await settled("docs", started.flowId)).phase).toBe("succeeded");

    expect(manager.authState("docs", mcp.url)).toBe("signed-in");
    const token = await manager.accessToken("docs", mcp.url);
    expect(oauth.isValid(`Bearer ${token}`)).toBe(true);
  });

  it("reuses the waiting flow when sign-in is started twice", async () => {
    await setup();
    const [a, b] = await Promise.all([manager.start("docs", mcp.url), manager.start("docs", mcp.url)]);
    expect(a.flowId).toBe(b.flowId);
    expect(oauth.counts.register).toBe(1);
  });

  it("rejects a callback with the wrong state and keeps waiting", async () => {
    await setup();
    const started = await manager.start("docs", mcp.url);
    const redirect = new URL(new URL(started.authorizationUrl!).searchParams.get("redirect_uri")!);
    redirect.searchParams.set("state", "forged");
    redirect.searchParams.set("code", "stolen");
    const response = await fetch(redirect);
    expect(response.status).toBe(400);
    expect(manager.status("docs", started.flowId)?.phase).toBe("waiting");
  });

  it("expires a sign-in nobody finishes and closes its listener", async () => {
    await setup({}, 100);
    const started = await manager.start("docs", mcp.url);
    expect((await settled("docs", started.flowId)).phase).toBe("expired");
    await expect(approve(started)).rejects.toThrow();
  });

  it("reports a denied sign-in", async () => {
    await setup({ deny: true });
    const started = await manager.start("docs", mcp.url);
    await approve(started);
    const status = await settled("docs", started.flowId);
    expect(status.phase).toBe("failed");
    expect(status.message).toMatch(/not approved/);
    expect(manager.authState("docs", mcp.url)).toBe("none");
  });

  it("explains a server that offers no client registration", async () => {
    await setup({ noRegistration: true });
    await expect(manager.start("docs", mcp.url)).rejects.toMatchObject({ code: "no-registration" });
  });

  it("shows the server's own reason when it refuses to register the app", async () => {
    await setup({ registerRefusal: { status: 429, body: { error: "too_many_requests", error_description: "Too many client registrations from this address. Please try again in an hour." }, headers: { "retry-after": "3600" } } });
    const error = await manager.start("docs", mcp.url).catch((cause: unknown) => cause);
    expect((error as Error).message).toBe("This server would not register OpenMausBot for sign-in (HTTP 429): Too many client registrations from this address. Please try again in an hour. It asked to wait: Retry-After 3600 seconds. Try again later.");
  });

  it("keeps the plain refusal when the server gives no reason", async () => {
    await setup({ registerRefusal: { status: 500, body: {} } });
    const error = await manager.start("docs", mcp.url).catch((cause: unknown) => cause);
    expect((error as Error).message).toBe("This server would not register OpenMausBot for sign-in. Try again later.");
  });

  it("still reports the refusal when the body is JSON null", async () => {
    await setup({ registerRefusal: { status: 429, body: null, headers: { "retry-after": "Wed, 21 Oct 2026 07:28:00 GMT" } } });
    const error = await manager.start("docs", mcp.url).catch((cause: unknown) => cause);
    expect((error as Error).message).toBe("This server would not register OpenMausBot for sign-in (HTTP 429). It asked to wait: Retry-After Wed, 21 Oct 2026 07:28:00 GMT. Try again later.");
  });

  it("refuses a server that does not use OAuth", async () => {
    await setup();
    const open = await startFakeHttpMcp();
    try {
      const error = await manager.start("open", open.url).catch((cause: unknown) => cause);
      expect(error).toBeInstanceOf(McpOAuthError);
      expect(error).toMatchObject({ code: "not-oauth" });
    } finally {
      await open.close();
    }
  });
});

describe("McpOAuthManager tokens", () => {
  async function signedIn(options: FakeOAuthOptions = {}) {
    await setup(options);
    const started = await manager.start("docs", mcp.url);
    await approve(started);
    await settled("docs", started.flowId);
  }

  it("requests offline access advertised by the authorization server and refreshes the resulting token", async () => {
    await signedIn({ expiresIn: 60, offlineAccess: true });
    expect(oauth.lastAuthorize?.get("scope")).toBe("mcp offline_access");
    expect(oauth.isValid(`Bearer ${await manager.accessToken("docs", mcp.url)}`)).toBe(true);
    expect(oauth.counts.refresh).toBe(1);
    expect(manager.authState("docs", mcp.url)).toBe("signed-in");
  });

  it("refreshes a token about to expire, once for concurrent callers", async () => {
    await signedIn({ expiresIn: 60 });
    const [a, b] = await Promise.all([manager.accessToken("docs", mcp.url), manager.accessToken("docs", mcp.url)]);
    expect(oauth.counts.refresh).toBe(1);
    expect(a).toBe(b);
    expect(oauth.isValid(`Bearer ${a}`)).toBe(true);
  });

  it("does not refresh a token with time left", async () => {
    await signedIn({ expiresIn: 3600 });
    await manager.accessToken("docs", mcp.url);
    expect(oauth.counts.refresh).toBe(0);
  });

  it("marks the server as needing sign-in when refresh is refused", async () => {
    await signedIn({ expiresIn: 60, rejectRefresh: true });
    expect(await manager.accessToken("docs", mcp.url)).toBeNull();
    expect(manager.authState("docs", mcp.url)).toBe("needs-sign-in");
  });

  it("never hands a token to a different URL", async () => {
    await signedIn();
    expect(await manager.accessToken("docs", "https://elsewhere.example.com/mcp")).toBeNull();
    expect(manager.authState("docs", "https://elsewhere.example.com/mcp")).toBe("none");
  });

  it("signs out: revokes and forgets", async () => {
    await signedIn();
    await manager.signOut("docs", mcp.url);
    expect(oauth.counts.revoke).toBeGreaterThan(0);
    expect(manager.authState("docs", mcp.url)).toBe("none");
    expect(await manager.accessToken("docs", mcp.url)).toBeNull();
  });

  it("remembers a server that needs sign-in until it is forgotten", async () => {
    await setup();
    manager.markNeedsSignIn("docs", mcp.url);
    expect(manager.authState("docs", mcp.url)).toBe("needs-sign-in");
    manager.forget("docs");
    expect(manager.authState("docs", mcp.url)).toBe("none");
  });

  it("records that a sign-in is required for a server with no record", async () => {
    await setup();
    manager.requireSignIn("docs", mcp.url);
    expect(manager.authState("docs", mcp.url)).toBe("needs-sign-in");
  });

  it("leaves a signed-in server signed in and never revokes", async () => {
    await signedIn();
    const token = await manager.accessToken("docs", mcp.url);
    manager.requireSignIn("docs", mcp.url);
    manager.requireSignIn("docs", mcp.url);
    expect(manager.authState("docs", mcp.url)).toBe("signed-in");
    expect(await manager.accessToken("docs", mcp.url)).toBe(token);
    expect(oauth.counts.revoke).toBe(0);
  });

  it("keeps a requirement it already recorded", async () => {
    await setup();
    manager.requireSignIn("docs", mcp.url);
    manager.requireSignIn("docs", mcp.url);
    expect(manager.authState("docs", mcp.url)).toBe("needs-sign-in");
    expect(oauth.counts.revoke).toBe(0);
  });
});

describe("turn servers", () => {
  const stdio = { command: "npx", args: ["notes"], env: {} };

  it("leaves out a URL server that needs sign-in, keeps the rest", async () => {
    await setup();
    manager.markNeedsSignIn("docs", mcp.url);
    const servers = { docs: { type: "http" as const, url: mcp.url, headers: {} }, notes: stdio };
    expect(withoutPendingSignIn(servers, manager)).toEqual({ notes: stdio });
  });

  it("sends a signed-in server its bearer token in place of any Authorization header", async () => {
    await setup();
    const started = await manager.start("docs", mcp.url);
    await approve(started);
    await settled("docs", started.flowId);
    const servers = {
      docs: { type: "http" as const, url: mcp.url, headers: { authorization: "Bearer stale", "X-Org": "acme" } },
      plain: { type: "http" as const, url: "https://plain.example.com/mcp", headers: { Authorization: "Bearer mine" } },
      notes: stdio,
    };
    const out = await withMcpSignIn(servers, manager);
    const token = await manager.accessToken("docs", mcp.url);
    expect(out.docs).toEqual({ type: "http", url: mcp.url, headers: { "X-Org": "acme", Authorization: `Bearer ${token}` } });
    expect(out.plain).toEqual(servers.plain);
    expect(out.notes).toEqual(stdio);
    expect(servers.docs.headers.authorization).toBe("Bearer stale");
  });

  it("drops a signed-in server whose token cannot be refreshed", async () => {
    await setup({ expiresIn: 60, rejectRefresh: true });
    const started = await manager.start("docs", mcp.url);
    await approve(started);
    await settled("docs", started.flowId);
    const out = await withMcpSignIn({ docs: { type: "http" as const, url: mcp.url, headers: {} } }, manager);
    expect(out).toEqual({});
  });
  it("refreshes every signed-in server's token at once, keeping server order", async () => {
    let inFlight = 0;
    let most = 0;
    const fake = {
      authState: () => "signed-in" as const,
      accessToken: async (name: string) => {
        inFlight += 1;
        most = Math.max(most, inFlight);
        await new Promise((resolve) => setTimeout(resolve, name === "first" ? 30 : 5));
        inFlight -= 1;
        return name === "gone" ? null : `token-${name}`;
      },
    } as unknown as McpOAuthManager;
    const url = (name: string) => ({ type: "http" as const, url: `https://${name}.example.com/mcp`, headers: {} });
    const out = await withMcpSignIn({ first: url("first"), gone: url("gone"), notes: stdio, last: url("last") }, fake);
    expect(most).toBe(3);
    expect(Object.keys(out)).toEqual(["first", "notes", "last"]);
    expect(out.last).toEqual({ ...url("last"), headers: { Authorization: "Bearer token-last" } });
  });
});

describe("review fixes", () => {
  it("keeps no tokens when the server is removed while the code is being exchanged", async () => {
    await setup({ tokenDelayMs: 150 });
    const started = await manager.start("docs", mcp.url);
    const browser = approve(started);
    await new Promise((resolve) => setTimeout(resolve, 50));
    manager.forget("docs");
    await browser;
    await new Promise((resolve) => setTimeout(resolve, 50));
    expect(manager.authState("docs", mcp.url)).toBe("none");
  });

  it("keeps no tokens when sign-in is cancelled while the code is being exchanged", async () => {
    await setup({ tokenDelayMs: 150 });
    const started = await manager.start("docs", mcp.url);
    const browser = approve(started);
    await new Promise((resolve) => setTimeout(resolve, 50));
    manager.cancel("docs");
    await browser;
    expect(manager.status("docs", started.flowId)?.phase).toBe("cancelled");
    expect(manager.authState("docs", mcp.url)).toBe("none");
  });

  it("does not expire a sign-in whose code is already being exchanged", async () => {
    await setup({ tokenDelayMs: 200 }, 100);
    const started = await manager.start("docs", mcp.url);
    await approve(started);
    expect((await settled("docs", started.flowId)).phase).toBe("succeeded");
    expect(manager.authState("docs", mcp.url)).toBe("signed-in");
  });

  it("stays signed in when a refresh fails for a reason other than a dead grant", async () => {
    await setup({ expiresIn: 60, refreshStatus: 429 });
    const started = await manager.start("docs", mcp.url);
    await approve(started);
    await settled("docs", started.flowId);
    const token = await manager.accessToken("docs", mcp.url);
    expect(oauth.isValid(`Bearer ${token}`)).toBe(true);
    expect(manager.authState("docs", mcp.url)).toBe("signed-in");
  });

  it("keeps a secret the server issues at registration and proves it on sign-in, refresh and sign-out", async () => {
    await setup({ registerConfidential: "client_secret_basic", expiresIn: 60 });
    const started = await manager.start("docs", mcp.url);
    await approve(started);
    expect((await settled("docs", started.flowId)).phase).toBe("succeeded");
    const token = await manager.accessToken("docs", mcp.url);
    expect(oauth.counts.refresh).toBe(1);
    expect(oauth.isValid(`Bearer ${token}`)).toBe(true);
    await manager.signOut("docs", mcp.url);
    expect(oauth.counts.revoke).toBe(1);
    expect(oauth.clientAuths).toEqual([
      { method: "basic", clientId: "client_1" },
      { method: "basic", clientId: "client_1" },
      { method: "basic", clientId: "client_1" },
    ]);
  });

  it("sends an issued secret in the form when the registration asks for that", async () => {
    await setup({ registerConfidential: "client_secret_post" });
    const started = await manager.start("docs", mcp.url);
    await approve(started);
    expect((await settled("docs", started.flowId)).phase).toBe("succeeded");
    expect(oauth.clientAuths).toEqual([{ method: "post", clientId: "client_1" }]);
    const stored = JSON.parse(readFileSync(join(dir, "mcp-oauth.json"), "utf8")).servers.docs;
    expect(stored).toMatchObject({ clientId: "client_1", tokenAuth: "client_secret_post" });
    expect(stored.clientSecret).toMatch(/^secret_/);
  });

  it("asks to be a confidential client when the server's methods leave out none", async () => {
    await setup({ noPublicClients: true, tokenAuthMethods: ["client_secret_basic", "client_secret_post"] });
    const started = await manager.start("docs", mcp.url);
    await approve(started);
    expect((await settled("docs", started.flowId)).phase).toBe("succeeded");
    expect(oauth.lastRegistration?.token_endpoint_auth_method).toBe("client_secret_basic");
    expect(oauth.clientAuths).toEqual([{ method: "basic", clientId: "client_1" }]);
    expect(manager.authState("docs", mcp.url)).toBe("signed-in");
  });

  it.each([
    ["no list", undefined],
    ["a list with none", ["none", "client_secret_basic"]],
  ])("still asks to be public when the server advertises %s", async (_, tokenAuthMethods) => {
    await setup(tokenAuthMethods ? { tokenAuthMethods } : {});
    const started = await manager.start("docs", mcp.url);
    await approve(started);
    expect((await settled("docs", started.flowId)).phase).toBe("succeeded");
    expect(oauth.lastRegistration?.token_endpoint_auth_method).toBe("none");
    expect(oauth.clientAuths).toEqual([{ method: "none", clientId: "client_1" }]);
  });

  it("registers a fresh client for every sign-in", async () => {
    await setup();
    for (let i = 0; i < 2; i++) {
      const started = await manager.start("docs", mcp.url);
      await approve(started);
      await settled("docs", started.flowId);
    }
    expect(oauth.counts.register).toBe(2);
  });

  it("accepts a returned iss when the resource lists its issuer with a trailing slash", async () => {
    await setup({ listIssuerWithSlash: true, returnIss: true });
    const started = await manager.start("docs", mcp.url);
    const callback = (await fetch(started.authorizationUrl!, { redirect: "manual" })).headers.get("location")!;
    expect(new URL(callback).searchParams.get("iss")).toBe(oauth.issuer);
    expect((await manager.completeCallback("docs", started.flowId, callback)).phase).toBe("succeeded");
    expect(oauth.counts.token).toBe(1);
  });

  it("refuses metadata that claims another issuer", async () => {
    await setup({ claimIssuer: "https://someone-else.example.com" });
    await expect(manager.start("docs", mcp.url)).rejects.toMatchObject({ code: "not-oauth" });
  });
});

describe("a sign-in app registered in advance", () => {
  async function signIn(): Promise<McpSignInStatus> {
    const started = await manager.start("docs", mcp.url);
    await approve(started);
    return settled("docs", started.flowId);
  }

  it("signs in as a public app on a server without registration, with its own scopes", async () => {
    await setup({ noRegistration: true, preRegistered: { "entra-app": null } });
    registered = { clientId: "entra-app", scopes: ["api://mcp/read", "offline_access"] };
    const started = await manager.start("docs", mcp.url);
    const link = new URL(started.authorizationUrl!);
    expect(link.searchParams.get("client_id")).toBe("entra-app");
    expect(link.searchParams.get("scope")).toBe("api://mcp/read offline_access");
    expect(link.searchParams.get("redirect_uri")).toBe(mcpOAuthRedirectUri(mcp.url));
    await approve(started);
    expect((await settled("docs", started.flowId)).phase).toBe("succeeded");
    expect(oauth.counts.register).toBe(0);
    expect(oauth.clientAuths).toEqual([{ method: "none", clientId: "entra-app" }]);
    expect(oauth.isValid(`Bearer ${await manager.accessToken("docs", mcp.url)}`)).toBe(true);
  });

  it("preserves explicitly configured scopes when offline access is advertised", async () => {
    await setup({ noRegistration: true, preRegistered: { "read-only": null }, offlineAccess: true });
    registered = { clientId: "read-only", scopes: ["mcp"] };
    const started = await manager.start("docs", mcp.url);
    expect(new URL(started.authorizationUrl!).searchParams.get("scope")).toBe("mcp");
  });

  it("registers itself with the scopes typed without a client ID", async () => {
    await setup({ offlineAccess: true });
    registered = { scopes: ["api://mcp/read"] };
    const started = await manager.start("docs", mcp.url);
    expect(new URL(started.authorizationUrl!).searchParams.get("scope")).toBe("api://mcp/read");
    expect(oauth.counts.register).toBe(1);
    await approve(started);
    expect((await settled("docs", started.flowId)).phase).toBe("succeeded");
  });

  it("uses the app in place of registration even when the server offers it", async () => {
    await setup({ preRegistered: { mine: null } });
    registered = { clientId: "mine" };
    expect((await signIn()).phase).toBe("succeeded");
    expect(oauth.counts.register).toBe(0);
    expect(oauth.lastAuthorize?.get("scope")).toBe("mcp");
  });

  it("proves a secret with HTTP Basic by default, on sign-in, refresh and sign-out, and never stores it", async () => {
    const secret = "s3cret:with/odd+chars";
    await setup({ noRegistration: true, expiresIn: 60, preRegistered: { corp: secret } });
    registered = { clientId: "corp", clientSecret: secret };
    expect((await signIn()).phase).toBe("succeeded");
    const token = await manager.accessToken("docs", mcp.url);
    expect(oauth.counts.refresh).toBe(1);
    expect(oauth.isValid(`Bearer ${token}`)).toBe(true);
    await manager.signOut("docs", mcp.url);
    expect(oauth.counts.revoke).toBe(1);
    expect(oauth.clientAuths).toEqual([
      { method: "basic", clientId: "corp" },
      { method: "basic", clientId: "corp" },
      { method: "basic", clientId: "corp" },
    ]);
    expect(oauth.lastAuthorize?.has("client_secret")).toBe(false);
  });

  it("sends the secret in the form when the server only takes that", async () => {
    const secret = "app-secret-never-stored";
    await setup({ noRegistration: true, preRegistered: { corp: secret }, tokenAuthMethods: ["client_secret_post"] });
    registered = { clientId: "corp", clientSecret: secret };
    expect((await signIn()).phase).toBe("succeeded");
    expect(oauth.clientAuths).toEqual([{ method: "post", clientId: "corp" }]);
    const stored = readFileSync(join(dir, "mcp-oauth.json"), "utf8");
    expect(stored).not.toContain(secret);
    expect(JSON.parse(stored).servers.docs).toMatchObject({ clientId: "corp", tokenAuth: "client_secret_post" });
  });

  it("reports a refused secret as a failed sign-in", async () => {
    await setup({ noRegistration: true, preRegistered: { corp: "right" } });
    registered = { clientId: "corp", clientSecret: "wrong" };
    const status = await signIn();
    expect(status.phase).toBe("failed");
    expect(manager.authState("docs", mcp.url)).toBe("none");
  });

  it("does not refresh with a secret once the app is no longer configured", async () => {
    await setup({ noRegistration: true, expiresIn: 60, preRegistered: { corp: "pw" } });
    registered = { clientId: "corp", clientSecret: "pw" };
    await signIn();
    registered = { clientId: "another-app", clientSecret: "pw" };
    expect(oauth.isValid(`Bearer ${await manager.accessToken("docs", mcp.url)}`)).toBe(true);
    expect(oauth.counts.refresh).toBe(0);
  });

  it("lets two connections to one server sign in at the same time", async () => {
    await setup({ noRegistration: true, preRegistered: { "app-a": null, "app-b": null } });
    const two = new McpOAuthManager({
      file: join(dir, "mcp-oauth-two.json"),
      clients: (name) => (name === "docs" ? { clientId: "app-a" } : name === "mirror" ? { clientId: "app-b" } : undefined),
    });
    const wait = async (name: string, flowId: string) => {
      for (let i = 0; i < 100; i++) {
        const status = two.status(name, flowId);
        if (status && status.phase !== "waiting") return status;
        await new Promise((resolve) => setTimeout(resolve, 10));
      }
      throw new Error("sign-in never settled");
    };
    try {
      const a = await two.start("docs", mcp.url);
      const b = await two.start("mirror", mcp.url);
      expect(a.phase).toBe("waiting");
      expect(b.phase).toBe("waiting");
      const address = (status: McpSignInStatus) => new URL(status.authorizationUrl!).searchParams.get("redirect_uri");
      expect(address(a)).toBe(address(b));
      await approve(a);
      await approve(b);
      expect((await wait("docs", a.flowId!)).phase).toBe("succeeded");
      expect((await wait("mirror", b.flowId!)).phase).toBe("succeeded");
      const tokenA = await two.accessToken("docs", mcp.url);
      const tokenB = await two.accessToken("mirror", mcp.url);
      expect(tokenA).not.toBe(tokenB);
      expect(oauth.isValid(`Bearer ${tokenA}`)).toBe(true);
      expect(oauth.isValid(`Bearer ${tokenB}`)).toBe(true);
    } finally {
      two.dispose();
    }
  });

  it("explains a busy redirect port instead of moving to another one", async () => {
    await setup({ noRegistration: true, preRegistered: { corp: null } });
    registered = { clientId: "corp" };
    const blocker = createServer();
    const port = Number(new URL(mcpOAuthRedirectUri(mcp.url)).port);
    await new Promise<void>((resolve) => {
      blocker.once("error", () => resolve());
      blocker.listen(port, "127.0.0.1", resolve);
    });
    try {
      await expect(manager.start("docs", mcp.url)).rejects.toMatchObject({ code: "port-busy" });
    } finally {
      await new Promise<void>((resolve) => blocker.close(() => resolve()));
    }
  });
});

describe("MCP sign-in from another computer", () => {
  function deferred() {
    let resolve!: () => void;
    const promise = new Promise<void>((done) => { resolve = done; });
    return { promise, resolve };
  }
  async function redirect(status: McpSignInStatus): Promise<string> {
    const response = await fetch(status.authorizationUrl!, { redirect: "manual" });
    return response.headers.get("location")!;
  }

  it.each(["none", "basic", "post"] as const)("completes a pre-registered %s app remotely and retains its refresh authentication", async (method) => {
    const secret = method === "none" ? undefined : "private-client-secret";
    await setup({
      noRegistration: true, expiresIn: 60, preRegistered: { corp: secret ?? null },
      ...(method === "post" ? { tokenAuthMethods: ["client_secret_post"] } : {}),
    });
    registered = { clientId: "corp", scopes: ["mcp", "offline_access"], ...(secret ? { clientSecret: secret } : {}) };
    const started = await manager.start("docs", mcp.url, undefined, "alice");
    const callback = await redirect(started);
    await expect(manager.completeCallback("docs", started.flowId, callback, "bob")).rejects.toMatchObject({ status: 404 });
    expect((await manager.completeCallback("docs", started.flowId, callback, "alice")).phase).toBe("succeeded");
    expect(oauth.counts.register).toBe(0);
    expect(oauth.isValid(`Bearer ${await manager.accessToken("docs", mcp.url)}`)).toBe(true);
    expect(oauth.counts.refresh).toBe(1);
    await manager.signOut("docs", mcp.url);
    expect(oauth.clientAuths).toEqual(Array.from({ length: 3 }, () => ({ method, clientId: "corp" })));
    expect(oauth.counts.revoke).toBe(1);
  });

  it("completes a pasted redirect with PKCE, without visiting the loopback listener", async () => {
    await setup();
    const started = await manager.start("docs", mcp.url, undefined, "alice");
    const callback = await redirect(started);
    const result = await manager.completeCallback("docs", started.flowId, callback, "alice");
    expect(result).toMatchObject({ phase: "succeeded", authorizationUrl: null });
    expect(oauth.isValid(`Bearer ${await manager.accessToken("docs", mcp.url)}`)).toBe(true);
    await expect(manager.completeCallback("docs", started.flowId, callback, "alice")).rejects.toMatchObject({ status: 409 });
    expect(oauth.counts.token).toBe(1);
  });

  it("isolates starts, status, cancellation and completion by owner", async () => {
    await setup();
    const starting = manager.start("docs", mcp.url, undefined, "alice");
    await expect(manager.start("docs", mcp.url, undefined, "bob")).rejects.toMatchObject({ status: 409 });
    const started = await starting;
    const callback = await redirect(started);
    await expect(manager.start("docs", mcp.url, undefined, "bob")).rejects.toMatchObject({ status: 409 });
    expect(manager.status("docs", started.flowId, "bob")).toBeUndefined();
    expect(() => manager.cancelFlow("docs", "bob", started.flowId)).toThrow();
    await expect(manager.completeCallback("docs", started.flowId, callback, "bob")).rejects.toMatchObject({ status: 404 });
    expect(oauth.counts.token).toBe(0);
    expect((await manager.completeCallback("docs", started.flowId, callback, "alice")).phase).toBe("succeeded");
  });

  it("rejects malformed callbacks without spending the flow or requesting their addresses", async () => {
    await setup();
    const started = await manager.start("docs", mcp.url);
    const callback = await redirect(started);
    const variants = [
      "not a URL", "x".repeat(16_385),
      callback.replace("127.0.0.1", "attacker.example"),
      callback.replace("http:", "https:"),
      callback.replace("127.0.0.1", "user:[email protected]"),
      callback.replace("/mcp-oauth/callback", "/other"),
      callback + "#fragment", callback + "&code=duplicate", callback + "&error=access_denied",
      callback + "&iss=https%3A%2F%2Fwrong.example",
    ];
    const wrongState = new URL(callback);
    wrongState.searchParams.set("state", "wrong");
    variants.push(wrongState.href);
    const wrongPort = new URL(callback);
    wrongPort.port = String(Number(wrongPort.port) + 1);
    variants.push(wrongPort.href);
    for (const invalid of variants) {
      await expect(manager.completeCallback("docs", started.flowId, invalid)).rejects.toMatchObject({ status: 400 });
      expect(manager.status("docs", started.flowId)?.phase).toBe("waiting");
    }
    expect(oauth.counts.token).toBe(0);
    expect((await manager.completeCallback("docs", started.flowId, callback)).phase).toBe("succeeded");
  });

  it("reports a pasted provider denial without exposing its error description", async () => {
    await setup({ deny: true });
    const started = await manager.start("docs", mcp.url);
    const result = await manager.completeCallback("docs", started.flowId, await redirect(started) + "&error_description=private");
    expect(result).toMatchObject({ phase: "failed", message: "Sign-in was not approved." });
    expect(JSON.stringify(result)).not.toContain("private");
    expect(oauth.counts.token).toBe(0);
  });

  it.each(["cancel", "revoke", "forget", "expire"])("rejects a pasted callback after %s", async (action) => {
    await setup({}, action === "expire" ? 100 : undefined);
    const started = await manager.start("docs", mcp.url);
    const callback = await redirect(started);
    if (action === "cancel") manager.cancelFlow("docs", "loopback", started.flowId);
    if (action === "revoke") manager.revokeOwner("loopback");
    if (action === "forget") manager.forget("docs");
    if (action === "expire") await expect.poll(() => manager.status("docs", started.flowId)?.phase).toBe("expired");
    await expect(manager.completeCallback("docs", started.flowId, callback)).rejects.toThrow();
    expect(oauth.counts.token).toBe(0);
    expect(manager.authState("docs", mcp.url)).toBe("none");
  });

  it("checks session expiry even before its revocation event is delivered", async () => {
    await setup();
    manager.dispose();
    let live = true;
    manager = new McpOAuthManager({ file: join(dir, "mcp-oauth.json"), isOwnerLive: () => live });
    const started = await manager.start("docs", mcp.url, undefined, "alice");
    const callback = await redirect(started);
    live = false;
    await expect(manager.completeCallback("docs", started.flowId, callback, "alice")).rejects.toMatchObject({ status: 409 });
    expect(oauth.counts.token).toBe(0);
  });

  it("releases a dead owner's waiting flow before accepting another admin's start", async () => {
    await setup();
    manager.dispose();
    let aliceLive = true;
    manager = new McpOAuthManager({ file: join(dir, "mcp-oauth.json"), isOwnerLive: (owner) => owner !== "alice" || aliceLive });
    const old = await manager.start("docs", mcp.url, undefined, "alice");
    aliceLive = false;
    const current = await manager.start("docs", mcp.url, undefined, "bob");
    expect(current.phase).toBe("waiting");
    expect(current.flowId).not.toBe(old.flowId);
    expect(manager.status("docs", current.flowId, "bob")?.phase).toBe("waiting");
    expect(manager.status("docs", old.flowId, "alice")).toBeUndefined();
  });

  it("does not let an old cancellation cancel a new attempt", async () => {
    await setup();
    const old = await manager.start("docs", mcp.url);
    manager.cancel("docs");
    const current = await manager.start("docs", mcp.url);
    expect(() => manager.cancelFlow("docs", "loopback", old.flowId)).toThrow();
    expect(manager.status("docs", current.flowId)?.phase).toBe("waiting");
  });

  it("revokes a start still waiting for registration", async () => {
    const entered = deferred();
    const release = deferred();
    await setup({ beforeRegister: async () => { entered.resolve(); await release.promise; } });
    const started = manager.start("docs", mcp.url, undefined, "alice");
    const rejected = expect(started).rejects.toMatchObject({ status: 409 });
    await entered.promise;
    manager.revokeOwner("alice");
    release.resolve();
    await rejected;
    expect(manager.authState("docs", mcp.url)).toBe("none");
  });

  it("spends one code when pasted and listener callbacks race, and discards tokens on logout", async () => {
    const entered = deferred();
    const release = deferred();
    await setup({ beforeToken: async () => { entered.resolve(); await release.promise; } });
    const started = await manager.start("docs", mcp.url, undefined, "alice");
    const callback = await redirect(started);
    const completing = manager.completeCallback("docs", started.flowId, callback, "alice");
    const rejected = expect(completing).rejects.toMatchObject({ status: 409 });
    await entered.promise;
    expect((await fetch(callback)).status).toBe(409);
    await expect(manager.completeCallback("docs", started.flowId, callback, "alice")).rejects.toMatchObject({ status: 409 });
    manager.revokeOwner("alice");
    release.resolve();
    await rejected;
    expect(oauth.counts.token).toBe(1);
    expect(manager.authState("docs", mcp.url)).toBe("none");
    await expect.poll(() => oauth.counts.revoke).toBe(1);
  });
});

describe("McpOAuthManager onSettled", () => {
  async function watched(lifetimeMs?: number) {
    await setup({}, lifetimeMs);
    const calls: Array<[string, McpSignInPhase, string]> = [];
    manager = new McpOAuthManager({
      file: join(dir, "mcp-oauth.json"), ...(lifetimeMs ? { lifetimeMs } : {}),
      onSettled: (name, phase, owner) => { calls.push([name, phase, owner]); },
    });
    return calls;
  }

  it("onSettled fires once with succeeded for a card owner", async () => {
    const calls = await watched();
    const owner = cardOwner("thread-1", "msg-1");
    expect(owner).toBe("card:thread-1:msg-1");
    const started = await manager.start("docs", mcp.url, undefined, owner);
    expect(calls).toEqual([]);
    expect((await approve(started)).status).toBe(200);
    expect(calls).toEqual([["docs", "succeeded", owner]]);
    manager.cancel("docs");
    manager.dispose();
    expect(calls).toHaveLength(1);
  });

  it("reports a sign-in that expires or is cancelled, and nothing when the server shuts down", async () => {
    const calls = await watched(100);
    await manager.start("docs", mcp.url, undefined, "loopback");
    await expect.poll(() => calls).toEqual([["docs", "expired", "loopback"]]);
    await manager.start("docs", mcp.url, undefined, "loopback");
    manager.cancel("docs");
    expect(calls.at(-1)).toEqual(["docs", "cancelled", "loopback"]);
    await manager.start("docs", mcp.url, undefined, "loopback");
    manager.dispose();
    expect(calls).toHaveLength(2);
  });
});

describe("McpOAuthManager with a failing onSettled", () => {
  it("a throwing onSettled neither breaks a callback nor escapes the expiry timer", async () => {
    await setup({}, 150);
    const heard: McpSignInPhase[] = [];
    manager = new McpOAuthManager({ file: join(dir, "mcp-oauth.json"), lifetimeMs: 150, onSettled: (_name, phase) => { heard.push(phase); throw new Error("listener broke"); } });
    const started = await manager.start("docs", mcp.url);
    const page = await approve(started);
    expect(page.status).toBe(200);
    expect(await page.text()).toBe("Signed in. You can close this tab and return to OpenMausBot.");
    expect(manager.authState("docs", mcp.url)).toBe("signed-in");
    await manager.start("docs", mcp.url);
    await expect.poll(() => heard).toEqual(["succeeded", "expired"]);
  });
});

describe("McpOAuthManager sign-in from a browser on another computer", () => {
  const elsewhere = { remote: true, origin: "https://cloud.example" };

  async function returnAddress(status: McpSignInStatus): Promise<URL> {
    return new URL((await fetch(status.authorizationUrl!, { redirect: "manual" })).headers.get("location")!);
  }

  const portFree = (port: number) => new Promise<boolean>((resolve) => {
    const probe = createServer();
    probe.once("error", () => resolve(false));
    probe.listen(port, "127.0.0.1", () => probe.close(() => resolve(true)));
  });

  it("registers for this server's https address and listens on no loopback port", async () => {
    await setup();
    mcp = { ...mcp, url: await withFreeSignInPort(mcp.url) };
    const started = await manager.start("docs", mcp.url, undefined, "alice", elsewhere);
    const link = new URL(started.authorizationUrl!);
    expect(link.searchParams.get("redirect_uri")).toBe("https://cloud.example/mcp-oauth/callback");
    expect(started.pasteBack).toBeUndefined();
    expect(await portFree(Number(new URL(mcpOAuthRedirectUri(mcp.url)).port))).toBe(true);

    const callback = await returnAddress(started);
    expect(callback.origin + callback.pathname).toBe("https://cloud.example/mcp-oauth/callback");
    const page = await manager.publicCallback("cloud.example", callback.search);
    expect(page).toEqual({ status: 200, text: "Signed in. You can close this tab and return to OpenMausBot." });
    expect(manager.status("docs", started.flowId, "alice")?.phase).toBe("succeeded");
    expect(oauth.isValid(`Bearer ${await manager.accessToken("docs", mcp.url)}`)).toBe(true);

    const replay = await manager.publicCallback("cloud.example", callback.search);
    expect(replay.status).toBe(409);
    expect(replay.text).not.toContain(callback.searchParams.get("code"));
    expect(oauth.counts.token).toBe(1);
  });

  it("answers a wrong state or another address with a plain 400 and keeps waiting", async () => {
    await setup();
    const started = await manager.start("docs", mcp.url, undefined, "alice", elsewhere);
    const callback = await returnAddress(started);
    const forged = new URLSearchParams(callback.search);
    forged.set("state", "forged");
    for (const [host, search] of [["cloud.example", `?${forged}`], ["attacker.example", callback.search], [undefined, callback.search], ["cloud.example", ""]] as const) {
      expect(await manager.publicCallback(host, search)).toEqual({ status: 400, text: "Invalid sign-in callback. Return to OpenMausBot and try again." });
    }
    expect(manager.status("docs", started.flowId, "alice")?.phase).toBe("waiting");
    expect(oauth.counts.token).toBe(0);
    expect((await manager.publicCallback("CLOUD.example", callback.search)).status).toBe(200);
  });

  it("refuses a callback after the sign-in expired", async () => {
    await setup({}, 100);
    const started = await manager.start("docs", mcp.url, undefined, "alice", elsewhere);
    const callback = await returnAddress(started);
    await expect.poll(() => manager.status("docs", started.flowId, "alice")?.phase).toBe("expired");
    expect((await manager.publicCallback("cloud.example", callback.search)).status).toBe(409);
    expect(oauth.counts.token).toBe(0);
  });

  it("reports a denied approval on the page without the server's own words", async () => {
    await setup({ deny: true });
    const started = await manager.start("docs", mcp.url, undefined, "alice", elsewhere);
    const callback = await returnAddress(started);
    const page = await manager.publicCallback("cloud.example", `${callback.search}&error_description=private`);
    expect(page).toEqual({ status: 400, text: "Sign-in was not approved. You can close this tab." });
    expect(manager.status("docs", started.flowId, "alice")?.phase).toBe("failed");
  });

  it("never completes a loopback sign-in through the public address", async () => {
    await setup();
    const started = await manager.start("docs", mcp.url, undefined, "alice");
    const callback = await returnAddress(started);
    expect((await manager.publicCallback(callback.host, callback.search)).status).toBe(400);
    expect(manager.status("docs", started.flowId, "alice")?.phase).toBe("waiting");
  });

  it("falls back to the loopback callback, pasted, when the server refuses the https address", async () => {
    await setup({ refuseRedirect: (uri) => uri.startsWith("https:") });
    const started = await manager.start("docs", mcp.url, undefined, "alice", elsewhere);
    expect(new URL(started.authorizationUrl!).searchParams.get("redirect_uri")).toMatch(/^http:\/\/127\.0\.0\.1:\d+\/mcp-oauth\/callback$/);
    expect(started.pasteBack).toBe(true);
    expect(oauth.counts.register).toBe(2);
    const callback = await returnAddress(started);
    expect((await manager.completeCallback("docs", started.flowId, callback.href, "alice")).phase).toBe("succeeded");
  });

  it("keeps an app registered in advance on its loopback redirect, pasted", async () => {
    await setup({ noRegistration: true, preRegistered: { corp: null } });
    registered = { clientId: "corp" };
    const started = await manager.start("docs", mcp.url, undefined, "alice", elsewhere);
    expect(new URL(started.authorizationUrl!).searchParams.get("redirect_uri")).toBe(mcpOAuthRedirectUri(mcp.url));
    expect(started.pasteBack).toBe(true);
    expect(oauth.counts.register).toBe(0);
  });

  it("returns a configured app to its configured https address, on any browser, with no loopback listener", async () => {
    await setup({ noRegistration: true, preRegistered: { corp: null } });
    registered = { clientId: "corp", redirectUri: "https://cloud.example/mcp-oauth/callback" };
    const started = await manager.start("docs", mcp.url, undefined, "alice", elsewhere);
    expect(new URL(started.authorizationUrl!).searchParams.get("redirect_uri")).toBe("https://cloud.example/mcp-oauth/callback");
    expect(started.pasteBack).toBeUndefined();
    expect(oauth.counts.register).toBe(0);
    expect(await portFree(Number(new URL(mcpOAuthRedirectUri(mcp.url)).port))).toBe(true);
    const callback = await returnAddress(started);
    expect((await manager.publicCallback("cloud.example", callback.search)).status).toBe(200);
    expect(manager.status("docs", started.flowId, "alice")?.phase).toBe("succeeded");
    expect(oauth.isValid(`Bearer ${await manager.accessToken("docs", mcp.url)}`)).toBe(true);
  });

  it("asks a browser on this machine to paste nothing", async () => {
    await setup();
    const started = await manager.start("docs", mcp.url, undefined, "loopback", { remote: false, origin: "https://cloud.example" });
    expect(new URL(started.authorizationUrl!).searchParams.get("redirect_uri")).toMatch(/^http:\/\/127\.0\.0\.1:/);
    expect(started.pasteBack).toBeUndefined();
  });
});

describe("mcpCallbackOrigin", () => {
  it("uses the origin the browser proved when nothing is configured or it is configured", () => {
    expect(mcpCallbackOrigin("https://me.fly.dev", [])).toBe("https://me.fly.dev");
    expect(mcpCallbackOrigin("https://bots.acme.com", ["https://me.fly.dev", "https://bots.acme.com"])).toBe("https://bots.acme.com");
  });

  it("prefers a configured address over one nobody vouched for", () => {
    expect(mcpCallbackOrigin("https://elsewhere.example", ["https://me.fly.dev"])).toBe("https://me.fly.dev");
    expect(mcpCallbackOrigin(null, [null, undefined, "https://me.fly.dev/"])).toBe("https://me.fly.dev");
  });

  it("returns null for plain http, a path or credentials", () => {
    expect(mcpCallbackOrigin("http://192.168.1.5:3000", [])).toBeNull();
    expect(mcpCallbackOrigin(null, ["http://me.example", "https://me.example/omb", "https://user:[email protected]", "not a url"])).toBeNull();
    expect(mcpCallbackOrigin(null, [])).toBeNull();
  });
});