pub struct OAuthTokenProvider { /* private fields */ }Expand description
Manages OAuth access tokens, including caching and automatic refresh.
This provider implements thread-safe token management with automatic expiration handling. It uses a double-checked locking pattern to minimize lock contention while ensuring only one thread fetches a new token at a time.
§Example
use crate::OAuthTokenProvider;
let provider = OAuthTokenProvider::new("client_id".to_string(), "client_secret".to_string());
// Get or fetch a token (sync)
let token = provider.get_or_fetch(|| {
// Your token fetching logic here
// Returns (access_token, expires_in_seconds)
Ok(("token".to_string(), Some(3600)))
})?;
// Get or fetch a token (async)
let token = provider.get_or_fetch_async(|| async {
// Your async token fetching logic here
Ok(("token".to_string(), Some(3600)))
}).await?;Implementations§
Source§impl OAuthTokenProvider
impl OAuthTokenProvider
Sourcepub fn new(client_id: String, client_secret: String) -> Self
pub fn new(client_id: String, client_secret: String) -> Self
Creates a new OAuthTokenProvider with the given credentials.
Sourcepub fn client_secret(&self) -> &str
pub fn client_secret(&self) -> &str
Returns the client secret.
Sourcepub fn set_token(&self, access_token: String, expires_in: u64)
pub fn set_token(&self, access_token: String, expires_in: u64)
Sets the cached access token and its expiration time.
The expires_in parameter is the number of seconds until the token expires.
A buffer is applied to refresh before actual expiration.
Sourcepub fn get_token(&self) -> Option<String>
pub fn get_token(&self) -> Option<String>
Returns the cached access token if it’s still valid.
Returns None if the token is expired or not set.
Sourcepub fn get_or_fetch<F, E>(&self, fetch_func: F) -> Result<String, E>
pub fn get_or_fetch<F, E>(&self, fetch_func: F) -> Result<String, E>
Returns a valid token, fetching a new one if necessary (synchronous version).
The fetch_func is called at most once even if multiple threads call get_or_fetch
concurrently when the token is expired. It should return (access_token, expires_in_seconds).
§Arguments
fetch_func- A function that fetches a new token. ReturnsResult<(String, u64), E>where the tuple contains (access_token, expires_in_seconds).
§Example
let token = provider.get_or_fetch(|| {
// Call your OAuth endpoint here (sync)
let response = auth_client.get_token(&provider.client_id(), &provider.client_secret())?;
Ok((response.access_token, response.expires_in.unwrap_or(3600)))
})?;Sourcepub async fn get_or_fetch_async<F, Fut, E>(
&self,
fetch_func: F,
) -> Result<String, E>
pub async fn get_or_fetch_async<F, Fut, E>( &self, fetch_func: F, ) -> Result<String, E>
Returns a valid token, fetching a new one if necessary (async version).
This is the async version of get_or_fetch for use with async token fetching.
The fetch_func is called at most once even if multiple tasks call get_or_fetch_async
concurrently when the token is expired.
§Arguments
fetch_func- An async function that fetches a new token. ReturnsResult<(String, u64), E>where the tuple contains (access_token, expires_in_seconds).
§Example
let token = provider.get_or_fetch_async(|| async {
// Call your OAuth endpoint here (async)
let response = auth_client.get_token(&provider.client_id(), &provider.client_secret()).await?;
Ok((response.access_token, response.expires_in.unwrap_or(3600)))
}).await?;Sourcepub fn needs_refresh(&self) -> bool
pub fn needs_refresh(&self) -> bool
Returns true if the token needs to be refreshed.
This is useful for proactively refreshing tokens before they expire.