Skip to main content

OAuthSession

Struct OAuthSession 

Source
pub struct OAuthSession {
    pub tokens: OAuthTokenData,
    /* private fields */
}
Expand description

Manages OAuth token lifecycle decisions. Pure computation — no I/O.

The I/O layer owns an OAuthSession and consults it before and after each API request to decide whether a token refresh is needed.

Fields§

§tokens: OAuthTokenData

Current token data. Public for persistence by the I/O layer.

Implementations§

Source§

impl OAuthSession

Source

pub const fn new(tokens: OAuthTokenData, refresh_margin: Duration) -> Self

Creates a new session with the given tokens and refresh margin.

The refresh_margin is how far before expiry the proactive refresh should trigger (e.g. 60 seconds).

Source

pub fn pre_execute_action(&self, now: DateTime<Utc>) -> PreExecuteAction

Decide whether to refresh before making a request.

Injects now for testability.

Source

pub const fn post_execute_action( &self, status: u16, already_refreshed: bool, ) -> PostExecuteAction

Decide what to do after an API response.

already_refreshed prevents infinite refresh loops: if we already refreshed once during this request cycle and still got 401, give up.

Source

pub fn apply_refresh( &mut self, response: &BasicTokenResponse, now: DateTime<Utc>, )

Apply a successful refresh response.

Converts the expires_in duration to an absolute expires_at timestamp using the provided now value. The caller is responsible for persisting to disk and rebuilding the HTTP client.

Source

pub fn apply_external_tokens( &mut self, file_tokens: OAuthTokenData, now: DateTime<Utc>, ) -> bool

Try adopting externally-refreshed tokens (e.g. from a token file written by another process).

Returns true if the file tokens were fresher and were adopted. Returns false (tokens unchanged) if:

  • The file tokens have the same or earlier expiry
  • Either side has no expiry set (None)
  • The file tokens are already expired
Source

pub const fn access_token(&self) -> &AccessToken

Returns a reference to the current access token.

Source

pub const fn refresh_token(&self) -> &RefreshToken

Returns a reference to the current refresh token.

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, 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> 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