Merge pull request #123 from Hexilee/master

fix issue 120: DecodingKey can be converted to static
This commit is contained in:
Vincent Prouillet 2020-03-01 19:55:01 +01:00 committed by GitHub
commit d00d2f4e4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 5 deletions

View File

@ -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,

View File

@ -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()
);
}
}
}