1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! The ephemeral key model.
use std::fmt;
use serde::Deserialize;
use serde_json::Value;
/// A short-lived key granting a mobile or browser client temporary access to
/// one customer's data, as returned by `EphemeralKeys::create` and
/// `EphemeralKeys::delete`.
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct EphemeralKeyResponse {
/// The customer this key is scoped to.
pub customer_id: String,
/// Unix timestamp the key was created at.
pub created_at: i64,
/// Unix timestamp the key expires at.
pub expires: i64,
/// The plaintext key secret. Hand it to the client; never log it.
/// Redacted in `Debug`.
pub secret: String,
/// Any additional response fields not modeled above.
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
// Hand-written rather than derived: `secret` IS the credential this object
// exists to deliver -- anything holding it can read the customer's data for
// the key's lifetime. `{:?}` on the response is the easiest way to leak it
// into a log, so the field prints as a fixed marker. Unlike the `Option`
// secrets elsewhere in these models this one is always present, so there is
// no presence signal to preserve.
impl fmt::Debug for EphemeralKeyResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EphemeralKeyResponse")
.field("customer_id", &self.customer_id)
.field("created_at", &self.created_at)
.field("expires", &self.expires)
.field("secret", &"[redacted]")
.field("extra", &self.extra)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Kills a derived `Debug`, or one printing `&self.secret`.
#[test]
fn ephemeral_key_response_debug_redacts_the_secret() {
let res: EphemeralKeyResponse = serde_json::from_str(
r#"{"customer_id":"cus_1","created_at":1,"expires":2,
"secret":"ek_live_supersecretvalue"}"#,
)
.unwrap();
let printed = format!("{res:?}");
assert!(!printed.contains("ek_live_supersecretvalue"));
assert!(printed.contains("secret: \"[redacted]\""));
assert!(printed.contains("cus_1"));
}
}