Skip to main content

AuthClient

Struct AuthClient 

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

The unauthenticated entry point: turns credentials (or a persisted session) into an authenticated Session.

Implementations§

Source§

impl AuthClient

Source

pub fn new(base_url: &str) -> Result<Self, AuthError>

Build a client against the auth base URL (e.g. https://api.example.com/auth).

Source

pub fn with_cohort_hash(self, cohort_hash: String) -> Self

Attach the advisory device-cohort hash that rides every subsequent session-creation request (login/register) from this client.

The value is a lowercase-hex SHA-256 digest computed by the platform app from its primary identifier and the account’s user_id (crate::cohort::compute_cohort_hash). It is a grouping aid only — the server never reads it for any authorization decision. Not setting it (the default) is fully legal: nothing is sent and the server behaves identically.

Source

pub async fn login( &self, email: &str, password: &str, ) -> Result<LoginOutcome, AuthError>

Authenticate with email + password, returning an authenticated Session.

If a cohort hash is configured (with_cohort_hash) it rides the request body; otherwise the field is omitted entirely.

Source

pub async fn sign_in_options(&self) -> Result<SignInOptions, AuthError>

Read how this server signs people in from its unauthenticated .well-known/capsule/server-info record (one generated call, nothing sent but the handshake).

§Errors

AuthError::Network when the server cannot be reached, AuthError::Unexpected for a status the record does not answer with, and AuthError::MalformedResponse for a body that is not the record. A caller treats every one as “unknown” rather than as a refusal: the record is advisory.

Source

pub async fn begin_oidc_login( &self, redirect_uri: &str, ) -> Result<OidcAuthorization, AuthError>

Begin a sign-in through the server’s identity provider.

redirect_uri is where the provider will send the person back — this client’s own callback, which the server admits if it is the deployment’s configured one or a loopback IP literal on any port (the shape a CLI’s or desktop app’s listener has). What comes back is where to send the person, the state the redirect will echo, and the secret binding this client keeps: a fresh one per ceremony, of which only the hash is sent.

§Errors

AuthError::OidcNotConfigured when the server has no provider, AuthError::OidcRedirectInvalid when it refuses the redirect.

Source

pub async fn complete_oidc_login( &self, pending: &OidcAuthorization, redirect_state: &str, code: &str, ) -> Result<LoginOutcome, AuthError>

Finish a sign-in through the identity provider with what its redirect carried.

Answers a LoginOutcome exactly as login does — a session, or a second-factor challenge for an account that enrolled one — and the configured cohort hash rides this request, because this is the one that opens the session.

pending is what begin_oidc_login answered; redirect_state and code are what the provider’s redirect carried. A redirect whose state is not the one this client began is refused here, before anything is sent: it is someone else’s ceremony arriving at this client (RFC 6749 §10.12). The server makes the same refusal on the binding, which is what holds when a client skips this check.

§Errors

AuthError::OidcRejected for a redirect carrying another ceremony’s state, a spent or expired state, a refused exchange or a refused ID token (start again); AuthError::OidcAddressTaken when the provider asserted an address that already has a local account.

Source

pub async fn begin_oidc_device_login( &self, ) -> Result<OidcDeviceAuthorization, AuthError>

Begin a sign-in through the server’s identity provider for a client with no browser of its own (RFC 8628, issue #635).

What comes back is what to show the person and the handle to poll with, bound to this client by a fresh secret of which only the hash is sent.

§Errors

AuthError::OidcNotConfigured when the server has no provider, AuthError::OidcDeviceUnsupported when its provider does not offer the grant, AuthError::RateLimited when too many were begun.

Source

pub async fn poll_oidc_device_login( &self, pending: &OidcDeviceAuthorization, ) -> Result<OidcDevicePoll, AuthError>

Poll a device sign-in once (RFC 8628, issue #635).

OidcDevicePoll::Pending and OidcDevicePoll::SlowDown are RFC 8628’s “keep polling”; OidcDevicePoll::Complete is exactly what login answers — a session, or a second-factor challenge — and the configured cohort hash rides this request, because it is the one that opens the session. Waiting is the caller’s: this sends one request and returns.

§Errors

AuthError::OidcDeviceDenied when the person declined, AuthError::OidcDeviceExpired when the ceremony is over, AuthError::OidcRejected for a refused exchange or ID token, AuthError::OidcAddressTaken as on the callback.

Source

pub async fn verify_second_factor( &self, mfa_token: &SecretString, totp_code: &str, ) -> Result<Session, AuthError>

Complete a sign-in with the code an authenticator app is showing.

mfa_token is the challenge LoginOutcome::SecondFactorRequired carried. It is good once and for five minutes; a code is good once, full stop, so a retry after a wrong code re-uses the same challenge and a retry after a right one has to start from the password.

Source

pub async fn register( &self, email: &str, password: &str, ) -> Result<Session, AuthError>

Create a new account and return an authenticated Session (the server issues tokens on registration). The configured cohort hash rides the body under the same advisory contract as login.

Source

pub fn resume(&self, persisted: PersistedSession) -> Result<Session, AuthError>

Rebuild a Session from a persisted token set (e.g. loaded from the platform keychain at launch). The next request pre-flight-refreshes if the restored access token is already stale.

Trait Implementations§

Source§

impl Clone for AuthClient

Source§

fn clone(&self) -> AuthClient

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> CompatExt for T

§

fn compat(self) -> Compat<T>
where T: Sized,

Applies the [Compat] adapter by value. Read more
§

fn compat_ref(&self) -> Compat<&T>

Applies the [Compat] adapter by shared reference. Read more
§

fn compat_mut(&mut self) -> Compat<&mut T>

Applies the [Compat] adapter by mutable reference. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, UT> HandleAlloc<UT> for T
where T: Send + Sync,

§

fn new_handle(value: Arc<T>) -> Handle

Create a new handle for an Arc value Read more
§

unsafe fn clone_handle(handle: Handle) -> Handle

Clone a handle Read more
§

unsafe fn consume_handle(handle: Handle) -> Arc<T> ⓘ

Consume a handle, getting back the initial Arc<> Read more
§

unsafe fn get_arc(handle: Handle) -> Arc<Self> ⓘ

Get a clone of the Arc<> using a “borrowed” handle. Read more
§

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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T> MaybeSync for T
where T: Sync + ?Sized,

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

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
§

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
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

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