Skip to content

Commit

Permalink
Merge branch 'main' into ps/bitwarden-send
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Jun 13, 2024
2 parents 9c76c10 + e5a8dba commit 9316f60
Show file tree
Hide file tree
Showing 40 changed files with 473 additions and 376 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bitwarden-send = { path = "crates/bitwarden-send", version = "=0.5.0" }
bitwarden-vault = { path = "crates/bitwarden-vault", version = "=0.5.0" }

[workspace.lints.clippy]
unused_async = "deny"
unwrap_used = "deny"

# Compile all dependencies with some optimizations when building this crate on debug
Expand Down
2 changes: 2 additions & 0 deletions crates/bitwarden-exporters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ keywords.workspace = true

[dependencies]
base64 = ">=0.21.2, <0.23"
bitwarden-core = { workspace = true }
bitwarden-crypto = { workspace = true }
bitwarden-vault = { workspace = true }
chrono = { version = ">=0.4.26, <0.5", features = [
"clock",
"serde",
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-exporters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::csv::export_csv;
mod json;
use json::export_json;
mod encrypted_json;

use encrypted_json::export_encrypted_json;
mod models;

pub enum Format {
Csv,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use bitwarden_core::{require, MissingFieldError};
use bitwarden_vault::{
CipherType, CipherView, FieldView, FolderView, LoginUriView, SecureNoteType,
};

use crate::{login::LoginUriView, CipherType, CipherView, FieldView, FolderView, SecureNoteType};

impl TryFrom<FolderView> for bitwarden_exporters::Folder {
impl TryFrom<FolderView> for crate::Folder {
type Error = MissingFieldError;

fn try_from(value: FolderView) -> Result<Self, Self::Error> {
Expand All @@ -13,14 +14,14 @@ impl TryFrom<FolderView> for bitwarden_exporters::Folder {
}
}

impl TryFrom<CipherView> for bitwarden_exporters::Cipher {
impl TryFrom<CipherView> for crate::Cipher {
type Error = MissingFieldError;

fn try_from(value: CipherView) -> Result<Self, Self::Error> {
let r = match value.r#type {
CipherType::Login => {
let l = require!(value.login);
bitwarden_exporters::CipherType::Login(Box::new(bitwarden_exporters::Login {
crate::CipherType::Login(Box::new(crate::Login {
username: l.username,
password: l.password,
login_uris: l
Expand All @@ -32,18 +33,16 @@ impl TryFrom<CipherView> for bitwarden_exporters::Cipher {
totp: l.totp,
}))
}
CipherType::SecureNote => bitwarden_exporters::CipherType::SecureNote(Box::new(
bitwarden_exporters::SecureNote {
r#type: value
.secure_note
.map(|t| t.r#type)
.unwrap_or(SecureNoteType::Generic)
.into(),
},
)),
CipherType::SecureNote => crate::CipherType::SecureNote(Box::new(crate::SecureNote {
r#type: value
.secure_note
.map(|t| t.r#type)
.unwrap_or(SecureNoteType::Generic)
.into(),
})),
CipherType::Card => {
let c = require!(value.card);
bitwarden_exporters::CipherType::Card(Box::new(bitwarden_exporters::Card {
crate::CipherType::Card(Box::new(crate::Card {
cardholder_name: c.cardholder_name,
exp_month: c.exp_month,
exp_year: c.exp_year,
Expand All @@ -54,7 +53,7 @@ impl TryFrom<CipherView> for bitwarden_exporters::Cipher {
}
CipherType::Identity => {
let i = require!(value.identity);
bitwarden_exporters::CipherType::Identity(Box::new(bitwarden_exporters::Identity {
crate::CipherType::Identity(Box::new(crate::Identity {
title: i.title,
first_name: i.first_name,
middle_name: i.middle_name,
Expand Down Expand Up @@ -98,7 +97,7 @@ impl TryFrom<CipherView> for bitwarden_exporters::Cipher {
}
}

impl From<FieldView> for bitwarden_exporters::Field {
impl From<FieldView> for crate::Field {
fn from(value: FieldView) -> Self {
Self {
name: value.name,
Expand All @@ -109,7 +108,7 @@ impl From<FieldView> for bitwarden_exporters::Field {
}
}

impl From<LoginUriView> for bitwarden_exporters::LoginUri {
impl From<LoginUriView> for crate::LoginUri {
fn from(value: LoginUriView) -> Self {
Self {
r#match: value.r#match.map(|v| v as u8),
Expand All @@ -118,20 +117,20 @@ impl From<LoginUriView> for bitwarden_exporters::LoginUri {
}
}

impl From<SecureNoteType> for bitwarden_exporters::SecureNoteType {
impl From<SecureNoteType> for crate::SecureNoteType {
fn from(value: SecureNoteType) -> Self {
match value {
SecureNoteType::Generic => bitwarden_exporters::SecureNoteType::Generic,
SecureNoteType::Generic => crate::SecureNoteType::Generic,
}
}
}

#[cfg(test)]
mod tests {
use bitwarden_vault::{CipherRepromptType, LoginView};
use chrono::{DateTime, Utc};

use super::*;
use crate::{CipherRepromptType, LoginView};

#[test]
fn test_try_from_folder_view() {
Expand All @@ -141,7 +140,7 @@ mod tests {
revision_date: "2024-01-30T17:55:36.150Z".parse().unwrap(),
};

let f: bitwarden_exporters::Folder = view.try_into().unwrap();
let f: crate::Folder = view.try_into().unwrap();

assert_eq!(
f.id,
Expand Down Expand Up @@ -187,7 +186,7 @@ mod tests {
revision_date: "2024-01-30T17:55:36.150Z".parse().unwrap(),
};

let cipher: bitwarden_exporters::Cipher = cipher_view.try_into().unwrap();
let cipher: crate::Cipher = cipher_view.try_into().unwrap();

assert_eq!(
cipher.id,
Expand All @@ -209,7 +208,7 @@ mod tests {
);
assert_eq!(cipher.deleted_date, None);

if let bitwarden_exporters::CipherType::Login(l) = cipher.r#type {
if let crate::CipherType::Login(l) = cipher.r#type {
assert_eq!(l.username, Some("test_username".to_string()));
assert_eq!(l.password, Some("test_password".to_string()));
assert!(l.login_uris.is_empty());
Expand Down
2 changes: 0 additions & 2 deletions crates/bitwarden-uniffi/src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl ClientAuth {
.await
.auth()
.password_strength(password, email, additional_inputs)
.await
}

/// Evaluate if the provided password satisfies the provided policy
Expand All @@ -42,7 +41,6 @@ impl ClientAuth {
.await
.auth()
.satisfies_policy(password, strength, &policy)
.await
}

/// Hash the user password
Expand Down
8 changes: 3 additions & 5 deletions crates/bitwarden-uniffi/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,14 @@ impl ClientCrypto {
.write()
.await
.crypto()
.update_password(new_password)
.await?)
.update_password(new_password)?)
}

/// Generates a PIN protected user key from the provided PIN. The result can be stored and later
/// used to initialize another client instance by using the PIN and the PIN key with
/// `initialize_user_crypto`.
pub async fn derive_pin_key(&self, pin: String) -> Result<DerivePinKeyResponse> {
Ok(self.0 .0.write().await.crypto().derive_pin_key(pin).await?)
Ok(self.0 .0.write().await.crypto().derive_pin_key(pin)?)
}

/// Derives the pin protected user key from encrypted pin. Used when pin requires master
Expand All @@ -80,8 +79,7 @@ impl ClientCrypto {
.write()
.await
.crypto()
.derive_pin_user_key(encrypted_pin)
.await?)
.derive_pin_user_key(encrypted_pin)?)
}

pub async fn enroll_admin_password_reset(
Expand Down
36 changes: 26 additions & 10 deletions crates/bitwarden-uniffi/src/platform/fido2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use bitwarden::{
error::Error,
platform::fido2::{
CheckUserOptions, ClientData, Fido2CallbackError as BitFido2CallbackError,
GetAssertionRequest, GetAssertionResult, MakeCredentialRequest, MakeCredentialResult,
Expand Down Expand Up @@ -62,9 +63,12 @@ impl ClientFido2Authenticator {
let mut fido2 = platform.fido2();
let ui = UniffiTraitBridge(self.1.as_ref());
let cs = UniffiTraitBridge(self.2.as_ref());
let mut auth = fido2.create_authenticator(&ui, &cs)?;
let mut auth = fido2.create_authenticator(&ui, &cs);

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

Expand All @@ -75,9 +79,12 @@ impl ClientFido2Authenticator {
let mut fido2 = platform.fido2();
let ui = UniffiTraitBridge(self.1.as_ref());
let cs = UniffiTraitBridge(self.2.as_ref());
let mut auth = fido2.create_authenticator(&ui, &cs)?;
let mut auth = fido2.create_authenticator(&ui, &cs);

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

Expand All @@ -91,9 +98,12 @@ impl ClientFido2Authenticator {
let mut fido2 = platform.fido2();
let ui = UniffiTraitBridge(self.1.as_ref());
let cs = UniffiTraitBridge(self.2.as_ref());
let mut auth = fido2.create_authenticator(&ui, &cs)?;
let mut auth = fido2.create_authenticator(&ui, &cs);

let result = auth.silently_discover_credentials(rp_id).await?;
let result = auth
.silently_discover_credentials(rp_id)
.await
.map_err(Error::SilentlyDiscoverCredentials)?;
Ok(result)
}
}
Expand All @@ -115,9 +125,12 @@ impl ClientFido2Client {
let mut fido2 = platform.fido2();
let ui = UniffiTraitBridge(self.0 .1.as_ref());
let cs = UniffiTraitBridge(self.0 .2.as_ref());
let mut client = fido2.create_client(&ui, &cs)?;
let mut client = fido2.create_client(&ui, &cs);

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

Expand All @@ -133,9 +146,12 @@ impl ClientFido2Client {
let mut fido2 = platform.fido2();
let ui = UniffiTraitBridge(self.0 .1.as_ref());
let cs = UniffiTraitBridge(self.0 .2.as_ref());
let mut client = fido2.create_client(&ui, &cs)?;
let mut client = fido2.create_client(&ui, &cs);

let result = client.authenticate(origin, request, client_data).await?;
let result = client
.authenticate(origin, request, client_data)
.await
.map_err(Error::Fido2Client)?;
Ok(result)
}
}
Expand Down
24 changes: 4 additions & 20 deletions crates/bitwarden-uniffi/src/tool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,12 @@ pub struct ClientGenerators(pub(crate) Arc<Client>);
impl ClientGenerators {
/// **API Draft:** Generate Password
pub async fn password(&self, settings: PasswordGeneratorRequest) -> Result<String> {
Ok(self
.0
.0
.read()
.await
.generator()
.password(settings)
.await?)
Ok(self.0 .0.read().await.generator().password(settings)?)
}

/// **API Draft:** Generate Passphrase
pub async fn passphrase(&self, settings: PassphraseGeneratorRequest) -> Result<String> {
Ok(self
.0
.0
.read()
.await
.generator()
.passphrase(settings)
.await?)
Ok(self.0 .0.read().await.generator().passphrase(settings)?)
}

/// **API Draft:** Generate Username
Expand Down Expand Up @@ -71,8 +57,7 @@ impl ClientExporters {
.read()
.await
.exporters()
.export_vault(folders, ciphers, format)
.await?)
.export_vault(folders, ciphers, format)?)
}

/// **API Draft:** Export organization vault
Expand All @@ -88,7 +73,6 @@ impl ClientExporters {
.read()
.await
.exporters()
.export_organization_vault(collections, ciphers, format)
.await?)
.export_organization_vault(collections, ciphers, format)?)
}
}
Loading

0 comments on commit 9316f60

Please sign in to comment.