use std::fmt;
use std::str::FromStr;
use super::TokenError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum JwtAlgorithm {
RS256,
RS384,
RS512,
PS256,
PS384,
PS512,
ES256,
ES384,
}
impl JwtAlgorithm {
pub fn as_str(self) -> &'static str {
match self {
Self::RS256 => "RS256",
Self::RS384 => "RS384",
Self::RS512 => "RS512",
Self::PS256 => "PS256",
Self::PS384 => "PS384",
Self::PS512 => "PS512",
Self::ES256 => "ES256",
Self::ES384 => "ES384",
}
}
pub(crate) fn to_jsonwebtoken(self) -> jsonwebtoken::Algorithm {
match self {
Self::RS256 => jsonwebtoken::Algorithm::RS256,
Self::RS384 => jsonwebtoken::Algorithm::RS384,
Self::RS512 => jsonwebtoken::Algorithm::RS512,
Self::PS256 => jsonwebtoken::Algorithm::PS256,
Self::PS384 => jsonwebtoken::Algorithm::PS384,
Self::PS512 => jsonwebtoken::Algorithm::PS512,
Self::ES256 => jsonwebtoken::Algorithm::ES256,
Self::ES384 => jsonwebtoken::Algorithm::ES384,
}
}
}
impl fmt::Display for JwtAlgorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for JwtAlgorithm {
type Err = TokenError;
fn from_str(name: &str) -> Result<Self, Self::Err> {
match name {
"RS256" => Ok(Self::RS256),
"RS384" => Ok(Self::RS384),
"RS512" => Ok(Self::RS512),
"PS256" => Ok(Self::PS256),
"PS384" => Ok(Self::PS384),
"PS512" => Ok(Self::PS512),
"ES256" => Ok(Self::ES256),
"ES384" => Ok(Self::ES384),
other => Err(TokenError::UnsupportedAlgorithm(format!(
"'{other}' cannot be verified against a JWKS. Use one of RS256, RS384, RS512, PS256, PS384, PS512, ES256 or ES384."
))),
}
}
}
#[cfg(test)]
mod tests {
use super::JwtAlgorithm;
use crate::sso::TokenError;
const ALL: [JwtAlgorithm; 8] = [
JwtAlgorithm::RS256,
JwtAlgorithm::RS384,
JwtAlgorithm::RS512,
JwtAlgorithm::PS256,
JwtAlgorithm::PS384,
JwtAlgorithm::PS512,
JwtAlgorithm::ES256,
JwtAlgorithm::ES384,
];
#[test]
fn every_algorithm_round_trips_through_its_jws_name() {
for algorithm in ALL {
assert_eq!(algorithm.as_str().parse::<JwtAlgorithm>().unwrap(), algorithm);
assert_eq!(algorithm.to_string(), algorithm.as_str());
assert_eq!(format!("{:?}", algorithm.to_jsonwebtoken()), algorithm.as_str());
}
}
#[test]
fn symmetric_none_and_unknown_names_are_refused() {
for name in ["HS256", "HS384", "HS512", "none", "None", "EdDSA", "rs256", ""] {
let refused = name.parse::<JwtAlgorithm>();
assert!(matches!(&refused, Err(TokenError::UnsupportedAlgorithm(_))), "{name}");
}
}
}