Spaces:
Paused
Paused
File size: 16,117 Bytes
5dd5107 abe58f1 5dd5107 abe58f1 5dd5107 1b6fb15 5dd5107 1b6fb15 5dd5107 1b6fb15 5dd5107 abe58f1 22a7de1 5dd5107 22a7de1 5dd5107 22a7de1 5dd5107 abe58f1 22a7de1 5dd5107 22a7de1 5dd5107 22a7de1 5dd5107 abe58f1 22a7de1 5dd5107 22a7de1 5dd5107 22a7de1 5dd5107 abe58f1 22a7de1 5dd5107 22a7de1 5dd5107 8fc11c2 5dd5107 22a7de1 5dd5107 50ae780 1b6fb15 50ae780 5dd5107 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | /**
* Native OAuth PKCE flow for Auth0/OpenAI authentication.
* Replaces the Codex CLI dependency for login and token refresh.
*/
import { randomBytes, createHash } from "crypto";
import { createServer, type Server } from "http";
import { readFileSync, existsSync } from "fs";
import { resolve } from "path";
import { homedir } from "os";
import { getConfig } from "../config.js";
import { curlFetchPost, type CurlFetchResponse } from "../tls/curl-fetch.js";
import { withDirectFallback, isCloudflareChallengeResponse } from "../tls/direct-fallback.js";
export interface PKCEChallenge {
codeVerifier: string;
codeChallenge: string;
}
export interface TokenResponse {
access_token: string;
refresh_token?: string;
id_token?: string;
token_type: string;
expires_in?: number;
}
export interface DeviceCodeResponse {
device_code: string;
user_code: string;
verification_uri: string;
verification_uri_complete: string;
expires_in: number;
interval: number;
}
interface PendingSession {
codeVerifier: string;
redirectUri: string;
returnHost: string;
source: "login" | "dashboard";
createdAt: number;
}
const isCfResponse = (r: CurlFetchResponse) => isCloudflareChallengeResponse(r.status, r.body);
/** In-memory store for pending OAuth sessions, keyed by `state`. */
const pendingSessions = new Map<string, PendingSession>();
/** Track completed sessions so code-relay doesn't error after callback server already handled it. */
const completedSessions = new Map<string, number>();
// Clean up expired sessions every 60 seconds
const SESSION_TTL_MS = 5 * 60 * 1000; // 5 minutes
setInterval(() => {
const now = Date.now();
for (const [state, session] of pendingSessions) {
if (now - session.createdAt > SESSION_TTL_MS) {
pendingSessions.delete(state);
}
}
for (const [state, completedAt] of completedSessions) {
if (now - completedAt > SESSION_TTL_MS) {
completedSessions.delete(state);
}
}
}, 60_000).unref();
/** Mark a session as successfully completed. */
export function markSessionCompleted(state: string): void {
completedSessions.set(state, Date.now());
}
/** Check if a session was already completed (callback server handled it). */
export function isSessionCompleted(state: string): boolean {
return completedSessions.has(state);
}
/**
* Generate a PKCE code_verifier + code_challenge (S256).
*/
export function generatePKCE(): PKCEChallenge {
const codeVerifier = randomBytes(32)
.toString("base64url")
.replace(/[^a-zA-Z0-9\-._~]/g, "")
.slice(0, 128);
const codeChallenge = createHash("sha256")
.update(codeVerifier)
.digest("base64url");
return { codeVerifier, codeChallenge };
}
/**
* Build the Auth0 authorization URL for the PKCE flow.
*/
export function buildAuthUrl(
redirectUri: string,
state: string,
codeChallenge: string,
): string {
const config = getConfig();
// Build query string manually β OpenAI's auth server requires %20 for spaces,
// but URLSearchParams encodes spaces as '+' which causes AuthApiFailure.
const params: Record<string, string> = {
response_type: "code",
client_id: config.auth.oauth_client_id,
redirect_uri: redirectUri,
scope: "openid profile email offline_access",
code_challenge: codeChallenge,
code_challenge_method: "S256",
id_token_add_organizations: "true",
codex_cli_simplified_flow: "true",
state,
originator: "codex_cli_rs",
};
const qs = Object.entries(params)
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join("&");
const url = `${config.auth.oauth_auth_endpoint}?${qs}`;
console.log(`[OAuth] Auth URL: ${url}`);
return url;
}
/**
* Exchange an authorization code for tokens.
*/
export async function exchangeCode(
code: string,
codeVerifier: string,
redirectUri: string,
): Promise<TokenResponse> {
const config = getConfig();
const body = new URLSearchParams({
grant_type: "authorization_code",
client_id: config.auth.oauth_client_id,
code,
redirect_uri: redirectUri,
code_verifier: codeVerifier,
});
const resp = await withDirectFallback(
(proxyUrl) => curlFetchPost(
config.auth.oauth_token_endpoint,
"application/x-www-form-urlencoded",
body.toString(),
{ proxyUrl },
),
{ tag: "OAuth/exchangeCode", shouldFallback: isCfResponse },
);
if (!resp.ok) {
throw new Error(`Token exchange failed (${resp.status}): ${resp.body}`);
}
return JSON.parse(resp.body) as TokenResponse;
}
/**
* Refresh an access token using a refresh_token.
*/
export async function refreshAccessToken(
refreshToken: string,
): Promise<TokenResponse> {
const config = getConfig();
const body = new URLSearchParams({
grant_type: "refresh_token",
client_id: config.auth.oauth_client_id,
refresh_token: refreshToken,
});
const resp = await withDirectFallback(
(proxyUrl) => curlFetchPost(
config.auth.oauth_token_endpoint,
"application/x-www-form-urlencoded",
body.toString(),
{ proxyUrl },
),
{ tag: "OAuth/refresh", shouldFallback: isCfResponse },
);
if (!resp.ok) {
throw new Error(`Token refresh failed (${resp.status}): ${resp.body}`);
}
return JSON.parse(resp.body) as TokenResponse;
}
// ββ Pending session management βββββββββββββββββββββββββββββββββββββ
/**
* OpenAI only whitelists http://localhost:1455/auth/callback for this client_id.
* The Codex CLI always uses this port β no fallback to random ports.
*/
const OAUTH_CALLBACK_PORT = 1455;
/**
* Create and store a new pending OAuth session.
*
* The redirect_uri is always http://localhost:1455/auth/callback to match
* the Codex CLI and OpenAI's whitelist. The caller must start a callback
* server on port 1455 via `startCallbackServer()`.
*/
export function createOAuthSession(
originalHost: string,
source: "login" | "dashboard" = "login",
): { state: string; authUrl: string; port: number } {
const { codeVerifier, codeChallenge } = generatePKCE();
const state = randomBytes(16).toString("hex");
const port = OAUTH_CALLBACK_PORT;
const redirectUri = `http://localhost:${port}/auth/callback`;
pendingSessions.set(state, {
codeVerifier,
redirectUri,
returnHost: originalHost,
source,
createdAt: Date.now(),
});
const authUrl = buildAuthUrl(redirectUri, state, codeChallenge);
return { state, authUrl, port };
}
/**
* Retrieve and consume a pending session by state.
* Returns null if not found or expired.
*/
export function consumeSession(
state: string,
): PendingSession | null {
const session = pendingSessions.get(state);
if (!session) return null;
pendingSessions.delete(state);
// Check expiry
if (Date.now() - session.createdAt > SESSION_TTL_MS) {
return null;
}
return session;
}
// ββ Temporary callback server ββββββββββββββββββββββββββββββββββββββ
/** Track the active callback server so we can close it before starting a new one. */
let activeCallbackServer: Server | null = null;
/**
* Start a temporary HTTP server on 0.0.0.0:{port} that handles the OAuth
* callback (`/auth/callback`). Closes any previously active callback server
* first (since we always reuse port 1455).
*
* Auto-closes after 5 minutes or after a successful callback.
*
* @param port The port from createOAuthSession() (always 1455)
* @param onAccount Called with (accessToken, refreshToken) on success
*/
export function startCallbackServer(
port: number,
onAccount: (accessToken: string, refreshToken: string | undefined) => void,
): Server {
// Close any existing callback server on this port
if (activeCallbackServer) {
try { activeCallbackServer.close(); } catch {}
activeCallbackServer = null;
}
const server = createServer(async (req, res) => {
const url = new URL(req.url || "/", `http://localhost:${port}`);
if (url.pathname !== "/auth/callback") {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("Not found");
return;
}
const code = url.searchParams.get("code");
const state = url.searchParams.get("state");
const error = url.searchParams.get("error");
const errorDesc = url.searchParams.get("error_description");
if (error) {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(callbackResultHtml(false, errorDesc || error));
scheduleClose();
return;
}
if (!code || !state) {
res.writeHead(400, { "Content-Type": "text/html" });
res.end(callbackResultHtml(false, "Missing code or state parameter"));
scheduleClose();
return;
}
const session = consumeSession(state);
if (!session) {
res.writeHead(400, { "Content-Type": "text/html" });
res.end(callbackResultHtml(false, "Invalid or expired session. Please try again."));
scheduleClose();
return;
}
try {
const tokens = await exchangeCode(code, session.codeVerifier, session.redirectUri);
onAccount(tokens.access_token, tokens.refresh_token);
console.log(`[OAuth] Callback server on port ${port} β login successful`);
res.writeHead(200, { "Content-Type": "text/html" });
res.end(callbackResultHtml(true));
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
console.error(`[OAuth] Callback server token exchange failed: ${msg}`);
res.writeHead(200, { "Content-Type": "text/html" });
res.end(callbackResultHtml(false, msg));
}
scheduleClose();
});
function scheduleClose() {
setTimeout(() => {
try { server.close(); } catch {}
if (activeCallbackServer === server) activeCallbackServer = null;
}, 2000);
}
server.on("error", (err: NodeJS.ErrnoException) => {
if (err.code === "EADDRINUSE") {
console.error(`[OAuth] Port ${port} is in use β callback server not started. Previous login session may still be active.`);
} else {
console.error(`[OAuth] Callback server error: ${err.message}`);
}
});
server.listen(port, "0.0.0.0");
activeCallbackServer = server;
console.log(`[OAuth] Temporary callback server started on port ${port}`);
// Auto-close after 5 minutes
const timeout = setTimeout(() => {
try { server.close(); } catch {}
if (activeCallbackServer === server) activeCallbackServer = null;
console.log(`[OAuth] Temporary callback server on port ${port} timed out`);
}, 5 * 60 * 1000);
timeout.unref();
server.on("close", () => {
clearTimeout(timeout);
});
return server;
}
// ββ Device Code Flow (RFC 8628) ββββββββββββββββββββββββββββββββββββ
/**
* Request a device code from Auth0/OpenAI.
*/
export async function requestDeviceCode(): Promise<DeviceCodeResponse> {
const config = getConfig();
const body = new URLSearchParams({
client_id: config.auth.oauth_client_id,
scope: "openid profile email offline_access",
});
const resp = await withDirectFallback(
(proxyUrl) => curlFetchPost(
"https://auth.openai.com/oauth/device/code",
"application/x-www-form-urlencoded",
body.toString(),
{ proxyUrl },
),
{ tag: "OAuth/deviceCode", shouldFallback: isCfResponse },
);
if (!resp.ok) {
throw new Error(`Device code request failed (${resp.status}): ${resp.body}`);
}
return JSON.parse(resp.body) as DeviceCodeResponse;
}
/**
* Poll the token endpoint for a device code authorization.
* Returns tokens on success, or throws with "authorization_pending" / "slow_down" / other errors.
*/
export async function pollDeviceToken(deviceCode: string): Promise<TokenResponse> {
const config = getConfig();
const body = new URLSearchParams({
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
device_code: deviceCode,
client_id: config.auth.oauth_client_id,
});
const resp = await withDirectFallback(
(proxyUrl) => curlFetchPost(
config.auth.oauth_token_endpoint,
"application/x-www-form-urlencoded",
body.toString(),
{ proxyUrl },
),
{ tag: "OAuth/pollDevice", shouldFallback: isCfResponse },
);
if (!resp.ok) {
const data = JSON.parse(resp.body) as { error?: string; error_description?: string };
const err = new Error(data.error_description || data.error || `Poll failed (${resp.status})`);
(err as Error & { code?: string }).code = data.error;
throw err;
}
return JSON.parse(resp.body) as TokenResponse;
}
// ββ CLI Token Import βββββββββββββββββββββββββββββββββββββββββββββββ
export interface CliAuthJson {
access_token?: string;
refresh_token?: string;
id_token?: string;
expires_at?: number;
}
/**
* Start an OAuth flow with callback server in one call.
* Combines createOAuthSession + startCallbackServer + account registration.
* Used by /auth/login, /auth/login-start, and /auth/accounts/login.
*/
export function startOAuthFlow(
originalHost: string,
returnTo: "login" | "dashboard",
pool: { addAccount(accessToken: string, refreshToken?: string): string },
scheduler: { scheduleOne(entryId: string, accessToken: string): void },
): { authUrl: string; state: string } {
const { authUrl, state, port } = createOAuthSession(originalHost, returnTo);
startCallbackServer(port, (accessToken, refreshToken) => {
const entryId = pool.addAccount(accessToken, refreshToken);
scheduler.scheduleOne(entryId, accessToken);
markSessionCompleted(state);
console.log(`[Auth] OAuth via callback server β account ${entryId} added`);
});
return { authUrl, state };
}
/**
* Read and parse the Codex CLI auth.json file.
* Path: $CODEX_HOME/auth.json (default: ~/.codex/auth.json)
*/
export function importCliAuth(): CliAuthJson {
const codexHome = process.env.CODEX_HOME || resolve(homedir(), ".codex");
const authPath = resolve(codexHome, "auth.json");
if (!existsSync(authPath)) {
throw new Error(`CLI auth file not found: ${authPath}`);
}
const raw = readFileSync(authPath, "utf-8");
const data = JSON.parse(raw) as CliAuthJson;
if (!data.access_token) {
throw new Error("CLI auth.json does not contain access_token");
}
return data;
}
function callbackResultHtml(success: boolean, error?: string): string {
const esc = (s: string) =>
s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
if (success) {
return `<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Login Successful</title>
<style>body{font-family:-apple-system,sans-serif;background:#0d1117;color:#c9d1d9;display:flex;align-items:center;justify-content:center;min-height:100vh;margin:0}
.card{background:#161b22;border:1px solid #30363d;border-radius:12px;padding:2rem;text-align:center;max-width:400px}
h2{color:#3fb950;margin-bottom:1rem}</style></head>
<body><div class="card"><h2>Login Successful</h2><p>You can close this window.</p></div>
<script>
if(window.opener){try{window.opener.postMessage({type:'oauth-callback-success'},'*')}catch(e){}}
try{window.close()}catch{}
</script></body></html>`;
}
return `<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Login Failed</title>
<style>body{font-family:-apple-system,sans-serif;background:#0d1117;color:#c9d1d9;display:flex;align-items:center;justify-content:center;min-height:100vh;margin:0}
.card{background:#161b22;border:1px solid #30363d;border-radius:12px;padding:2rem;text-align:center;max-width:400px}
h2{color:#f85149;margin-bottom:1rem}</style></head>
<body><div class="card"><h2>Login Failed</h2><p>${esc(error || "Unknown error")}</p></div>
<script>
if(window.opener){try{window.opener.postMessage({type:'oauth-callback-error',error:${JSON.stringify(error || "Unknown error")}},'*')}catch(e){}}
</script></body></html>`;
}
|