From 029ada114ac3808343c506a01ab8e1ee71a20fcd Mon Sep 17 00:00:00 2001 From: asonix Date: Tue, 31 Dec 2019 18:23:25 -0600 Subject: [PATCH] Add prepare_unverified for warp also Cargo fmt --- http-signature-normalization-warp/src/lib.rs | 40 +++++++++++++++++--- src/verify.rs | 15 ++++++-- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/http-signature-normalization-warp/src/lib.rs b/http-signature-normalization-warp/src/lib.rs index 92492ca..38970be 100644 --- a/http-signature-normalization-warp/src/lib.rs +++ b/http-signature-normalization-warp/src/lib.rs @@ -1,11 +1,43 @@ use http::{header::HeaderMap, Method}; -pub use http_signature_normalization_http::{Config, verify::Unverified}; +pub use http_signature_normalization_http::{ + verify::{Algorithm, DeprecatedAlgorithm, Unverified}, + Config, +}; use std::{future::Future, pin::Pin}; use warp::{path::FullPath, Filter, Rejection}; +pub fn prepare_unverified( + config: Config, +) -> impl Filter + Clone { + warp::any() + .map(move || config.clone()) + .and(warp::header::headers_cloned()) + .and(warp::method()) + .and(warp::path::full()) + .and(warp::query::raw()) + .and_then( + move |config: Config, + headers: HeaderMap, + method: Method, + path: FullPath, + query: String| { + let path_and_query = format!("{}?{}", path.as_str(), query).parse().unwrap(); + + async move { + config + .begin_verify(&method, Some(&path_and_query), headers) + .map_err(|_| warp::reject::not_found()) + } + }, + ) +} + pub fn verify( config: Config, - verifier: impl Fn(Unverified) -> Pin> + Send>> + Clone + Send + Sync, + verifier: impl Fn(Unverified) -> Pin> + Send>> + + Clone + + Send + + Sync, ) -> impl Filter + Clone where T: Send, @@ -32,9 +64,7 @@ async fn do_verify( let path_and_query = format!("{}?{}", path.as_str(), query).parse().unwrap(); match config.begin_verify(&method, Some(&path_and_query), headers) { - Ok(v) => verifier(v) - .await - .map_err(|_| warp::reject::not_found()), + Ok(v) => verifier(v).await.map_err(|_| warp::reject::not_found()), Err(_) => Err(warp::reject::not_found()), } } diff --git a/src/verify.rs b/src/verify.rs index 3c8f38a..86f4fa8 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -347,7 +347,10 @@ mod tests { let time1 = Utc::now().timestamp(); let time2 = Utc::now().timestamp(); - let h = format!(r#"Signature keyId="my-key-id",algorithm="hs2019",created="{}",expires="{}",headers="(request-target) (created) (expires) date content-type",signature="blah blah blah""#, time1, time2); + let h = format!( + r#"Signature keyId="my-key-id",algorithm="hs2019",created="{}",expires="{}",headers="(request-target) (created) (expires) date content-type",signature="blah blah blah""#, + time1, time2 + ); parse_signature(&h) } @@ -357,7 +360,10 @@ mod tests { let time1 = Utc::now().timestamp(); let time2 = Utc::now().timestamp(); - let h = format!(r#"Signature keyId="my-key-id",algorithm="rsa-sha256",created="{}",expires="{}",signature="blah blah blah""#, time1, time2); + let h = format!( + r#"Signature keyId="my-key-id",algorithm="rsa-sha256",created="{}",expires="{}",signature="blah blah blah""#, + time1, time2 + ); parse_signature(&h) } @@ -366,7 +372,10 @@ mod tests { fn parses_header_succesfully_3() { let time1 = Utc::now().timestamp(); - let h = format!(r#"Signature keyId="my-key-id",algorithm="rsa-sha256",created="{}",headers="(request-target) (created) date content-type",signature="blah blah blah""#, time1); + let h = format!( + r#"Signature keyId="my-key-id",algorithm="rsa-sha256",created="{}",headers="(request-target) (created) date content-type",signature="blah blah blah""#, + time1 + ); parse_signature(&h) }