actix_web_security/user_details/
request_extension.rs

1//! Extension function to inject a `UserDetails` object into an endpoint.
2
3use std::future::Future;
4use std::pin::Pin;
5
6use actix_web::dev::{Payload, PayloadStream};
7use actix_web::{Error, FromRequest, HttpRequest};
8
9use crate::authentication::error::error_type::AuthenticationError;
10use crate::user_details::UserDetails;
11
12impl FromRequest for Box<dyn UserDetails> {
13    type Error = Error;
14    type Future = Pin<Box<dyn Future<Output = Result<Self, Error>>>>;
15    type Config = ();
16
17    fn from_request(req: &HttpRequest, _payload: &mut Payload<PayloadStream>) -> Self::Future {
18        let req = req.clone();
19
20        Box::pin(async move {
21            req.extensions()
22                .get::<Box<dyn UserDetails>>()
23                .map(|x| x.clone_box())
24                .ok_or_else(|| AuthenticationError::UsernameNotFound.into())
25        })
26    }
27}