Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 5 additions & 0 deletions src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down