mirror of
https://git.asonix.dog/asonix/http-signature-normalization.git
synced 2024-11-25 02:41:00 +00:00
Add warp
This commit is contained in:
parent
0673b818e9
commit
08be4047ef
3 changed files with 55 additions and 1 deletions
|
@ -13,8 +13,9 @@ edition = "2018"
|
|||
|
||||
[workspace]
|
||||
members = [
|
||||
"http-signature-normalization-http",
|
||||
"http-signature-normalization-actix",
|
||||
"http-signature-normalization-http",
|
||||
"http-signature-normalization-warp",
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
|
|
13
http-signature-normalization-warp/Cargo.toml
Normal file
13
http-signature-normalization-warp/Cargo.toml
Normal file
|
@ -0,0 +1,13 @@
|
|||
[package]
|
||||
name = "http-signature-normalization-warp"
|
||||
version = "0.1.0"
|
||||
authors = ["asonix <asonix@asonix.dog>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
http = "0.2.0"
|
||||
http-signature-normalization-http = { version = "0.2.0", path = "../http-signature-normalization-http" }
|
||||
warp = { git = "https://github.com/seanmonstar/warp" }
|
||||
futures = "0.3.1"
|
40
http-signature-normalization-warp/src/lib.rs
Normal file
40
http-signature-normalization-warp/src/lib.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
use http::{header::HeaderMap, Method};
|
||||
pub use http_signature_normalization_http::{Config, verify::Unverified};
|
||||
use std::{future::Future, pin::Pin};
|
||||
use warp::{path::FullPath, Filter, Rejection};
|
||||
|
||||
pub fn verify<T>(
|
||||
config: Config,
|
||||
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,
|
||||
{
|
||||
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, headers, method, path, query| {
|
||||
do_verify(config, headers, method, path, query, verifier.clone())
|
||||
})
|
||||
}
|
||||
|
||||
async fn do_verify<T>(
|
||||
config: Config,
|
||||
headers: HeaderMap,
|
||||
method: Method,
|
||||
path: FullPath,
|
||||
query: String,
|
||||
verifier: impl Fn(Unverified) -> Pin<Box<dyn Future<Output = Result<T, ()>> + Send>>,
|
||||
) -> Result<T, warp::Rejection> {
|
||||
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()),
|
||||
Err(_) => Err(warp::reject::not_found()),
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue