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
65
66
67
68
69
70
71
pub use ft_sys::DecryptionError;

#[derive(Clone, diesel::expression::AsExpression)]
#[diesel(sql_type = diesel::sql_types::Text)]
pub struct EncryptedString(String);

impl EncryptedString {
    /// construct EncryptedString from an already encrypted string.
    /// useful for wrapping say string from a cookie.
    pub fn from_already_encrypted_string(input: String) -> Self {
        EncryptedString(input)
    }
}

impl std::fmt::Display for EncryptedString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.0.as_str())
    }
}

impl From<PlainText> for EncryptedString {
    fn from(input: PlainText) -> Self {
        EncryptedString(ft_sys::encrypt(input.0.as_str()))
    }
}

#[cfg(feature = "postgres")]
impl diesel::serialize::ToSql<diesel::sql_types::Text, diesel::pg::Pg> for EncryptedString {
    fn to_sql<'a>(
        &'a self,
        out: &mut diesel::serialize::Output<'a, '_, diesel::pg::Pg>,
    ) -> diesel::serialize::Result {
        diesel::serialize::ToSql::<diesel::sql_types::Text, diesel::pg::Pg>::to_sql(&self.0, out)
    }
}

impl TryInto<PlainText> for EncryptedString {
    type Error = ft_sdk::DecryptionError;

    fn try_into(self) -> Result<PlainText, Self::Error> {
        Ok(PlainText(ft_sys::decrypt(self.0.as_str())?))
    }
}

impl std::fmt::Debug for EncryptedString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("EncryptedString")
            .field(&"[REDACTED]")
            .finish()
    }
}

pub struct PlainText(String);

impl From<&str> for PlainText {
    fn from(input: &str) -> Self {
        PlainText(input.to_owned())
    }
}

impl From<PlainText> for String {
    fn from(val: PlainText) -> String {
        val.0
    }
}

impl std::fmt::Display for PlainText {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.0.as_str())
    }
}