|
1 | 1 | use crate::error as session_error; |
2 | | -use crate::error::Result; |
| 2 | +use crate::error::{Error, Result}; |
3 | 3 | use crate::session::{ |
4 | 4 | DFSessionId, SESSION_ID_COOKIE_NAME, SessionStore, extract_token_from_cookie, |
5 | 5 | }; |
6 | | -use axum::extract::{Request, State}; |
| 6 | +use axum::extract::{FromRequestParts, Request, State}; |
| 7 | +use axum::http::{HeaderMap, HeaderName, request::Parts}; |
7 | 8 | use axum::middleware::Next; |
8 | 9 | use axum::response::IntoResponse; |
9 | 10 | use http::header::SET_COOKIE; |
10 | | -use http::{HeaderMap, HeaderName}; |
11 | 11 | use snafu::ResultExt; |
12 | 12 | use tower_sessions::cookie::{Cookie, SameSite}; |
13 | 13 |
|
| 14 | +#[derive(Debug, Clone)] |
| 15 | +pub struct Host(pub String); |
| 16 | + |
| 17 | +impl<S> FromRequestParts<S> for Host |
| 18 | +where |
| 19 | + S: Send + Sync, |
| 20 | +{ |
| 21 | + type Rejection = Error; |
| 22 | + |
| 23 | + #[allow(clippy::unwrap_used)] |
| 24 | + async fn from_request_parts( |
| 25 | + req: &mut Parts, |
| 26 | + state: &S, |
| 27 | + ) -> std::result::Result<Self, Self::Rejection> { |
| 28 | + let headers = HeaderMap::from_request_parts(req, state) |
| 29 | + .await |
| 30 | + .map_err(|err| match err {}) |
| 31 | + .unwrap(); // unwrap on Infallibe error is safe |
| 32 | + let host = headers.get("host"); |
| 33 | + let host = host.and_then(|host| host.to_str().ok()); |
| 34 | + if let Some(host) = host { |
| 35 | + Ok(Self(host.to_string())) |
| 36 | + } else { |
| 37 | + session_error::MissingHostSnafu.fail() |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
14 | 42 | #[allow(clippy::unwrap_used, clippy::cognitive_complexity)] |
15 | 43 | pub async fn propagate_session_cookie( |
16 | 44 | State(state): State<SessionStore>, |
|
0 commit comments