delopay 0.3.0

Official Rust SDK for the DeloPay payments API
Documentation
//! 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"));
    }
}