kraken_async_rs/crypto/
secrets.rs

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
use secrecy::{ExposeSecret, Secret};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt::{Debug, Formatter};

#[derive(Clone)]
pub struct Token(Secret<String>);

impl Token {
    pub fn new(token: impl Into<String>) -> Self {
        Token(Secret::new(token.into()))
    }
}

impl Debug for Token {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "[REDACTED TOKEN]")
    }
}

impl Eq for Token {}

impl PartialEq for Token {
    fn eq(&self, _: &Self) -> bool {
        true
    }
}

impl<'de> Deserialize<'de> for Token {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let token = String::deserialize(deserializer)?;
        Ok(Token(Secret::new(token)))
    }
}

impl Serialize for Token {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.0.expose_secret())
    }
}

#[cfg(test)]
mod tests {
    use crate::crypto::secrets::Token;

    #[test]
    fn test_secret_debug() {
        let token = Token::new("shhh");

        let str = format!("{:?}", token);

        assert_eq!("[REDACTED TOKEN]", str);
    }
}