http-signature-normalization/http-signature-normalization-actix/src/digest/mod.rs
2019-09-13 17:56:06 -05:00

88 lines
1.8 KiB
Rust

use actix_web::{
client::{ClientRequest, ClientResponse},
error::PayloadError,
http::header::{InvalidHeaderValue, ToStrError},
web::Bytes,
};
use futures::{Future, Stream};
use std::fmt::Display;
use crate::{Config, Sign};
pub mod middleware;
#[cfg(feature = "sha-2")]
pub mod sha2;
#[cfg(feature = "sha-3")]
pub mod sha3;
mod sign;
pub trait DigestCreate {
const NAME: &'static str;
fn compute(&mut self, input: &[u8]) -> String;
}
pub trait DigestVerify {
fn verify(&mut self, digests: &[DigestPart], payload: &[u8]) -> bool;
}
pub trait SignExt: Sign {
fn authorization_signature_with_digest<F, E, K, D, V>(
self,
config: &Config,
key_id: K,
digest: &mut D,
v: V,
f: F,
) -> Result<DigestClient<V>, E>
where
F: FnOnce(&str) -> Result<String, E>,
E: From<ToStrError> + From<InvalidHeaderValue>,
K: Display,
D: DigestCreate,
V: AsRef<[u8]>,
Self: Sized;
fn signature_with_digest<F, E, K, D, V>(
self,
config: &Config,
key_id: K,
digest: &mut D,
v: V,
f: F,
) -> Result<DigestClient<V>, E>
where
F: FnOnce(&str) -> Result<String, E>,
E: From<ToStrError> + From<InvalidHeaderValue>,
K: Display,
D: DigestCreate,
V: AsRef<[u8]>,
Self: Sized;
}
pub struct DigestPart {
pub algorithm: String,
pub digest: String,
}
pub struct DigestClient<V> {
req: ClientRequest,
body: V,
}
impl<V> DigestClient<V>
where
V: AsRef<[u8]>,
{
pub fn new(req: ClientRequest, body: V) -> Self {
DigestClient { req, body }
}
pub fn send(
self,
) -> impl Future<Item = ClientResponse<impl Stream<Item = Bytes, Error = PayloadError>>> {
self.req.send_body(self.body.as_ref().to_vec())
}
}