Skip to main content

Authenticator

Struct Authenticator 

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

Owns the in-memory session and sends authenticated requests: bearer header, proactive and reactive refresh (single flight, persisted to the store), cross-process pickup, and the “session lost” state.

Implementations§

Source§

impl Authenticator

Source

pub fn new( config: AuthConfig, store: Arc<dyn SessionStore>, session: Session, clock: Arc<dyn Clock>, ) -> Result<Self, AuthError>

An authenticator for session, which store holds.

Source

pub fn from_store( config: AuthConfig, store: Arc<dyn SessionStore>, clock: Arc<dyn Clock>, ) -> Result<Option<Self>, AuthError>

An authenticator for the session in store, or None when not logged in.

Source

pub fn status(&self) -> Receiver<AuthStatus>

Follows AuthStatus changes.

Source

pub async fn account(&self) -> (u64, String)

The account’s user ID and country code.

Source

pub async fn refresh_client(&self) -> Option<RefreshClient>

The client of this process’s last successful refresh, None before one (AC19).

Source

pub async fn get_json<T: DeserializeOwned>( &self, path: &str, ) -> Result<T, AuthError>

GET {api_base}{path} with the bearer token, decoded as JSON.

As Self::get, then any non-2xx status is an AuthError::Http.

Source

pub async fn get( &self, path: &str, query: &[(&str, &str)], timeout: Option<Duration>, ) -> Result<ApiResponse, AuthError>

GET {api_base}{path}?{query} with the bearer token; the response is returned whatever its status, with its body read.

Refreshes first when the token expires within 60 s, and once more on a 401 before retrying once (AC5); a second 401 is AuthError::Unauthorized. A 401 with subStatus SUB_STATUS_NOT_AVAILABLE is not about the token: it is returned as is, with no refresh and no retry (AC18). Fails fast with AuthError::LoginRequired while the session is lost (AC7, AC9). timeout bounds each HTTP request (none by default).

Source

pub async fn send( &self, method: Method, path: &str, query: &[(&str, &str)], form: Option<&[(&str, &str)]>, headers: &[(&str, &str)], timeout: Option<Duration>, ) -> Result<ApiResponse, AuthError>

{method} {api_base}{path}?{query} with the bearer token, an optional form body (application/x-www-form-urlencoded) and extra request headers; the refresh and 401 rules of Self::get apply, and the response is returned whatever its status, headers included.

Trait Implementations§

Source§

impl Debug for Authenticator

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.

Source§

impl<T> Instrument for T

Source§

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

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

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.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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