Skip to content

Commit

Permalink
Change uniffi to depend directly on the individual bitwarden crates
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Oct 10, 2024
1 parent a76013c commit 7ae040c
Show file tree
Hide file tree
Showing 20 changed files with 93 additions and 207 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

4 changes: 1 addition & 3 deletions crates/bitwarden-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ repository.workspace = true
license-file.workspace = true

[features]
docs = ["dep:schemars"] # Docs

[lib]
crate-type = ["lib", "staticlib", "cdylib"]
bench = false

[dependencies]
async-trait = "0.1.80"
bitwarden = { workspace = true, features = ["internal", "uniffi"] }
bitwarden-core = { workspace = true, features = ["uniffi"] }
bitwarden-crypto = { workspace = true, features = ["uniffi"] }
bitwarden-exporters = { workspace = true, features = ["uniffi"] }
Expand All @@ -28,8 +26,8 @@ bitwarden-generators = { workspace = true, features = ["uniffi"] }
bitwarden-send = { workspace = true, features = ["uniffi"] }
bitwarden-vault = { workspace = true, features = ["uniffi"] }
chrono = { workspace = true, features = ["std"] }
log = { workspace = true }
env_logger = "0.11.1"
log = { workspace = true }
schemars = { workspace = true, optional = true }
thiserror = { workspace = true }
uniffi = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-uniffi/src/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use bitwarden::{
use bitwarden_core::{
auth::{
password::MasterPasswordPolicyOptions, AuthRequestResponse, KeyConnectorResponse,
RegisterKeyResponse, RegisterTdeKeyResponse,
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-uniffi/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use bitwarden::{
use bitwarden_core::{
mobile::crypto::{
DeriveKeyConnectorRequest, DerivePinKeyResponse, InitOrgCryptoRequest,
InitUserCryptoRequest, UpdatePasswordResponse,
Expand Down
49 changes: 0 additions & 49 deletions crates/bitwarden-uniffi/src/docs.rs

This file was deleted.

61 changes: 34 additions & 27 deletions crates/bitwarden-uniffi/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
use std::fmt::{Display, Formatter};
use bitwarden_exporters::ExportError;
use bitwarden_generators::{PassphraseError, PasswordError, UsernameError};

// Name is converted from *Error to *Exception, so we can't just name the enum Error because
// Exception already exists
#[derive(uniffi::Error, Debug)]
#[derive(uniffi::Error, thiserror::Error, Debug)]
#[uniffi(flat_error)]
pub enum BitwardenError {
E(bitwarden::error::Error),
}
#[error(transparent)]
Core(#[from] bitwarden_core::Error),

impl From<bitwarden::Error> for BitwardenError {
fn from(e: bitwarden::Error) -> Self {
Self::E(e.into())
}
}
// Generators
#[error(transparent)]
UsernameError(#[from] UsernameError),
#[error(transparent)]
PassphraseError(#[from] PassphraseError),
#[error(transparent)]
PasswordError(#[from] PasswordError),

impl From<bitwarden::error::Error> for BitwardenError {
fn from(e: bitwarden::error::Error) -> Self {
Self::E(e)
}
}
// Vault
#[error(transparent)]
Cipher(#[from] bitwarden_vault::CipherError),
#[error(transparent)]
Totp(#[from] bitwarden_vault::TotpError),

impl Display for BitwardenError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::E(e) => Display::fmt(e, f),
}
}
}
#[error(transparent)]
ExportError(#[from] ExportError),

impl std::error::Error for BitwardenError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
BitwardenError::E(e) => Some(e),
}
}
// Fido
#[error(transparent)]
MakeCredential(#[from] bitwarden_fido::MakeCredentialError),
#[error(transparent)]
GetAssertion(#[from] bitwarden_fido::GetAssertionError),
#[error(transparent)]
SilentlyDiscoverCredentials(#[from] bitwarden_fido::SilentlyDiscoverCredentialsError),
#[error(transparent)]
CredentialsForAutofillError(#[from] bitwarden_fido::CredentialsForAutofillError),
#[error(transparent)]
DecryptFido2AutofillCredentialsError(
#[from] bitwarden_fido::DecryptFido2AutofillCredentialsError,
),
#[error(transparent)]
Fido2Client(#[from] bitwarden_fido::Fido2ClientError),
}

pub type Result<T, E = BitwardenError> = std::result::Result<T, E>;
13 changes: 5 additions & 8 deletions crates/bitwarden-uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uniffi::setup_scaffolding!();
use std::sync::Arc;

use auth::ClientAuth;
use bitwarden::ClientSettings;
use bitwarden_core::ClientSettings;

pub mod auth;
pub mod crypto;
Expand All @@ -13,9 +13,6 @@ pub mod tool;
mod uniffi_support;
pub mod vault;

#[cfg(feature = "docs")]
pub mod docs;

#[cfg(target_os = "android")]
mod android_support;

Expand All @@ -26,7 +23,7 @@ use tool::{ClientExporters, ClientGenerators, ClientSends};
use vault::ClientVault;

#[derive(uniffi::Object)]
pub struct Client(bitwarden::Client);
pub struct Client(bitwarden_core::Client);

#[uniffi::export(async_runtime = "tokio")]
impl Client {
Expand All @@ -38,7 +35,7 @@ impl Client {
#[cfg(target_os = "android")]
android_support::init();

Arc::new(Self(bitwarden::Client::new(settings)))
Arc::new(Self(bitwarden_core::Client::new(settings)))
}

/// Crypto operations
Expand Down Expand Up @@ -87,9 +84,9 @@ impl Client {
.get(&url)
.send()
.await
.map_err(bitwarden::Error::Reqwest)?;
.map_err(bitwarden_core::Error::Reqwest)?;

Ok(res.text().await.map_err(bitwarden::Error::Reqwest)?)
Ok(res.text().await.map_err(bitwarden_core::Error::Reqwest)?)
}
}

Expand Down
69 changes: 23 additions & 46 deletions crates/bitwarden-uniffi/src/platform/fido2.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use std::sync::Arc;

use bitwarden::{
error::Error,
fido::{
CheckUserOptions, ClientData, ClientFido2Ext, Fido2CallbackError as BitFido2CallbackError,
GetAssertionRequest, GetAssertionResult, MakeCredentialRequest, MakeCredentialResult,
PublicKeyCredentialAuthenticatorAssertionResponse,
PublicKeyCredentialAuthenticatorAttestationResponse, PublicKeyCredentialRpEntity,
PublicKeyCredentialUserEntity,
},
vault::{Cipher, CipherView, Fido2CredentialNewView},
use bitwarden_fido::{
CheckUserOptions, ClientData, ClientFido2Ext, Fido2CallbackError as BitFido2CallbackError,
Fido2CredentialAutofillView, GetAssertionRequest, GetAssertionResult, MakeCredentialRequest,
MakeCredentialResult, Origin, PublicKeyCredentialAuthenticatorAssertionResponse,
PublicKeyCredentialAuthenticatorAttestationResponse, PublicKeyCredentialRpEntity,
PublicKeyCredentialUserEntity,
};
use bitwarden_fido::{Fido2CredentialAutofillView, Origin};
use bitwarden_vault::{Cipher, CipherView, Fido2CredentialNewView};

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

Expand Down Expand Up @@ -52,8 +48,7 @@ impl ClientFido2 {
.0
.0
.fido2()
.decrypt_fido2_autofill_credentials(cipher_view)
.map_err(Error::DecryptFido2AutofillCredentialsError)?;
.decrypt_fido2_autofill_credentials(cipher_view)?;

Ok(result)
}
Expand All @@ -77,10 +72,7 @@ impl ClientFido2Authenticator {
let cs = UniffiTraitBridge(self.2.as_ref());
let mut auth = fido2.create_authenticator(&ui, &cs);

let result = auth
.make_credential(request)
.await
.map_err(Error::MakeCredential)?;
let result = auth.make_credential(request).await?;
Ok(result)
}

Expand All @@ -90,10 +82,7 @@ impl ClientFido2Authenticator {
let cs = UniffiTraitBridge(self.2.as_ref());
let mut auth = fido2.create_authenticator(&ui, &cs);

let result = auth
.get_assertion(request)
.await
.map_err(Error::GetAssertion)?;
let result = auth.get_assertion(request).await?;
Ok(result)
}

Expand All @@ -107,10 +96,7 @@ impl ClientFido2Authenticator {
let cs = UniffiTraitBridge(self.2.as_ref());
let mut auth = fido2.create_authenticator(&ui, &cs);

let result = auth
.silently_discover_credentials(rp_id)
.await
.map_err(Error::SilentlyDiscoverCredentials)?;
let result = auth.silently_discover_credentials(rp_id).await?;
Ok(result)
}

Expand All @@ -120,10 +106,7 @@ impl ClientFido2Authenticator {
let cs = UniffiTraitBridge(self.2.as_ref());
let mut auth = fido2.create_authenticator(&ui, &cs);

let result = auth
.credentials_for_autofill()
.await
.map_err(Error::CredentialsForAutofillError)?;
let result = auth.credentials_for_autofill().await?;
Ok(result)
}
}
Expand All @@ -144,10 +127,7 @@ impl ClientFido2Client {
let cs = UniffiTraitBridge(self.0 .2.as_ref());
let mut client = fido2.create_client(&ui, &cs);

let result = client
.register(origin, request, client_data)
.await
.map_err(Error::Fido2Client)?;
let result = client.register(origin, request, client_data).await?;
Ok(result)
}

Expand All @@ -162,10 +142,7 @@ impl ClientFido2Client {
let cs = UniffiTraitBridge(self.0 .2.as_ref());
let mut client = fido2.create_client(&ui, &cs);

let result = client
.authenticate(origin, request, client_data)
.await
.map_err(Error::Fido2Client)?;
let result = client.authenticate(origin, request, client_data).await?;
Ok(result)
}
}
Expand All @@ -180,7 +157,7 @@ pub struct CheckUserResult {
user_verified: bool,
}

impl From<CheckUserResult> for bitwarden::fido::CheckUserResult {
impl From<CheckUserResult> for bitwarden_fido::CheckUserResult {
fn from(val: CheckUserResult) -> Self {
Self {
user_present: val.user_present,
Expand Down Expand Up @@ -268,7 +245,7 @@ pub trait Fido2CredentialStore: Send + Sync {
struct UniffiTraitBridge<T>(T);

#[async_trait::async_trait]
impl bitwarden::fido::Fido2CredentialStore for UniffiTraitBridge<&dyn Fido2CredentialStore> {
impl bitwarden_fido::Fido2CredentialStore for UniffiTraitBridge<&dyn Fido2CredentialStore> {
async fn find_credentials(
&self,
ids: Option<Vec<Vec<u8>>>,
Expand Down Expand Up @@ -306,9 +283,9 @@ pub enum UIHint {
RequestExistingCredential(CipherView),
}

impl From<bitwarden::fido::UIHint<'_, CipherView>> for UIHint {
fn from(hint: bitwarden::fido::UIHint<'_, CipherView>) -> Self {
use bitwarden::fido::UIHint as BWUIHint;
impl From<bitwarden_fido::UIHint<'_, CipherView>> for UIHint {
fn from(hint: bitwarden_fido::UIHint<'_, CipherView>) -> Self {
use bitwarden_fido::UIHint as BWUIHint;
match hint {
BWUIHint::InformExcludedCredentialFound(cipher) => {
UIHint::InformExcludedCredentialFound(cipher.clone())
Expand All @@ -333,12 +310,12 @@ impl From<bitwarden::fido::UIHint<'_, CipherView>> for UIHint {
}

#[async_trait::async_trait]
impl bitwarden::fido::Fido2UserInterface for UniffiTraitBridge<&dyn Fido2UserInterface> {
impl bitwarden_fido::Fido2UserInterface for UniffiTraitBridge<&dyn Fido2UserInterface> {
async fn check_user<'a>(
&self,
options: CheckUserOptions,
hint: bitwarden::fido::UIHint<'a, CipherView>,
) -> Result<bitwarden::fido::CheckUserResult, BitFido2CallbackError> {
hint: bitwarden_fido::UIHint<'a, CipherView>,
) -> Result<bitwarden_fido::CheckUserResult, BitFido2CallbackError> {
self.0
.check_user(options.clone(), hint.into())
.await
Expand All @@ -359,7 +336,7 @@ impl bitwarden::fido::Fido2UserInterface for UniffiTraitBridge<&dyn Fido2UserInt
&self,
options: CheckUserOptions,
new_credential: Fido2CredentialNewView,
) -> Result<(CipherView, bitwarden::fido::CheckUserResult), BitFido2CallbackError> {
) -> Result<(CipherView, bitwarden_fido::CheckUserResult), BitFido2CallbackError> {
self.0
.check_user_and_pick_credential_for_creation(options, new_credential)
.await
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-uniffi/src/platform/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use bitwarden::platform::FingerprintRequest;
use bitwarden_core::platform::FingerprintRequest;

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

Expand Down
Loading

0 comments on commit 7ae040c

Please sign in to comment.