Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@
/// - 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)?)

Check warning on line 53 in crates/bitwarden-uniffi/src/vault/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/vault/mod.rs#L49-L53

Added lines #L49 - L53 were not covered by tests
}

/// Generate a TOTP code from a provided cipher list view.
Expand All @@ -57,6 +61,7 @@
) -> Result<TotpResponse> {
Ok(self
.0
.totp()

Check warning on line 64 in crates/bitwarden-uniffi/src/vault/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/vault/mod.rs#L64

Added line #L64 was not covered by tests
.generate_totp_cipher_view(view, time)
.map_err(Error::Totp)?)
}
Expand Down
37 changes: 23 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_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, VaultClient,
};

#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct AttachmentsClient {
pub(crate) client: Client,
}
Expand All @@ -34,6 +37,24 @@
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)?,
})?)

Check warning on line 54 in crates/bitwarden-vault/src/cipher/attachment_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-vault/src/cipher/attachment_client.rs#L54

Added line #L54 was not covered by tests
}
}

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

pub fn encrypt_file(
&self,
cipher: Cipher,
Expand All @@ -65,20 +87,6 @@
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 All @@ -93,6 +101,7 @@
}
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl VaultClient {
pub fn attachments(&self) -> AttachmentsClient {
AttachmentsClient {
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-vault/src/cipher/cipher_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl CiphersClient {
}
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl VaultClient {
pub fn ciphers(&self) -> CiphersClient {
CiphersClient {
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-vault/src/folder_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl FoldersClient {
}
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl VaultClient {
pub fn folders(&self) -> FoldersClient {
FoldersClient {
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)
}

Check warning on line 35 in crates/bitwarden-vault/src/totp_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-vault/src/totp_client.rs#L27-L35

Added lines #L27 - L35 were not covered by tests
}

impl TotpClient {
/// Generate a TOTP code from a provided key.
///
/// Key can be either:
Expand All @@ -30,3 +61,12 @@
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(),
}
}

Check warning on line 71 in crates/bitwarden-vault/src/totp_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-vault/src/totp_client.rs#L67-L71

Added lines #L67 - L71 were not covered by tests
}
3 changes: 3 additions & 0 deletions crates/bitwarden-vault/src/vault_client.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use bitwarden_core::Client;
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

use crate::{
sync::{sync, SyncError},
SyncRequest, SyncResponse,
};

#[derive(Clone)]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct VaultClient {
pub(crate) client: Client,
}
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_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 @@
}

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

Check warning on line 49 in crates/bitwarden-wasm-internal/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/client.rs#L49

Added line #L49 was not covered by tests
}

/// 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.