Skip to content

Commit

Permalink
Mock a TOTP implementation (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton authored Oct 17, 2023
1 parent fdfc2e9 commit f51b5b9
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/bitwarden-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ bench = false

[dependencies]
async-lock = "2.7.0"
chrono = { version = ">=0.4.26, <0.5", features = [
"serde",
"std",
], default-features = false }
env_logger = "0.10.0"
uniffi = "=0.24.1"
schemars = { version = ">=0.8, <0.9", optional = true }
Expand Down
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),
}
1 change: 1 addition & 0 deletions crates/bitwarden-uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use bitwarden::{client::client_settings::ClientSettings, mobile::crypto::InitCry
pub mod auth;
mod error;
pub mod tool;
mod uniffi_support;
pub mod vault;

#[cfg(feature = "docs")]
Expand Down
16 changes: 16 additions & 0 deletions crates/bitwarden-uniffi/src/uniffi_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::UniffiCustomTypeConverter;

type DateTime = chrono::DateTime<chrono::Utc>;
uniffi::custom_type!(DateTime, std::time::SystemTime);

impl UniffiCustomTypeConverter for chrono::DateTime<chrono::Utc> {
type Builtin = std::time::SystemTime;

fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
Ok(Self::from(val))
}

fn from_custom(obj: Self) -> Self::Builtin {
obj.into()
}
}
19 changes: 19 additions & 0 deletions crates/bitwarden-uniffi/src/vault/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::sync::Arc;

use bitwarden::vault::TotpResponse;
use chrono::{DateTime, Utc};

use crate::Client;

pub mod ciphers;
Expand Down Expand Up @@ -37,4 +40,20 @@ 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, time: Option<DateTime<Utc>>) -> TotpResponse {
self.0
.0
.read()
.await
.vault()
.generate_totp(key, time)
.await
}
}
18 changes: 18 additions & 0 deletions crates/bitwarden/src/mobile/vault/client_totp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use chrono::{DateTime, Utc};

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, time: Option<DateTime<Utc>>) -> TotpResponse {
generate_totp(key, time).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};
30 changes: 30 additions & 0 deletions crates/bitwarden/src/vault/totp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use chrono::{DateTime, Utc};
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 {
/// Generated TOTP code
pub code: String,
/// Time period
pub period: u32,
}

/// Generate a OATH or RFC 6238 TOTP code from a provided key.
///
/// <https://datatracker.ietf.org/doc/html/rfc6238>
///
/// Key can be either:
/// - A base32 encoded string
/// - OTP Auth URI
/// - Steam URI
///
/// Supports providing an optional time, and defaults to current system time if none is provided.
pub async fn generate_totp(_key: String, _time: Option<DateTime<Utc>>) -> TotpResponse {
TotpResponse {
code: "000 000".to_string(),
period: 30,
}
}
52 changes: 45 additions & 7 deletions languages/kotlin/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,24 @@ 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
- time: Option<DateTime>

**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 +559,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 +624,7 @@ implementations.
</tr>
<tr>
<th>notes</th>
<th>string</th>
<th>string,null</th>
<th></th>
</tr>
<tr>
Expand Down Expand Up @@ -666,17 +684,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 +1271,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>Generated TOTP code</th>
</tr>
<tr>
<th>period</th>
<th>integer</th>
<th>Time period</th>
</tr>
</table>

0 comments on commit f51b5b9

Please sign in to comment.