UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

113 lines (112 loc) • 4.67 kB
import { r as createLazyRuntimeModule } from "./lazy-runtime-zGjszCtt.mjs"; import { t as createOAuthRefreshError } from "./oauth-refresh-outcome-DGKa-Dzy.mjs"; import { n as oauthErrorHtml, r as oauthSuccessHtml } from "./oauth-page-UJbx3jkp.mjs"; import { i as resolveOpenAICodexAuthIdentity } from "./provider-openai-chatgpt-auth-Lx-WyDpB.mjs"; import { a as parseOAuthAuthorizationInput, c as throwIfOAuthLoginAborted, l as withOAuthLoginAbort, n as createOAuthLoginCancelledError } from "./provider-oauth-runtime-C_hG37Q1.mjs"; import { n as resolveOpenAICallbackHost, r as resolveOpenAIRedirectUri, t as createOpenAIAuthorizationFlow } from "./openai-chatgpt-oauth-authorization.runtime.js"; import { n as refreshOpenAIAccessToken, t as exchangeOpenAIAuthorizationCode } from "./openai-chatgpt-oauth-token.runtime.js"; //#region extensions/openai/openai-chatgpt-oauth-flow.runtime.ts const CALLBACK_HOST = resolveOpenAICallbackHost(); const REDIRECT_URI = resolveOpenAIRedirectUri(CALLBACK_HOST); const loadOAuthCallbackServer = createLazyRuntimeModule(() => import("./plugin-sdk/provider-auth-runtime.js").then(({ startProviderOAuthLoopbackCallbackServer: e }) => e)); function parseAuthorizationCode(e, t) { const n = parseOAuthAuthorizationInput(e); if (n.state && n.state !== t) throw new Error("State mismatch"); return n.code; } async function promptForAuthorizationCode(e, t) { return parseAuthorizationCode(await e({ message: "Paste the authorization code (or full redirect URL):" }), t); } function resolveOpenAICredentials(e) { if (e.type !== "success") { if (e.cancelled) throw createOAuthLoginCancelledError(); const t = [ e.status ? `HTTP ${e.status}` : void 0, e.code ? `code=${e.code}` : void 0, e.errorType ? `type=${e.errorType}` : void 0 ].filter((e) => Boolean(e)); const n = t.length > 0 ? `OpenAI Codex token ${e.operation} failed (${t.join("; ")}).` : void 0; throw Object.assign(new Error([e.summary, n].filter(Boolean).join("\n\n")), { ...e.oauthRefreshOutcome ? { oauthRefreshOutcome: e.oauthRefreshOutcome } : {}, oauthRefreshFailure: { summary: e.summary, ...e.errorType ? { errorType: e.errorType } : {}, ...e.reason ? { reason: e.reason } : {}, ...e.status ? { status: e.status } : {} } }); } const t = resolveOpenAICodexAuthIdentity({ access: e.access }).accountId; if (!t) throw createOAuthRefreshError("Failed to extract accountId from token. Sign in again.", { version: 1, phase: "after-token-response", outcome: "unknown" }); return { access: e.access, refresh: e.refresh, expires: e.expires, accountId: t }; } async function loginOpenAICodex(e) { e.assertCurrent?.(); throwIfOAuthLoginAborted(e.signal); const { verifier: t, redirectUri: n, state: r, url: i } = await createOpenAIAuthorizationFlow(e.originator ?? "openclaw", REDIRECT_URI); const a = await loadOAuthCallbackServer(); e.assertCurrent?.(); throwIfOAuthLoginAborted(e.signal); let o; try { o = await a({ redirectUrl: REDIRECT_URI, expectedState: r, bindOnlyHostname: CALLBACK_HOST, signal: e.signal, renderSuccess: () => ({ body: oauthSuccessHtml("OpenAI authentication completed. You can close this window."), contentType: "text/html; charset=utf-8" }), renderError: (e) => ({ body: oauthErrorHtml(e), contentType: "text/html; charset=utf-8" }) }); } catch { e.assertCurrent?.(); throwIfOAuthLoginAborted(e.signal); } const waitForLogin = (t) => withOAuthLoginAbort(t, e.signal); let s; try { e.assertCurrent?.(); throwIfOAuthLoginAborted(e.signal); await waitForLogin(Promise.resolve(e.onAuth({ url: i, instructions: "A browser window should open. Complete login to finish." }))); throwIfOAuthLoginAborted(e.signal); const a = o?.waitForCallback().then((e) => { if (e.type === "oauth_error") throw new Error("OpenAI authorization was not completed."); return e.code; }); a?.catch(() => void 0); const c = e.onManualCodeInput().then((e) => parseAuthorizationCode(e, r), (e) => { throw e instanceof Error ? e : new Error(String(e)); }); s = await waitForLogin(a ? Promise.race([a, c]) : c); if (!s) s = await waitForLogin(promptForAuthorizationCode(e.onPrompt, r)); if (!s) throw new Error("Missing authorization code"); return resolveOpenAICredentials(await exchangeOpenAIAuthorizationCode(s, t, n, { signal: e.signal, assertCurrent: e.assertCurrent })); } finally { await o?.close(); } } async function refreshOpenAICodexToken(e) { return resolveOpenAICredentials(await refreshOpenAIAccessToken(e)); } //#endregion export { refreshOpenAICodexToken as n, loginOpenAICodex as t };