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

123 lines
3.4 KiB
Rust
Raw Permalink Normal View History

use crate::{create::Signed, Config, PrepareSignError, Sign, Spawn};
use actix_rt::task::JoinError;
use awc::{
http::header::{HttpDate, InvalidHeaderValue, TryIntoHeaderValue},
ClientRequest,
};
use std::{fmt::Display, future::Future, pin::Pin, time::SystemTime};
2019-09-13 01:12:35 +00:00
impl Sign for ClientRequest {
fn authorization_signature<F, E, K, S>(
2019-09-13 01:12:35 +00:00
mut self,
config: Config<S>,
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
F: FnOnce(&str) -> Result<String, E> + Send + 'static,
2022-02-26 17:47:49 +00:00
E: From<JoinError>
+ From<PrepareSignError>
+ From<crate::Canceled>
+ From<InvalidHeaderValue>
+ std::fmt::Debug
+ Send
+ 'static,
2020-03-30 05:53:45 +00:00
K: Display + 'static,
S: Spawn + 'static,
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(&mut self, &config, key_id, f).await?;
2020-03-30 05:53:45 +00:00
signed.authorization_header(self.headers_mut())?;
Ok(self)
})
2019-09-13 01:12:35 +00:00
}
fn signature<F, E, K, S>(
2020-03-30 05:53:45 +00:00
mut self,
config: Config<S>,
2020-03-30 05:53:45 +00:00
key_id: K,
f: F,
) -> Pin<Box<dyn Future<Output = Result<Self, E>>>>
2019-09-13 01:12:35 +00:00
where
F: FnOnce(&str) -> Result<String, E> + Send + 'static,
2022-02-26 17:47:49 +00:00
E: From<JoinError>
+ From<PrepareSignError>
+ From<InvalidHeaderValue>
+ From<crate::Canceled>
+ std::fmt::Debug
+ Send
+ 'static,
2020-03-30 05:53:45 +00:00
K: Display + 'static,
S: Spawn + 'static,
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(&mut self, &config, key_id, f).await?;
2020-03-30 05:53:45 +00:00
signed.signature_header(self.headers_mut())?;
Ok(self)
})
2019-09-13 01:12:35 +00:00
}
}
async fn prepare<F, E, K, S>(
request: &mut ClientRequest,
config: &Config<S>,
key_id: K,
f: F,
) -> Result<Signed, E>
2019-09-13 01:12:35 +00:00
where
F: FnOnce(&str) -> Result<String, E> + Send + 'static,
E: From<JoinError>
+ From<PrepareSignError>
+ From<crate::Canceled>
+ std::fmt::Debug
+ Send
+ 'static,
2019-09-13 01:12:35 +00:00
K: Display,
S: Spawn + 'static,
2019-09-13 01:12:35 +00:00
{
2022-11-23 00:15:50 +00:00
if config.set_date && !request.headers().contains_key("date") {
request.headers_mut().insert(
actix_http::header::DATE,
HttpDate::from(SystemTime::now())
.try_into_value()
.expect("Date is valid"),
);
}
2020-09-07 21:42:06 +00:00
let mut headers = request.headers().clone();
if config.set_host {
let header_string = request
.get_uri()
.host()
.ok_or_else(|| PrepareSignError::Host(request.get_uri().to_string()))?
.to_string();
let header_string = match request.get_uri().port().map(|p| p.as_u16()) {
None | Some(443) | Some(80) => header_string,
Some(port) => format!("{}:{}", header_string, port),
};
headers.insert(
"Host".parse().unwrap(),
header_string
.parse()
.map_err(|_| PrepareSignError::Host(request.get_uri().to_string()))?,
);
}
2019-09-13 01:12:35 +00:00
let unsigned = config.begin_sign(
request.get_method(),
request.get_uri().path_and_query(),
2020-09-07 21:42:06 +00:00
headers,
2019-09-13 01:12:35 +00:00
)?;
let key_id = key_id.to_string();
let signed = config
.spawner
.spawn_blocking(move || unsigned.sign(key_id, f))
.await??;
2019-09-13 01:12:35 +00:00
Ok(signed)
}