impl Display for Algorithm, own values in actix verifier

This commit is contained in:
asonix 2020-04-22 17:15:32 -05:00
parent 3e83434eff
commit 08686beb8f
7 changed files with 56 additions and 21 deletions

View file

@ -1,7 +1,7 @@
[package] [package]
name = "http-signature-normalization" name = "http-signature-normalization"
description = "An HTTP Signatures library that leaves the signing to you" description = "An HTTP Signatures library that leaves the signing to you"
version = "0.4.1" version = "0.4.2"
authors = ["asonix <asonix@asonix.dog>"] authors = ["asonix <asonix@asonix.dog>"]
license-file = "LICENSE" license-file = "LICENSE"
readme = "README.md" readme = "README.md"

View file

@ -1,7 +1,7 @@
[package] [package]
name = "http-signature-normalization-actix" name = "http-signature-normalization-actix"
description = "An HTTP Signatures library that leaves the signing to you" 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 <asonix@asonix.dog>"] authors = ["asonix <asonix@asonix.dog>"]
license-file = "LICENSE" license-file = "LICENSE"
readme = "README.md" readme = "README.md"
@ -31,7 +31,7 @@ base64 = { version = "0.12", optional = true }
bytes = "0.5.4" bytes = "0.5.4"
chrono = "0.4.6" chrono = "0.4.6"
futures = "0.3" futures = "0.3"
http-signature-normalization = { version = "0.4.1", path = ".." } http-signature-normalization = { version = "0.4.2", path = ".." }
log = "0.4" log = "0.4"
sha2 = { version = "0.8", optional = true } sha2 = { version = "0.8", optional = true }
sha3 = { version = "0.8", optional = true } sha3 = { version = "0.8", optional = true }

View file

@ -16,7 +16,7 @@ This crate provides extensions the ClientRequest type from Actix Web, and provid
actix = "0.10.0-alpha.1" actix = "0.10.0-alpha.1"
actix-web = "3.0.0-alpha.1" actix-web = "3.0.0-alpha.1"
thiserror = "0.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" sha2 = "0.8"
``` ```
@ -89,9 +89,9 @@ impl SignatureVerify for MyVerify {
fn signature_verify( fn signature_verify(
&mut self, &mut self,
algorithm: Option<Algorithm>, algorithm: Option<Algorithm>,
key_id: &str, key_id: String,
signature: &str, signature: String,
signing_string: &str, signing_string: String,
) -> Self::Future { ) -> Self::Future {
match algorithm { match algorithm {
Some(Algorithm::Hs2019) => (), Some(Algorithm::Hs2019) => (),
@ -102,7 +102,7 @@ impl SignatureVerify for MyVerify {
return err(MyError::Key); return err(MyError::Key);
} }
let decoded = match base64::decode(signature) { let decoded = match base64::decode(&signature) {
Ok(decoded) => decoded, Ok(decoded) => decoded,
Err(_) => return err(MyError::Decode), Err(_) => return err(MyError::Decode),
}; };

View file

@ -17,9 +17,9 @@ impl SignatureVerify for MyVerify {
fn signature_verify( fn signature_verify(
&mut self, &mut self,
algorithm: Option<Algorithm>, algorithm: Option<Algorithm>,
key_id: &str, key_id: String,
signature: &str, signature: String,
signing_string: &str, signing_string: String,
) -> Self::Future { ) -> Self::Future {
match algorithm { match algorithm {
Some(Algorithm::Hs2019) => (), Some(Algorithm::Hs2019) => (),
@ -30,7 +30,7 @@ impl SignatureVerify for MyVerify {
return err(MyError::Key); return err(MyError::Key);
} }
let decoded = match base64::decode(signature) { let decoded = match base64::decode(&signature) {
Ok(decoded) => decoded, Ok(decoded) => decoded,
Err(_) => return err(MyError::Decode), Err(_) => return err(MyError::Decode),
}; };

View file

@ -23,9 +23,9 @@
//! fn signature_verify( //! fn signature_verify(
//! &mut self, //! &mut self,
//! algorithm: Option<Algorithm>, //! algorithm: Option<Algorithm>,
//! key_id: &str, //! key_id: String,
//! signature: &str, //! signature: String,
//! signing_string: &str, //! signing_string: String,
//! ) -> Self::Future { //! ) -> Self::Future {
//! match algorithm { //! match algorithm {
//! Some(Algorithm::Hs2019) => (), //! Some(Algorithm::Hs2019) => (),
@ -36,7 +36,7 @@
//! return err(MyError::Key); //! return err(MyError::Key);
//! } //! }
//! //!
//! let decoded = match base64::decode(signature) { //! let decoded = match base64::decode(&signature) {
//! Ok(decoded) => decoded, //! Ok(decoded) => decoded,
//! Err(_) => return err(MyError::Decode), //! Err(_) => return err(MyError::Decode),
//! }; //! };
@ -224,9 +224,9 @@ pub trait SignatureVerify {
fn signature_verify( fn signature_verify(
&mut self, &mut self,
algorithm: Option<Algorithm>, algorithm: Option<Algorithm>,
key_id: &str, key_id: String,
signature: &str, signature: String,
signing_string: &str, signing_string: String,
) -> Self::Future; ) -> Self::Future;
} }

View file

@ -130,8 +130,12 @@ where
let key_id = unverified.key_id().to_owned(); let key_id = unverified.key_id().to_owned();
let f1 = unverified.verify(|signature, signing_string| { let f1 = unverified.verify(|signature, signing_string| {
self.4 self.4.signature_verify(
.signature_verify(algorithm, &key_id, signature, signing_string) algorithm,
key_id.clone(),
signature.to_string(),
signing_string.to_string(),
)
}); });
req.extensions_mut().insert(SignatureVerified(key_id)); req.extensions_mut().insert(SignatureVerified(key_id));

View file

@ -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 { impl fmt::Display for ParseSignatureError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Error when parsing {} from Http Signature", self.0) write!(f, "Error when parsing {} from Http Signature", self.0)