pub struct TokenProvider { /* private fields */ }Expand description
Fetches and caches an OAuth2 bearer token via the client-credentials grant, refreshing it shortly before expiry. Cloning shares the cache.
Implementations§
Source§impl TokenProvider
impl TokenProvider
Sourcepub fn new(config: OidcConfig) -> Self
pub fn new(config: OidcConfig) -> Self
Create a provider for the given OIDC configuration.
Token fetches are bounded by a per-request timeout (see
Self::token), so a hung token endpoint can never block callers
indefinitely (the fetch holds the cache lock while in flight).
Sourcepub async fn invalidate(&self)
pub async fn invalidate(&self)
Drop the cached token so the next Self::token call fetches a fresh
one. Call this after a server rejects a token as expired/invalid so the
client can self-heal instead of replaying the stale token.
Sourcepub async fn token(&self) -> Result<String>
pub async fn token(&self) -> Result<String>
Return a valid bearer token, fetching or refreshing if the cached one is absent or within the refresh-skew window of expiry.
Concurrent callers that arrive during a refresh serialize behind a single in-flight fetch (single-flight de-duplication), bounded by the HTTP client’s timeout — a slow IdP therefore stalls concurrent token consumers for at most that timeout rather than triggering a fetch storm.
§Errors
Returns Error::Auth if the endpoint rejects the credentials
(401/403, e.g. invalid_client); Error::Http for other non-success
statuses (5xx/429 stay retriable); Error::Connection if the endpoint
is unreachable or the fetch times out; Error::Json if the response
cannot be parsed.
Trait Implementations§
Source§impl Clone for TokenProvider
impl Clone for TokenProvider
Source§fn clone(&self) -> TokenProvider
fn clone(&self) -> TokenProvider
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TokenProvider
impl Debug for TokenProvider
Source§impl TokenSource for TokenProvider
Lets a TokenProvider back the SDK’s shared canton_core::Auth without
canton-core depending on this crate: Config::with_oidc(provider) stores
it as an Arc<dyn TokenSource>.
impl TokenSource for TokenProvider
Lets a TokenProvider back the SDK’s shared canton_core::Auth without
canton-core depending on this crate: Config::with_oidc(provider) stores
it as an Arc<dyn TokenSource>.
Auto Trait Implementations§
impl !RefUnwindSafe for TokenProvider
impl !UnwindSafe for TokenProvider
impl Freeze for TokenProvider
impl Send for TokenProvider
impl Sync for TokenProvider
impl Unpin for TokenProvider
impl UnsafeUnpin for TokenProvider
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request