UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

131 lines • 5.77 kB
import { createServer } from "node:http"; import { oauthErrorHtml, oauthSuccessHtml } from "@earendil-works/pi-ai/utils/oauth-page"; import { safeTimer } from "../wrap.js"; import { OAuthFlowError } from "./oauth-errors.js"; // Chooses the redirect channel for an authorization: a real loopback listener, // or — when `mcp.oauthCallbackUrl` is configured — a paste channel with ZERO // local listeners (completion arrives via /mcp auth-complete). export async function openCallbackChannel(options) { if (options.overrideUrl !== undefined && options.overrideUrl.length > 0) { return { redirectUrl: options.overrideUrl, usesLoopback: false, waitForCode: () => Promise.reject(new OAuthFlowError("headless", `MCP server ${options.serverName} uses a callback URL override; complete with /mcp auth-complete <redirect-url>.`, { serverName: options.serverName })), close: () => Promise.resolve(), }; } const server = new LoopbackCallbackServer(options); const redirectUrl = await server.start(); return { redirectUrl, usesLoopback: true, waitForCode: () => server.waitForCode(), close: () => server.close(), }; } const DEFAULT_TIMEOUT_MS = 5 * 60 * 1000; // Lazily-bound 127.0.0.1 listener that exists only for the duration of one // OAuth authorization. The handle is unref'd so it never keeps the process // alive, and it tears down on success, invalid state, or a 5-minute timeout. export class LoopbackCallbackServer { #options; #server; #redirectUrl; #result; #resolve; #reject; #timer; #settled = false; constructor(options) { this.#options = options; } get redirectUrl() { return this.#redirectUrl; } get #sink() { const logger = this.#options.logger; return { logger: { error: (message, data) => logger?.error(message, data) } }; } async start() { const host = this.#options.host ?? "127.0.0.1"; const path = this.#options.path ?? "/callback"; const server = createServer((req, res) => this.#handle(req, res, path)); server.unref(); this.#server = server; await this.#listen(server, host, this.#options.port ?? 0); const address = server.address(); this.#redirectUrl = `http://${host}:${address.port}${path}`; this.#result = new Promise((resolve, reject) => { this.#resolve = resolve; this.#reject = reject; }); this.#timer = safeTimer("auth.callback.timeout", this.#options.timeoutMs ?? DEFAULT_TIMEOUT_MS, () => this.#fail(new OAuthFlowError("needs_auth", `MCP server ${this.#options.serverName} authorization timed out after 5 minutes.`, { serverName: this.#options.serverName })), this.#sink); return this.#redirectUrl; } waitForCode() { if (this.#result === undefined) throw new Error("callback server not started"); return this.#result; } async close() { if (this.#timer !== undefined) clearTimeout(this.#timer); const server = this.#server; this.#server = undefined; if (server === undefined) return; await new Promise((resolve) => server.close(() => resolve())); } #listen(server, host, port) { return new Promise((resolve, reject) => { const onError = (error) => { const detail = error.code === "EADDRINUSE" ? `port ${port} is already in use (another senpi auth flow may be running); free it or configure a different callback.` : error.message; reject(new OAuthFlowError("needs_auth", `MCP server ${this.#options.serverName} callback listener failed: ${detail}`, { cause: error, serverName: this.#options.serverName, })); }; server.once("error", onError); server.listen(port, host, () => { server.removeListener("error", onError); resolve(); }); }); } #handle(req, res, path) { const url = new URL(req.url ?? "/", this.#redirectUrl); if (url.pathname !== path) { res.writeHead(404).end(); return; } const code = url.searchParams.get("code"); const state = url.searchParams.get("state") ?? undefined; const error = url.searchParams.get("error"); if (error !== null || code === null || state === undefined || !this.#options.validateState(state)) { res.writeHead(400, { "content-type": "text/html" }).end(oauthErrorHtml("MCP server authorization failed. Return to the app and start the sign-in again.")); void this.#fail(new OAuthFlowError("state_mismatch", `MCP server ${this.#options.serverName} authorization state did not match (possible CSRF or a stale/replayed link); restart the auth flow.`, { serverName: this.#options.serverName })); return; } res.writeHead(200, { "content-type": "text/html" }).end(oauthSuccessHtml("MCP server authorization complete. You can close this tab.")); this.#succeed({ code, state, iss: url.searchParams.get("iss") ?? undefined }); } #succeed(result) { if (this.#settled) return; this.#settled = true; if (this.#timer !== undefined) clearTimeout(this.#timer); this.#resolve?.(result); void this.close(); } async #fail(error) { if (this.#settled) return; this.#settled = true; this.#reject?.(error); await this.close(); } } //# sourceMappingURL=callback.js.map