Skip to main content

potree_auth/user/http/
extractors.rs

1use axum::extract::FromRequestParts;
2use http::request::Parts;
3use tower_sessions::Session;
4
5use super::super::domain::User;
6use crate::common::utils::http::api_error::ApiError;
7
8/// The key to which the logged in user data will be stored in a session.
9pub const USER_SESSION_KEY: &str = "user_session";
10
11pub struct UserExtractor(pub Option<User>);
12
13/// Defines how `axum` should extract a [`User`] from a request.
14///
15/// The user is stored in the web session.
16impl<S> FromRequestParts<S> for UserExtractor
17where
18    S: Send + Sync,
19{
20    type Rejection = ApiError;
21
22    async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
23        let session = Session::from_request_parts(parts, state).await.map_err(|_err| ApiError::ServerConfiguration{message:"could not find tower sessions in request - ensure that tower sessions is in the middleware stack".to_owned()})?;
24        let user = session.get::<User>(USER_SESSION_KEY).await.unwrap();
25
26        Ok(Self(user))
27    }
28}