2019-12-30 22:53:36 +00:00
|
|
|
use http::{header::HeaderMap, Method};
|
2020-01-01 00:23:25 +00:00
|
|
|
pub use http_signature_normalization_http::{
|
|
|
|
verify::{Algorithm, DeprecatedAlgorithm, Unverified},
|
|
|
|
Config,
|
|
|
|
};
|
2019-12-30 22:53:36 +00:00
|
|
|
use std::{future::Future, pin::Pin};
|
|
|
|
use warp::{path::FullPath, Filter, Rejection};
|
|
|
|
|
2020-01-17 22:31:44 +00:00
|
|
|
#[cfg(feature = "digest")]
|
|
|
|
pub mod digest;
|
|
|
|
|
2020-01-01 00:23:25 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:53:36 +00:00
|
|
|
pub fn verify<T>(
|
|
|
|
config: Config,
|
2020-01-01 00:23:25 +00:00
|
|
|
verifier: impl Fn(Unverified) -> Pin<Box<dyn Future<Output = Result<T, ()>> + Send>>
|
|
|
|
+ Clone
|
|
|
|
+ Send
|
|
|
|
+ Sync,
|
2019-12-30 22:53:36 +00:00
|
|
|
) -> 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) {
|
2020-01-01 00:23:25 +00:00
|
|
|
Ok(v) => verifier(v).await.map_err(|_| warp::reject::not_found()),
|
2019-12-30 22:53:36 +00:00
|
|
|
Err(_) => Err(warp::reject::not_found()),
|
|
|
|
}
|
|
|
|
}
|