diff options
author | Teddy Wing | 2018-11-10 15:07:17 +0100 |
---|---|---|
committer | Teddy Wing | 2018-11-10 15:07:17 +0100 |
commit | e594577e14812ff4a7ca26dd2ffb506efc469b68 (patch) | |
tree | 5f1f55a5e60b8cb23553609676c6fb5b946acdda | |
parent | 8edf2808e1b2f11ecb5e395452886e98ce8acb18 (diff) | |
download | dome-key-web-e594577e14812ff4a7ca26dd2ffb506efc469b68.tar.bz2 |
paddle: Take any kind of `str` reference input, not just `&str`
Use `AsRef<str>` instead of `&str` to offer a more flexible interface.
We need this because `url::form_urlencoded::parse()` gives us an
iterator of `(Cow<_, str>, Cow<_, str>)`, and we want to pass that into
`verify_signature()`.
Also change `key.len()` and `value.len()` to `.chars().count()` because
I was having a hard time getting the `len()` method from a trait (`str`
doesn't implement `ExactSizeIterator`), and I learned this:
> This length is in bytes, not chars or graphemes. In other words, it
> may not be what a human considers the length of the string.
(https://doc.rust-lang.org/std/primitive.str.html#method.len)
Also:
https://stackoverflow.com/questions/46290655/get-the-string-length-in-characters-in-rust/46290728#46290728
I assume the PHP serializer uses character count instead of byte length.
-rw-r--r-- | license-generator/paddle/src/lib.rs | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/license-generator/paddle/src/lib.rs b/license-generator/paddle/src/lib.rs index 3ebc3f2..11fd87f 100644 --- a/license-generator/paddle/src/lib.rs +++ b/license-generator/paddle/src/lib.rs @@ -1,5 +1,8 @@ extern crate openssl; +use std::fmt::Display; +use std::ops::Deref; + use openssl::hash::MessageDigest; use openssl::pkey::PKey; use openssl::rsa::Rsa; @@ -7,8 +10,11 @@ use openssl::sign::Verifier; // https://paddle.com/docs/reference-verifying-webhooks/ -fn verify_signature<'a, I>(pem: &[u8], signature: &str, params: I) -> bool -where I: IntoIterator<Item = (&'a str, &'a str)> + PartialOrd { +pub fn verify_signature<'a, S, I>(pem: &[u8], signature: &str, params: I) -> bool +where + S: AsRef<str> + Deref<Target = str> + Display, + I: IntoIterator<Item = (S, S)> + PartialOrd, +{ let rsa = Rsa::public_key_from_pem(pem).unwrap(); let pkey = PKey::from_rsa(rsa).unwrap(); let mut verifier = Verifier::new(MessageDigest::sha1(), &pkey).unwrap(); @@ -19,8 +25,11 @@ where I: IntoIterator<Item = (&'a str, &'a str)> + PartialOrd { verifier.verify(signature.as_ref()).unwrap() } -fn php_serialize<'a, I>(pairs: I) -> String -where I: IntoIterator<Item = (&'a str, &'a str)> + PartialOrd { +fn php_serialize<'a, S, I>(pairs: I) -> String +where + S: AsRef<str> + Deref<Target = str> + Display, + I: IntoIterator<Item = (S, S)> + PartialOrd, +{ let mut serialized = String::with_capacity(500); let mut len = 0; @@ -28,9 +37,9 @@ where I: IntoIterator<Item = (&'a str, &'a str)> + PartialOrd { serialized.push_str( &format!( "s:{key_length}:\"{key}\";s:{value_length}:\"{value}\";", - key_length = key.len(), + key_length = key.chars().count(), key = key, - value_length = value.len(), + value_length = value.chars().count(), value = value ) ); |