Skip to content

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

12.3 KB
Raw Blame
// A tiny OAuth 2.1 authorization server for tests: metadata, dynamic registration, an /authorize that
// approves at once, a PKCE-checking token endpoint, and revocation.
import { createHash, randomBytes } from "node:crypto";
import { createServer, type IncomingMessage, type Server } from "node:http";
import type { AddressInfo } from "node:net";

export interface FakeOAuthOptions {
  /** seconds until an access token expires (default 3600) */
  expiresIn?: number;
  /** Deterministic barriers for cancellation/revocation races. */
  beforeRegister?: () => Promise<void>;
  beforeToken?: () => Promise<void>;
  /** omit registration_endpoint from the metadata */
  noRegistration?: boolean;
  /** advertise client_id_metadata_document_supported: true in the metadata */
  clientIdMetadataDocument?: boolean;
  /** refuse to register a client for a redirect URI this matches */
  refuseRedirect?: (redirectUri: string) => boolean;
  /** answer every registration with this status, JSON body and headers */
  registerRefusal?: { status: number; body: unknown; headers?: Record<string, string> };
  /** answer every refresh with invalid_grant */
  rejectRefresh?: boolean;
  /** /authorize answers ?error=access_denied instead of a code */
  deny?: boolean;
  /** hold every token response this long */
  tokenDelayMs?: number;
  /** answer refreshes with this status and a non-grant error */
  refreshStatus?: number;
  /** metadata claims this issuer instead of its own address */
  claimIssuer?: string;
  /** resource metadata lists the issuer with a trailing slash, as Google's does */
  listIssuerWithSlash?: boolean;
  /** /authorize returns its issuer as iss (RFC 9207) */
  returnIss?: boolean;
  /** apps registered in advance: client id → its secret, or null for a public app */
  preRegistered?: Record<string, string | null>;
  /** advertised token_endpoint_auth_methods_supported */
  tokenAuthMethods?: string[];
  /** registration issues a secret with this method, whatever the client asked for, and the token endpoint requires it */
  registerConfidential?: "client_secret_basic" | "client_secret_post";
  /** registration issues a secret only when asked for client_secret_basic or client_secret_post; the token endpoint refuses a public client */
  noPublicClients?: boolean;
  /** Require offline_access to issue a refresh token and advertise it only on the authorization server. */
  offlineAccess?: boolean;
}

export interface FakeClientAuth {
  method: "basic" | "post" | "none";
  clientId: string;
}

export interface FakeOAuth {
  issuer: string;
  prmUrl: string;
  /** `WWW-Authenticate` value an MCP server behind this issuer would send */
  challenge: string;
  isValid(authorization: string | undefined): boolean;
  /** a valid access token, as if issued out of band (a personal token) */
  mint(): string;
  counts: { register: number; token: number; refresh: number; revoke: number };
  lastAuthorize: URLSearchParams | null;
  /** the body of the latest client registration */
  lastRegistration: { token_endpoint_auth_method?: string } | null;
  /** every /token and /revoke client identification, in order */
  clientAuths: FakeClientAuth[];
  options: FakeOAuthOptions;
  close(): Promise<void>;
}

function readBody(req: IncomingMessage): Promise<string> {
  return new Promise((resolve) => {
    let body = "";
    req.setEncoding("utf8");
    req.on("data", (chunk: string) => { body += chunk; });
    req.on("end", () => resolve(body));
  });
}

export async function startFakeOAuth(options: FakeOAuthOptions = {}): Promise<FakeOAuth> {
  const codes = new Map<string, { challenge: string; clientId: string; redirectUri: string; offline: boolean }>();
  const access = new Set<string>();
  const refresh = new Set<string>();
  const clients = new Set<string>();
  const counts = { register: 0, token: 0, refresh: 0, revoke: 0 };
  let issuer = "";
  const clientAuths: FakeClientAuth[] = [];
  const fake: Partial<FakeOAuth> = { lastAuthorize: null, lastRegistration: null, clientAuths, options };
  const preRegistered = { ...options.preRegistered };

  /** The authenticated client, or null when its secret is missing or wrong (RFC 6749 §2.3: one method per request). */
  const authenticate = (req: IncomingMessage, form: URLSearchParams): string | null => {
    const header = req.headers.authorization;
    let auth: FakeClientAuth;
    let secret: string | null = null;
    if (header?.startsWith("Basic ")) {
      const [id = "", key = ""] = Buffer.from(header.slice(6), "base64").toString("utf8").split(":");
      const decode = (value: string) => decodeURIComponent(value.replace(/\+/g, " "));
      if (form.has("client_secret")) return null;
      auth = { method: "basic", clientId: decode(id) };
      secret = decode(key);
    } else if (form.has("client_secret")) {
      auth = { method: "post", clientId: form.get("client_id") ?? "" };
      secret = form.get("client_secret");
    } else {
      auth = { method: "none", clientId: form.get("client_id") ?? "" };
    }
    clientAuths.push(auth);
    if (Object.hasOwn(preRegistered, auth.clientId)) {
      const expected = preRegistered[auth.clientId];
      return expected === null ? (secret === null ? auth.clientId : null) : (secret === expected ? auth.clientId : null);
    }
    return clients.has(auth.clientId) && secret === null && !options.noPublicClients ? auth.clientId : null;
  };

  const issue = (offline = true) => {
    const token = `at_${randomBytes(12).toString("hex")}`;
    const refreshToken = `rt_${randomBytes(12).toString("hex")}`;
    access.add(token);
    refresh.add(refreshToken);
    return { access_token: token, token_type: "Bearer", expires_in: options.expiresIn ?? 3600, ...(offline ? { refresh_token: refreshToken } : {}), scope: offline && options.offlineAccess ? "mcp offline_access" : "mcp" };
  };
  const json = (res: import("node:http").ServerResponse, status: number, body: unknown) =>
    res.writeHead(status, { "content-type": "application/json" }).end(JSON.stringify(body));

  const server: Server = createServer((req, res) => {
    void (async () => {
      const url = new URL(req.url ?? "/", issuer);
      if (req.method === "GET" && url.pathname.startsWith("/.well-known/oauth-protected-resource")) {
        return json(res, 200, { resource: `${issuer}/mcp`, authorization_servers: [options.listIssuerWithSlash ? `${issuer}/` : issuer], scopes_supported: ["mcp"] });
      }
      if (req.method === "GET" && url.pathname === "/.well-known/oauth-authorization-server") {
        return json(res, 200, {
          issuer: options.claimIssuer ?? issuer,
          authorization_endpoint: `${issuer}/authorize`,
          token_endpoint: `${issuer}/token`,
          ...(options.noRegistration ? {} : { registration_endpoint: `${issuer}/register` }),
          ...(options.clientIdMetadataDocument ? { client_id_metadata_document_supported: true } : {}),
          revocation_endpoint: `${issuer}/revoke`,
          code_challenge_methods_supported: ["S256"],
          ...(options.offlineAccess ? { scopes_supported: ["mcp", "offline_access"] } : {}),
          ...(options.tokenAuthMethods ? { token_endpoint_auth_methods_supported: options.tokenAuthMethods } : {}),
        });
      }
      if (req.method === "POST" && url.pathname === "/register") {
        counts.register += 1;
        await options.beforeRegister?.();
        if (options.registerRefusal) {
          res.writeHead(options.registerRefusal.status, { "content-type": "application/json", ...options.registerRefusal.headers });
          return res.end(JSON.stringify(options.registerRefusal.body));
        }
        const body = JSON.parse(await readBody(req)) as { redirect_uris?: string[]; token_endpoint_auth_method?: string };
        if (body.redirect_uris?.some((uri) => options.refuseRedirect?.(uri))) return json(res, 400, { error: "invalid_redirect_uri" });
        const clientId = `client_${counts.register}`;
        fake.lastRegistration = body;
        const asked = body.token_endpoint_auth_method;
        const confidential = options.registerConfidential
          ?? (options.noPublicClients && (asked === "client_secret_basic" || asked === "client_secret_post") ? asked : undefined);
        if (confidential) {
          const secret = `secret_${randomBytes(8).toString("hex")}`;
          preRegistered[clientId] = secret;
          return json(res, 201, { client_id: clientId, client_secret: secret, token_endpoint_auth_method: confidential, redirect_uris: body.redirect_uris });
        }
        clients.add(clientId);
        return json(res, 201, { client_id: clientId, redirect_uris: body.redirect_uris });
      }
      if (req.method === "GET" && url.pathname === "/authorize") {
        fake.lastAuthorize = url.searchParams;
        const redirect = new URL(url.searchParams.get("redirect_uri") ?? "");
        redirect.searchParams.set("state", url.searchParams.get("state") ?? "");
        if (options.returnIss) redirect.searchParams.set("iss", options.claimIssuer ?? issuer);
        const clientId = url.searchParams.get("client_id") ?? "";
        if (!clients.has(clientId) && !Object.hasOwn(preRegistered, clientId)) {
          redirect.searchParams.set("error", "unauthorized_client");
        } else if (options.deny) {
          redirect.searchParams.set("error", "access_denied");
        } else {
          const code = `code_${randomBytes(8).toString("hex")}`;
          codes.set(code, {
            challenge: url.searchParams.get("code_challenge") ?? "",
            clientId: url.searchParams.get("client_id") ?? "",
            redirectUri: url.searchParams.get("redirect_uri") ?? "",
            offline: !options.offlineAccess || (url.searchParams.get("scope") ?? "").split(" ").includes("offline_access"),
          });
          redirect.searchParams.set("code", code);
        }
        res.writeHead(302, { location: redirect.toString() }).end();
        return;
      }
      if (req.method === "POST" && url.pathname === "/token") {
        const form = new URLSearchParams(await readBody(req));
        if (options.tokenDelayMs) await new Promise((resolve) => setTimeout(resolve, options.tokenDelayMs));
        const client = authenticate(req, form);
        if (!client) return json(res, 401, { error: "invalid_client" });
        if (form.get("grant_type") === "authorization_code") {
          counts.token += 1;
          await options.beforeToken?.();
          const entry = codes.get(form.get("code") ?? "");
          codes.delete(form.get("code") ?? "");
          const verifier = form.get("code_verifier") ?? "";
          const ok = entry
            && entry.clientId === client
            && entry.redirectUri === form.get("redirect_uri")
            && createHash("sha256").update(verifier).digest("base64url") === entry.challenge;
          return ok ? json(res, 200, issue(entry.offline)) : json(res, 400, { error: "invalid_grant" });
        }
        if (form.get("grant_type") === "refresh_token") {
          counts.refresh += 1;
          if (options.refreshStatus) return json(res, options.refreshStatus, { error: "slow_down" });
          const token = form.get("refresh_token") ?? "";
          if (options.rejectRefresh || !refresh.has(token)) return json(res, 400, { error: "invalid_grant" });
          refresh.delete(token);
          return json(res, 200, issue());
        }
        return json(res, 400, { error: "unsupported_grant_type" });
      }
      if (req.method === "POST" && url.pathname === "/revoke") {
        counts.revoke += 1;
        const form = new URLSearchParams(await readBody(req));
        if (!authenticate(req, form)) return json(res, 401, { error: "invalid_client" });
        access.delete(form.get("token") ?? "");
        refresh.delete(form.get("token") ?? "");
        res.writeHead(200).end();
        return;
      }
      res.writeHead(404).end();
    })();
  });
  await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
  issuer = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
  return Object.assign(fake, {
    issuer,
    prmUrl: `${issuer}/.well-known/oauth-protected-resource`,
    challenge: `Bearer resource_metadata="${issuer}/.well-known/oauth-protected-resource"`,
    mint: () => issue().access_token,
    isValid: (authorization: string | undefined) => Boolean(authorization?.startsWith("Bearer ") && access.has(authorization.slice(7))),
    counts,
    options,
    close: () => new Promise<void>((resolve) => {
      server.closeAllConnections();
      server.close(() => resolve());
    }),
  }) as FakeOAuth;
}