Trait axum_login::AuthnBackend
source · pub trait AuthnBackend: Clone + Send + Sync {
type User: AuthUser;
type Credentials: Send + Sync;
type Error: Error + Send + Sync;
// Required methods
fn authenticate<'life0, 'async_trait>(
&'life0 self,
creds: Self::Credentials
) -> Pin<Box<dyn Future<Output = Result<Option<Self::User>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn get_user<'life0, 'life1, 'async_trait>(
&'life0 self,
user_id: &'life1 UserId<Self>
) -> Pin<Box<dyn Future<Output = Result<Option<Self::User>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
}
Expand description
A backend which can authenticate users.
Backends must implement:
authenticate
, a method for authenticating users with credentials and,get_user
a method for getting a user by an identifying feature.
With these two methods, users may be authenticated and later retrieved via the backend.
Examples
use std::collections::HashMap;
use async_trait::async_trait;
use axum_login::{AuthUser, AuthnBackend, UserId};
#[derive(Debug, Clone)]
struct User {
id: i64,
pw_hash: Vec<u8>,
}
impl AuthUser for User {
type Id = i64;
fn id(&self) -> Self::Id {
self.id
}
fn session_auth_hash(&self) -> &[u8] {
&self.pw_hash
}
}
#[derive(Clone)]
struct Backend {
users: HashMap<i64, User>,
}
#[derive(Clone)]
struct Credentials {
user_id: i64,
}
#[async_trait]
impl AuthnBackend for Backend {
type User = User;
type Credentials = Credentials;
type Error = std::convert::Infallible;
async fn authenticate(
&self,
Credentials { user_id }: Self::Credentials,
) -> Result<Option<Self::User>, Self::Error> {
Ok(self.users.get(&user_id).cloned())
}
async fn get_user(
&self,
user_id: &UserId<Self>,
) -> Result<Option<Self::User>, Self::Error> {
Ok(self.users.get(user_id).cloned())
}
}
Required Associated Types§
sourcetype Credentials: Send + Sync
type Credentials: Send + Sync
Credential type used for authentication.
Required Methods§
sourcefn authenticate<'life0, 'async_trait>(
&'life0 self,
creds: Self::Credentials
) -> Pin<Box<dyn Future<Output = Result<Option<Self::User>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn authenticate<'life0, 'async_trait>(
&'life0 self,
creds: Self::Credentials
) -> Pin<Box<dyn Future<Output = Result<Option<Self::User>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Authenticates the given credentials with the backend.
sourcefn get_user<'life0, 'life1, 'async_trait>(
&'life0 self,
user_id: &'life1 UserId<Self>
) -> Pin<Box<dyn Future<Output = Result<Option<Self::User>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get_user<'life0, 'life1, 'async_trait>(
&'life0 self,
user_id: &'life1 UserId<Self>
) -> Pin<Box<dyn Future<Output = Result<Option<Self::User>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Gets the user by provided ID from the backend.
Object Safety§
This trait is not object safe.