Skip to content
Merged
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
20 changes: 18 additions & 2 deletions src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
},
}
}

Expand Down Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

}
}