@@ -6,8 +6,8 @@ use sha2::{Sha256, Sha384, Sha512};
66use signature:: { Signer , Verifier } ;
77
88use crate :: crypto:: { JwtSigner , JwtVerifier } ;
9- use crate :: errors:: { ErrorKind , Result } ;
10- use crate :: { Algorithm , DecodingKey , EncodingKey } ;
9+ use crate :: errors:: { ErrorKind , Result , new_error } ;
10+ use crate :: { Algorithm , AlgorithmFamily , DecodingKey , EncodingKey } ;
1111
1212type HmacSha256 = Hmac < Sha256 > ;
1313type HmacSha384 = Hmac < Sha384 > ;
@@ -20,7 +20,11 @@ macro_rules! define_hmac_signer {
2020
2121 impl $name {
2222 pub ( crate ) fn new( encoding_key: & EncodingKey ) -> Result <Self > {
23- let inner = <$hmac_type>:: new_from_slice( encoding_key. try_get_hmac_secret( ) ?)
23+ if encoding_key. family( ) != AlgorithmFamily :: Hmac {
24+ return Err ( new_error( ErrorKind :: InvalidKeyFormat ) ) ;
25+ }
26+
27+ let inner = <$hmac_type>:: new_from_slice( encoding_key. as_bytes( ) )
2428 . map_err( |_| ErrorKind :: InvalidKeyFormat ) ?;
2529
2630 Ok ( Self ( inner) )
@@ -52,7 +56,11 @@ macro_rules! define_hmac_verifier {
5256
5357 impl $name {
5458 pub ( crate ) fn new( decoding_key: & DecodingKey ) -> Result <Self > {
55- let inner = <$hmac_type>:: new_from_slice( decoding_key. try_get_hmac_secret( ) ?)
59+ if decoding_key. family( ) != AlgorithmFamily :: Hmac {
60+ return Err ( new_error( ErrorKind :: InvalidKeyFormat ) ) ;
61+ }
62+
63+ let inner = <$hmac_type>:: new_from_slice( decoding_key. try_get_as_bytes( ) ?)
5664 . map_err( |_| ErrorKind :: InvalidKeyFormat ) ?;
5765
5866 Ok ( Self ( inner) )
0 commit comments