fedimint_server_ui/
auth.rs

1use axum::extract::FromRequestParts;
2use axum::http::request::Parts;
3use axum::response::Redirect;
4use axum_extra::extract::CookieJar;
5
6use crate::{LOGIN_ROUTE, UiState};
7
8/// Extractor that validates user authentication
9pub struct UserAuth;
10
11impl<Api> FromRequestParts<UiState<Api>> for UserAuth
12where
13    Api: Send + Sync + 'static,
14{
15    type Rejection = Redirect;
16
17    async fn from_request_parts(
18        parts: &mut Parts,
19        state: &UiState<Api>,
20    ) -> Result<Self, Self::Rejection> {
21        let jar = CookieJar::from_request_parts(parts, state)
22            .await
23            .map_err(|_| Redirect::to(LOGIN_ROUTE))?;
24
25        // Check if the auth cookie exists and has the correct value
26        match jar.get(&state.auth_cookie_name) {
27            Some(cookie) if cookie.value() == state.auth_cookie_value => Ok(UserAuth),
28            _ => Err(Redirect::to(LOGIN_ROUTE)),
29        }
30    }
31}