Skip to content

Commit 903ecff

Browse files
committed
Move try_get_hmac_secret directly into en-/decoding key impls
1 parent 01eca4c commit 903ecff

File tree

6 files changed

+22
-48
lines changed

6 files changed

+22
-48
lines changed

src/crypto/aws_lc/hmac.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
use aws_lc_rs::hmac;
55
use signature::{Signer, Verifier};
66

7-
use crate::crypto::utils::{
8-
try_get_hmac_secret_from_decoding_key, try_get_hmac_secret_from_encoding_key,
9-
};
107
use crate::crypto::{JwtSigner, JwtVerifier};
118
use crate::errors::Result;
129
use crate::{Algorithm, DecodingKey, EncodingKey};
@@ -17,10 +14,7 @@ macro_rules! define_hmac_signer {
1714

1815
impl $name {
1916
pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> {
20-
Ok(Self(hmac::Key::new(
21-
$hmac_alg,
22-
try_get_hmac_secret_from_encoding_key(encoding_key)?,
23-
)))
17+
Ok(Self(hmac::Key::new($hmac_alg, encoding_key.try_get_hmac_secret()?)))
2418
}
2519
}
2620

@@ -44,10 +38,7 @@ macro_rules! define_hmac_verifier {
4438

4539
impl $name {
4640
pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> {
47-
Ok(Self(hmac::Key::new(
48-
$hmac_alg,
49-
try_get_hmac_secret_from_decoding_key(decoding_key)?,
50-
)))
41+
Ok(Self(hmac::Key::new($hmac_alg, decoding_key.try_get_hmac_secret()?)))
5142
}
5243
}
5344

src/crypto/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::{DecodingKey, EncodingKey};
1010
pub(crate) mod aws_lc;
1111
#[cfg(feature = "rust_crypto")]
1212
pub(crate) mod rust_crypto;
13-
pub(crate) mod utils;
1413

1514
use crate::serialization::{b64_decode, b64_encode};
1615
use signature::{Signer, Verifier};

src/crypto/rust_crypto/hmac.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ use hmac::{Hmac, Mac};
55
use sha2::{Sha256, Sha384, Sha512};
66
use signature::{Signer, Verifier};
77

8-
use crate::crypto::utils::{
9-
try_get_hmac_secret_from_decoding_key, try_get_hmac_secret_from_encoding_key,
10-
};
118
use crate::crypto::{JwtSigner, JwtVerifier};
129
use crate::errors::Result;
1310
use crate::{Algorithm, DecodingKey, EncodingKey};
@@ -23,10 +20,8 @@ macro_rules! define_hmac_signer {
2320

2421
impl $name {
2522
pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> {
26-
let inner = <$hmac_type>::new_from_slice(try_get_hmac_secret_from_encoding_key(
27-
encoding_key,
28-
)?)
29-
.map_err(|_e| crate::errors::ErrorKind::InvalidKeyFormat)?;
23+
let inner = <$hmac_type>::new_from_slice(encoding_key.try_get_hmac_secret()?)
24+
.map_err(|_e| crate::errors::ErrorKind::InvalidKeyFormat)?;
3025

3126
Ok(Self(inner))
3227
}
@@ -57,10 +52,8 @@ macro_rules! define_hmac_verifier {
5752

5853
impl $name {
5954
pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> {
60-
let inner = <$hmac_type>::new_from_slice(try_get_hmac_secret_from_decoding_key(
61-
decoding_key,
62-
)?)
63-
.map_err(|_e| crate::errors::ErrorKind::InvalidKeyFormat)?;
55+
let inner = <$hmac_type>::new_from_slice(decoding_key.try_get_hmac_secret()?)
56+
.map_err(|_e| crate::errors::ErrorKind::InvalidKeyFormat)?;
6457

6558
Ok(Self(inner))
6659
}

src/crypto/utils.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/decoding.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ impl DecodingKey {
221221
DecodingKeyKind::RsaModulusExponent { .. } => unreachable!(),
222222
}
223223
}
224+
225+
pub(crate) fn try_get_hmac_secret(&self) -> Result<&[u8]> {
226+
if self.family == AlgorithmFamily::Hmac {
227+
Ok(self.as_bytes())
228+
} else {
229+
Err(new_error(ErrorKind::InvalidKeyFormat))
230+
}
231+
}
224232
}
225233

226234
/// Decode and validate a JWT

src/encoding.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ impl EncodingKey {
114114
pub(crate) fn inner(&self) -> &[u8] {
115115
&self.content
116116
}
117+
118+
pub(crate) fn try_get_hmac_secret(&self) -> Result<&[u8]> {
119+
if self.family == AlgorithmFamily::Hmac {
120+
Ok(self.inner())
121+
} else {
122+
Err(new_error(ErrorKind::InvalidKeyFormat))
123+
}
124+
}
117125
}
118126

119127
/// Encode the header and claims given and sign the payload using the algorithm from the header and the key.

0 commit comments

Comments
 (0)