OAuthClient

Struct OAuthClient 

Source
pub struct OAuthClient { /* private fields */ }
Expand description

Synchronous Anthropic OAuth client for authentication

This client handles the OAuth 2.0 flow with PKCE for Anthropic/Claude authentication using blocking I/O. No async runtime required.

§Example

use anthropic_auth::{OAuthClient, OAuthConfig, OAuthMode};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = OAuthClient::new(OAuthConfig::default())?;
    let flow = client.start_flow(OAuthMode::Max)?;
     
    println!("Visit: {}", flow.authorization_url);
    // User authorizes and you get the code and state...
     
    let tokens = client.exchange_code("code_value", "state_value", &flow.verifier)?;
    println!("Got tokens!");
    Ok(())
}

Implementations§

Source§

impl OAuthClient

Source

pub fn new(config: OAuthConfig) -> Result<Self>

Create a new OAuth client with the given configuration

§Arguments
  • config - OAuth configuration (client ID, redirect URI)
§Errors

Returns an error if the configuration is invalid

Source

pub fn start_flow(&self, mode: OAuthMode) -> Result<OAuthFlow>

Start the OAuth authorization flow

This generates a PKCE challenge and state token, then creates the authorization URL that the user should visit to authorize the application.

§Arguments
  • mode - The OAuth mode (Max for subscription, Console for API key creation)
§Returns

An OAuthFlow containing the authorization URL, PKCE verifier, state token, and mode

§Example
let client = OAuthClient::new(OAuthConfig::default())?;
let flow = client.start_flow(OAuthMode::Max)?;
println!("Visit: {}", flow.authorization_url);
Source

pub fn exchange_code( &self, code_with_state: &str, expected_state: &str, verifier: &str, ) -> Result<TokenSet>

Exchange an authorization code for access and refresh tokens (blocking)

After the user authorizes the application, Anthropic returns a combined string in the format code#state. This method parses that format, validates the state for CSRF protection, and exchanges the code for tokens.

§Arguments
  • code_with_state - The combined authorization response (format: “code#state”) or just the code if already separated
  • expected_state - The state token from the original flow (for CSRF validation)
  • verifier - The PKCE verifier from the original flow
§Returns

A TokenSet containing access token, refresh token, and expiration time

§Errors

Returns an error if:

  • The code, state, or verifier is invalid or empty
  • The state doesn’t match the expected state (CSRF protection)
  • The token exchange fails (invalid code, network error, etc.)
  • The response contains invalid token data
§Example
// User pastes the combined response from Anthropic
let response = "code123#state456";
let tokens = client.exchange_code(response, &flow.state, &flow.verifier)?;
println!("Access token expires in: {:?}", tokens.expires_in());
Source

pub fn refresh_token(&self, refresh_token: &str) -> Result<TokenSet>

Refresh an expired access token (blocking)

When an access token expires, use the refresh token to obtain a new access token without requiring the user to re-authorize.

§Arguments
  • refresh_token - The refresh token from a previous token exchange
§Returns

A new TokenSet with fresh access token

§Errors

Returns an error if the refresh fails (invalid refresh token, network error, etc.)

§Example
let new_tokens = client.refresh_token(&old_tokens.refresh_token)?;
Source

pub fn create_api_key(&self, access_token: &str) -> Result<String>

Create an API key using a Console OAuth access token (blocking)

This method is only available when using Console mode OAuth. It creates a new API key that can be used with Anthropic’s API.

§Arguments
  • access_token - The access token from Console mode OAuth
§Returns

The API key as a string

§Errors

Returns an error if API key creation fails

§Example
let api_key = client.create_api_key(&tokens.access_token)?;
println!("API Key: {}", api_key);

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more