Skip to main content

bitwarden_vault/
totp_client.rs

1use chrono::{DateTime, Utc};
2#[cfg(feature = "wasm")]
3use wasm_bindgen::prelude::*;
4
5use crate::{CipherListView, TotpError, TotpResponse, generate_totp, generate_totp_cipher_view};
6
7#[allow(missing_docs)]
8#[cfg_attr(feature = "wasm", wasm_bindgen)]
9pub struct TotpClient;
10
11#[cfg(feature = "wasm")]
12#[wasm_bindgen]
13impl TotpClient {
14    /// Generates a TOTP code from a provided key
15    ///
16    /// # Arguments
17    /// - `key` - Can be:
18    ///     - A base32 encoded string
19    ///     - OTP Auth URI
20    ///     - Steam URI
21    /// - `time_ms` - Optional timestamp in milliseconds
22    #[wasm_bindgen(js_name = "generate_totp")]
23    pub fn generate_totp_wasm(
24        &self,
25        key: String,
26        time_ms: Option<f64>,
27    ) -> Result<TotpResponse, TotpError> {
28        let datetime = time_ms.and_then(|time| DateTime::<Utc>::from_timestamp_millis(time as i64));
29
30        self.generate_totp(key, datetime)
31    }
32}
33
34impl TotpClient {
35    /// Generate a TOTP code from a provided key.
36    ///
37    /// Key can be either:
38    /// - A base32 encoded string
39    /// - OTP Auth URI
40    /// - Steam URI
41    pub fn generate_totp(
42        &self,
43        key: String,
44        time: Option<DateTime<Utc>>,
45    ) -> Result<TotpResponse, TotpError> {
46        generate_totp(key, time)
47    }
48
49    /// Generate a TOTP code from a provided cipher list view.
50    pub fn generate_totp_cipher_view(
51        &self,
52        view: CipherListView,
53        time: Option<DateTime<Utc>>,
54    ) -> Result<TotpResponse, TotpError> {
55        generate_totp_cipher_view(view, time)
56    }
57}