OAuthClient

Struct OAuthClient 

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

Async OpenAI OAuth client for authentication

This client handles the OAuth 2.0 flow with PKCE for OpenAI/ChatGPT authentication using asynchronous operations (runtime-agnostic).

For blocking/synchronous operations, use blocking::OAuthClient (requires the blocking feature).

§Example

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);
    // User authorizes and you get the code...
     
    let tokens = client.exchange_code("code", &flow.pkce_verifier).await?;
    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, endpoints, redirect URI)
§Errors

Returns an error if the configuration is invalid

Source

pub fn start_flow(&self) -> Result<OAuthFlow>

Start the OAuth authorization flow

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

§Returns

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

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

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

Extract ChatGPT account ID from an access token

OpenAI access tokens contain the ChatGPT account ID in their JWT claims. This is useful for making API requests that require the account ID.

§Arguments
  • access_token - The access token to extract the account ID from
§Returns

The ChatGPT account ID as a string

§Errors

Returns an error if the JWT is malformed or doesn’t contain the account ID

Source

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

Exchange an authorization code for access and refresh tokens

After the user authorizes the application, they’ll receive an authorization code. This method exchanges that code for access and refresh tokens.

§Arguments
  • code - The authorization code from the OAuth callback
  • 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 token exchange fails (invalid code, network error, etc.)

§Example
let code = "authorization_code_from_callback";
let tokens = client.exchange_code(code, &flow.pkce_verifier).await?;
println!("Access token expires in: {:?}", tokens.expires_in());
Source

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

Refresh an expired access token

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
if tokens.is_expired() {
    let new_tokens = client.refresh_token(&tokens.refresh_token).await?;
    println!("Refreshed! New token expires in: {:?}", new_tokens.expires_in());
}

Trait Implementations§

Source§

impl Default for OAuthClient

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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