Skip to content

Commit

Permalink
fix: Request Id should use header_name as headers key (#893)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
chrislearn and github-actions[bot] authored Sep 7, 2024
1 parent aa9ba27 commit 9c6f867
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
6 changes: 3 additions & 3 deletions crates/csrf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>(CSRF_TOKEN_KEY).map(|v| &**v).ok()
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/extra/src/basic_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>(USERNAME_KEY).map(|v|&**v).ok()
}
}

Expand Down
15 changes: 14 additions & 1 deletion crates/extra/src/request_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>(REQUST_ID_KEY).map(|v|&**v).ok()
}
}

/// A middleware for generate request id.
#[non_exhaustive]
pub struct RequestId {
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions crates/jwt-auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C>(&self) -> Option<&TokenData<C>>
where
Expand All @@ -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::<String>(JWT_AUTH_TOKEN_KEY).map(|v| &**v).ok()
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions examples/csrf-cookie-store/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")));
}

Expand All @@ -35,7 +35,7 @@ pub async fn post_page(req: &mut Request, depot: &mut Depot, res: &mut Response)
}
let data = req.parse_form::<Data>().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));
}
Expand Down
4 changes: 2 additions & 2 deletions examples/csrf-session-store/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")));
}

Expand All @@ -35,7 +35,7 @@ pub async fn post_page(req: &mut Request, depot: &mut Depot, res: &mut Response)
}
let data = req.parse_form::<Data>().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));
}
Expand Down

0 comments on commit 9c6f867

Please sign in to comment.