Skip to content

Commit 8b8fc1f

Browse files
authored
Implement Debug on Decoding/EncodingKey while redacting sensitive fields (#406)
1 parent a670368 commit 8b8fc1f

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/decoding.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt::{Debug, Formatter};
2+
13
use base64::{engine::general_purpose::STANDARD, Engine};
24
use serde::de::DeserializeOwned;
35

@@ -69,9 +71,22 @@ pub(crate) enum DecodingKeyKind {
6971
RsaModulusExponent { n: Vec<u8>, e: Vec<u8> },
7072
}
7173

74+
impl Debug for DecodingKeyKind {
75+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
76+
match self {
77+
Self::SecretOrDer(_) => f.debug_tuple("SecretOrDer").field(&"[redacted]").finish(),
78+
Self::RsaModulusExponent { .. } => f
79+
.debug_struct("RsaModulusExponent")
80+
.field("n", &"[redacted]")
81+
.field("e", &"[redacted]")
82+
.finish(),
83+
}
84+
}
85+
}
86+
7287
/// All the different kind of keys we can use to decode a JWT.
7388
/// This key can be re-used so make sure you only initialize it once if you can for better performance.
74-
#[derive(Clone)]
89+
#[derive(Clone, Debug)]
7590
pub struct DecodingKey {
7691
pub(crate) family: AlgorithmFamily,
7792
pub(crate) kind: DecodingKeyKind,

src/encoding.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt::{Debug, Formatter};
2+
13
use base64::{engine::general_purpose::STANDARD, Engine};
24
use serde::ser::Serialize;
35

@@ -124,6 +126,15 @@ impl EncodingKey {
124126
}
125127
}
126128

129+
impl Debug for EncodingKey {
130+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
131+
f.debug_struct("EncodingKey")
132+
.field("family", &self.family)
133+
.field("content", &"[redacted]")
134+
.finish()
135+
}
136+
}
137+
127138
/// Encode the header and claims given and sign the payload using the algorithm from the header and the key.
128139
/// If the algorithm given is RSA or EC, the key needs to be in the PEM format.
129140
///

0 commit comments

Comments
 (0)