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

129 lines
3.7 KiB
Rust
Raw Normal View History

2019-09-21 16:26:11 +00:00
//! Types and Traits for creating and verifying Digest headers
//!
//! Digest headers are commonly used in conjunction with HTTP Signatures to verify the whole
//! request when request bodies are present
use actix_http::encoding::Decoder;
2019-09-13 01:12:35 +00:00
use actix_web::{
client::{ClientRequest, ClientResponse, SendRequestError},
dev::Payload,
error::BlockingError,
http::header::InvalidHeaderValue,
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::{Config, PrepareSignError, Sign};
2019-09-13 01:12:35 +00:00
pub mod middleware;
#[cfg(feature = "sha-2")]
2019-09-21 16:26:11 +00:00
mod sha2;
2019-09-13 01:12:35 +00:00
#[cfg(feature = "sha-3")]
2019-09-21 16:26:11 +00:00
mod sha3;
2019-09-13 01:12:35 +00:00
mod sign;
2019-09-21 16:26:11 +00:00
/// A trait for creating digests of an array of bytes
2019-09-13 01:12:35 +00:00
pub trait DigestCreate {
2019-09-21 16:26:11 +00:00
/// The name of the digest algorithm
2019-09-13 01:12:35 +00:00
const NAME: &'static str;
2019-09-21 16:26:11 +00:00
/// Compute the digest of the input bytes
2019-09-13 01:12:35 +00:00
fn compute(&mut self, input: &[u8]) -> String;
}
2019-09-21 16:26:11 +00:00
/// A trait for verifying digests
2019-09-13 01:12:35 +00:00
pub trait DigestVerify {
2019-09-21 16:26:11 +00:00
/// Verify the payload of the request against a slice of digests
///
/// The slice of digests should never be empty
2019-09-13 01:12:35 +00:00
fn verify(&mut self, digests: &[DigestPart], payload: &[u8]) -> bool;
}
2019-09-21 16:26:11 +00:00
/// Extend the Sign trait with support for adding Digest Headers to the request
///
/// It generates HTTP Signatures after the Digest header has been added, in order to have
/// verification that the body has not been tampered with, or that the request can't be replayed by
/// a malicious entity
2019-09-13 01:12:35 +00:00
pub trait SignExt: Sign {
2019-09-21 16:26:11 +00:00
/// Set the Digest and Authorization headers on the request
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
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,
E: From<BlockingError<E>>
+ 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;
2019-09-21 16:26:11 +00:00
/// Set the Digest and Signature headers on the request
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
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,
E: From<BlockingError<E>>
+ 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;
}
2019-09-21 16:26:11 +00:00
/// A parsed digest from the request
2019-09-13 01:12:35 +00:00
pub struct DigestPart {
2019-09-21 16:26:11 +00:00
/// The alrogithm used to produce the digest
2019-09-13 01:12:35 +00:00
pub algorithm: String,
2019-09-21 16:26:11 +00:00
/// The digest itself
2019-09-13 01:12:35 +00:00
pub digest: String,
}
2019-09-21 16:26:11 +00:00
/// An intermediate type between setting the Digest and Signature or Authorization headers, and
/// actually sending the request
///
/// This exists so that the return type for the [`SignExt`] trait can be named
2019-09-13 01:12:35 +00:00
pub struct DigestClient<V> {
req: ClientRequest,
body: V,
}
impl<V> DigestClient<V>
where
V: AsRef<[u8]>,
{
2019-09-21 16:26:11 +00:00
fn new(req: ClientRequest, body: V) -> Self {
2019-09-13 01:12:35 +00:00
DigestClient { req, body }
}
2019-09-21 16:26:11 +00:00
/// Send the request
///
/// This is analogous to `ClientRequest::send_body` and uses the body provided when producing
/// the digest
2019-09-13 01:12:35 +00:00
pub fn send(
self,
) -> impl Future<Output = Result<ClientResponse<Decoder<Payload>>, SendRequestError>> {
2019-09-13 01:12:35 +00:00
self.req.send_body(self.body.as_ref().to_vec())
}
}