diff --git a/src/decoding.rs b/src/decoding.rs index c8ea4e4..d0d3497 100644 --- a/src/decoding.rs +++ b/src/decoding.rs @@ -54,6 +54,25 @@ impl<'a> DecodingKey<'a> { } } + /// If you're using HMAC with a base64 encoded, use this. + pub fn from_base64_secret(secret: &str) -> Result { + let out = base64::decode(&secret)?; + Ok(DecodingKey { + family: AlgorithmFamily::Hmac, + kind: DecodingKeyKind::SecretOrDer(Cow::Owned(out)), + }) + } + + /// If you are loading a public RSA key in a PEM format, use this. + pub fn from_rsa_pem(key: &'a [u8]) -> Result { + let pem_key = PemEncodedKey::new(key)?; + let content = pem_key.as_rsa_key()?; + Ok(DecodingKey { + family: AlgorithmFamily::Rsa, + kind: DecodingKeyKind::SecretOrDer(Cow::Owned(content.to_vec())), + }) + } + /// If you have (n, e) RSA public key components, use this. pub fn from_rsa_components(modulus: &'a str, exponent: &'a str) -> Self { DecodingKey { @@ -65,6 +84,16 @@ impl<'a> DecodingKey<'a> { } } + /// If you have a ECDSA public key in PEM format, use this. + pub fn from_ec_pem(key: &'a [u8]) -> Result { + let pem_key = PemEncodedKey::new(key)?; + let content = pem_key.as_ec_public_key()?; + Ok(DecodingKey { + family: AlgorithmFamily::Ec, + kind: DecodingKeyKind::SecretOrDer(Cow::Owned(content.to_vec())), + }) + } + /// If you know what you're doing and have a RSA DER encoded public key, use this. pub fn from_rsa_der(der: &'a [u8]) -> Self { DecodingKey { @@ -102,37 +131,6 @@ impl<'a> DecodingKey<'a> { } } -impl DecodingKey<'static> { - /// If you're using HMAC with a base64 encoded, use this. - pub fn from_base64_secret(secret: &str) -> Result { - let out = base64::decode(&secret)?; - Ok(DecodingKey { - family: AlgorithmFamily::Hmac, - kind: DecodingKeyKind::SecretOrDer(Cow::Owned(out)), - }) - } - - /// If you are loading a public RSA key in a PEM format, use this. - pub fn from_rsa_pem(key: &[u8]) -> Result { - let pem_key = PemEncodedKey::new(key)?; - let content = pem_key.as_rsa_key()?; - Ok(DecodingKey { - family: AlgorithmFamily::Rsa, - kind: DecodingKeyKind::SecretOrDer(Cow::Owned(content.to_vec())), - }) - } - - /// If you have a ECDSA public key in PEM format, use this. - pub fn from_ec_pem(key: &[u8]) -> Result { - let pem_key = PemEncodedKey::new(key)?; - let content = pem_key.as_ec_public_key()?; - Ok(DecodingKey { - family: AlgorithmFamily::Ec, - kind: DecodingKeyKind::SecretOrDer(Cow::Owned(content.to_vec())), - }) - } -} - /// Decode and validate a JWT /// /// If the token or its signature is invalid or the claims fail validation, it will return an error.