Crate openai_auth

Crate openai_auth 

Source
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§

OAuthClient
Async OpenAI OAuth client for authentication
OAuthConfig
Configuration for the OpenAI OAuth client
OAuthConfigBuilder
Builder for OAuthConfig
OAuthFlow
OAuth authorization flow information
SessionData
Session data extracted from OAuth tokens
TokenSet
OAuth token set containing access token, refresh token, and expiration info

Enums§

OpenAIAuthError
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