From ebc60aa82540e873fa5b2e773fe80536f969a34d Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 20 Apr 2026 14:43:08 +0300 Subject: [PATCH] feat(ai): allow overriding oauth callback bind host closes #3396 (#3409) --- packages/ai/src/utils/oauth/anthropic.ts | 2 +- packages/ai/src/utils/oauth/google-antigravity.ts | 4 +++- packages/ai/src/utils/oauth/google-gemini-cli.ts | 4 +++- packages/ai/src/utils/oauth/openai-codex.ts | 5 +++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/ai/src/utils/oauth/anthropic.ts b/packages/ai/src/utils/oauth/anthropic.ts index 48032f1e..1d404266 100644 --- a/packages/ai/src/utils/oauth/anthropic.ts +++ b/packages/ai/src/utils/oauth/anthropic.ts @@ -28,7 +28,7 @@ const decode = (s: string) => atob(s); const CLIENT_ID = decode("OWQxYzI1MGEtZTYxYi00NGQ5LTg4ZWQtNTk0NGQxOTYyZjVl"); const AUTHORIZE_URL = "https://claude.ai/oauth/authorize"; const TOKEN_URL = "https://platform.claude.com/v1/oauth/token"; -const CALLBACK_HOST = "127.0.0.1"; +const CALLBACK_HOST = process.env.PI_OAUTH_CALLBACK_HOST || "127.0.0.1"; const CALLBACK_PORT = 53692; const CALLBACK_PATH = "/callback"; const REDIRECT_URI = `http://localhost:${CALLBACK_PORT}${CALLBACK_PATH}`; diff --git a/packages/ai/src/utils/oauth/google-antigravity.ts b/packages/ai/src/utils/oauth/google-antigravity.ts index b601f886..d5843e32 100644 --- a/packages/ai/src/utils/oauth/google-antigravity.ts +++ b/packages/ai/src/utils/oauth/google-antigravity.ts @@ -15,6 +15,8 @@ type AntigravityCredentials = OAuthCredentials & { projectId: string; }; +const CALLBACK_HOST = process.env.PI_OAUTH_CALLBACK_HOST || "127.0.0.1"; + let _createServer: typeof import("node:http").createServer | null = null; let _httpImportPromise: Promise | null = null; if (typeof process !== "undefined" && (process.versions?.node || process.versions?.bun)) { @@ -110,7 +112,7 @@ async function startCallbackServer(): Promise { reject(err); }); - server.listen(51121, "127.0.0.1", () => { + server.listen(51121, CALLBACK_HOST, () => { resolve({ server, cancelWait: () => { diff --git a/packages/ai/src/utils/oauth/google-gemini-cli.ts b/packages/ai/src/utils/oauth/google-gemini-cli.ts index 2ca6dd6a..ad4f3991 100644 --- a/packages/ai/src/utils/oauth/google-gemini-cli.ts +++ b/packages/ai/src/utils/oauth/google-gemini-cli.ts @@ -15,6 +15,8 @@ type GeminiCredentials = OAuthCredentials & { projectId: string; }; +const CALLBACK_HOST = process.env.PI_OAUTH_CALLBACK_HOST || "127.0.0.1"; + let _createServer: typeof import("node:http").createServer | null = null; let _httpImportPromise: Promise | null = null; if (typeof process !== "undefined" && (process.versions?.node || process.versions?.bun)) { @@ -102,7 +104,7 @@ async function startCallbackServer(): Promise { reject(err); }); - server.listen(8085, "127.0.0.1", () => { + server.listen(8085, CALLBACK_HOST, () => { resolve({ server, cancelWait: () => { diff --git a/packages/ai/src/utils/oauth/openai-codex.ts b/packages/ai/src/utils/oauth/openai-codex.ts index 86ccb107..0bc705a5 100644 --- a/packages/ai/src/utils/oauth/openai-codex.ts +++ b/packages/ai/src/utils/oauth/openai-codex.ts @@ -21,6 +21,7 @@ import { oauthErrorHtml, oauthSuccessHtml } from "./oauth-page.js"; import { generatePKCE } from "./pkce.js"; import type { OAuthCredentials, OAuthLoginCallbacks, OAuthPrompt, OAuthProviderInterface } from "./types.js"; +const CALLBACK_HOST = process.env.PI_OAUTH_CALLBACK_HOST || "127.0.0.1"; const CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann"; const AUTHORIZE_URL = "https://auth.openai.com/oauth/authorize"; const TOKEN_URL = "https://auth.openai.com/oauth/token"; @@ -248,7 +249,7 @@ function startLocalOAuthServer(state: string): Promise { return new Promise((resolve) => { server - .listen(1455, "127.0.0.1", () => { + .listen(1455, CALLBACK_HOST, () => { resolve({ close: () => server.close(), cancelWait: () => { @@ -259,7 +260,7 @@ function startLocalOAuthServer(state: string): Promise { }) .on("error", (err: NodeJS.ErrnoException) => { console.error( - "[openai-codex] Failed to bind http://127.0.0.1:1455 (", + `[openai-codex] Failed to bind http://${CALLBACK_HOST}:1455 (`, err.code, ") Falling back to manual paste.", );