Skip to content

Commit d00d2f4

Browse files
authored
Merge pull request #123 from Hexilee/master
fix issue 120: DecodingKey can be converted to static
2 parents cecc353 + 946aef1 commit d00d2f4

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/decoding.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ macro_rules! expect_two {
3434
#[derive(Debug, Clone, PartialEq)]
3535
pub(crate) enum DecodingKeyKind<'a> {
3636
SecretOrDer(Cow<'a, [u8]>),
37-
RsaModulusExponent { n: &'a str, e: &'a str },
37+
RsaModulusExponent { n: Cow<'a, str>, e: Cow<'a, str> },
3838
}
3939

4040
/// All the different kind of keys we can use to decode a JWT
@@ -77,7 +77,10 @@ impl<'a> DecodingKey<'a> {
7777
pub fn from_rsa_components(modulus: &'a str, exponent: &'a str) -> Self {
7878
DecodingKey {
7979
family: AlgorithmFamily::Rsa,
80-
kind: DecodingKeyKind::RsaModulusExponent { n: modulus, e: exponent },
80+
kind: DecodingKeyKind::RsaModulusExponent {
81+
n: Cow::Borrowed(modulus),
82+
e: Cow::Borrowed(exponent),
83+
},
8184
}
8285
}
8386

@@ -107,6 +110,19 @@ impl<'a> DecodingKey<'a> {
107110
}
108111
}
109112

113+
/// Convert self to `DecodingKey<'static>`.
114+
pub fn into_static(self) -> DecodingKey<'static> {
115+
use DecodingKeyKind::*;
116+
let DecodingKey { family, kind } = self;
117+
let static_kind = match kind {
118+
SecretOrDer(key) => SecretOrDer(Cow::Owned(key.into_owned())),
119+
RsaModulusExponent { n, e } => {
120+
RsaModulusExponent { n: Cow::Owned(n.into_owned()), e: Cow::Owned(e.into_owned()) }
121+
}
122+
};
123+
DecodingKey { family, kind: static_kind }
124+
}
125+
110126
pub(crate) fn as_bytes(&self) -> &[u8] {
111127
match &self.kind {
112128
DecodingKeyKind::SecretOrDer(b) => &b,

src/errors.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ mod tests {
169169

170170
#[test]
171171
fn test_error_rendering() {
172-
assert_eq!("InvalidAlgorithmName", Error::from(ErrorKind::InvalidAlgorithmName).to_string());
172+
assert_eq!(
173+
"InvalidAlgorithmName",
174+
Error::from(ErrorKind::InvalidAlgorithmName).to_string()
175+
);
173176
}
174-
175-
}
177+
}

0 commit comments

Comments
 (0)