From 08686beb8f13ace611f730f3e9bf40fa04e00d79 Mon Sep 17 00:00:00 2001 From: asonix Date: Wed, 22 Apr 2020 17:15:32 -0500 Subject: [PATCH] impl Display for Algorithm, own values in actix verifier --- Cargo.toml | 2 +- http-signature-normalization-actix/Cargo.toml | 4 +-- http-signature-normalization-actix/README.md | 10 +++--- .../examples/server.rs | 8 ++--- http-signature-normalization-actix/src/lib.rs | 14 ++++----- .../src/middleware.rs | 8 +++-- src/verify.rs | 31 +++++++++++++++++++ 7 files changed, 56 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4c52224..88dc6d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "http-signature-normalization" description = "An HTTP Signatures library that leaves the signing to you" -version = "0.4.1" +version = "0.4.2" authors = ["asonix "] license-file = "LICENSE" readme = "README.md" diff --git a/http-signature-normalization-actix/Cargo.toml b/http-signature-normalization-actix/Cargo.toml index 76c42ac..6d92f81 100644 --- a/http-signature-normalization-actix/Cargo.toml +++ b/http-signature-normalization-actix/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "http-signature-normalization-actix" description = "An HTTP Signatures library that leaves the signing to you" -version = "0.3.0-alpha.9" +version = "0.3.0-alpha.10" authors = ["asonix "] license-file = "LICENSE" readme = "README.md" @@ -31,7 +31,7 @@ base64 = { version = "0.12", optional = true } bytes = "0.5.4" chrono = "0.4.6" futures = "0.3" -http-signature-normalization = { version = "0.4.1", path = ".." } +http-signature-normalization = { version = "0.4.2", path = ".." } log = "0.4" sha2 = { version = "0.8", optional = true } sha3 = { version = "0.8", optional = true } diff --git a/http-signature-normalization-actix/README.md b/http-signature-normalization-actix/README.md index ef4c638..6f84061 100644 --- a/http-signature-normalization-actix/README.md +++ b/http-signature-normalization-actix/README.md @@ -16,7 +16,7 @@ This crate provides extensions the ClientRequest type from Actix Web, and provid actix = "0.10.0-alpha.1" actix-web = "3.0.0-alpha.1" thiserror = "0.1" -http-signature-normalization-actix = { version = "0.3.0-alpha.9", default-features = false, features = ["sha-2"] } +http-signature-normalization-actix = { version = "0.3.0-alpha.10", default-features = false, features = ["sha-2"] } sha2 = "0.8" ``` @@ -89,9 +89,9 @@ impl SignatureVerify for MyVerify { fn signature_verify( &mut self, algorithm: Option, - key_id: &str, - signature: &str, - signing_string: &str, + key_id: String, + signature: String, + signing_string: String, ) -> Self::Future { match algorithm { Some(Algorithm::Hs2019) => (), @@ -102,7 +102,7 @@ impl SignatureVerify for MyVerify { return err(MyError::Key); } - let decoded = match base64::decode(signature) { + let decoded = match base64::decode(&signature) { Ok(decoded) => decoded, Err(_) => return err(MyError::Decode), }; diff --git a/http-signature-normalization-actix/examples/server.rs b/http-signature-normalization-actix/examples/server.rs index 1b8bd8b..2cec33e 100644 --- a/http-signature-normalization-actix/examples/server.rs +++ b/http-signature-normalization-actix/examples/server.rs @@ -17,9 +17,9 @@ impl SignatureVerify for MyVerify { fn signature_verify( &mut self, algorithm: Option, - key_id: &str, - signature: &str, - signing_string: &str, + key_id: String, + signature: String, + signing_string: String, ) -> Self::Future { match algorithm { Some(Algorithm::Hs2019) => (), @@ -30,7 +30,7 @@ impl SignatureVerify for MyVerify { return err(MyError::Key); } - let decoded = match base64::decode(signature) { + let decoded = match base64::decode(&signature) { Ok(decoded) => decoded, Err(_) => return err(MyError::Decode), }; diff --git a/http-signature-normalization-actix/src/lib.rs b/http-signature-normalization-actix/src/lib.rs index f319811..0990bf4 100644 --- a/http-signature-normalization-actix/src/lib.rs +++ b/http-signature-normalization-actix/src/lib.rs @@ -23,9 +23,9 @@ //! fn signature_verify( //! &mut self, //! algorithm: Option, -//! key_id: &str, -//! signature: &str, -//! signing_string: &str, +//! key_id: String, +//! signature: String, +//! signing_string: String, //! ) -> Self::Future { //! match algorithm { //! Some(Algorithm::Hs2019) => (), @@ -36,7 +36,7 @@ //! return err(MyError::Key); //! } //! -//! let decoded = match base64::decode(signature) { +//! let decoded = match base64::decode(&signature) { //! Ok(decoded) => decoded, //! Err(_) => return err(MyError::Decode), //! }; @@ -224,9 +224,9 @@ pub trait SignatureVerify { fn signature_verify( &mut self, algorithm: Option, - key_id: &str, - signature: &str, - signing_string: &str, + key_id: String, + signature: String, + signing_string: String, ) -> Self::Future; } diff --git a/http-signature-normalization-actix/src/middleware.rs b/http-signature-normalization-actix/src/middleware.rs index a4ce747..02cc7cf 100644 --- a/http-signature-normalization-actix/src/middleware.rs +++ b/http-signature-normalization-actix/src/middleware.rs @@ -130,8 +130,12 @@ where let key_id = unverified.key_id().to_owned(); let f1 = unverified.verify(|signature, signing_string| { - self.4 - .signature_verify(algorithm, &key_id, signature, signing_string) + self.4.signature_verify( + algorithm, + key_id.clone(), + signature.to_string(), + signing_string.to_string(), + ) }); req.extensions_mut().insert(SignatureVerified(key_id)); diff --git a/src/verify.rs b/src/verify.rs index 69d7850..2116784 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -326,6 +326,37 @@ impl From<&str> for Algorithm { } } +impl fmt::Display for DeprecatedAlgorithm { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let s = match self { + DeprecatedAlgorithm::HmacSha1 => "hmac-sha1", + DeprecatedAlgorithm::HmacSha256 => "hmac-sha256", + DeprecatedAlgorithm::HmacSha384 => "hmac-sha384", + DeprecatedAlgorithm::HmacSha512 => "hmac-sha512", + DeprecatedAlgorithm::RsaSha1 => "rsa-sha1", + DeprecatedAlgorithm::RsaSha256 => "rsa-sha256", + DeprecatedAlgorithm::RsaSha384 => "rsa-sha384", + DeprecatedAlgorithm::RsaSha512 => "rsa-sha512", + DeprecatedAlgorithm::EcdsaSha1 => "ecdsa-sha1", + DeprecatedAlgorithm::EcdsaSha256 => "ecdsa-sha256", + DeprecatedAlgorithm::EcdsaSha384 => "ecdsa-sha384", + DeprecatedAlgorithm::EcdsaSha512 => "ecdsa-sha512", + }; + + write!(f, "{}", s) + } +} + +impl fmt::Display for Algorithm { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Algorithm::Hs2019 => write!(f, "{}", "hs2019"), + Algorithm::Deprecated(d) => d.fmt(f), + Algorithm::Unknown(other) => write!(f, "{}", other), + } + } +} + impl fmt::Display for ParseSignatureError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Error when parsing {} from Http Signature", self.0)