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

76 lines
1.9 KiB
Rust

use actix_web::{
client::ClientRequest,
error::BlockingError,
http::header::{InvalidHeaderValue, ToStrError},
web,
};
use std::fmt::Display;
use crate::{create::Signed, Config, Sign};
#[async_trait::async_trait(?Send)]
impl Sign for ClientRequest {
async fn authorization_signature<F, E, K>(
mut self,
config: &Config,
key_id: K,
f: F,
) -> Result<Self, E>
where
F: FnOnce(&str) -> Result<String, E> + Send + 'static,
E: From<BlockingError<E>>
+ From<ToStrError>
+ From<InvalidHeaderValue>
+ std::fmt::Debug
+ Send
+ 'static,
K: Display,
Self: Sized,
{
let signed = prepare(&self, config, key_id, f).await?;
signed.authorization_header(self.headers_mut())?;
Ok(self)
}
async fn signature<F, E, K>(mut self, config: &Config, key_id: K, f: F) -> Result<Self, E>
where
F: FnOnce(&str) -> Result<String, E> + Send + 'static,
E: From<BlockingError<E>>
+ From<ToStrError>
+ From<InvalidHeaderValue>
+ std::fmt::Debug
+ Send
+ 'static,
K: Display,
Self: Sized,
{
let signed = prepare(&self, config, key_id, f).await?;
signed.signature_header(self.headers_mut())?;
Ok(self)
}
}
async fn prepare<F, E, K>(
request: &ClientRequest,
config: &Config,
key_id: K,
f: F,
) -> Result<Signed, E>
where
F: FnOnce(&str) -> Result<String, E> + Send + 'static,
E: From<BlockingError<E>> + From<ToStrError> + std::fmt::Debug + Send + 'static,
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();
let signed = web::block(move || unsigned.sign(key_id, f)).await?;
Ok(signed)
}