Expand description
§openai-auth
A Rust library for OpenAI/ChatGPT OAuth 2.0 authentication with PKCE support.
This library provides both synchronous (blocking) and asynchronous (runtime-agnostic) APIs for authenticating with OpenAI’s OAuth 2.0 endpoints.
§Features
- Async API (default): Runtime-agnostic async operations
- Blocking API (optional): Blocking operations, no async runtime required
- PKCE Support: Secure PKCE (SHA-256) authentication flow
- Configurable: Custom client IDs, endpoints, redirect URIs
- Browser Integration: Auto-open browser for authorization (default)
- Callback Server: Local server for automatic callback handling (optional, requires tokio)
- JWT Utilities: Extract ChatGPT account ID from access tokens
- API Key Exchange: Exchange id_token for OpenAI API key (Codex CLI flow)
§Quick Start (Async API)
use openai_auth::{OAuthClient, OAuthConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OAuthClient::new(OAuthConfig::default())?;
let flow = client.start_flow()?;
println!("Visit: {}", flow.authorization_url);
// Get code from user...
let tokens = client.exchange_code("code", &flow.pkce_verifier).await?;
println!("Got tokens!");
Ok(())
}§Quick Start (Blocking API)
use openai_auth::{blocking::OAuthClient, OAuthConfig};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OAuthClient::new(OAuthConfig::default())?;
let flow = client.start_flow()?;
println!("Visit: {}", flow.authorization_url);
// Get code from user...
let tokens = client.exchange_code("code", &flow.pkce_verifier)?;
println!("Got tokens!");
Ok(())
}Structs§
- OAuth
Client - Async OpenAI OAuth client for authentication
- OAuth
Config - Configuration for the OpenAI OAuth client
- OAuth
Config Builder - Builder for OAuthConfig
- OAuth
Flow - OAuth authorization flow information
- Session
Data - Session data extracted from OAuth tokens
- Token
Set - OAuth token set containing access token, refresh token, and expiration info
Enums§
- OpenAI
Auth Error - Error types for OpenAI OAuth authentication
Functions§
- open_
browser - Open a URL in the user’s default web browser
Type Aliases§
- Result
- Result type alias for OpenAI authentication operations