// @effect-diagnostics nodeBuiltinImport:off globalTimers:off - Native loopback helper uses a bounded Node listener with AbortController cleanup. import * as Schema from "effect/Schema"; import * as NodeHttp from "node:http"; import { codexAuthorizationRequest, codexCallbackUrl } from "@t3tools/shared/codexAuthHandoff"; export class CodexAuthCallbackError extends Schema.TaggedError()( "CodexAuthCallbackError", { detail: Schema.String }, ) { override get message() { return this.detail; } } const listeners = new Map(); export function cancelCodexAuthCallback(authorizationUrl: string) { const { state } = codexAuthorizationRequest(authorizationUrl); listeners.get(state)?.abort(); } /** Receive an authorization code locally. Credentials and PKCE stay on the target environment. */ export async function receiveCodexAuthCallback( authorizationUrl: string, openBrowser: (url: string) => Promise, destination?: (callbackUrl: string) => string, signal?: AbortSignal, ) { const request = codexAuthorizationRequest(authorizationUrl); if (listeners.has(request.state)) throw new Error("This sign-in is already open on this computer."); const abort = new AbortController(); const interrupted = () => abort.abort(); signal?.addEventListener("abort", interrupted, { once: true }); listeners.set(request.state, abort); const callback = Promise.withResolvers(); // Keep early open/bind failures from leaving an unobserved rejection behind. void callback.promise.catch(() => undefined); const server = NodeHttp.createServer((incoming, response) => { try { if (incoming.method !== "GET") throw new Error("method"); const url = codexCallbackUrl( new URL(incoming.url ?? "/", request.redirectUri).toString(), request.redirectUri, request.state, ).toString(); const returnUrl = destination?.(url); response.setHeader("cache-control", "no-store"); response.setHeader("referrer-policy", "no-referrer"); response.setHeader("x-content-type-options", "nosniff"); if (returnUrl) { response.writeHead(303, { location: returnUrl }).end(); } else { response.setHeader( "content-security-policy", "default-src 'none'; style-src 'unsafe-inline'; frame-ancestors 'none'", ); response .writeHead(200, { "content-type": "text/html; charset=utf-8" }) .end( 'T3 Code

Return to T3 Code

Your sign-in response has been received. T3 Code is finishing the connection. You can close this tab.

', ); } callback.resolve(url); } catch { response.writeHead(400).end("This response does not belong to the active sign-in."); } }); const cancelled = () => callback.reject(new Error("Sign-in cancelled on this computer.")); abort.signal.addEventListener("abort", cancelled, { once: true }); const timer = setTimeout( () => callback.reject(new Error("Sign-in expired. Try again.")), 300_000, ); timer.unref(); try { if (signal?.aborted) throw new Error("Sign-in cancelled on this computer."); await new Promise((resolve, reject) => { server.once("error", () => reject( new Error( "The ChatGPT callback port is in use on this computer. Close the other sign-in and try again, or paste the redirect URL in T3 Code.", ), ), ); server.listen(Number(new URL(request.redirectUri).port), "127.0.0.1", resolve); }); if (abort.signal.aborted) throw new Error("Sign-in cancelled on this computer."); if (!(await openBrowser(request.authorizationUrl))) throw new Error("Could not open your sign-in browser."); return await callback.promise; } finally { clearTimeout(timer); signal?.removeEventListener("abort", interrupted); abort.signal.removeEventListener("abort", cancelled); listeners.delete(request.state); server.closeAllConnections(); await new Promise((resolve) => server.close(() => resolve())); } }