mirror of
https://git.asonix.dog/asonix/http-signature-normalization.git
synced 2024-11-22 01:11:00 +00:00
actix: Split Server and Client into features
This commit is contained in:
parent
9be4a14206
commit
bb42dd3f9a
8 changed files with 640 additions and 533 deletions
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
name = "http-signature-normalization-actix"
|
||||
description = "An HTTP Signatures library that leaves the signing to you"
|
||||
version = "0.5.0-beta.9"
|
||||
version = "0.5.0-beta.10"
|
||||
authors = ["asonix <asonix@asonix.dog>"]
|
||||
license-file = "LICENSE"
|
||||
readme = "README.md"
|
||||
|
@ -11,22 +11,26 @@ edition = "2018"
|
|||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[features]
|
||||
default = ["sha-2", "sha-3"]
|
||||
default = ["server", "sha-2", "sha-3"]
|
||||
client = ["awc"]
|
||||
digest = ["base64"]
|
||||
server = ["actix-web"]
|
||||
sha-2 = ["digest", "sha2"]
|
||||
sha-3 = ["digest", "sha3"]
|
||||
|
||||
[[example]]
|
||||
name = "server"
|
||||
required-features = ["sha-2"]
|
||||
required-features = ["server", "sha-2"]
|
||||
|
||||
[[example]]
|
||||
name = "client"
|
||||
required-features = ["sha-2"]
|
||||
required-features = ["client", "sha-2"]
|
||||
|
||||
[dependencies]
|
||||
actix-web = { version = "4.0.0-beta.8", default-features = false }
|
||||
awc = { version = "3.0.0-beta.7", default-features = false }
|
||||
actix-http = { version = "3.0.0-beta.10", default-features = false }
|
||||
actix-rt = "2.2.0"
|
||||
actix-web = { version = "4.0.0-beta.8", default-features = false, optional = true }
|
||||
awc = { version = "3.0.0-beta.7", default-features = false, optional = true }
|
||||
base64 = { version = "0.13", optional = true }
|
||||
chrono = "0.4.6"
|
||||
futures-util = { version = "0.3", default-features = false }
|
||||
|
@ -40,6 +44,5 @@ tracing-error = "0.1"
|
|||
tracing-futures = "0.2"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "2.1.0"
|
||||
tracing-actix-web = { version = "0.4.0-beta.13" }
|
||||
tracing-subscriber = { version = "0.2", features = ["fmt"] }
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! Types for signing requests with Actix Web
|
||||
|
||||
use actix_web::http::header::{
|
||||
use actix_http::http::header::{
|
||||
HeaderMap, HeaderName, HeaderValue, InvalidHeaderValue, AUTHORIZATION,
|
||||
};
|
||||
|
||||
|
|
|
@ -3,36 +3,40 @@
|
|||
//! Digest headers are commonly used in conjunction with HTTP Signatures to verify the whole
|
||||
//! request when request bodies are present
|
||||
|
||||
use actix_web::{error::BlockingError, http::header::InvalidHeaderValue};
|
||||
use awc::{ClientRequest, SendClientRequest};
|
||||
use std::{fmt::Display, future::Future, pin::Pin};
|
||||
|
||||
use crate::{Config, PrepareSignError, Sign};
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub mod middleware;
|
||||
#[cfg(feature = "sha-2")]
|
||||
mod sha2;
|
||||
#[cfg(feature = "sha-3")]
|
||||
mod sha3;
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
mod sign;
|
||||
|
||||
/// A trait for creating digests of an array of bytes
|
||||
pub trait DigestCreate {
|
||||
#[cfg(feature = "client")]
|
||||
pub use self::client::{DigestClient, DigestCreate, SignExt};
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub use self::server::{DigestPart, DigestVerify};
|
||||
|
||||
/// Giving names to Digest implementations
|
||||
pub trait DigestName {
|
||||
/// The name of the digest algorithm
|
||||
const NAME: &'static str;
|
||||
|
||||
/// Compute the digest of the input bytes
|
||||
fn compute(&mut self, input: &[u8]) -> String;
|
||||
}
|
||||
|
||||
/// A trait for verifying digests
|
||||
pub trait DigestVerify {
|
||||
/// Update the verifier with bytes from the request body
|
||||
fn update(&mut self, part: &[u8]);
|
||||
#[cfg(feature = "client")]
|
||||
mod client {
|
||||
use crate::{Config, PrepareSignError, Sign};
|
||||
use actix_http::{error::BlockingError, http::header::InvalidHeaderValue};
|
||||
use awc::{ClientRequest, SendClientRequest};
|
||||
use std::{fmt::Display, future::Future, pin::Pin};
|
||||
|
||||
/// Verify the request body against the digests from the request headers
|
||||
fn verify(&mut self, digests: &[DigestPart]) -> bool;
|
||||
use super::DigestName;
|
||||
|
||||
/// A trait for creating digests of an array of bytes
|
||||
pub trait DigestCreate: DigestName {
|
||||
/// Compute the digest of the input bytes
|
||||
fn compute(&mut self, input: &[u8]) -> String;
|
||||
}
|
||||
|
||||
/// Extend the Sign trait with support for adding Digest Headers to the request
|
||||
|
@ -86,16 +90,6 @@ pub trait SignExt: Sign {
|
|||
Self: Sized;
|
||||
}
|
||||
|
||||
/// A parsed digest from the request
|
||||
#[derive(Debug)]
|
||||
pub struct DigestPart {
|
||||
/// The alrogithm used to produce the digest
|
||||
pub algorithm: String,
|
||||
|
||||
/// The digest itself
|
||||
pub digest: String,
|
||||
}
|
||||
|
||||
/// An intermediate type between setting the Digest and Signature or Authorization headers, and
|
||||
/// actually sending the request
|
||||
///
|
||||
|
@ -109,7 +103,7 @@ impl<V> DigestClient<V>
|
|||
where
|
||||
V: AsRef<[u8]>,
|
||||
{
|
||||
fn new(req: ClientRequest, body: V) -> Self {
|
||||
pub(super) fn new(req: ClientRequest, body: V) -> Self {
|
||||
DigestClient { req, body }
|
||||
}
|
||||
|
||||
|
@ -128,3 +122,28 @@ where
|
|||
(self.req, self.body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
mod server {
|
||||
use super::DigestName;
|
||||
|
||||
/// A trait for verifying digests
|
||||
pub trait DigestVerify: DigestName {
|
||||
/// Update the verifier with bytes from the request body
|
||||
fn update(&mut self, part: &[u8]);
|
||||
|
||||
/// Verify the request body against the digests from the request headers
|
||||
fn verify(&mut self, digests: &[DigestPart]) -> bool;
|
||||
}
|
||||
|
||||
/// A parsed digest from the request
|
||||
#[derive(Debug)]
|
||||
pub struct DigestPart {
|
||||
/// The alrogithm used to produce the digest
|
||||
pub algorithm: String,
|
||||
|
||||
/// The digest itself
|
||||
pub digest: String,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,83 @@
|
|||
use crate::digest::DigestName;
|
||||
use sha2::{Sha224, Sha256, Sha384, Sha512, Sha512Trunc224, Sha512Trunc256};
|
||||
use tracing::{debug, warn};
|
||||
|
||||
use super::{DigestCreate, DigestPart, DigestVerify};
|
||||
impl DigestName for Sha224 {
|
||||
const NAME: &'static str = "SHA-244";
|
||||
}
|
||||
|
||||
impl DigestName for Sha256 {
|
||||
const NAME: &'static str = "SHA-256";
|
||||
}
|
||||
|
||||
impl DigestName for Sha384 {
|
||||
const NAME: &'static str = "SHA-384";
|
||||
}
|
||||
|
||||
impl DigestName for Sha512 {
|
||||
const NAME: &'static str = "SHA-512";
|
||||
}
|
||||
|
||||
impl DigestName for Sha512Trunc224 {
|
||||
const NAME: &'static str = "SHA-512-224";
|
||||
}
|
||||
|
||||
impl DigestName for Sha512Trunc256 {
|
||||
const NAME: &'static str = "SHA-512-256";
|
||||
}
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
mod client {
|
||||
use super::*;
|
||||
use crate::digest::DigestCreate;
|
||||
|
||||
fn create(digest: &mut impl sha2::Digest, input: &[u8]) -> String {
|
||||
digest.update(input);
|
||||
base64::encode(&digest.finalize_reset())
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha224 {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha256 {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha384 {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha512 {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha512Trunc224 {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha512Trunc256 {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
mod server {
|
||||
use super::*;
|
||||
use crate::digest::{DigestPart, DigestVerify};
|
||||
use tracing::{debug, warn};
|
||||
|
||||
fn verify(digest: &mut impl sha2::Digest, name: &str, parts: &[DigestPart]) -> bool {
|
||||
if let Some(part) = parts
|
||||
.iter()
|
||||
|
@ -34,29 +104,13 @@ fn verify(digest: &mut impl sha2::Digest, name: &str, parts: &[DigestPart]) -> b
|
|||
false
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha224 {
|
||||
const NAME: &'static str = "SHA-224";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestVerify for Sha224 {
|
||||
fn update(&mut self, part: &[u8]) {
|
||||
sha2::Digest::update(self, part);
|
||||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha256 {
|
||||
const NAME: &'static str = "SHA-256";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -66,15 +120,7 @@ impl DigestVerify for Sha256 {
|
|||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha384 {
|
||||
const NAME: &'static str = "SHA-384";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,15 +130,7 @@ impl DigestVerify for Sha384 {
|
|||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha512 {
|
||||
const NAME: &'static str = "SHA-512";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -102,15 +140,7 @@ impl DigestVerify for Sha512 {
|
|||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha512Trunc224 {
|
||||
const NAME: &'static str = "SHA-512-224";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -120,15 +150,7 @@ impl DigestVerify for Sha512Trunc224 {
|
|||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha512Trunc256 {
|
||||
const NAME: &'static str = "SHA-512-256";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -138,6 +160,7 @@ impl DigestVerify for Sha512Trunc256 {
|
|||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,116 @@
|
|||
use crate::digest::DigestName;
|
||||
use sha3::{
|
||||
Keccak224, Keccak256, Keccak256Full, Keccak384, Keccak512, Sha3_224, Sha3_256, Sha3_384,
|
||||
Sha3_512,
|
||||
};
|
||||
use tracing::{debug, warn};
|
||||
|
||||
use super::{DigestCreate, DigestPart, DigestVerify};
|
||||
impl DigestName for Keccak224 {
|
||||
const NAME: &'static str = "keccak-224";
|
||||
}
|
||||
|
||||
impl DigestName for Keccak256 {
|
||||
const NAME: &'static str = "keccak-256";
|
||||
}
|
||||
|
||||
impl DigestName for Keccak256Full {
|
||||
const NAME: &'static str = "keccak-256-full";
|
||||
}
|
||||
|
||||
impl DigestName for Keccak384 {
|
||||
const NAME: &'static str = "keccak-384";
|
||||
}
|
||||
|
||||
impl DigestName for Keccak512 {
|
||||
const NAME: &'static str = "keccak-512";
|
||||
}
|
||||
|
||||
impl DigestName for Sha3_224 {
|
||||
const NAME: &'static str = "SHA3-224";
|
||||
}
|
||||
|
||||
impl DigestName for Sha3_256 {
|
||||
const NAME: &'static str = "SHA3-256";
|
||||
}
|
||||
|
||||
impl DigestName for Sha3_384 {
|
||||
const NAME: &'static str = "SHA3-384";
|
||||
}
|
||||
|
||||
impl DigestName for Sha3_512 {
|
||||
const NAME: &'static str = "SHA3-512";
|
||||
}
|
||||
|
||||
#[cfg(features = "client")]
|
||||
mod client {
|
||||
use super::*;
|
||||
use crate::digest::DigestCreate;
|
||||
|
||||
fn create(digest: &mut impl sha3::Digest, input: &[u8]) -> String {
|
||||
digest.update(input);
|
||||
base64::encode(&digest.finalize_reset())
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha3_224 {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha3_256 {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha3_384 {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha3_512 {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Keccak224 {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Keccak256 {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Keccak256Full {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Keccak384 {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Keccak512 {
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
mod server {
|
||||
use super::*;
|
||||
use crate::digest::{DigestPart, DigestVerify};
|
||||
use tracing::{debug, warn};
|
||||
|
||||
fn verify(digest: &mut impl sha3::Digest, name: &str, parts: &[DigestPart]) -> bool {
|
||||
if let Some(part) = parts
|
||||
.iter()
|
||||
|
@ -37,29 +137,13 @@ fn verify(digest: &mut impl sha3::Digest, name: &str, parts: &[DigestPart]) -> b
|
|||
false
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha3_224 {
|
||||
const NAME: &'static str = "SHA3-224";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestVerify for Sha3_224 {
|
||||
fn update(&mut self, part: &[u8]) {
|
||||
sha3::Digest::update(self, part);
|
||||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha3_256 {
|
||||
const NAME: &'static str = "SHA3-256";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -69,15 +153,7 @@ impl DigestVerify for Sha3_256 {
|
|||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha3_384 {
|
||||
const NAME: &'static str = "SHA3-384";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -87,15 +163,7 @@ impl DigestVerify for Sha3_384 {
|
|||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Sha3_512 {
|
||||
const NAME: &'static str = "SHA3-512";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,15 +173,7 @@ impl DigestVerify for Sha3_512 {
|
|||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Keccak224 {
|
||||
const NAME: &'static str = "keccak-224";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -123,15 +183,7 @@ impl DigestVerify for Keccak224 {
|
|||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Keccak256 {
|
||||
const NAME: &'static str = "keccak-256";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -141,15 +193,7 @@ impl DigestVerify for Keccak256 {
|
|||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Keccak256Full {
|
||||
const NAME: &'static str = "keccak-256-full";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -159,15 +203,7 @@ impl DigestVerify for Keccak256Full {
|
|||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Keccak384 {
|
||||
const NAME: &'static str = "keccak-384";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -177,15 +213,7 @@ impl DigestVerify for Keccak384 {
|
|||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
impl DigestCreate for Keccak512 {
|
||||
const NAME: &'static str = "keccak-512";
|
||||
|
||||
fn compute(&mut self, input: &[u8]) -> String {
|
||||
create(self, input)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -195,6 +223,7 @@ impl DigestVerify for Keccak512 {
|
|||
}
|
||||
|
||||
fn verify(&mut self, parts: &[DigestPart]) -> bool {
|
||||
verify(self, <Self as DigestCreate>::NAME, parts)
|
||||
verify(self, Self::NAME, parts)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use actix_web::{error::BlockingError, http::header::InvalidHeaderValue, web};
|
||||
use actix_http::{error::BlockingError, http::header::InvalidHeaderValue};
|
||||
use awc::ClientRequest;
|
||||
use std::{fmt::Display, future::Future, pin::Pin};
|
||||
|
||||
|
@ -30,11 +30,11 @@ impl SignExt for ClientRequest {
|
|||
Self: Sized,
|
||||
{
|
||||
Box::pin(async move {
|
||||
let (d, v) = web::block(move || {
|
||||
let (d, v) = actix_rt::task::spawn_blocking(move || {
|
||||
let d = digest.compute(v.as_ref());
|
||||
Ok((d, v)) as Result<(String, V), E>
|
||||
})
|
||||
.await??;
|
||||
.await.map_err(|_| BlockingError)??;
|
||||
|
||||
let c = self
|
||||
.insert_header(("Digest", format!("{}={}", D::NAME, d)))
|
||||
|
@ -67,11 +67,11 @@ impl SignExt for ClientRequest {
|
|||
Self: Sized,
|
||||
{
|
||||
Box::pin(async move {
|
||||
let (d, v) = web::block(move || {
|
||||
let (d, v) = actix_rt::task::spawn_blocking(move || {
|
||||
let d = digest.compute(v.as_ref());
|
||||
Ok((d, v)) as Result<(String, V), E>
|
||||
})
|
||||
.await??;
|
||||
.await.map_err(|_| BlockingError)??;
|
||||
|
||||
let c = self
|
||||
.insert_header(("Digest", format!("{}={}", D::NAME, d)))
|
||||
|
|
|
@ -159,44 +159,57 @@
|
|||
//! }
|
||||
//! ```
|
||||
|
||||
use actix_web::{
|
||||
error::BlockingError,
|
||||
http::{
|
||||
header::{HeaderMap, InvalidHeaderValue, ToStrError},
|
||||
use chrono::Duration;
|
||||
|
||||
#[cfg(any(feature = "client", feature = "server"))]
|
||||
use actix_http::http::{
|
||||
header::{HeaderMap, ToStrError},
|
||||
uri::PathAndQuery,
|
||||
Method,
|
||||
},
|
||||
};
|
||||
use chrono::Duration;
|
||||
use std::{collections::BTreeMap, fmt::Display, future::Future, pin::Pin};
|
||||
#[cfg(any(feature = "client", feature = "server"))]
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
mod sign;
|
||||
|
||||
#[cfg(feature = "digest")]
|
||||
pub mod digest;
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
pub mod create;
|
||||
#[cfg(feature = "server")]
|
||||
pub mod middleware;
|
||||
|
||||
pub use http_signature_normalization::RequiredError;
|
||||
|
||||
/// Useful types and traits for using this library in Actix Web
|
||||
pub mod prelude {
|
||||
pub use crate::{Config, RequiredError};
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
pub use crate::{PrepareSignError, Sign};
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub use crate::{
|
||||
middleware::{SignatureVerified, VerifySignature},
|
||||
verify::{Algorithm, DeprecatedAlgorithm, Unverified},
|
||||
Config, PrepareSignError, PrepareVerifyError, RequiredError, Sign, SignatureVerify,
|
||||
PrepareVerifyError, SignatureVerify,
|
||||
};
|
||||
|
||||
#[cfg(feature = "digest")]
|
||||
#[cfg(all(feature = "digest", feature = "client"))]
|
||||
pub use crate::digest::{DigestClient, DigestCreate, SignExt};
|
||||
|
||||
#[cfg(all(feature = "digest", feature = "server"))]
|
||||
pub use crate::digest::{
|
||||
middleware::{DigestVerified, VerifyDigest},
|
||||
DigestClient, DigestCreate, DigestPart, DigestVerify, SignExt,
|
||||
DigestPart, DigestVerify,
|
||||
};
|
||||
|
||||
pub use actix_web::http::header::{InvalidHeaderValue, ToStrError};
|
||||
pub use actix_http::http::header::{InvalidHeaderValue, ToStrError};
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
/// Types for Verifying an HTTP Signature
|
||||
pub mod verify {
|
||||
pub use http_signature_normalization::verify::{
|
||||
|
@ -205,31 +218,34 @@ pub mod verify {
|
|||
};
|
||||
}
|
||||
|
||||
use self::{
|
||||
create::Unsigned,
|
||||
verify::{Algorithm, Unverified},
|
||||
};
|
||||
#[cfg(feature = "client")]
|
||||
pub use self::client::{PrepareSignError, Sign};
|
||||
|
||||
/// A trait for verifying signatures
|
||||
pub trait SignatureVerify {
|
||||
/// An error produced while attempting to verify the signature. This can be anything
|
||||
/// implementing ResponseError
|
||||
type Error: actix_web::ResponseError;
|
||||
#[cfg(feature = "server")]
|
||||
pub use self::server::{PrepareVerifyError, SignatureVerify};
|
||||
|
||||
/// The future that resolves to the verification state of the signature
|
||||
type Future: Future<Output = Result<bool, Self::Error>>;
|
||||
#[derive(Clone, Debug, Default)]
|
||||
/// Configuration for signing and verifying signatures
|
||||
///
|
||||
/// By default, the config is set up to create and verify signatures that expire after 10
|
||||
/// seconds, and use the `(created)` and `(expires)` fields that were introduced in draft 11
|
||||
pub struct Config {
|
||||
/// The inner config type
|
||||
config: http_signature_normalization::Config,
|
||||
|
||||
/// Given the algorithm, key_id, signature, and signing_string, produce a future that resulves
|
||||
/// to a the verification status
|
||||
fn signature_verify(
|
||||
&mut self,
|
||||
algorithm: Option<Algorithm>,
|
||||
key_id: String,
|
||||
signature: String,
|
||||
signing_string: String,
|
||||
) -> Self::Future;
|
||||
/// Whether to set the Host header
|
||||
set_host: bool,
|
||||
}
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
mod client {
|
||||
use super::{Config, RequiredError};
|
||||
use actix_http::{
|
||||
error::BlockingError,
|
||||
http::header::{InvalidHeaderValue, ToStrError},
|
||||
};
|
||||
use std::{fmt::Display, future::Future, pin::Pin};
|
||||
|
||||
/// A trait implemented by the awc ClientRequest type to add an HTTP signature to the request
|
||||
pub trait Sign {
|
||||
/// Add an Authorization Signature to the request
|
||||
|
@ -269,17 +285,47 @@ pub trait Sign {
|
|||
Self: Sized;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
/// Configuration for signing and verifying signatures
|
||||
///
|
||||
/// By default, the config is set up to create and verify signatures that expire after 10
|
||||
/// seconds, and use the `(created)` and `(expires)` fields that were introduced in draft 11
|
||||
pub struct Config {
|
||||
/// The inner config type
|
||||
config: http_signature_normalization::Config,
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
/// An error when preparing to sign a request
|
||||
pub enum PrepareSignError {
|
||||
#[error("Failed to read header, {0}")]
|
||||
/// An error occurred when reading the request's headers
|
||||
Header(#[from] ToStrError),
|
||||
|
||||
/// Whether to set the Host header
|
||||
set_host: bool,
|
||||
#[error("{0}")]
|
||||
/// Some headers were marked as required, but are missing
|
||||
RequiredError(#[from] RequiredError),
|
||||
|
||||
#[error("No host provided for URL, {0}")]
|
||||
/// Missing host
|
||||
Host(String),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
mod server {
|
||||
use super::RequiredError;
|
||||
use actix_http::http::header::ToStrError;
|
||||
use std::future::Future;
|
||||
|
||||
/// A trait for verifying signatures
|
||||
pub trait SignatureVerify {
|
||||
/// An error produced while attempting to verify the signature. This can be anything
|
||||
/// implementing ResponseError
|
||||
type Error: actix_web::ResponseError;
|
||||
|
||||
/// The future that resolves to the verification state of the signature
|
||||
type Future: Future<Output = Result<bool, Self::Error>>;
|
||||
|
||||
/// Given the algorithm, key_id, signature, and signing_string, produce a future that resulves
|
||||
/// to a the verification status
|
||||
fn signature_verify(
|
||||
&mut self,
|
||||
algorithm: Option<super::verify::Algorithm>,
|
||||
key_id: String,
|
||||
signature: String,
|
||||
signing_string: String,
|
||||
) -> Self::Future;
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
|
@ -306,22 +352,6 @@ pub enum PrepareVerifyError {
|
|||
Required(#[from] RequiredError),
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
/// An error when preparing to sign a request
|
||||
pub enum PrepareSignError {
|
||||
#[error("Failed to read header, {0}")]
|
||||
/// An error occurred when reading the request's headers
|
||||
Header(#[from] ToStrError),
|
||||
|
||||
#[error("{0}")]
|
||||
/// Some headers were marked as required, but are missing
|
||||
RequiredError(#[from] RequiredError),
|
||||
|
||||
#[error("No host provided for URL, {0}")]
|
||||
/// Missing host
|
||||
Host(String),
|
||||
}
|
||||
|
||||
impl From<http_signature_normalization::PrepareVerifyError> for PrepareVerifyError {
|
||||
fn from(e: http_signature_normalization::PrepareVerifyError) -> Self {
|
||||
use http_signature_normalization as hsn;
|
||||
|
@ -340,6 +370,7 @@ impl From<http_signature_normalization::PrepareVerifyError> for PrepareVerifyErr
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// Create a new Config with a default expiration of 10 seconds
|
||||
|
@ -405,13 +436,14 @@ impl Config {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
/// Begin the process of singing a request
|
||||
pub fn begin_sign(
|
||||
&self,
|
||||
method: &Method,
|
||||
path_and_query: Option<&PathAndQuery>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Unsigned, PrepareSignError> {
|
||||
) -> Result<self::create::Unsigned, PrepareSignError> {
|
||||
let headers = headers
|
||||
.iter()
|
||||
.map(|(k, v)| v.to_str().map(|v| (k.to_string(), v.to_string())))
|
||||
|
@ -425,16 +457,17 @@ impl Config {
|
|||
.config
|
||||
.begin_sign(&method.to_string(), &path_and_query, headers)?;
|
||||
|
||||
Ok(Unsigned { unsigned })
|
||||
Ok(self::create::Unsigned { unsigned })
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
/// Begin the proess of verifying a request
|
||||
pub fn begin_verify(
|
||||
&self,
|
||||
method: &Method,
|
||||
path_and_query: Option<&PathAndQuery>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Unverified, PrepareVerifyError> {
|
||||
) -> Result<self::verify::Unverified, PrepareVerifyError> {
|
||||
let headers = headers
|
||||
.iter()
|
||||
.map(|(k, v)| v.to_str().map(|v| (k.to_string(), v.to_string())))
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use actix_web::{error::BlockingError, http::header::InvalidHeaderValue, web};
|
||||
use actix_http::{error::BlockingError, http::header::InvalidHeaderValue};
|
||||
use awc::ClientRequest;
|
||||
use std::{fmt::Display, future::Future, pin::Pin};
|
||||
|
||||
|
@ -92,7 +92,7 @@ where
|
|||
|
||||
let key_id = key_id.to_string();
|
||||
|
||||
let signed = web::block(move || unsigned.sign(key_id, f)).await??;
|
||||
let signed = actix_rt::task::spawn_blocking(move || unsigned.sign(key_id, f)).await.map_err(|_| BlockingError)??;
|
||||
|
||||
Ok(signed)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue