Skip to content

Commit

Permalink
Mock a TOTP implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Oct 17, 2023
1 parent 6b8b84f commit 82f32ec
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 8 deletions.
8 changes: 7 additions & 1 deletion crates/bitwarden-uniffi/src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -35,4 +38,7 @@ pub enum DocRef {

// Kdf
Kdf(Kdf),

/// TOTP
TotpResponse(TotpResponse),
}
12 changes: 12 additions & 0 deletions crates/bitwarden-uniffi/src/vault/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::sync::Arc;

use bitwarden::vault::TotpResponse;

use crate::Client;

pub mod ciphers;
Expand Down Expand Up @@ -37,4 +39,14 @@ impl ClientVault {
pub fn sends(self: Arc<Self>) -> Arc<sends::ClientSends> {
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
}
}
16 changes: 16 additions & 0 deletions crates/bitwarden/src/mobile/vault/client_totp.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions crates/bitwarden/src/mobile/vault/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ mod client_collection;
mod client_folders;
mod client_password_history;
mod client_sends;
mod client_totp;
mod client_vault;
2 changes: 2 additions & 0 deletions crates/bitwarden/src/vault/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
17 changes: 17 additions & 0 deletions crates/bitwarden/src/vault/totp.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
51 changes: 44 additions & 7 deletions languages/kotlin/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,23 @@ Sends operations

**Output**: Arc<sends::ClientSends>

### `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
Expand Down Expand Up @@ -541,17 +558,17 @@ implementations.
</tr>
<tr>
<th>attachments</th>
<th>array</th>
<th>array,null</th>
<th></th>
</tr>
<tr>
<th>fields</th>
<th>array</th>
<th>array,null</th>
<th></th>
</tr>
<tr>
<th>passwordHistory</th>
<th>array</th>
<th>array,null</th>
<th></th>
</tr>
<tr>
Expand Down Expand Up @@ -606,7 +623,7 @@ implementations.
</tr>
<tr>
<th>notes</th>
<th>string</th>
<th>string,null</th>
<th></th>
</tr>
<tr>
Expand Down Expand Up @@ -666,17 +683,17 @@ implementations.
</tr>
<tr>
<th>attachments</th>
<th>array</th>
<th>array,null</th>
<th></th>
</tr>
<tr>
<th>fields</th>
<th>array</th>
<th>array,null</th>
<th></th>
</tr>
<tr>
<th>passwordHistory</th>
<th>array</th>
<th>array,null</th>
<th></th>
</tr>
<tr>
Expand Down Expand Up @@ -1253,3 +1270,23 @@ implementations.
<th></th>
</tr>
</table>

## `TotpResponse`

<table>
<tr>
<th>Key</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<th>code</th>
<th>string</th>
<th></th>
</tr>
<tr>
<th>interval</th>
<th>integer</th>
<th></th>
</tr>
</table>

0 comments on commit 82f32ec

Please sign in to comment.