diff --git a/src/crypto.rs b/src/crypto.rs index 110de2e..81bbda1 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -4,8 +4,9 @@ use base64; use ring::{rand, digest, hmac, signature}; use ring::constant_time::verify_slices_are_equal; use untrusted; +use std::str::FromStr; -use errors::{Result, ErrorKind}; +use errors::{Result, Error, ErrorKind, new_error}; /// The algorithms supported for signing/verifying @@ -32,6 +33,21 @@ impl Default for Algorithm { } } +impl FromStr for Algorithm { + type Err = Error; + fn from_str(s: &str) -> Result { + match s { + "HS256" => Ok(Algorithm::HS256), + "HS384" => Ok(Algorithm::HS384), + "HS512" => Ok(Algorithm::HS512), + "RS256" => Ok(Algorithm::HS256), + "RS384" => Ok(Algorithm::HS384), + "RS512" => Ok(Algorithm::HS512), + _ => Err(new_error(ErrorKind::InvalidAlgorithmName)), + } + } +} + /// The actual HS signing + encoding fn sign_hmac(alg: &'static digest::Algorithm, key: &[u8], signing_input: &str) -> Result { let signing_key = hmac::SigningKey::new(alg, key); diff --git a/src/errors.rs b/src/errors.rs index 2f85b5c..6ff1f1d 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -39,6 +39,8 @@ pub enum ErrorKind { InvalidSignature, /// When the secret given is not a valid RSA key InvalidRsaKey, + /// When the algorithm from string doesn't match the one passed to `from_str` + InvalidAlgorithmName, // validation error diff --git a/tests/lib.rs b/tests/lib.rs index 98b926b..740d5c0 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -5,6 +5,7 @@ extern crate chrono; use jsonwebtoken::{encode, decode, decode_header, dangerous_unsafe_decode, Algorithm, Header, sign, verify, Validation}; use chrono::Utc; +use std::str::FromStr; #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] struct Claims { @@ -152,3 +153,14 @@ fn does_validation_in_right_order() { println!("{:?}", res); //assert!(res.is_ok()); } + +#[test] +fn generate_algorithm_enum_from_str() { + assert!(Algorithm::from_str("HS256").is_ok()); + assert!(Algorithm::from_str("HS384").is_ok()); + assert!(Algorithm::from_str("HS512").is_ok()); + assert!(Algorithm::from_str("RS256").is_ok()); + assert!(Algorithm::from_str("RS384").is_ok()); + assert!(Algorithm::from_str("RS512").is_ok()); + assert!(Algorithm::from_str("").is_err()); +}