Struct axum_login::AuthSession

source ยท
pub struct AuthSession<Backend: AuthnBackend> {
    pub user: Option<Backend::User>,
    pub backend: Backend,
    /* private fields */
}
Expand description

A specialized session for identification, authentication, and authorization of users associated with a backend.

The session is generic over some backend which implements AuthnBackend. The backend may also implement AuthzBackend, in which case it will also supply authorization methods.

Methods for authenticating the session and logging a user in are provided.

Generally this session will be used in the context of some authentication workflow, for example via a frontend login form. There a user would provide their credentials, such as username and password, and via the backend the session would authenticate those credentials.

Once the supplied credentials have been authenticated, a user will be returned. In the case the credentials are invalid, no user will be returned. When we do have a user, itโ€™s then possible to set the state of the session so that the user is logged in.

Fieldsยง

ยงuser: Option<Backend::User>

The user associated by the backend. None when not logged in.

ยงbackend: Backend

The authentication and authorization backend.

Implementationsยง

sourceยง

impl<Backend: AuthnBackend> AuthSession<Backend>

source

pub async fn authenticate( &self, creds: Backend::Credentials ) -> Result<Option<Backend::User>, Error<Backend>>

Verifies the provided credentials via the backend returning the authenticated user if valid and otherwise None.

source

pub async fn login( &mut self, user: &Backend::User ) -> Result<(), Error<Backend>>

Updates the session such that the user is logged in.

source

pub async fn logout(&mut self) -> Result<Option<Backend::User>, Error<Backend>>

Updates the session such that the user is logged out.

Trait Implementationsยง

sourceยง

impl<Backend: Clone + AuthnBackend> Clone for AuthSession<Backend>
where Backend::User: Clone,

sourceยง

fn clone(&self) -> AuthSession<Backend>

Returns a copy of the value. Read more
1.0.0 ยท sourceยง

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

Performs copy-assignment from source. Read more
sourceยง

impl<Backend: Debug + AuthnBackend> Debug for AuthSession<Backend>
where Backend::User: Debug,

sourceยง

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

Formats the value using the given formatter. Read more
sourceยง

impl<S, Backend> FromRequestParts<S> for AuthSession<Backend>
where S: Send + Sync, Backend: AuthnBackend + Send + Sync + 'static,

ยง

type Rejection = (StatusCode, &'static str)

If the extractor fails itโ€™ll use this โ€œrejectionโ€ type. A rejection is a kind of error that can be converted into a response.
sourceยง

fn from_request_parts<'life0, 'life1, 'async_trait>( parts: &'life0 mut Parts, _state: &'life1 S ) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Perform the extraction.

Auto Trait Implementationsยง

ยง

impl<Backend> !RefUnwindSafe for AuthSession<Backend>

ยง

impl<Backend> Send for AuthSession<Backend>

ยง

impl<Backend> Sync for AuthSession<Backend>

ยง

impl<Backend> Unpin for AuthSession<Backend>
where Backend: Unpin, <<Backend as AuthnBackend>::User as AuthUser>::Id: Unpin, <Backend as AuthnBackend>::User: Unpin,

ยง

impl<Backend> !UnwindSafe for AuthSession<Backend>

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> FromRef<T> for T
where T: Clone,

ยง

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
ยง

impl<S, T> FromRequest<S, ViaParts> for T
where S: Send + Sync, T: FromRequestParts<S>,

ยง

type Rejection = <T as FromRequestParts<S>>::Rejection

If the extractor fails itโ€™ll use this โ€œrejectionโ€ type. A rejection is a kind of error that can be converted into a response.
ยง

fn from_request<'life0, 'async_trait>( req: Request<Body>, state: &'life0 S ) -> Pin<Box<dyn Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>> + Send + 'async_trait>>
where 'life0: 'async_trait, T: 'async_trait,

Perform the extraction.
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> ToOwned for T
where T: Clone,

ยง

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>,

ยง

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>,

ยง

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