Skip to main content

bitwarden_vault/
totp_client.rs

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