openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
8 lines • 5.19 kB
JavaScript
import{t as loginOpenAICodex}from"./openai-chatgpt-oauth-flow.runtime-C6tw4HRf.mjs";import{raceWithTimeout}from"openclaw/plugin-sdk/time-runtime";import{formatErrorMessage}from"openclaw/plugin-sdk/error-runtime";import{runHttpTlsPreflight}from"openclaw/plugin-sdk/provider-http";import path from"node:path";import{ensureGlobalUndiciEnvProxyDispatcher}from"openclaw/plugin-sdk/runtime-env";import{formatCliCommand}from"openclaw/plugin-sdk/setup-tools";
//!extensions/openai/openai-chatgpt-oauth-preflight.runtime.ts
const OPENAI_AUTH_PROBE_URL=`https://auth.openai.com/oauth/authorize?response_type=code&client_id=openclaw-preflight&redirect_uri=http%3A%2F%2Flocalhost%3A1455%2Fauth%2Fcallback&scope=openid+profile+email`;function runOpenAIOAuthTlsPreflight(e){return runHttpTlsPreflight(OPENAI_AUTH_PROBE_URL,e)}
//!extensions/openai/openai-chatgpt-oauth.runtime.ts
const manualInputPromptMessage=`Paste the authorization code (or full redirect URL):`;const callbackStillOpenMessage=`Still waiting for the browser. Finish signing in there, or paste the redirect URL below.`;const openAICodexOAuthOriginator=`openclaw`;const callbackUnavailableMessage=`OpenClaw could not use localhost:1455, so the browser cannot finish sign-in here. Sign in there, then paste the redirect URL from its address bar below.`;const localManualFallbackDelayMs=15e3;const localManualFallbackGraceMs=1e3;function createOpenAICodexOAuthError(e,t,n){return Object.assign(new Error(`OpenAI Codex OAuth failed (${e}): ${t}`,{cause:n}),{code:e})}function rewriteOpenAICodexOAuthError(e){const t=formatErrorMessage(e);if(/unsupported_country_region_territory/i.test(t))return createOpenAICodexOAuthError(`unsupported_region`,[`OpenAI rejected the token exchange for this country, region, or network route.`,"If you normally use a proxy, verify HTTPS_PROXY, HTTP_PROXY, or ALL_PROXY is set for the OpenClaw process and then retry `openclaw models auth login --provider openai`."].join(` `),e);if(/state mismatch|missing authorization code/i.test(t))return createOpenAICodexOAuthError(`callback_validation_failed`,t,e);return e instanceof Error?e:new Error(t)}async function loginOpenAICodexOAuth(e){const{prompter:t,runtime:n,isRemote:r,openUrl:i,localBrowserMessage:a}=e;ensureGlobalUndiciEnvProxyDispatcher();const o=await runOpenAIOAuthTlsPreflight({signal:e.signal,assertCurrent:e.assertCurrent});e.assertCurrent?.();if(!o.ok&&o.kind===`tls-cert`){const e=process.execPath;const r=`${path.sep}Cellar${path.sep}`;const i=e.indexOf(r);const a=i>0?e.slice(0,i):process.env.HOMEBREW_PREFIX?.trim()||null;const s=a?path.join(a,`etc`,`openssl@3`,`cert.pem`):null;const c=[`OpenAI OAuth prerequisites check failed: Node/OpenSSL cannot validate TLS certificates.`,`Cause: ${o.code?`${o.code} (${o.message})`:o.message}`,``,`Fix (Homebrew Node/OpenSSL):`,`- ${formatCliCommand(`brew postinstall ca-certificates`)}`,`- ${formatCliCommand(`brew postinstall openssl@3`)}`];if(s)c.push(`- Verify cert bundle exists: ${s}`);c.push(`- Retry the OAuth login flow.`);const l=c.join(`
`);await t.note(l,`OAuth prerequisites`);n.error(l);throw new Error(`OpenAI Codex OAuth prerequisites failed: ${o.message}`)}await t.note(r?[`You are running in a remote/VPS environment.`,`A URL will be shown for you to open in your LOCAL browser.`,`Open it, sign in, then paste the redirect URL here.`,`If this OpenClaw process can receive the browser callback, sign-in may finish automatically before you paste.`].join(`
`):[`Browser will open for OpenAI authentication.`,`If the callback doesn't auto-complete, paste the redirect URL.`,`OpenAI OAuth uses localhost:1455 for the callback.`].join(`
`),`OpenAI Codex OAuth`);const s=t.progress(`Starting OAuth flow...`);let c=true;let l=false;const stopProgress=e=>{if(c){c=false;s.stop(e)}};let u;const d=new Promise(e=>{u=e});const f=new AbortController;try{const{onAuth:o,onPrompt:c}=e.oauth.createVpsAwareHandlers({isRemote:r,prompter:t,runtime:n,spin:s,openUrl:i,localBrowserMessage:a??`Complete sign-in in browser...`,manualPromptMessage:manualInputPromptMessage,manualPromptSignal:f.signal});const promptForManualCode=()=>c({message:manualInputPromptMessage});const offerManualEntry=async e=>{stopProgress(e);l=true;return await promptForManualCode()};const runLocalManualFallback=async e=>{if(!e.listening)return await offerManualEntry(callbackUnavailableMessage);for(const e of[localManualFallbackDelayMs,localManualFallbackGraceMs])if(await raceWithTimeout(d.then(()=>`settled`,()=>`settled`),e,()=>`delay`)===`settled`)return await new Promise(()=>{});return await offerManualEntry(callbackStillOpenMessage)};const u=await loginOpenAICodex({onAuth:o,onPrompt:c,originator:openAICodexOAuthOriginator,onManualCodeInput:e.onManualCodeInput??(r?promptForManualCode:runLocalManualFallback),signal:e.signal,assertCurrent:e.assertCurrent});stopProgress(`OpenAI OAuth complete`);if(l){f.abort();n.log(`OpenAI OAuth complete`)}return u??null}catch(e){stopProgress(`OpenAI OAuth failed`);const r=rewriteOpenAICodexOAuthError(e);n.error(String(r));await t.note(`Trouble with OAuth? See https://docs.openclaw.ai/start/faq`,`OAuth help`);throw r}finally{f.abort();u()}}export{loginOpenAICodexOAuth};