feat: Implement FromStr trait for Algorithm

This commit is contained in:
himanoa 2018-08-31 03:53:55 +09:00
parent 1f9af2e505
commit 4bd4c8b3de
3 changed files with 31 additions and 1 deletions

View File

@ -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<Self> {
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<String> {
let signing_key = hmac::SigningKey::new(alg, key);

View File

@ -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

View File

@ -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());
}