|
4 | 4 | use aws_lc_rs::hmac;
|
5 | 5 | use signature::{Signer, Verifier};
|
6 | 6 |
|
| 7 | +use crate::crypto::utils::{ |
| 8 | + try_get_hmac_secret_from_decoding_key, try_get_hmac_secret_from_encoding_key, |
| 9 | +}; |
7 | 10 | use crate::crypto::{JwtSigner, JwtVerifier};
|
8 | 11 | use crate::errors::Result;
|
9 |
| -use crate::{Algorithm, HmacSecret}; |
| 12 | +use crate::{Algorithm, DecodingKey, EncodingKey}; |
10 | 13 |
|
11 |
| -pub struct Hs256(hmac::Key); |
| 14 | +pub struct Hs256Signer(hmac::Key); |
12 | 15 |
|
13 |
| -impl Hs256 { |
14 |
| - pub(crate) fn new(secret: HmacSecret) -> Result<Self> { |
15 |
| - Ok(Self(hmac::Key::new(hmac::HMAC_SHA256, &secret))) |
| 16 | +impl Hs256Signer { |
| 17 | + pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> { |
| 18 | + Ok(Self(hmac::Key::new( |
| 19 | + hmac::HMAC_SHA256, |
| 20 | + try_get_hmac_secret_from_encoding_key(encoding_key)?, |
| 21 | + ))) |
16 | 22 | }
|
17 | 23 | }
|
18 | 24 |
|
19 |
| -impl Signer<Vec<u8>> for Hs256 { |
| 25 | +impl Signer<Vec<u8>> for Hs256Signer { |
20 | 26 | fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> {
|
21 | 27 | Ok(hmac::sign(&self.0, msg).as_ref().to_vec())
|
22 | 28 | }
|
23 | 29 | }
|
24 | 30 |
|
25 |
| -impl JwtSigner for Hs256 { |
| 31 | +impl JwtSigner for Hs256Signer { |
26 | 32 | fn algorithm(&self) -> Algorithm {
|
27 | 33 | Algorithm::HS256
|
28 | 34 | }
|
29 | 35 | }
|
30 | 36 |
|
31 |
| -impl Verifier<Vec<u8>> for Hs256 { |
| 37 | +pub struct Hs256Verifier(hmac::Key); |
| 38 | + |
| 39 | +impl Hs256Verifier { |
| 40 | + pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> { |
| 41 | + Ok(Self(hmac::Key::new( |
| 42 | + hmac::HMAC_SHA256, |
| 43 | + try_get_hmac_secret_from_decoding_key(decoding_key)?, |
| 44 | + ))) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl Verifier<Vec<u8>> for Hs256Verifier { |
32 | 49 | fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> {
|
33 | 50 | hmac::verify(&self.0, msg, &signature).map_err(|err| signature::Error::from_source(err))
|
34 | 51 | }
|
35 | 52 | }
|
36 | 53 |
|
37 |
| -impl JwtVerifier for Hs256 { |
| 54 | +impl JwtVerifier for Hs256Verifier { |
38 | 55 | fn algorithm(&self) -> Algorithm {
|
39 | 56 | Algorithm::HS256
|
40 | 57 | }
|
41 | 58 | }
|
42 | 59 |
|
43 |
| -pub struct Hs384(hmac::Key); |
| 60 | +pub struct Hs384Signer(hmac::Key); |
44 | 61 |
|
45 |
| -impl Hs384 { |
46 |
| - pub(crate) fn new(secret: HmacSecret) -> Result<Self> { |
47 |
| - Ok(Self(hmac::Key::new(hmac::HMAC_SHA384, &secret))) |
| 62 | +impl Hs384Signer { |
| 63 | + pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> { |
| 64 | + Ok(Self(hmac::Key::new( |
| 65 | + hmac::HMAC_SHA384, |
| 66 | + try_get_hmac_secret_from_encoding_key(encoding_key)?, |
| 67 | + ))) |
48 | 68 | }
|
49 | 69 | }
|
50 | 70 |
|
51 |
| -impl Signer<Vec<u8>> for Hs384 { |
| 71 | +impl Signer<Vec<u8>> for Hs384Signer { |
52 | 72 | fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> {
|
53 | 73 | Ok(hmac::sign(&self.0, msg).as_ref().to_vec())
|
54 | 74 | }
|
55 | 75 | }
|
56 | 76 |
|
57 |
| -impl JwtSigner for Hs384 { |
| 77 | +impl JwtSigner for Hs384Signer { |
58 | 78 | fn algorithm(&self) -> Algorithm {
|
59 | 79 | Algorithm::HS384
|
60 | 80 | }
|
61 | 81 | }
|
62 | 82 |
|
63 |
| -impl Verifier<Vec<u8>> for Hs384 { |
| 83 | +pub struct Hs384Verifier(hmac::Key); |
| 84 | + |
| 85 | +impl Hs384Verifier { |
| 86 | + pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> { |
| 87 | + Ok(Self(hmac::Key::new( |
| 88 | + hmac::HMAC_SHA384, |
| 89 | + try_get_hmac_secret_from_decoding_key(decoding_key)?, |
| 90 | + ))) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl Verifier<Vec<u8>> for Hs384Verifier { |
64 | 95 | fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> {
|
65 | 96 | hmac::verify(&self.0, msg, &signature).map_err(|err| signature::Error::from_source(err))
|
66 | 97 | }
|
67 | 98 | }
|
68 | 99 |
|
69 |
| -impl JwtVerifier for Hs384 { |
| 100 | +impl JwtVerifier for Hs384Verifier { |
70 | 101 | fn algorithm(&self) -> Algorithm {
|
71 | 102 | Algorithm::HS384
|
72 | 103 | }
|
73 | 104 | }
|
74 | 105 |
|
75 |
| -pub struct Hs512(hmac::Key); |
| 106 | +pub struct Hs512Signer(hmac::Key); |
76 | 107 |
|
77 |
| -impl Hs512 { |
78 |
| - pub(crate) fn new(secret: HmacSecret) -> Result<Self> { |
79 |
| - Ok(Self(hmac::Key::new(hmac::HMAC_SHA512, &secret))) |
| 108 | +impl Hs512Signer { |
| 109 | + pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> { |
| 110 | + Ok(Self(hmac::Key::new( |
| 111 | + hmac::HMAC_SHA512, |
| 112 | + try_get_hmac_secret_from_encoding_key(encoding_key)?, |
| 113 | + ))) |
80 | 114 | }
|
81 | 115 | }
|
82 | 116 |
|
83 |
| -impl Signer<Vec<u8>> for Hs512 { |
| 117 | +impl Signer<Vec<u8>> for Hs512Signer { |
84 | 118 | fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> {
|
85 | 119 | Ok(hmac::sign(&self.0, msg).as_ref().to_vec())
|
86 | 120 | }
|
87 | 121 | }
|
88 | 122 |
|
89 |
| -impl JwtSigner for Hs512 { |
| 123 | +impl JwtSigner for Hs512Signer { |
90 | 124 | fn algorithm(&self) -> Algorithm {
|
91 | 125 | Algorithm::HS512
|
92 | 126 | }
|
93 | 127 | }
|
94 | 128 |
|
95 |
| -impl Verifier<Vec<u8>> for Hs512 { |
| 129 | +pub struct Hs512Verifier(hmac::Key); |
| 130 | + |
| 131 | +impl Hs512Verifier { |
| 132 | + pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> { |
| 133 | + Ok(Self(hmac::Key::new( |
| 134 | + hmac::HMAC_SHA512, |
| 135 | + try_get_hmac_secret_from_decoding_key(decoding_key)?, |
| 136 | + ))) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl Verifier<Vec<u8>> for Hs512Verifier { |
96 | 141 | fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> {
|
97 | 142 | hmac::verify(&self.0, msg, &signature).map_err(|err| signature::Error::from_source(err))
|
98 | 143 | }
|
99 | 144 | }
|
100 | 145 |
|
101 |
| -impl JwtVerifier for Hs512 { |
| 146 | +impl JwtVerifier for Hs512Verifier { |
102 | 147 | fn algorithm(&self) -> Algorithm {
|
103 | 148 | Algorithm::HS512
|
104 | 149 | }
|
|
0 commit comments