2019-11-06 17:32:13 -05:00
|
|
|
use crate::errors::{Error, ErrorKind, Result};
|
2019-11-03 10:46:08 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-06-16 11:51:43 -04:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2020-01-13 13:38:33 -05:00
|
|
|
#[derive(Debug, Eq, PartialEq, Copy, Clone, Serialize, Deserialize)]
|
|
|
|
pub(crate) enum AlgorithmFamily {
|
|
|
|
Hmac,
|
|
|
|
Rsa,
|
|
|
|
Ec,
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:43:43 -05:00
|
|
|
/// The algorithms supported for signing/verifying JWTs
|
2019-06-16 11:51:43 -04:00
|
|
|
#[derive(Debug, PartialEq, Copy, Clone, Serialize, Deserialize)]
|
|
|
|
pub enum Algorithm {
|
|
|
|
/// HMAC using SHA-256
|
|
|
|
HS256,
|
|
|
|
/// HMAC using SHA-384
|
|
|
|
HS384,
|
|
|
|
/// HMAC using SHA-512
|
|
|
|
HS512,
|
|
|
|
|
|
|
|
/// ECDSA using SHA-256
|
|
|
|
ES256,
|
|
|
|
/// ECDSA using SHA-384
|
|
|
|
ES384,
|
|
|
|
|
|
|
|
/// RSASSA-PKCS1-v1_5 using SHA-256
|
|
|
|
RS256,
|
|
|
|
/// RSASSA-PKCS1-v1_5 using SHA-384
|
|
|
|
RS384,
|
|
|
|
/// RSASSA-PKCS1-v1_5 using SHA-512
|
|
|
|
RS512,
|
2019-06-16 12:00:00 -04:00
|
|
|
|
|
|
|
/// RSASSA-PSS using SHA-256
|
|
|
|
PS256,
|
|
|
|
/// RSASSA-PSS using SHA-384
|
|
|
|
PS384,
|
|
|
|
/// RSASSA-PSS using SHA-512
|
|
|
|
PS512,
|
2019-06-16 11:51:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Algorithm {
|
|
|
|
fn default() -> Self {
|
|
|
|
Algorithm::HS256
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
"ES256" => Ok(Algorithm::ES256),
|
|
|
|
"ES384" => Ok(Algorithm::ES384),
|
|
|
|
"RS256" => Ok(Algorithm::RS256),
|
|
|
|
"RS384" => Ok(Algorithm::RS384),
|
2019-06-16 12:00:00 -04:00
|
|
|
"PS256" => Ok(Algorithm::PS256),
|
|
|
|
"PS384" => Ok(Algorithm::PS384),
|
|
|
|
"PS512" => Ok(Algorithm::PS512),
|
2019-06-16 11:51:43 -04:00
|
|
|
"RS512" => Ok(Algorithm::RS512),
|
2019-11-06 17:32:13 -05:00
|
|
|
_ => Err(ErrorKind::InvalidAlgorithmName.into()),
|
2019-06-16 11:51:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-03 10:36:19 -05:00
|
|
|
|
2020-01-13 13:38:33 -05:00
|
|
|
impl Algorithm {
|
|
|
|
pub(crate) fn family(self) -> AlgorithmFamily {
|
|
|
|
match self {
|
|
|
|
Algorithm::HS256 | Algorithm::HS384 | Algorithm::HS512 => AlgorithmFamily::Hmac,
|
|
|
|
Algorithm::RS256
|
|
|
|
| Algorithm::RS384
|
|
|
|
| Algorithm::RS512
|
|
|
|
| Algorithm::PS256
|
|
|
|
| Algorithm::PS384
|
|
|
|
| Algorithm::PS512 => AlgorithmFamily::Rsa,
|
|
|
|
Algorithm::ES256 | Algorithm::ES384 => AlgorithmFamily::Ec,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-03 10:36:19 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[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("PS256").is_ok());
|
|
|
|
assert!(Algorithm::from_str("PS384").is_ok());
|
|
|
|
assert!(Algorithm::from_str("PS512").is_ok());
|
|
|
|
assert!(Algorithm::from_str("").is_err());
|
|
|
|
}
|
|
|
|
}
|