2019-09-13 01:12:35 +00:00
|
|
|
use actix_web::{
|
2020-04-23 17:54:56 +00:00
|
|
|
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
|
|
|
|
2020-04-23 17:54:56 +00:00
|
|
|
use crate::{create::Signed, Config, PrepareSignError, Sign};
|
2019-09-13 01:12:35 +00:00
|
|
|
|
|
|
|
impl Sign for ClientRequest {
|
2020-03-30 05:53:45 +00:00
|
|
|
fn authorization_signature<F, E, K>(
|
2019-09-13 01:12:35 +00:00
|
|
|
mut self,
|
2020-03-30 05:53:45 +00:00
|
|
|
config: Config,
|
2019-09-13 01:12:35 +00:00
|
|
|
key_id: K,
|
|
|
|
f: F,
|
2020-03-30 05:53:45 +00:00
|
|
|
) -> Pin<Box<dyn Future<Output = Result<Self, E>>>>
|
2019-09-13 01:12:35 +00:00
|
|
|
where
|
2020-03-30 03:21:09 +00:00
|
|
|
F: FnOnce(&str) -> Result<String, E> + Send + 'static,
|
|
|
|
E: From<BlockingError<E>>
|
2020-04-23 17:54:56 +00:00
|
|
|
+ From<PrepareSignError>
|
2020-03-30 03:21:09 +00:00
|
|
|
+ From<InvalidHeaderValue>
|
|
|
|
+ std::fmt::Debug
|
|
|
|
+ Send
|
|
|
|
+ 'static,
|
2020-03-30 05:53:45 +00:00
|
|
|
K: Display + 'static,
|
2020-03-30 03:21:09 +00:00
|
|
|
Self: Sized,
|
2019-09-13 01:12:35 +00:00
|
|
|
{
|
2020-03-30 05:53:45 +00:00
|
|
|
Box::pin(async move {
|
|
|
|
let signed = prepare(&self, &config, key_id, f).await?;
|
|
|
|
signed.authorization_header(self.headers_mut())?;
|
|
|
|
Ok(self)
|
|
|
|
})
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 05:53:45 +00:00
|
|
|
fn signature<F, E, K>(
|
|
|
|
mut self,
|
|
|
|
config: Config,
|
|
|
|
key_id: K,
|
|
|
|
f: F,
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<Self, E>>>>
|
2019-09-13 01:12:35 +00:00
|
|
|
where
|
2020-03-30 03:21:09 +00:00
|
|
|
F: FnOnce(&str) -> Result<String, E> + Send + 'static,
|
|
|
|
E: From<BlockingError<E>>
|
2020-04-23 17:54:56 +00:00
|
|
|
+ From<PrepareSignError>
|
2020-03-30 03:21:09 +00:00
|
|
|
+ From<InvalidHeaderValue>
|
|
|
|
+ std::fmt::Debug
|
|
|
|
+ Send
|
|
|
|
+ 'static,
|
2020-03-30 05:53:45 +00:00
|
|
|
K: Display + 'static,
|
2020-03-30 03:21:09 +00:00
|
|
|
Self: Sized,
|
2019-09-13 01:12:35 +00:00
|
|
|
{
|
2020-03-30 05:53:45 +00:00
|
|
|
Box::pin(async move {
|
|
|
|
let signed = prepare(&self, &config, key_id, f).await?;
|
|
|
|
signed.signature_header(self.headers_mut())?;
|
|
|
|
Ok(self)
|
|
|
|
})
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 03:21:09 +00:00
|
|
|
async fn prepare<F, E, K>(
|
|
|
|
request: &ClientRequest,
|
|
|
|
config: &Config,
|
|
|
|
key_id: K,
|
|
|
|
f: F,
|
|
|
|
) -> Result<Signed, E>
|
2019-09-13 01:12:35 +00:00
|
|
|
where
|
2020-03-30 03:21:09 +00:00
|
|
|
F: FnOnce(&str) -> Result<String, E> + Send + 'static,
|
2020-04-23 17:54:56 +00:00
|
|
|
E: From<BlockingError<E>> + From<PrepareSignError> + std::fmt::Debug + Send + 'static,
|
2019-09-13 01:12:35 +00:00
|
|
|
K: Display,
|
|
|
|
{
|
|
|
|
let unsigned = config.begin_sign(
|
|
|
|
request.get_method(),
|
|
|
|
request.get_uri().path_and_query(),
|
|
|
|
request.headers().clone(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let key_id = key_id.to_string();
|
|
|
|
|
2020-03-30 03:21:09 +00:00
|
|
|
let signed = web::block(move || unsigned.sign(key_id, f)).await?;
|
2019-09-13 01:12:35 +00:00
|
|
|
|
|
|
|
Ok(signed)
|
|
|
|
}
|