Accessing Request body as bytes in custom middleware #2265
-
I am writing a custom middleware for verifying an hmac signature header by reproducing the signature from a secret and the body of the request being sent. Below is the code I am using and would like to pass to The issue I am running into is that I need to access the body of the request as bytes or a String however, the generic async fn verify_orb_signature<B>(
headers: HeaderMap,
request: Request<B>,
next: Next<B>,
) -> Result<Response, StatusCode> {
let (orb_timestamp, orb_signature) = match (
headers.get("x-orb-timestamp"),
headers.get("x-orb-signature"),
) {
(Some(ts), Some(sig)) => (ts.to_str().unwrap(), sig.as_bytes()),
_ => return Err(StatusCode::BAD_REQUEST),
};
let secret = std::env::var("ORB_WEBHOOK_SECRET").unwrap();
// let body = request.body();
// let verified = webhooks::verify_signature(orb_signature, orb_timestamp, secret, body);
// if !verified {
// return Err(StatusCode::BAD_REQUEST);
// }
let response = next.run(request).await;
Ok(response)
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Have a look at https://github.com/tokio-rs/axum/blob/axum-v0.6.20/examples/consume-body-in-extractor-or-middleware/src/main.rs |
Beta Was this translation helpful? Give feedback.
Have a look at https://github.com/tokio-rs/axum/blob/axum-v0.6.20/examples/consume-body-in-extractor-or-middleware/src/main.rs