Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async callbacks test #757

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
43 changes: 22 additions & 21 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/bitwarden-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ sha1 = ">=0.10.5, <0.11"
sha2 = ">=0.10.6, <0.11"
subtle = ">=2.5.0, <3.0"
thiserror = ">=1.0.40, <2.0"
uniffi = { version = "=0.26.1", optional = true }
uniffi = { version = "=0.27.1", optional = true }
uuid = { version = ">=1.3.3, <2.0", features = ["serde"] }
zeroize = { version = ">=1.7.0, <2.0", features = ["derive", "aarch64"] }

Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-generators/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ schemars = { version = ">=0.8.9, <0.9", features = ["uuid1", "chrono"] }
serde = { version = ">=1.0, <2.0", features = ["derive"] }
serde_json = ">=1.0.96, <2.0"
thiserror = ">=1.0.40, <2.0"
uniffi = { version = "=0.26.1", optional = true }
uniffi = { version = "=0.27.1", optional = true }

[dev-dependencies]
rand_chacha = "0.3.1"
Expand Down
5 changes: 3 additions & 2 deletions crates/bitwarden-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bench = false

[dependencies]
async-lock = "3.3.0"
async-trait = "0.1.80"
bitwarden = { workspace = true, features = ["mobile", "internal"] }
bitwarden-crypto = { workspace = true, features = ["mobile"] }
bitwarden-generators = { workspace = true, features = ["mobile"] }
Expand All @@ -28,11 +29,11 @@ chrono = { version = ">=0.4.26, <0.5", features = [
], default-features = false }
env_logger = "0.11.1"
schemars = { version = ">=0.8, <0.9", optional = true }
uniffi = "=0.26.1"
uniffi = "=0.27.1"
uuid = ">=1.3.3, <2"

[build-dependencies]
uniffi = { version = "=0.26.1", features = ["build"] }
uniffi = { version = "=0.27.1", features = ["build"] }

[lints]
workspace = true
8 changes: 8 additions & 0 deletions crates/bitwarden-uniffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
}
}

// Need to implement this From<> impl in order to handle unexpected callback errors. See the
// Callback Interfaces section of the handbook for more info.
impl From<uniffi::UnexpectedUniFFICallbackError> for BitwardenError {
fn from(e: uniffi::UnexpectedUniFFICallbackError) -> Self {
Self::E(bitwarden::error::Error::UniffiCallback(e))
}

Check warning on line 22 in crates/bitwarden-uniffi/src/error.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/error.rs#L20-L22

Added lines #L20 - L22 were not covered by tests
}

impl Display for BitwardenError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
7 changes: 7 additions & 0 deletions crates/bitwarden-uniffi/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

mod passkeys;

#[derive(uniffi::Object)]
pub struct ClientPlatform(pub(crate) Arc<Client>);

Expand Down Expand Up @@ -37,4 +39,9 @@
self.0 .0.write().await.load_flags(flags);
Ok(())
}

/// Passkey operations
pub fn passkeys(self: Arc<Self>) -> Arc<passkeys::ClientPasskeys> {
Arc::new(passkeys::ClientPasskeys(self.0.clone()))
}

Check warning on line 46 in crates/bitwarden-uniffi/src/platform/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/platform/mod.rs#L44-L46

Added lines #L44 - L46 were not covered by tests
}
61 changes: 61 additions & 0 deletions crates/bitwarden-uniffi/src/platform/passkeys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::sync::Arc;

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

#[derive(uniffi::Object)]

Check warning on line 5 in crates/bitwarden-uniffi/src/platform/passkeys.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/platform/passkeys.rs#L5

Added line #L5 was not covered by tests
pub struct ClientPasskeys(pub(crate) Arc<Client>);

#[uniffi::export(async_runtime = "tokio")]

Check warning on line 8 in crates/bitwarden-uniffi/src/platform/passkeys.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/platform/passkeys.rs#L8

Added line #L8 was not covered by tests
impl ClientPasskeys {
pub async fn passkey_test_sync(&self, t: Arc<dyn TestTraitSync>) -> Result<String> {
Ok(self
.0
.0
.write()
.await
.platform()
.passkeys()
.passkey_test_sync(&UniffiTraitBridge(t.as_ref()))
.await?)
}

Check warning on line 20 in crates/bitwarden-uniffi/src/platform/passkeys.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/platform/passkeys.rs#L10-L20

Added lines #L10 - L20 were not covered by tests

pub async fn passkey_test_async(&self, t: Arc<dyn TestTraitAsync>) -> Result<String> {
Ok(self
.0
.0
.write()
.await
.platform()
.passkeys()
.passkey_test_async(&UniffiTraitBridge(t.as_ref()))
.await?)
}

Check warning on line 32 in crates/bitwarden-uniffi/src/platform/passkeys.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/platform/passkeys.rs#L22-L32

Added lines #L22 - L32 were not covered by tests
}

// Note that uniffi doesn't support external traits for now it seems, so we have to duplicate them here.

#[uniffi::export(with_foreign)]

Check warning on line 37 in crates/bitwarden-uniffi/src/platform/passkeys.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/platform/passkeys.rs#L37

Added line #L37 was not covered by tests
pub trait TestTraitSync: Send + Sync {
fn give_me_a_name(&self) -> String;
}

#[uniffi::export(with_foreign)]

Check warning on line 42 in crates/bitwarden-uniffi/src/platform/passkeys.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/platform/passkeys.rs#L42

Added line #L42 was not covered by tests
#[async_trait::async_trait]
pub trait TestTraitAsync: Send + Sync {
async fn give_me_a_name(&self) -> String;
}

struct UniffiTraitBridge<T>(T);

impl bitwarden::platform::TestTraitSync for UniffiTraitBridge<&dyn TestTraitSync> {
fn give_me_a_name(&self) -> String {
self.0.give_me_a_name()
}

Check warning on line 53 in crates/bitwarden-uniffi/src/platform/passkeys.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/platform/passkeys.rs#L51-L53

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

#[async_trait::async_trait]
impl bitwarden::platform::TestTraitAsync for UniffiTraitBridge<&dyn TestTraitAsync> {
async fn give_me_a_name(&self) -> String {
self.0.give_me_a_name().await
}

Check warning on line 60 in crates/bitwarden-uniffi/src/platform/passkeys.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/platform/passkeys.rs#L58-L60

Added lines #L58 - L60 were not covered by tests
}
3 changes: 2 additions & 1 deletion crates/bitwarden/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ mobile = [
wasm-bindgen = ["chrono/wasmbind"]

[dependencies]
async-trait = ">=0.1.80, <0.2"
base64 = ">=0.21.2, <0.22"
bitwarden-api-api = { workspace = true }
bitwarden-api-identity = { workspace = true }
Expand Down Expand Up @@ -59,7 +60,7 @@ serde_repr = ">=0.1.12, <0.2"
sha1 = ">=0.10.5, <0.11"
sha2 = ">=0.10.6, <0.11"
thiserror = ">=1.0.40, <2.0"
uniffi = { version = "=0.26.1", optional = true, features = ["tokio"] }
uniffi = { version = "=0.27.1", optional = true, features = ["tokio"] }
uuid = { version = ">=1.3.3, <2.0", features = ["serde"] }
zxcvbn = ">= 2.2.2, <3.0"

Expand Down
4 changes: 4 additions & 0 deletions crates/bitwarden/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ pub enum Error {
#[error(transparent)]
ExportError(#[from] ExportError),

#[cfg(feature = "mobile")]
#[error("Uniffi callback error: {0}")]
UniffiCallback(#[from] uniffi::UnexpectedUniFFICallbackError),

#[error("Internal error: {0}")]
Internal(Cow<'static, str>),
}
Expand Down
8 changes: 7 additions & 1 deletion crates/bitwarden/src/platform/client_platform.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
generate_fingerprint::{generate_fingerprint, generate_user_fingerprint},
FingerprintRequest, FingerprintResponse,
ClientPasskeys, FingerprintRequest, FingerprintResponse,
};
use crate::{error::Result, Client};

Expand All @@ -16,6 +16,12 @@
pub fn user_fingerprint(self, fingerprint_material: String) -> Result<String> {
generate_user_fingerprint(self.client, fingerprint_material)
}

pub fn passkeys(&'a mut self) -> ClientPasskeys<'a> {
ClientPasskeys {
client: self.client,
}
}

Check warning on line 24 in crates/bitwarden/src/platform/client_platform.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/client_platform.rs#L20-L24

Added lines #L20 - L24 were not covered by tests
}

impl<'a> Client {
Expand Down
2 changes: 2 additions & 0 deletions crates/bitwarden/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ pub mod client_platform;
mod domain;
mod generate_fingerprint;
mod get_user_api_key;
mod passkeys;
mod secret_verification_request;
mod sync;

pub use generate_fingerprint::{FingerprintRequest, FingerprintResponse};
pub(crate) use get_user_api_key::get_user_api_key;
pub use get_user_api_key::UserApiKeyResponse;
pub use passkeys::{ClientPasskeys, TestTraitAsync, TestTraitSync};
pub use secret_verification_request::SecretVerificationRequest;
pub(crate) use sync::sync;
pub use sync::{SyncRequest, SyncResponse};
30 changes: 30 additions & 0 deletions crates/bitwarden/src/platform/passkeys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::{error::Result, Client};

pub struct ClientPasskeys<'a> {
#[allow(dead_code)]
pub(crate) client: &'a mut Client,
}

impl<'a> ClientPasskeys<'a> {
pub async fn passkey_test_sync(&self, t: &dyn TestTraitSync) -> Result<String> {
Ok(format!("Hello {}!", t.give_me_a_name()))
}

Check warning on line 11 in crates/bitwarden/src/platform/passkeys.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/passkeys.rs#L9-L11

Added lines #L9 - L11 were not covered by tests

pub async fn passkey_test_async(&self, t: &dyn TestTraitAsync) -> Result<String> {
Ok(format!(
"Hello {}, we're using async!",
t.give_me_a_name().await

Check warning on line 16 in crates/bitwarden/src/platform/passkeys.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/passkeys.rs#L13-L16

Added lines #L13 - L16 were not covered by tests
))
}

Check warning on line 18 in crates/bitwarden/src/platform/passkeys.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/platform/passkeys.rs#L18

Added line #L18 was not covered by tests
}

//#[cfg_attr(feature = "mobile", uniffi::export(with_foreign))]
pub trait TestTraitSync: Send + Sync {
fn give_me_a_name(&self) -> String;
}

//#[cfg_attr(feature = "mobile", uniffi::export(with_foreign))]
#[async_trait::async_trait]
pub trait TestTraitAsync: Send + Sync {
async fn give_me_a_name(&self) -> String;
}
2 changes: 1 addition & 1 deletion crates/uniffi-bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ name = "uniffi-bindgen"
path = "uniffi-bindgen.rs"

[dependencies]
uniffi = { version = "=0.26.1", features = ["cli"] }
uniffi = { version = "=0.27.1", features = ["cli"] }
2 changes: 1 addition & 1 deletion languages/java/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Loading
Loading