From 9c6f8674e2d58522012b02eb1a1b30a54682ed20 Mon Sep 17 00:00:00 2001 From: Chrislearn Young Date: Sun, 8 Sep 2024 05:02:59 +0800 Subject: [PATCH] fix: Request Id should use header_name as headers key (#893) * fix: Request Id should use header_name as headers key * Format Rust code using rustfmt * test --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- crates/csrf/src/lib.rs | 6 +++--- crates/extra/src/basic_auth.rs | 6 +++--- crates/extra/src/request_id.rs | 15 ++++++++++++++- crates/jwt-auth/src/lib.rs | 6 +++--- examples/csrf-cookie-store/src/main.rs | 4 ++-- examples/csrf-session-store/src/main.rs | 4 ++-- 6 files changed, 27 insertions(+), 14 deletions(-) diff --git a/crates/csrf/src/lib.rs b/crates/csrf/src/lib.rs index 7d02d7bf7..c78b3ba33 100644 --- a/crates/csrf/src/lib.rs +++ b/crates/csrf/src/lib.rs @@ -197,13 +197,13 @@ pub trait CsrfCipher: Send + Sync + 'static { /// Extesion for Depot. pub trait CsrfDepotExt { /// Get csrf token reference from depot. - fn csrf_token(&self) -> Option<&String>; + fn csrf_token(&self) -> Option<&str>; } impl CsrfDepotExt for Depot { #[inline] - fn csrf_token(&self) -> Option<&String> { - self.get(CSRF_TOKEN_KEY).ok() + fn csrf_token(&self) -> Option<&str> { + self.get::(CSRF_TOKEN_KEY).map(|v| &**v).ok() } } diff --git a/crates/extra/src/basic_auth.rs b/crates/extra/src/basic_auth.rs index a114885d3..3280d45e1 100644 --- a/crates/extra/src/basic_auth.rs +++ b/crates/extra/src/basic_auth.rs @@ -45,12 +45,12 @@ pub trait BasicAuthValidator: Send + Sync { /// BasicAuthDepotExt pub trait BasicAuthDepotExt { /// Get basic auth username reference. - fn basic_auth_username(&self) -> Option<&String>; + fn basic_auth_username(&self) -> Option<&str>; } impl BasicAuthDepotExt for Depot { - fn basic_auth_username(&self) -> Option<&String> { - self.get(USERNAME_KEY).ok() + fn basic_auth_username(&self) -> Option<&str> { + self.get::(USERNAME_KEY).map(|v|&**v).ok() } } diff --git a/crates/extra/src/request_id.rs b/crates/extra/src/request_id.rs index 6b4f52e0c..9d7c0eb6d 100644 --- a/crates/extra/src/request_id.rs +++ b/crates/extra/src/request_id.rs @@ -26,6 +26,19 @@ use salvo_core::{async_trait, Depot, FlowCtrl, Handler}; /// Key for incoming flash messages in depot. pub const REQUST_ID_KEY: &str = "::salvo::request_id"; +/// Extesion for Depot. +pub trait RequestIdDepotExt { + /// Get request id reference from depot. + fn csrf_token(&self) -> Option<&str>; +} + +impl RequestIdDepotExt for Depot { + #[inline] + fn csrf_token(&self) -> Option<&str> { + self.get::(REQUST_ID_KEY).map(|v|&**v).ok() + } +} + /// A middleware for generate request id. #[non_exhaustive] pub struct RequestId { @@ -105,7 +118,7 @@ impl IdGenerator for UlidGenerator { #[async_trait] impl Handler for RequestId { async fn handle(&self, req: &mut Request, depot: &mut Depot, _res: &mut Response, _ctrl: &mut FlowCtrl) { - if !self.overwrite && req.headers().contains_key(REQUST_ID_KEY) { + if !self.overwrite && req.headers().contains_key(&self.header_name) { return; } let id = self.generator.generate(req, depot); diff --git a/crates/jwt-auth/src/lib.rs b/crates/jwt-auth/src/lib.rs index 0ff8ac8db..fd295c988 100644 --- a/crates/jwt-auth/src/lib.rs +++ b/crates/jwt-auth/src/lib.rs @@ -212,7 +212,7 @@ pub enum JwtAuthState { /// JwtAuthDepotExt pub trait JwtAuthDepotExt { /// get jwt auth token reference from depot. - fn jwt_auth_token(&self) -> Option<&String>; + fn jwt_auth_token(&self) -> Option<&str>; /// get jwt auth decoded data from depot. fn jwt_auth_data(&self) -> Option<&TokenData> where @@ -225,8 +225,8 @@ pub trait JwtAuthDepotExt { impl JwtAuthDepotExt for Depot { #[inline] - fn jwt_auth_token(&self) -> Option<&String> { - self.get(JWT_AUTH_TOKEN_KEY).ok() + fn jwt_auth_token(&self) -> Option<&str> { + self.get::(JWT_AUTH_TOKEN_KEY).map(|v| &**v).ok() } #[inline] diff --git a/examples/csrf-cookie-store/src/main.rs b/examples/csrf-cookie-store/src/main.rs index 35cc0df27..8d5ec268b 100644 --- a/examples/csrf-cookie-store/src/main.rs +++ b/examples/csrf-cookie-store/src/main.rs @@ -22,7 +22,7 @@ pub async fn home(res: &mut Response) { #[handler] pub async fn get_page(depot: &mut Depot, res: &mut Response) { - let new_token = depot.csrf_token().map(|s| &**s).unwrap_or_default(); + let new_token = depot.csrf_token().unwrap_or_default(); res.render(Text::Html(get_page_html(new_token, ""))); } @@ -35,7 +35,7 @@ pub async fn post_page(req: &mut Request, depot: &mut Depot, res: &mut Response) } let data = req.parse_form::().await.unwrap(); tracing::info!("posted data: {:?}", data); - let new_token = depot.csrf_token().map(|s| &**s).unwrap_or_default(); + let new_token = depot.csrf_token().unwrap_or_default(); let html = get_page_html(new_token, &format!("{data:#?}")); res.render(Text::Html(html)); } diff --git a/examples/csrf-session-store/src/main.rs b/examples/csrf-session-store/src/main.rs index 6a1f18605..791bcd7b5 100644 --- a/examples/csrf-session-store/src/main.rs +++ b/examples/csrf-session-store/src/main.rs @@ -22,7 +22,7 @@ pub async fn home(res: &mut Response) { #[handler] pub async fn get_page(depot: &mut Depot, res: &mut Response) { - let new_token = depot.csrf_token().map(|s| &**s).unwrap_or_default(); + let new_token = depot.csrf_token().unwrap_or_default(); res.render(Text::Html(get_page_html(new_token, ""))); } @@ -35,7 +35,7 @@ pub async fn post_page(req: &mut Request, depot: &mut Depot, res: &mut Response) } let data = req.parse_form::().await.unwrap(); tracing::info!("posted data: {:?}", data); - let new_token = depot.csrf_token().map(|s| &**s).unwrap_or_default(); + let new_token = depot.csrf_token().unwrap_or_default(); let html = get_page_html(new_token, &format!("{data:#?}")); res.render(Text::Html(html)); }