Add prepare_unverified for warp

also Cargo fmt
This commit is contained in:
asonix 2019-12-31 18:23:25 -06:00
parent 58871c67ac
commit 029ada114a
2 changed files with 47 additions and 8 deletions

View file

@ -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<Extract = (Unverified,), Error = Rejection> + 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<T>(
config: Config,
verifier: impl Fn(Unverified) -> Pin<Box<dyn Future<Output = Result<T, ()>> + Send>> + Clone + Send + Sync,
verifier: impl Fn(Unverified) -> Pin<Box<dyn Future<Output = Result<T, ()>> + Send>>
+ Clone
+ Send
+ Sync,
) -> impl Filter<Extract = (T,), Error = Rejection> + Clone
where
T: Send,
@ -32,9 +64,7 @@ async fn do_verify<T>(
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()),
}
}

View file

@ -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)
}