totp_rs/
custom_providers.rs

1#[cfg(feature = "steam")]
2use crate::{Algorithm, TOTP};
3
4#[cfg(feature = "steam")]
5#[cfg_attr(docsrs, doc(cfg(feature = "steam")))]
6impl TOTP {
7    #[cfg(feature = "otpauth")]
8    /// Will create a new instance of TOTP using the Steam algorithm with given parameters. See [the doc](struct.TOTP.html#fields) for reference as to how to choose those values
9    ///
10    /// # Description
11    /// * `secret`: expect a non-encoded value, to pass in base32 string use `Secret::Encoded(String)`
12    ///
13    /// # Example
14    ///
15    /// ```rust
16    /// use totp_rs::{Secret, TOTP};
17    /// let secret = Secret::Encoded("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".into());
18    /// let totp = TOTP::new_steam(secret.to_bytes().unwrap(), "username".into());
19    /// ```
20    pub fn new_steam(secret: Vec<u8>, account_name: String) -> TOTP {
21        Self::new_unchecked(
22            Algorithm::Steam,
23            5,
24            1,
25            30,
26            secret,
27            Some("Steam".into()),
28            account_name,
29        )
30    }
31
32    #[cfg(not(feature = "otpauth"))]
33    /// Will create a new instance of TOTP using the Steam algorithm with given parameters. See [the doc](struct.TOTP.html#fields) for reference as to how to choose those values
34    ///
35    /// # Description
36    /// * `secret`: expect a non-encoded value, to pass in base32 string use `Secret::Encoded(String)`
37    ///
38    /// # Example
39    ///
40    /// ```rust
41    /// use totp_rs::{Secret, TOTP};
42    /// let secret = Secret::Encoded("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".to_string());
43    /// let totp = TOTP::new_steam(secret.to_bytes().unwrap());
44    /// ```
45    pub fn new_steam(secret: Vec<u8>) -> TOTP {
46        Self::new_unchecked(Algorithm::Steam, 5, 1, 30, secret)
47    }
48}
49
50#[cfg(all(test, feature = "steam"))]
51mod test {
52    #[cfg(feature = "otpauth")]
53    use super::*;
54
55    #[test]
56    #[cfg(feature = "otpauth")]
57    fn get_url_steam() {
58        let totp = TOTP::new_steam("TestSecretSuperSecret".into(), "constantoine".into());
59        let url = totp.get_url();
60        assert_eq!(url.as_str(), "otpauth://steam/Steam:constantoine?secret=KRSXG5CTMVRXEZLUKN2XAZLSKNSWG4TFOQ&digits=5&algorithm=SHA1&issuer=Steam");
61    }
62}