diff --git a/src/algorithms.rs b/src/algorithms.rs index e162bab2..646a63c6 100644 --- a/src/algorithms.rs +++ b/src/algorithms.rs @@ -3,13 +3,35 @@ use serde::{Deserialize, Serialize}; use std::str::FromStr; #[derive(Debug, Eq, PartialEq, Copy, Clone, Serialize, Deserialize)] -pub(crate) enum AlgorithmFamily { +pub enum AlgorithmFamily { Hmac, Rsa, Ec, Ed, } +impl AlgorithmFamily { + /// A list of all possible Algorithms that are part of the family. + pub fn algorithms(&self) -> &[Algorithm] { + match self { + Self::Hmac => &[Algorithm::HS256, Algorithm::HS384, Algorithm::HS512], + Self::Rsa => &[ + Algorithm::RS256, + Algorithm::RS384, + Algorithm::RS512, + Algorithm::PS256, + Algorithm::PS384, + Algorithm::PS384, + Algorithm::PS512, + ], + Self::Ec => &[Algorithm::ES256, Algorithm::ES384], + Self::Ed => &[Algorithm::EdDSA], + } + } +} + + + /// The algorithms supported for signing/verifying JWTs #[allow(clippy::upper_case_acronyms)] #[derive(Debug, Default, PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize)] diff --git a/src/decoding.rs b/src/decoding.rs index 8d87f03d..7dea76ca 100644 --- a/src/decoding.rs +++ b/src/decoding.rs @@ -56,6 +56,11 @@ pub struct DecodingKey { } impl DecodingKey { + /// The algorithm family this key is for. + pub fn family(&self) -> AlgorithmFamily { + self.family + } + /// If you're using HMAC, use this. pub fn from_secret(secret: &[u8]) -> Self { DecodingKey { diff --git a/src/encoding.rs b/src/encoding.rs index 26f5c4c3..fc4304a7 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -18,6 +18,11 @@ pub struct EncodingKey { } impl EncodingKey { + /// The algorithm family this key is for. + pub fn family(&self) -> AlgorithmFamily { + self.family + } + /// If you're using a HMAC secret that is not base64, use that. pub fn from_secret(secret: &[u8]) -> Self { EncodingKey { family: AlgorithmFamily::Hmac, content: secret.to_vec() }