openai_auth/
lib.rs

1//! # openai-auth
2//!
3//! A Rust library for OpenAI/ChatGPT OAuth 2.0 authentication with PKCE support.
4//!
5//! This library provides both synchronous (blocking) and asynchronous (runtime-agnostic)
6//! APIs for authenticating with OpenAI's OAuth 2.0 endpoints.
7//!
8//! ## Features
9//!
10//! - **Async API** (default): Runtime-agnostic async operations
11//! - **Blocking API** (optional): Blocking operations, no async runtime required
12//! - **PKCE Support**: Secure PKCE (SHA-256) authentication flow
13//! - **Configurable**: Custom client IDs, endpoints, redirect URIs
14//! - **Browser Integration**: Auto-open browser for authorization (default)
15//! - **Callback Server**: Local server for automatic callback handling (optional, requires tokio)
16//! - **JWT Utilities**: Extract ChatGPT account ID from access tokens
17//! - **API Key Exchange**: Exchange id_token for OpenAI API key (Codex CLI flow)
18//!
19//! ## Quick Start (Async API)
20//!
21//! ```no_run
22//! use openai_auth::{OAuthClient, OAuthConfig};
23//!
24//! #[tokio::main]
25//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
26//!     let client = OAuthClient::new(OAuthConfig::default())?;
27//!     let flow = client.start_flow()?;
28//!     
29//!     println!("Visit: {}", flow.authorization_url);
30//!     // Get code from user...
31//!     
32//!     let tokens = client.exchange_code("code", &flow.pkce_verifier).await?;
33//!     println!("Got tokens!");
34//!     Ok(())
35//! }
36//! ```
37//!
38//! ## Quick Start (Blocking API)
39//!
40//! ```no_run
41//! use openai_auth::{blocking::OAuthClient, OAuthConfig};
42//!
43//! fn main() -> Result<(), Box<dyn std::error::Error>> {
44//!     let client = OAuthClient::new(OAuthConfig::default())?;
45//!     let flow = client.start_flow()?;
46//!     
47//!     println!("Visit: {}", flow.authorization_url);
48//!     // Get code from user...
49//!     
50//!     let tokens = client.exchange_code("code", &flow.pkce_verifier)?;
51//!     println!("Got tokens!");
52//!     Ok(())
53//! }
54//! ```
55
56mod error;
57mod jwt;
58mod types;
59
60#[cfg(feature = "async")]
61mod client;
62
63#[cfg(feature = "blocking")]
64pub mod blocking;
65
66#[cfg(feature = "browser")]
67mod browser;
68
69#[cfg(feature = "callback-server")]
70mod server;
71
72// Public API exports
73pub use error::{OpenAIAuthError, Result};
74pub use types::{OAuthConfig, OAuthConfigBuilder, OAuthFlow, SessionData, TokenSet};
75
76#[cfg(feature = "async")]
77pub use client::OAuthClient;
78
79#[cfg(feature = "browser")]
80pub use browser::open_browser;
81
82#[cfg(feature = "callback-server")]
83pub use server::{CallbackEvent, run_callback_server, run_callback_server_with_html};