http-signature-normalization/http-signature-normalization-actix/src/digest/sign.rs

86 lines
2.4 KiB
Rust
Raw Normal View History

2019-09-13 01:12:35 +00:00
use actix_web::{
client::ClientRequest, error::BlockingError, http::header::InvalidHeaderValue, web,
2019-09-13 01:12:35 +00:00
};
2020-03-30 05:53:45 +00:00
use std::{fmt::Display, future::Future, pin::Pin};
2019-09-13 01:12:35 +00:00
use crate::{
digest::{DigestClient, DigestCreate, SignExt},
Config, PrepareSignError, Sign,
2019-09-13 01:12:35 +00:00
};
impl SignExt for ClientRequest {
2020-03-30 05:53:45 +00:00
fn authorization_signature_with_digest<F, E, K, D, V>(
2019-09-13 01:12:35 +00:00
self,
2020-03-30 05:53:45 +00:00
config: Config,
2019-09-13 01:12:35 +00:00
key_id: K,
2020-03-30 05:53:45 +00:00
mut digest: D,
2019-09-13 01:12:35 +00:00
v: V,
f: F,
2020-03-30 05:53:45 +00:00
) -> Pin<Box<dyn Future<Output = Result<DigestClient<V>, E>>>>
2019-09-13 01:12:35 +00:00
where
F: FnOnce(&str) -> Result<String, E> + Send + 'static,
2021-02-10 21:45:51 +00:00
E: From<BlockingError>
+ From<PrepareSignError>
+ From<InvalidHeaderValue>
+ std::fmt::Debug
+ Send
+ 'static,
2020-03-30 05:53:45 +00:00
K: Display + 'static,
D: DigestCreate + Send + 'static,
V: AsRef<[u8]> + Send + 'static,
2019-09-13 01:12:35 +00:00
Self: Sized,
{
2020-03-30 05:53:45 +00:00
Box::pin(async move {
let (d, v) = web::block(move || {
let d = digest.compute(v.as_ref());
Ok((d, v)) as Result<(String, V), E>
})
2021-02-10 21:45:51 +00:00
.await??;
2019-09-13 01:12:35 +00:00
2020-03-30 05:53:45 +00:00
let c = self
2021-02-10 21:45:51 +00:00
.insert_header(("Digest", format!("{}={}", D::NAME, d)))
2020-03-30 05:53:45 +00:00
.authorization_signature(config, key_id, f)
.await?;
Ok(DigestClient::new(c, v))
})
2019-09-13 01:12:35 +00:00
}
2020-03-30 05:53:45 +00:00
fn signature_with_digest<F, E, K, D, V>(
2019-09-13 01:12:35 +00:00
self,
2020-03-30 05:53:45 +00:00
config: Config,
2019-09-13 01:12:35 +00:00
key_id: K,
2020-03-30 05:53:45 +00:00
mut digest: D,
2019-09-13 01:12:35 +00:00
v: V,
f: F,
2020-03-30 05:53:45 +00:00
) -> Pin<Box<dyn Future<Output = Result<DigestClient<V>, E>>>>
2019-09-13 01:12:35 +00:00
where
F: FnOnce(&str) -> Result<String, E> + Send + 'static,
2021-02-10 21:45:51 +00:00
E: From<BlockingError>
+ From<PrepareSignError>
+ From<InvalidHeaderValue>
+ std::fmt::Debug
+ Send
+ 'static,
2020-03-30 05:53:45 +00:00
K: Display + 'static,
D: DigestCreate + Send + 'static,
V: AsRef<[u8]> + Send + 'static,
2019-09-13 01:12:35 +00:00
Self: Sized,
{
2020-03-30 05:53:45 +00:00
Box::pin(async move {
let (d, v) = web::block(move || {
let d = digest.compute(v.as_ref());
Ok((d, v)) as Result<(String, V), E>
})
2021-02-10 21:45:51 +00:00
.await??;
2020-03-30 05:53:45 +00:00
let c = self
2021-02-10 21:45:51 +00:00
.insert_header(("Digest", format!("{}={}", D::NAME, d)))
2020-03-30 05:53:45 +00:00
.signature(config, key_id, f)
.await?;
2019-09-13 01:12:35 +00:00
2020-03-30 05:53:45 +00:00
Ok(DigestClient::new(c, v))
})
2019-09-13 01:12:35 +00:00
}
}