Skip to main content

anthropic_oauth/
lib.rs

1//! PKCE OAuth login for Anthropic Claude Pro/Max.
2//!
3//! Thin wrapper around [`motosan_ai_oauth`] with the Anthropic provider
4//! pre-configured.
5//!
6//! # Usage
7//!
8//! ```no_run
9//! #[tokio::main]
10//! async fn main() -> Result<(), anthropic_oauth::Error> {
11//!     let token = anthropic_oauth::login().await?;
12//!     // `token.access_token` is a "sk-ant-oat01-*" string usable directly
13//!     // with motosan_ai's AnthropicProvider (which auto-detects the prefix
14//!     // and applies Claude Code identity headers).
15//!     println!("{}", token.access_token);
16//!     Ok(())
17//! }
18//! ```
19//!
20//! # Notes
21//!
22//! - Requires port 53692 to be free on localhost (registered with Anthropic's
23//!   Claude Code app).
24//! - `client_id` is hardcoded to Anthropic's Claude Code app registration and
25//!   is not configurable. See the project README for the associated ToS
26//!   disclosure before depending on this crate.
27
28pub use motosan_ai_oauth::{Error, Token};
29
30/// Open a browser-based PKCE login flow and return the resulting OAuth token.
31pub async fn login() -> Result<Token, Error> {
32    motosan_ai_oauth::login(&motosan_ai_oauth::providers::anthropic::claude_pro_max()).await
33}
34
35/// Exchange a stored refresh token for a new [`Token`].
36pub async fn refresh(refresh_token: &str) -> Result<Token, Error> {
37    motosan_ai_oauth::refresh(
38        &motosan_ai_oauth::providers::anthropic::claude_pro_max(),
39        refresh_token,
40    )
41    .await
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn re_exports_compile() {
50        let _: fn() -> bool = || {
51            let t = Token {
52                access_token: String::new(),
53                refresh_token: String::new(),
54                id_token: None,
55                expires_in: 0,
56                issued_at: 0,
57            };
58            t.is_expired()
59        };
60    }
61}