actix_web_security/authentication/scheme/
authentication_provider.rs

1//! The trait definition of authentication providers.
2
3use async_trait::async_trait;
4
5use crate::authentication::error::error_type::AuthenticationError;
6use crate::authentication::scheme::authentication::Authentication;
7use crate::user_details::UserDetails;
8
9/// An authentication provider executes the authentication for an `Authentication` object and returns
10/// either a object of `UserDetails` (implementation of the marker trait) or an `AuthenticationError`.
11#[async_trait]
12pub trait AuthenticationProvider: AuthenticationProviderClone {
13    #[allow(clippy::borrowed_box)]
14    async fn authenticate(
15        &self,
16        authentication: &Box<dyn Authentication>,
17    ) -> Result<Box<dyn UserDetails>, AuthenticationError>;
18}
19
20/// An authentication provider must be cloneable, `send` and `sync`.
21/// Therefore it has to implement the `AuthenticationProviderClone` trait to be cloneable as a boxed object.
22pub trait AuthenticationProviderClone: Send + Sync {
23    fn clone_box(&self) -> Box<dyn AuthenticationProvider>;
24}
25
26impl<U> AuthenticationProviderClone for U
27where
28    U: 'static + AuthenticationProvider + Clone,
29{
30    fn clone_box(&self) -> Box<dyn AuthenticationProvider> {
31        Box::new(self.clone())
32    }
33}
34
35impl Clone for Box<dyn AuthenticationProvider> {
36    fn clone(&self) -> Self {
37        self.clone_box()
38    }
39}