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//!
18//! ## Quick Start (Async API)
19//!
20//! ```no_run
21//! use openai_auth::{OAuthClient, OAuthConfig};
22//!
23//! #[tokio::main]
24//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
25//! let client = OAuthClient::new(OAuthConfig::default())?;
26//! let flow = client.start_flow()?;
27//!
28//! println!("Visit: {}", flow.authorization_url);
29//! // Get code from user...
30//!
31//! let tokens = client.exchange_code("code", &flow.pkce_verifier).await?;
32//! println!("Got tokens!");
33//! Ok(())
34//! }
35//! ```
36//!
37//! ## Quick Start (Blocking API)
38//!
39//! ```no_run
40//! use openai_auth::{blocking::OAuthClient, OAuthConfig};
41//!
42//! fn main() -> Result<(), Box<dyn std::error::Error>> {
43//! let client = OAuthClient::new(OAuthConfig::default())?;
44//! let flow = client.start_flow()?;
45//!
46//! println!("Visit: {}", flow.authorization_url);
47//! // Get code from user...
48//!
49//! let tokens = client.exchange_code("code", &flow.pkce_verifier)?;
50//! println!("Got tokens!");
51//! Ok(())
52//! }
53//! ```
54
55mod error;
56mod jwt;
57mod types;
58
59#[cfg(feature = "async")]
60mod client;
61
62#[cfg(feature = "blocking")]
63pub mod blocking;
64
65#[cfg(feature = "browser")]
66mod browser;
67
68#[cfg(feature = "callback-server")]
69mod server;
70
71// Public API exports
72pub use error::{OpenAIAuthError, Result};
73pub use types::{OAuthConfig, OAuthConfigBuilder, OAuthFlow, TokenSet};
74
75#[cfg(feature = "async")]
76pub use client::OAuthClient;
77
78#[cfg(feature = "browser")]
79pub use browser::open_browser;
80
81#[cfg(feature = "callback-server")]
82pub use server::run_callback_server;