From 82f32ec6fe3260764491ac1fc9b8c25a9d309e09 Mon Sep 17 00:00:00 2001 From: Hinton Date: Tue, 17 Oct 2023 11:45:35 +0200 Subject: [PATCH] Mock a TOTP implementation --- crates/bitwarden-uniffi/src/docs.rs | 8 ++- crates/bitwarden-uniffi/src/vault/mod.rs | 12 +++++ .../bitwarden/src/mobile/vault/client_totp.rs | 16 ++++++ crates/bitwarden/src/mobile/vault/mod.rs | 1 + crates/bitwarden/src/vault/mod.rs | 2 + crates/bitwarden/src/vault/totp.rs | 17 +++++++ languages/kotlin/doc.md | 51 ++++++++++++++++--- 7 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 crates/bitwarden/src/mobile/vault/client_totp.rs create mode 100644 crates/bitwarden/src/vault/totp.rs diff --git a/crates/bitwarden-uniffi/src/docs.rs b/crates/bitwarden-uniffi/src/docs.rs index bd30e974e..bc47a19c3 100644 --- a/crates/bitwarden-uniffi/src/docs.rs +++ b/crates/bitwarden-uniffi/src/docs.rs @@ -3,7 +3,10 @@ use bitwarden::{ client::kdf::Kdf, mobile::crypto::InitCryptoRequest, tool::{ExportFormat, PassphraseGeneratorRequest, PasswordGeneratorRequest}, - vault::{Cipher, CipherView, Collection, Folder, FolderView, Send, SendListView, SendView}, + vault::{ + Cipher, CipherView, Collection, Folder, FolderView, Send, SendListView, SendView, + TotpResponse, + }, }; use schemars::JsonSchema; @@ -35,4 +38,7 @@ pub enum DocRef { // Kdf Kdf(Kdf), + + /// TOTP + TotpResponse(TotpResponse), } diff --git a/crates/bitwarden-uniffi/src/vault/mod.rs b/crates/bitwarden-uniffi/src/vault/mod.rs index 2692632ab..dda37203c 100644 --- a/crates/bitwarden-uniffi/src/vault/mod.rs +++ b/crates/bitwarden-uniffi/src/vault/mod.rs @@ -1,5 +1,7 @@ use std::sync::Arc; +use bitwarden::vault::TotpResponse; + use crate::Client; pub mod ciphers; @@ -37,4 +39,14 @@ impl ClientVault { pub fn sends(self: Arc) -> Arc { Arc::new(sends::ClientSends(self.0.clone())) } + + /// Generate a TOTP code from a provided key. + /// + /// The key can be either: + /// - A base32 encoded string + /// - OTP Auth URI + /// - Steam URI + pub async fn generate_totp(&self, key: String) -> TotpResponse { + self.0 .0.read().await.vault().generate_totp(key).await + } } diff --git a/crates/bitwarden/src/mobile/vault/client_totp.rs b/crates/bitwarden/src/mobile/vault/client_totp.rs new file mode 100644 index 000000000..f2c31ced7 --- /dev/null +++ b/crates/bitwarden/src/mobile/vault/client_totp.rs @@ -0,0 +1,16 @@ +use crate::vault::{generate_totp, TotpResponse}; + +use super::client_vault::ClientVault; + +impl<'a> ClientVault<'a> { + /// Generate a TOTP code from a provided key. + /// + /// Key can be either: + /// - A base32 encoded string + /// - OTP Auth URI + /// - Steam URI + /// + pub async fn generate_totp(&'a self, key: String) -> TotpResponse { + generate_totp(key).await + } +} diff --git a/crates/bitwarden/src/mobile/vault/mod.rs b/crates/bitwarden/src/mobile/vault/mod.rs index f0fe4ca76..f22f004ce 100644 --- a/crates/bitwarden/src/mobile/vault/mod.rs +++ b/crates/bitwarden/src/mobile/vault/mod.rs @@ -3,4 +3,5 @@ mod client_collection; mod client_folders; mod client_password_history; mod client_sends; +mod client_totp; mod client_vault; diff --git a/crates/bitwarden/src/vault/mod.rs b/crates/bitwarden/src/vault/mod.rs index 9eecd6620..12910283c 100644 --- a/crates/bitwarden/src/vault/mod.rs +++ b/crates/bitwarden/src/vault/mod.rs @@ -3,9 +3,11 @@ mod collection; mod folder; mod password_history; mod send; +mod totp; pub use cipher::{Cipher, CipherListView, CipherView}; pub use collection::{Collection, CollectionView}; pub use folder::{Folder, FolderView}; pub use password_history::{PasswordHistory, PasswordHistoryView}; pub use send::{Send, SendListView, SendView}; +pub use totp::{generate_totp, TotpResponse}; diff --git a/crates/bitwarden/src/vault/totp.rs b/crates/bitwarden/src/vault/totp.rs new file mode 100644 index 000000000..7d04deb96 --- /dev/null +++ b/crates/bitwarden/src/vault/totp.rs @@ -0,0 +1,17 @@ +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, JsonSchema)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +#[cfg_attr(feature = "mobile", derive(uniffi::Record))] +pub struct TotpResponse { + pub code: String, + pub interval: u32, +} + +pub async fn generate_totp(_key: String) -> TotpResponse { + TotpResponse { + code: "000 000".to_string(), + interval: 30, + } +} diff --git a/languages/kotlin/doc.md b/languages/kotlin/doc.md index 9e8c19929..8168888e9 100644 --- a/languages/kotlin/doc.md +++ b/languages/kotlin/doc.md @@ -441,6 +441,23 @@ Sends operations **Output**: Arc +### `generate_totp` + +Generate a TOTP code from a provided key. + +The key can be either: + +- A base32 encoded string +- OTP Auth URI +- Steam URI + +**Arguments**: + +- self: +- key: String + +**Output**: [TotpResponse](#totpresponse) + # References References are generated from the JSON schemas and should mostly match the kotlin and swift @@ -541,17 +558,17 @@ implementations. attachments - array + array,null fields - array + array,null passwordHistory - array + array,null @@ -606,7 +623,7 @@ implementations. notes - string + string,null @@ -666,17 +683,17 @@ implementations. attachments - array + array,null fields - array + array,null passwordHistory - array + array,null @@ -1253,3 +1270,23 @@ implementations. + +## `TotpResponse` + + + + + + + + + + + + + + + + + +
KeyTypeDescription
codestring
intervalinteger