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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::{str::FromStr, string::ToString};

use gettextrs::gettext;
use gtk::glib;
use ring::hmac;
use serde::{de::Deserializer, ser::Serializer, Deserialize, Serialize};

#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Default, Eq, PartialEq, Clone, Copy, glib::Enum)]
#[repr(u32)]
#[enum_type(name = "OTPMethod")]
pub enum Method {
    #[enum_value(name = "TOTP")]
    #[default]
    TOTP = 0,
    #[enum_value(name = "HOTP")]
    HOTP = 1,
    Steam = 2,
}

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

impl<'de> Deserialize<'de> for Method {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Ok(Self::from_str(&String::deserialize(deserializer)?).unwrap())
    }
}

impl From<u32> for Method {
    fn from(u: u32) -> Self {
        match u {
            1 => Self::HOTP,
            2 => Self::Steam,
            _ => Self::default(),
        }
    }
}

impl Method {
    pub fn is_time_based(self) -> bool {
        matches!(self, Self::TOTP | Self::Steam)
    }

    pub fn is_event_based(self) -> bool {
        matches!(self, Self::HOTP)
    }

    pub fn to_locale_string(self) -> String {
        match self {
            Self::HOTP => gettext("Counter-based"),
            Self::TOTP => gettext("Time-based"),
            // Translators: Steam refers to the gaming application by Valve.
            Self::Steam => gettext("Steam"),
        }
    }
}

impl FromStr for Method {
    type Err = anyhow::Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_ref() {
            "totp" | "otp" => Ok(Self::TOTP),
            "hotp" => Ok(Self::HOTP),
            "steam" => Ok(Self::Steam),
            _ => anyhow::bail!("Unsupported Method"),
        }
    }
}

impl ToString for Method {
    fn to_string(&self) -> String {
        match *self {
            Self::TOTP => "totp",
            Self::HOTP => "hotp",
            Self::Steam => "steam",
        }
        .to_string()
    }
}

#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Default, Eq, PartialEq, Clone, Copy, glib::Enum)]
#[repr(u32)]
#[enum_type(name = "OTPAlgorithm")]
pub enum Algorithm {
    #[enum_value(name = "SHA1")]
    #[default]
    SHA1 = 0,
    #[enum_value(name = "SHA256")]
    SHA256 = 1,
    #[enum_value(name = "SHA512")]
    SHA512 = 2,
}

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

impl<'de> Deserialize<'de> for Algorithm {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Ok(Self::from_str(&String::deserialize(deserializer)?).unwrap())
    }
}

impl Algorithm {
    pub fn to_locale_string(self) -> String {
        match self {
            Self::SHA1 => gettext("SHA-1"),
            Self::SHA256 => gettext("SHA-256"),
            Self::SHA512 => gettext("SHA-512"),
        }
    }
}

impl FromStr for Algorithm {
    type Err = anyhow::Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_ref() {
            "sha1" => Ok(Self::SHA1),
            "sha256" => Ok(Self::SHA256),
            "sha512" => Ok(Self::SHA512),
            _ => anyhow::bail!("Unsupported HMAC-algorithm"),
        }
    }
}

impl ToString for Algorithm {
    fn to_string(&self) -> String {
        match *self {
            Self::SHA1 => "sha1",
            Self::SHA256 => "sha256",
            Self::SHA512 => "sha512",
        }
        .to_string()
    }
}

impl From<Algorithm> for hmac::Algorithm {
    fn from(h: Algorithm) -> Self {
        match h {
            Algorithm::SHA1 => hmac::HMAC_SHA1_FOR_LEGACY_USE_ONLY,
            Algorithm::SHA256 => hmac::HMAC_SHA256,
            Algorithm::SHA512 => hmac::HMAC_SHA512,
        }
    }
}

impl From<u32> for Algorithm {
    fn from(u: u32) -> Self {
        match u {
            1 => Self::SHA256,
            2 => Self::SHA512,
            _ => Self::default(),
        }
    }
}