anthropic_auth/
lib.rs

1//! # anthropic-auth
2//!
3//! A Rust library for Anthropic/Claude OAuth 2.0 authentication with PKCE support.
4//!
5//! This library provides both synchronous (blocking) and asynchronous (runtime-agnostic)
6//! APIs for authenticating with Anthropic's OAuth 2.0 endpoints.
7//!
8//! ## Features
9//!
10//! - **Sync API** (default): Blocking operations, no async runtime required
11//! - **Async API** (optional): Runtime-agnostic async operations
12//! - **PKCE Support**: Secure PKCE (SHA-256) authentication flow with separate CSRF state tokens
13//! - **Two OAuth Modes**: Max (subscription) and Console (API key creation)
14//! - **Configurable**: Custom client IDs, redirect URIs
15//! - **Browser Integration**: Auto-open browser for authorization (default)
16//! - **Callback Server**: Local server for automatic callback handling (optional, requires tokio)
17//! - **API Key Creation**: Create API keys via Console OAuth
18//! - **Token Validation**: Built-in validation for tokens and parameters
19//!
20//! ## Choosing Between Sync and Async
21//!
22//! - Use [`OAuthClient`] (sync) if you're building a CLI tool or simple application without async
23//! - Use [`AsyncOAuthClient`] (async) if you're building a web server or async application
24//!
25//! ## Quick Start (Sync API)
26//!
27//! ```no_run
28//! use anthropic_auth::{OAuthClient, OAuthConfig, OAuthMode};
29//!
30//! fn main() -> Result<(), Box<dyn std::error::Error>> {
31//!     let client = OAuthClient::new(OAuthConfig::default())?;
32//!     let flow = client.start_flow(OAuthMode::Max)?;
33//!     
34//!     println!("Visit: {}", flow.authorization_url);
35//!     // User authorizes and receives response in format: "code#state"
36//!     let response = "code123#state456";
37//!     
38//!     // Library automatically parses and validates the state
39//!     let tokens = client.exchange_code(response, &flow.state, &flow.verifier)?;
40//!     println!("Got tokens!");
41//!     Ok(())
42//! }
43//! ```
44//!
45//! ## Quick Start (Async API)
46//!
47//! ```no_run
48//! # #[cfg(feature = "async")]
49//! # {
50//! use anthropic_auth::{AsyncOAuthClient, OAuthConfig, OAuthMode};
51//!
52//! # #[tokio::main]
53//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
54//! let client = AsyncOAuthClient::new(OAuthConfig::default())?;
55//! let flow = client.start_flow(OAuthMode::Max)?;
56//!
57//! println!("Visit: {}", flow.authorization_url);
58//! // User authorizes and receives response in format: "code#state"
59//! let response = "code123#state456";
60//!
61//! // Library automatically parses and validates the state
62//! let tokens = client.exchange_code(response, &flow.state, &flow.verifier).await?;
63//! println!("Got tokens!");
64//! # Ok(())
65//! # }
66//! # }
67//! ```
68
69mod error;
70mod types;
71
72#[cfg(any(feature = "blocking", feature = "async"))]
73mod client;
74
75#[cfg(feature = "browser")]
76mod browser;
77
78#[cfg(feature = "callback-server")]
79mod server;
80
81// Public API exports
82pub use error::{AnthropicAuthError, Result};
83pub use types::{OAuthConfig, OAuthConfigBuilder, OAuthFlow, OAuthMode, TokenSet};
84
85#[cfg(feature = "blocking")]
86pub use client::OAuthClient;
87
88#[cfg(feature = "async")]
89pub use client::AsyncOAuthClient;
90
91#[cfg(feature = "browser")]
92pub use browser::open_browser;
93
94#[cfg(feature = "callback-server")]
95pub use server::{run_callback_server, CallbackData};