Skip to content

Commit

Permalink
Add uniffi support
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Mar 25, 2024
1 parent d2de7d6 commit a5a8097
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
18 changes: 17 additions & 1 deletion crates/bitwarden-crypto/src/uniffi_support.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{num::NonZeroU32, str::FromStr};

use crate::{AsymmetricEncString, CryptoError, EncString, UniffiCustomTypeConverter};
use crate::{
AsymmetricEncString, CryptoError, EncString, SensitiveString, UniffiCustomTypeConverter,
};

uniffi::custom_type!(NonZeroU32, u32);

Expand Down Expand Up @@ -43,3 +45,17 @@ impl UniffiCustomTypeConverter for AsymmetricEncString {
obj.to_string()
}
}

uniffi::custom_type!(SensitiveString, String);

impl UniffiCustomTypeConverter for SensitiveString {
type Builtin = String;

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

Check warning on line 56 in crates/bitwarden-crypto/src/uniffi_support.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-crypto/src/uniffi_support.rs#L54-L56

Added lines #L54 - L56 were not covered by tests

fn from_custom(obj: Self) -> Self::Builtin {
obj.expose().to_owned()
}

Check warning on line 60 in crates/bitwarden-crypto/src/uniffi_support.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-crypto/src/uniffi_support.rs#L58-L60

Added lines #L58 - L60 were not covered by tests
}
4 changes: 2 additions & 2 deletions crates/bitwarden-uniffi/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use bitwarden::mobile::crypto::{
DerivePinKeyResponse, InitOrgCryptoRequest, InitUserCryptoRequest, UpdatePasswordResponse,
};
use bitwarden_crypto::{AsymmetricEncString, EncString};
use bitwarden_crypto::{AsymmetricEncString, EncString, SensitiveString};

use crate::{error::Result, Client};

Expand Down Expand Up @@ -40,7 +40,7 @@ impl ClientCrypto {

/// Get the uses's decrypted encryption key. Note: It's very important
/// to keep this key safe, as it can be used to decrypt all of the user's data
pub async fn get_user_encryption_key(&self) -> Result<String> {
pub async fn get_user_encryption_key(&self) -> Result<SensitiveString> {

Check warning on line 43 in crates/bitwarden-uniffi/src/crypto.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/crypto.rs#L43

Added line #L43 was not covered by tests
Ok(self
.0
.0
Expand Down
3 changes: 2 additions & 1 deletion crates/bitwarden-uniffi/src/uniffi_support.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use bitwarden_crypto::{AsymmetricEncString, EncString};
use bitwarden_crypto::{AsymmetricEncString, EncString, SensitiveString};

// Forward the type definitions to the main bitwarden crate
type DateTime = chrono::DateTime<chrono::Utc>;
uniffi::ffi_converter_forward!(DateTime, bitwarden::UniFfiTag, crate::UniFfiTag);
uniffi::ffi_converter_forward!(EncString, bitwarden::UniFfiTag, crate::UniFfiTag);
uniffi::ffi_converter_forward!(AsymmetricEncString, bitwarden::UniFfiTag, crate::UniFfiTag);
uniffi::ffi_converter_forward!(SensitiveString, bitwarden::UniFfiTag, crate::UniFfiTag);
4 changes: 2 additions & 2 deletions crates/bitwarden/src/mobile/client_crypto.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "internal")]
use bitwarden_crypto::{AsymmetricEncString, EncString};
use bitwarden_crypto::{AsymmetricEncString, EncString, SensitiveString};

use crate::Client;
#[cfg(feature = "internal")]
Expand Down Expand Up @@ -28,7 +28,7 @@ impl<'a> ClientCrypto<'a> {
}

#[cfg(feature = "internal")]
pub async fn get_user_encryption_key(&mut self) -> Result<String> {
pub async fn get_user_encryption_key(&mut self) -> Result<SensitiveString> {

Check warning on line 31 in crates/bitwarden/src/mobile/client_crypto.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/mobile/client_crypto.rs#L31

Added line #L31 was not covered by tests
get_user_encryption_key(self.client).await
}

Expand Down
8 changes: 5 additions & 3 deletions crates/bitwarden/src/mobile/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::collections::HashMap;

use bitwarden_crypto::{AsymmetricEncString, EncString};
#[cfg(feature = "internal")]
use bitwarden_crypto::{KeyDecryptable, KeyEncryptable, MasterKey, SymmetricCryptoKey};
use bitwarden_crypto::{
KeyDecryptable, KeyEncryptable, MasterKey, SensitiveString, SymmetricCryptoKey,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -168,13 +170,13 @@ pub async fn initialize_org_crypto(client: &mut Client, req: InitOrgCryptoReques
}

#[cfg(feature = "internal")]
pub async fn get_user_encryption_key(client: &mut Client) -> Result<String> {
pub async fn get_user_encryption_key(client: &mut Client) -> Result<SensitiveString> {

Check warning on line 173 in crates/bitwarden/src/mobile/crypto.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/mobile/crypto.rs#L173

Added line #L173 was not covered by tests
let user_key = client
.get_encryption_settings()?
.get_key(&None)
.ok_or(Error::VaultLocked)?;

Ok(user_key.to_base64().expose().to_owned())
Ok(user_key.to_base64())
}

#[cfg(feature = "internal")]
Expand Down
7 changes: 6 additions & 1 deletion crates/bitwarden/src/uniffi_support.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::num::NonZeroU32;

use bitwarden_crypto::{AsymmetricEncString, EncString};
use bitwarden_crypto::{AsymmetricEncString, EncString, SensitiveString};
use uuid::Uuid;

use crate::UniffiCustomTypeConverter;
Expand All @@ -12,6 +12,11 @@ uniffi::ffi_converter_forward!(
bitwarden_crypto::UniFfiTag,
crate::UniFfiTag
);
uniffi::ffi_converter_forward!(
SensitiveString,
bitwarden_crypto::UniFfiTag,
crate::UniFfiTag
);

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

0 comments on commit a5a8097

Please sign in to comment.