pub struct TokenManager { /* private fields */ }Expand description
Logs in and keeps the token fresh.
Refresh is single-flighted: concurrent callers that all observe a stale token
share one RefreshToken round-trip. Without that, N tasks would each present
the same single-use refresh token, one would win, and the rest would be told
their credential is invalid.
Implementations§
Source§impl TokenManager
impl TokenManager
Sourcepub const DEFAULT_SKEW: Duration
pub const DEFAULT_SKEW: Duration
Default refresh skew: refresh this long before actual expiry.
pub fn new(channel: Channel, meta: Metadata) -> Self
pub fn with_skew(channel: Channel, meta: Metadata, skew: Duration) -> Self
Sourcepub async fn login(
&self,
username: impl Into<String>,
password: impl Into<String>,
) -> Result<Token, Status>
pub async fn login( &self, username: impl Into<String>, password: impl Into<String>, ) -> Result<Token, Status>
Authenticate with username and password, storing the result.
A transient failure (broker unreachable or restarting, overloaded, reply
lost) is retried, up to LOGIN_ATTEMPTS attempts in all with a
jittered, doubling wait between them. A refused credential is final.
The username and password are kept in memory so a session the broker
later loses can sign in again (see TokenManager::reject).
Sourcepub async fn access_token(&self) -> Result<String, Status>
pub async fn access_token(&self) -> Result<String, Status>
The access token, refreshing first if it is stale or the broker refused it.
Sourcepub async fn refresh(&self) -> Result<Token, Status>
pub async fn refresh(&self) -> Result<Token, Status>
Force a refresh. Holding the token lock across the RPC is what makes this single-flighted: a second caller waits and then observes the new token rather than presenting the spent one.
A token the broker refused (see TokenManager::reject) is renewed even
though it has not expired: refresh, else sign in again with the stored
login. While such a renewal keeps failing it is retried at most once per
backoff interval (1s doubling to 60s); calls in between are refused
locally with the last failure.
Sourcepub async fn reject(&self, access_token: &str) -> bool
pub async fn reject(&self, access_token: &str) -> bool
Report that the broker refused access_token (UNAUTHENTICATED) before
its local expiry. Returns true when a repeat of the refused call can
carry a different, renewed token: the token is this manager’s current
one (or was already marked by a concurrent call), and it was not itself
renewed less than 5s ago. The next TokenManager::access_token then
renews it.
Only for a call that carried this manager’s token and is not an
AuthnService call (a refusal there is about the credential in the
request, not the bearer). crate::UdbClient::with_token_manager calls
it for you.
Sourcepub async fn authenticated_metadata(&self) -> Result<Metadata, Status>
pub async fn authenticated_metadata(&self) -> Result<Metadata, Status>
Metadata carrying the current access token, ready for a data-plane client.
Trait Implementations§
Source§impl Clone for TokenManager
impl Clone for TokenManager
Source§impl Debug for TokenManager
Hand-written rather than derived: a derived Debug would print the stored
access and refresh tokens, and this type is exactly the sort of thing that
ends up in a tracing field or a panic message.
impl Debug for TokenManager
Hand-written rather than derived: a derived Debug would print the stored
access and refresh tokens, and this type is exactly the sort of thing that
ends up in a tracing field or a panic message.
Auto Trait Implementations§
impl !RefUnwindSafe for TokenManager
impl !UnwindSafe for TokenManager
impl Forget for TokenManager
impl Freeze for TokenManager
impl Move for TokenManager
impl Send for TokenManager
impl Sync for TokenManager
impl Unpin for TokenManager
impl UnsafeUnpin for TokenManager
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