removed unnecessary conversions (#180)

* removed unnecessary conversions
This commit is contained in:
Saber Haj Rabiee 2021-03-05 00:10:03 -08:00 committed by Vincent Prouillet
parent de5a1903b0
commit 45fb43c1f7
7 changed files with 19 additions and 21 deletions

View File

@ -34,5 +34,5 @@ pub fn sign(
let signing_key = signature::EcdsaKeyPair::from_pkcs8(alg, key)?; let signing_key = signature::EcdsaKeyPair::from_pkcs8(alg, key)?;
let rng = rand::SystemRandom::new(); let rng = rand::SystemRandom::new();
let out = signing_key.sign(&rng, message)?; let out = signing_key.sign(&rng, message)?;
Ok(b64_encode(out.as_ref())) Ok(b64_encode(out))
} }

View File

@ -19,5 +19,5 @@ pub(crate) fn alg_to_ec_verification(alg: Algorithm) -> &'static signature::EdDS
pub fn sign(key: &[u8], message: &[u8]) -> Result<String> { pub fn sign(key: &[u8], message: &[u8]) -> Result<String> {
let signing_key = signature::Ed25519KeyPair::from_pkcs8_maybe_unchecked(key)?; let signing_key = signature::Ed25519KeyPair::from_pkcs8_maybe_unchecked(key)?;
let out = signing_key.sign(message); let out = signing_key.sign(message);
Ok(b64_encode(out.as_ref())) Ok(b64_encode(out))
} }

View File

@ -15,7 +15,7 @@ pub(crate) mod rsa;
/// Could be in its own file to match RSA/EC but it's 2 lines... /// Could be in its own file to match RSA/EC but it's 2 lines...
pub(crate) fn sign_hmac(alg: hmac::Algorithm, key: &[u8], message: &[u8]) -> String { pub(crate) fn sign_hmac(alg: hmac::Algorithm, key: &[u8], message: &[u8]) -> String {
let digest = hmac::sign(&hmac::Key::new(alg, key), message); let digest = hmac::sign(&hmac::Key::new(alg, key), message);
b64_encode(digest.as_ref()) b64_encode(digest)
} }
/// Take the payload of a JWT, sign it using the algorithm given and return /// Take the payload of a JWT, sign it using the algorithm given and return

View File

@ -45,7 +45,7 @@ pub(crate) fn sign(
let rng = rand::SystemRandom::new(); let rng = rand::SystemRandom::new();
key_pair.sign(alg, &rng, message, &mut signature).map_err(|_| ErrorKind::RsaFailedSigning)?; key_pair.sign(alg, &rng, message, &mut signature).map_err(|_| ErrorKind::RsaFailedSigning)?;
Ok(b64_encode(&signature)) Ok(b64_encode(signature))
} }
/// Checks that a signature is valid based on the (n, e) RSA pubkey components /// Checks that a signature is valid based on the (n, e) RSA pubkey components

View File

@ -114,10 +114,10 @@ pub fn encode<T: Serialize>(header: &Header, claims: &T, key: &EncodingKey) -> R
if key.family != header.alg.family() { if key.family != header.alg.family() {
return Err(new_error(ErrorKind::InvalidAlgorithm)); return Err(new_error(ErrorKind::InvalidAlgorithm));
} }
let encoded_header = b64_encode_part(&header)?; let encoded_header = b64_encode_part(header)?;
let encoded_claims = b64_encode_part(&claims)?; let encoded_claims = b64_encode_part(claims)?;
let message = [encoded_header.as_ref(), encoded_claims.as_ref()].join("."); let message = [encoded_header, encoded_claims].join(".");
let signature = crypto::sign(&*message.as_bytes(), key, header.alg)?; let signature = crypto::sign(message.as_bytes(), key, header.alg)?;
Ok([message, signature].join(".")) Ok([message, signature].join("."))
} }

View File

@ -65,11 +65,9 @@ impl Header {
} }
/// Converts an encoded part into the Header struct if possible /// Converts an encoded part into the Header struct if possible
pub(crate) fn from_encoded(encoded_part: &str) -> Result<Self> { pub(crate) fn from_encoded<T: AsRef<[u8]>>(encoded_part: T) -> Result<Self> {
let decoded = b64_decode(encoded_part)?; let decoded = b64_decode(encoded_part)?;
let s = String::from_utf8(decoded)?; Ok(serde_json::from_slice(&decoded)?)
Ok(serde_json::from_str(&s)?)
} }
} }

View File

@ -1,32 +1,32 @@
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::ser::Serialize; use serde::ser::Serialize;
use serde_json::map::Map; use serde_json::map::Map;
use serde_json::{from_str, to_string, Value}; use serde_json::{from_slice, to_vec, Value};
use crate::errors::Result; use crate::errors::Result;
pub(crate) fn b64_encode(input: &[u8]) -> String { pub(crate) fn b64_encode<T: AsRef<[u8]>>(input: T) -> String {
base64::encode_config(input, base64::URL_SAFE_NO_PAD) base64::encode_config(input, base64::URL_SAFE_NO_PAD)
} }
pub(crate) fn b64_decode(input: &str) -> Result<Vec<u8>> { pub(crate) fn b64_decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>> {
base64::decode_config(input, base64::URL_SAFE_NO_PAD).map_err(|e| e.into()) base64::decode_config(input, base64::URL_SAFE_NO_PAD).map_err(|e| e.into())
} }
/// Serializes a struct to JSON and encodes it in base64 /// Serializes a struct to JSON and encodes it in base64
pub(crate) fn b64_encode_part<T: Serialize>(input: &T) -> Result<String> { pub(crate) fn b64_encode_part<T: Serialize>(input: &T) -> Result<String> {
let json = to_string(input)?; let json = to_vec(input)?;
Ok(b64_encode(json.as_bytes())) Ok(b64_encode(json))
} }
/// Decodes from base64 and deserializes from JSON to a struct AND a hashmap of Value so we can /// Decodes from base64 and deserializes from JSON to a struct AND a hashmap of Value so we can
/// run validation on it /// run validation on it
pub(crate) fn from_jwt_part_claims<B: AsRef<str>, T: DeserializeOwned>( pub(crate) fn from_jwt_part_claims<B: AsRef<[u8]>, T: DeserializeOwned>(
encoded: B, encoded: B,
) -> Result<(T, Map<String, Value>)> { ) -> Result<(T, Map<String, Value>)> {
let s = String::from_utf8(b64_decode(encoded.as_ref())?)?; let s = b64_decode(encoded)?;
let claims: T = from_str(&s)?; let claims: T = from_slice(&s)?;
let validation_map: Map<_, _> = from_str(&s)?; let validation_map: Map<_, _> = from_slice(&s)?;
Ok((claims, validation_map)) Ok((claims, validation_map))
} }