diff --git a/src/decoding.rs b/src/decoding.rs index 36837f0..d0d3497 100644 --- a/src/decoding.rs +++ b/src/decoding.rs @@ -34,7 +34,7 @@ macro_rules! expect_two { #[derive(Debug, Clone, PartialEq)] pub(crate) enum DecodingKeyKind<'a> { SecretOrDer(Cow<'a, [u8]>), - RsaModulusExponent { n: &'a str, e: &'a str }, + RsaModulusExponent { n: Cow<'a, str>, e: Cow<'a, str> }, } /// All the different kind of keys we can use to decode a JWT @@ -77,7 +77,10 @@ impl<'a> DecodingKey<'a> { pub fn from_rsa_components(modulus: &'a str, exponent: &'a str) -> Self { DecodingKey { family: AlgorithmFamily::Rsa, - kind: DecodingKeyKind::RsaModulusExponent { n: modulus, e: exponent }, + kind: DecodingKeyKind::RsaModulusExponent { + n: Cow::Borrowed(modulus), + e: Cow::Borrowed(exponent), + }, } } @@ -107,6 +110,19 @@ impl<'a> DecodingKey<'a> { } } + /// Convert self to `DecodingKey<'static>`. + pub fn into_static(self) -> DecodingKey<'static> { + use DecodingKeyKind::*; + let DecodingKey { family, kind } = self; + let static_kind = match kind { + SecretOrDer(key) => SecretOrDer(Cow::Owned(key.into_owned())), + RsaModulusExponent { n, e } => { + RsaModulusExponent { n: Cow::Owned(n.into_owned()), e: Cow::Owned(e.into_owned()) } + } + }; + DecodingKey { family, kind: static_kind } + } + pub(crate) fn as_bytes(&self) -> &[u8] { match &self.kind { DecodingKeyKind::SecretOrDer(b) => &b, diff --git a/src/errors.rs b/src/errors.rs index 49e6f09..c016be2 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -169,7 +169,9 @@ mod tests { #[test] fn test_error_rendering() { - assert_eq!("InvalidAlgorithmName", Error::from(ErrorKind::InvalidAlgorithmName).to_string()); + assert_eq!( + "InvalidAlgorithmName", + Error::from(ErrorKind::InvalidAlgorithmName).to_string() + ); } - -} \ No newline at end of file +}