OpenAI OAuth

repository·main·Indexed 19 days ago

https://github.com/evanzhoudev/openai-oauth

A toolkit for using ChatGPT accounts as an AI backend, enabling local development via a proxy and production integration through a 'Sign in with ChatGPT' flow. It includes a monorepo of packages such as @openai-oauth/ai-sdk for Vercel AI SDK integration, @openai-oauth/openai-client for the standard OpenAI JS SDK, @openai-oauth/local for machine-based credentials, and @openai-oauth/core for advanced transport and OAuth operations. The toolkit also provides a Chrome extension and Firefox add-on to handle secure browser handoffs for local callbacks.

Tokens
34.1K
Snippets
125
Records
157
Agent score
66%

What's inside openai-oauth

  1. What is the Sign in with ChatGPT Chrome Extension?

    main

    The Chrome extension serves as a secure browser handoff for the Sign in with ChatGPT flow.

    Because OpenAI OAuth accepts a local callback at http://localhost:1455/auth/callback, hosted applications (which cannot receive local callbacks directly) rely on this extension. The extension follows a specific workflow:

    1. It listens for the exact callback http://localhost:1455/auth/callback.
    2. It displays the destination application to the user for confirmation.
    3. It returns the callback to the application after user confirmation.

    The extension's only host permission is http://localhost:1455/*.

  2. How Credential Sources and Client Adapters work together

    main

    The SDK is built on two primary abstractions that allow you to bridge ChatGPT authentication with AI clients:

    1. Credential Sources: These provide the OAuth session.

      • @openai-oauth/local retrieves credentials from your machine (e.g., ~/.codex).
      • @openai-oauth/react retrieves credentials from a user's browser session via Sign in with ChatGPT.
    2. Client Adapters: These consume the credentials to interface with AI libraries.

      • @openai-oauth/ai-sdk adapts the credentials for use with the Vercel AI SDK.
      • Other adapters (like @openai-oauth/openai-client) allow usage with standard OpenAI-compatible clients.

    Workflow Pattern:

    1. Obtain credentials from a Source.
    2. Pass credentials to an Adapter.
    3. Use the resulting client to perform AI tasks.
    import { openaiCredentials } from "@openai-oauth/local";
    import { createOpenAIOAuth } from "@openai-oauth/ai-sdk";
    import { generateText } from "ai";
    
    // 1. Get credentials from a Source
    const credentials = openaiCredentials();
    
    // 2. Use those credentials to create a Client Adapter
    const openai = createOpenAIOAuth(credentials);
    
    // 3. Use the client to run requests
    const result = await generateText({
    	model: openai("gpt-5.4-mini"),
    	prompt: "Hello!",
    });
  3. Implement a custom SessionStore

    main

    If you do not want to use the default IndexedDB/WebCrypto store, you can provide your own implementation of the SessionStore interface.

    type SessionStore = {
    	get(): Promise<OpenAIOAuthSession | null>;
    	set(session: OpenAIOAuthSession): Promise<void>;
    	clear(): Promise<void>;
    };
  4. How the Firefox Add-on handles OpenAI OAuth callbacks

    main

    OpenAI OAuth accepts a local callback at http://localhost:1455/auth/callback. However, hosted applications cannot receive this callback directly.

    The Firefox add-on solves this by:

    1. Intercepting redirects to the exact callback: http://localhost:1455/auth/callback.
    2. Showing the destination app to the user for confirmation.
    3. Returning the callback to the application after the user confirms.

    The add-on uses the narrowest possible host permission (http://localhost/*) and specifically matches port 1455.

  5. Understand the Sign in with ChatGPT Extension behavior

    main

    The 'Sign in with ChatGPT' browser extension is a utility used exclusively to facilitate the OAuth sign-in flow.

    How it works:

    1. It intercepts the local OpenAI OAuth callback at http://localhost:1455/auth/callback.
    2. It displays an extension confirmation screen showing which application requested sign-in.
    3. Upon user confirmation, it redirects the user back to the requesting application.

    Data Handling:

    • The extension temporarily handles OAuth callback parameters (such as code and state) and the original app URL.
    • This data is not saved to extension storage.
    • This data is sent only to the application you confirm.
    • No data is sent to any OpenAI OAuth servers.
  6. Quickstart with openai-oauth

    main

    To turn your ChatGPT account into an OpenAI-compatible local API, run the following command:

    npx openai-oauth

    Once running, the proxy will be available at http://127.0.0.1:10531/v1. You can use this as your base_url in any OpenAI-compatible client. No API key is required for authentication when using this local proxy.

  7. Implement 'Sign in with ChatGPT' in React/Next.js

    main

    You can allow users to bring their own ChatGPT accounts to your application using the @openai-oauth/react component and @openai-oauth/ai-sdk adapter. This works for both free and paid ChatGPT plans.

    Installation:

    npm i @openai-oauth/react @openai-oauth/ai-sdk ai @ai-sdk/react

    Client-side implementation (Next.js App Router): Use <SignInWithChatGPT /> to show the login button and openaiAuthHeaders() to attach the user's credentials to outgoing requests.

    Server-side implementation (Next.js Route Handler): Use openaiCredentials(request) from @openai-oauth/react/server to extract the credentials from the incoming request and pass them to the AI SDK adapter.

    // app/page.tsx
    "use client";
    
    import { openaiAuthHeaders, SignInWithChatGPT } from "@openai-oauth/react";
    import { useCompletion } from "@ai-sdk/react";
    
    export default function Page() {
    	const { complete, completion, isLoading } = useCompletion({
    		api: "/api/chat",
    		streamProtocol: "text",
    	});
    
    	return (
    		<>
    			<SignInWithChatGPT />
    			<button
    				disabled={isLoading}
    				onClick={async () => {
    					await complete("Hello!", {
    						headers: await openaiAuthHeaders(),
    					});
    				}}
    			>
    				Ask
    			</button>
    			<p>{completion}</p>
    		</>
    	);
    }
    // app/api/chat/route.ts
    import { createOpenAIOAuth } from "@openai-oauth/ai-sdk";
    import { openaiCredentials } from "@openai-oauth/react/server";
    import { streamText } from "ai";
    
    export async function POST(request: Request) {
    	const { prompt } = await request.json();
    	const openai = createOpenAIOAuth(openaiCredentials(request));
    
    	const result = streamText({
    		model: openai("gpt-5.4-mini"),
    		prompt,
    	});
    
    	return result.toTextStreamResponse();
    }
  8. Install @openai-oauth/core

    main

    Install the core package using npm. Note that this package is intended for advanced integrations and adapter authors; most applications should use higher-level packages like openai-oauth, @openai-oauth/local, @openai-oauth/react, @openai-oauth/ai-sdk, or @openai-oauth/openai-client instead.

    npm i @openai-oauth/core
  9. Use browser credentials in a server environment

    main

    When using browser credentials established via 'Sign in with ChatGPT', pass the request object to openaiCredentials from @openai-oauth/react/server to extract the necessary credentials in your API routes.

    import { createOpenAIOAuth } from "@openai-oauth/ai-sdk";
    import { openaiCredentials } from "@openai-oauth/react/server";
    import { generateText } from "ai";
    
    export async function POST(request: Request) {
    	const openai = createOpenAIOAuth(openaiCredentials(request));
    
    	const result = await generateText({
    		model: openai("gpt-5.4-mini"),
    		prompt: await request.text(),
    	});
    
    	return new Response(result.text);
    }