Skip to main content

TokenManager

Struct TokenManager 

Source
pub struct TokenManager { /* private fields */ }
Expand description

Holds the current JWT and keeps it fresh. Share via Arc between connectors.

A background task renews the token ahead of expiry (see refresh_after for the rule). If a renewal fails, the old token stays in use and the task retries with a growing pause (1 s up to 30 s). The task ends when the last Arc is dropped.

The Debug output hides the secret.

See the module docs for an example.

Implementations§

Source§

impl TokenManager

Source

pub async fn start( api: Arc<dyn TokenApi>, secret: impl Into<String>, source_app_id: impl Into<String>, ) -> Result<Arc<Self>, FinamError>

Authenticates once (failure here means a bad secret or no connectivity) and starts the background refresher.

Must be called inside a Tokio runtime, because it spawns the refresher task.

§Errors

A failure of TokenDetails is not an error: the lifetime then comes from the JWT claims, or from ASSUMED_TTL.

Source

pub fn current(&self) -> Arc<str>

Current JWT (what goes into the authorization metadata).

Cheap: no network. Call it for every request instead of keeping the value.

Source

pub fn info(&self) -> TokenInfo

Details of the current token: lifetime, accounts, read-only flag. No network.

Source

pub fn account_ids(&self) -> Vec<String>

Accounts available to this secret (TokenDetails.account_ids).

Empty when TokenDetails failed or listed none.

Source

pub fn generation(&self) -> u64

Number of successful issues so far (1 after TokenManager::start).

Changes every time the token is replaced, so it tells a reader whether its copy is stale.

Source

pub fn request_refresh(&self)

Asks the refresher to renew now, e.g. after UNAUTHENTICATED. Non-blocking.

The renewal runs in the background. Use TokenManager::refresh to wait for it.

Source

pub async fn refresh(&self) -> Result<(), FinamError>

Renews the token immediately.

If another task renewed the token while this one waited for the lock, nothing more is sent and the call returns Ok. So parallel callers cause one Auth, not many.

§Errors

The same as TokenManager::start without the empty-secret check. The old token stays in use when the renewal fails.

Trait Implementations§

Source§

impl Debug for TokenManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more