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: OAuthTokenDataCurrent token data. Public for persistence by the I/O layer.
Implementations§
Source§impl OAuthSession
impl OAuthSession
Sourcepub const fn new(tokens: OAuthTokenData, refresh_margin: Duration) -> Self
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).
Sourcepub fn pre_execute_action(&self, now: DateTime<Utc>) -> PreExecuteAction
pub fn pre_execute_action(&self, now: DateTime<Utc>) -> PreExecuteAction
Decide whether to refresh before making a request.
Injects now for testability.
Sourcepub const fn post_execute_action(
&self,
status: u16,
already_refreshed: bool,
) -> PostExecuteAction
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.
Sourcepub fn apply_refresh(
&mut self,
response: &BasicTokenResponse,
now: DateTime<Utc>,
)
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.
Sourcepub fn apply_external_tokens(
&mut self,
file_tokens: OAuthTokenData,
now: DateTime<Utc>,
) -> bool
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
Sourcepub const fn access_token(&self) -> &AccessToken
pub const fn access_token(&self) -> &AccessToken
Returns a reference to the current access token.
Sourcepub const fn refresh_token(&self) -> &RefreshToken
pub const fn refresh_token(&self) -> &RefreshToken
Returns a reference to the current refresh token.