actix_web_security/authentication/scheme/bearer/jwt/
user_details_service.rs

1//! The trait definition of a user details service and its clone capabilities for the JWT based authentication.
2
3use async_trait::async_trait;
4use downcast_rs::impl_downcast;
5use downcast_rs::Downcast;
6
7use crate::authentication::scheme::bearer::jwt::Claims;
8use crate::user_details::UserDetails;
9
10/// The trait definition of a user details service for the JWT based authentication.
11/// A user details service is used to load the `UserDetails` for a given boxed `Claims`
12/// object from a datastore. The `Claims` object contains all claims contained in the
13/// decoded JWT.
14#[async_trait]
15pub trait JwtUserDetailsService: Downcast + JwtUserDetailsServiceClone {
16    #[allow(clippy::borrowed_box)]
17    async fn find_user(&self, token: &Box<dyn Claims>) -> Option<Box<dyn UserDetails>>;
18}
19impl_downcast!(JwtUserDetailsService);
20
21pub trait JwtUserDetailsServiceClone: Send + Sync {
22    fn clone_box(&self) -> Box<dyn JwtUserDetailsService>;
23}
24
25/// An user details service must be cloneable, `send` and `sync`.
26/// Therefore it has to implement the `JwtUserDetailsServiceClone` trait to be cloneable as a boxed object.
27impl<U> JwtUserDetailsServiceClone for U
28where
29    U: 'static + JwtUserDetailsService + Clone,
30{
31    fn clone_box(&self) -> Box<dyn JwtUserDetailsService> {
32        Box::new(self.clone())
33    }
34}
35
36impl Clone for Box<dyn JwtUserDetailsService> {
37    fn clone(&self) -> Self {
38        self.clone_box()
39    }
40}