Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/bitwarden-uniffi/src/vault/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ impl VaultClient {
/// - OTP Auth URI
/// - Steam URI
pub fn generate_totp(&self, key: String, time: Option<DateTime<Utc>>) -> Result<TotpResponse> {
Ok(self.0.generate_totp(key, time).map_err(Error::Totp)?)
Ok(self
.0
.totp()
.generate_totp(key, time)
.map_err(Error::Totp)?)
}

/// Generate a TOTP code from a provided cipher list view.
Expand All @@ -57,6 +61,7 @@ impl VaultClient {
) -> Result<TotpResponse> {
Ok(self
.0
.totp()
.generate_totp_cipher_view(view, time)
.map_err(Error::Totp)?)
}
Expand Down
36 changes: 22 additions & 14 deletions crates/bitwarden-vault/src/cipher/attachment_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ use bitwarden_core::Client;
use bitwarden_crypto::EncString;
use bitwarden_error::bitwarden_error;
use thiserror::Error;
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

use crate::{
Attachment, AttachmentEncryptResult, AttachmentFile, AttachmentFileView, AttachmentView,
Cipher, DecryptError, EncryptError,
};

#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct AttachmentsClient {
pub(crate) client: Client,
}
Expand All @@ -34,6 +37,24 @@ pub enum DecryptFileError {
Io(#[from] std::io::Error),
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl AttachmentsClient {
pub fn decrypt_buffer(
&self,
cipher: Cipher,
attachment: AttachmentView,
encrypted_buffer: &[u8],
) -> Result<Vec<u8>, DecryptError> {
let key_store = self.client.internal.get_key_store();

Ok(key_store.decrypt(&AttachmentFile {
cipher,
attachment,
contents: EncString::from_buffer(encrypted_buffer)?,
})?)
}
}

impl AttachmentsClient {
pub fn encrypt_buffer(
&self,
Expand All @@ -49,6 +70,7 @@ impl AttachmentsClient {
contents: buffer,
})?)
}

pub fn encrypt_file(
&self,
cipher: Cipher,
Expand All @@ -65,20 +87,6 @@ impl AttachmentsClient {
Ok(attachment)
}

pub fn decrypt_buffer(
&self,
cipher: Cipher,
attachment: AttachmentView,
encrypted_buffer: &[u8],
) -> Result<Vec<u8>, DecryptError> {
let key_store = self.client.internal.get_key_store();

Ok(key_store.decrypt(&AttachmentFile {
cipher,
attachment,
contents: EncString::from_buffer(encrypted_buffer)?,
})?)
}
pub fn decrypt_file(
&self,
cipher: Cipher,
Expand Down
4 changes: 3 additions & 1 deletion crates/bitwarden-vault/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ mod vault_client;
pub use vault_client::{VaultClient, VaultClientExt};

mod sync;
mod totp_client;
pub use sync::{SyncRequest, SyncResponse};

mod totp_client;
pub use totp_client::TotpClient;
42 changes: 41 additions & 1 deletion crates/bitwarden-vault/src/totp_client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
use bitwarden_core::Client;
use chrono::{DateTime, Utc};
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

use crate::{
generate_totp, generate_totp_cipher_view, CipherListView, TotpError, TotpResponse, VaultClient,
};

impl VaultClient {
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct TotpClient {
pub(crate) client: Client,
}

#[cfg(feature = "wasm")]
#[wasm_bindgen]
impl TotpClient {
/// Generates a TOTP code from a provided key
///
/// # Arguments
/// - `key` - Can be:
/// - A base32 encoded string
/// - OTP Auth URI
/// - Steam URI
/// - `time_ms` - Optional timestamp in milliseconds
#[wasm_bindgen(js_name = "generate_totp")]
pub fn generate_totp_wasm(
&self,
key: String,
time_ms: Option<f64>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DateTimes 😢

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're having a separate WASM function to deal with the parameters, we could use js_sys::Date or js_sys::Number here and then convert, that'll probably be a breaking change for the current SDK API though, so maybe later.

) -> Result<TotpResponse, TotpError> {
let datetime = time_ms.and_then(|time| DateTime::<Utc>::from_timestamp_millis(time as i64));

self.generate_totp(key, datetime)
}
}

impl TotpClient {
/// Generate a TOTP code from a provided key.
///
/// Key can be either:
Expand All @@ -30,3 +61,12 @@ impl VaultClient {
generate_totp_cipher_view(&mut key_store.context(), view, time)
}
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl VaultClient {
pub fn totp(&self) -> TotpClient {
TotpClient {
client: self.client.clone(),
}
}
}
42 changes: 24 additions & 18 deletions crates/bitwarden-vault/src/vault_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use bitwarden_core::Client;
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

use crate::{
sync::{sync, SyncError},
Expand All @@ -7,6 +9,7 @@ use crate::{
};

#[derive(Clone)]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct VaultClient {
pub(crate) client: Client,
}
Expand All @@ -16,6 +19,27 @@ impl VaultClient {
Self { client }
}

pub async fn sync(&self, input: &SyncRequest) -> Result<SyncResponse, SyncError> {
sync(&self.client, input).await
}

/// Collection related operations.
pub fn collections(&self) -> CollectionsClient {
CollectionsClient {
client: self.client.clone(),
}
}

/// Password history related operations.
pub fn password_history(&self) -> PasswordHistoryClient {
PasswordHistoryClient {
client: self.client.clone(),
}
}
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl VaultClient {
/// Attachment related operations.
pub fn attachments(&self) -> AttachmentsClient {
AttachmentsClient {
Expand All @@ -30,30 +54,12 @@ impl VaultClient {
}
}

/// Collection related operations.
pub fn collections(&self) -> CollectionsClient {
CollectionsClient {
client: self.client.clone(),
}
}

/// Folder related operations.
pub fn folders(&self) -> FoldersClient {
FoldersClient {
client: self.client.clone(),
}
}

/// Password history related operations.
pub fn password_history(&self) -> PasswordHistoryClient {
PasswordHistoryClient {
client: self.client.clone(),
}
}

pub async fn sync(&self, input: &SyncRequest) -> Result<SyncResponse, SyncError> {
sync(&self.client, input).await
}
}

pub trait VaultClientExt {
Expand Down
6 changes: 3 additions & 3 deletions crates/bitwarden-wasm-internal/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use bitwarden_core::{Client, ClientSettings};
use bitwarden_error::bitwarden_error;
use bitwarden_exporters::ExporterClientExt;
use bitwarden_generators::GeneratorClientsExt;
use bitwarden_vault::VaultClientExt;
use bitwarden_vault::{VaultClient, VaultClientExt};
use wasm_bindgen::prelude::*;

use crate::{CryptoClient, VaultClient};
use crate::CryptoClient;

#[wasm_bindgen]
pub struct BitwardenClient(pub(crate) Client);
Expand Down Expand Up @@ -46,7 +46,7 @@ impl BitwardenClient {
}

pub fn vault(&self) -> VaultClient {
VaultClient::new(self.0.vault())
self.0.vault()
}

/// Constructs a specific client for generating passwords and passphrases
Expand Down
2 changes: 0 additions & 2 deletions crates/bitwarden-wasm-internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ mod custom_types;
mod init;
mod pure_crypto;
mod ssh;
mod vault;

pub use bitwarden_ipc::wasm::*;
pub use client::BitwardenClient;
pub use crypto::CryptoClient;
pub use init::init_sdk;
pub use vault::VaultClient;
24 changes: 0 additions & 24 deletions crates/bitwarden-wasm-internal/src/vault/attachments.rs

This file was deleted.

35 changes: 0 additions & 35 deletions crates/bitwarden-wasm-internal/src/vault/mod.rs

This file was deleted.

36 changes: 0 additions & 36 deletions crates/bitwarden-wasm-internal/src/vault/totp.rs

This file was deleted.