Skip to content

Commit

Permalink
Add Account struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Nov 14, 2024
1 parent cc9a081 commit 27d8d4b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 19 deletions.
10 changes: 7 additions & 3 deletions crates/bitwarden-exporters/src/client_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bitwarden_vault::{Cipher, Collection, Folder};

use crate::{
export::{export_cxf, export_organization_vault, export_vault},
ExportError, ExportFormat,
Account, ExportError, ExportFormat,
};

pub struct ClientExporters<'a> {
Expand Down Expand Up @@ -33,8 +33,12 @@ impl<'a> ClientExporters<'a> {
export_organization_vault(collections, ciphers, format)
}

pub fn export_cxf(&self, ciphers: Vec<Cipher>) -> Result<String, ExportError> {
export_cxf(self.client, ciphers)
pub fn export_cxf(
&self,
account: Account,
ciphers: Vec<Cipher>,
) -> Result<String, ExportError> {
export_cxf(self.client, account, ciphers)
}

Check warning on line 42 in crates/bitwarden-exporters/src/client_exporter.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-exporters/src/client_exporter.rs#L36-L42

Added lines #L36 - L42 were not covered by tests
}

Expand Down
30 changes: 20 additions & 10 deletions crates/bitwarden-exporters/src/cxp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,42 @@ use bitwarden_crypto::generate_random_bytes;
use chrono::Utc;
use credential_exchange_types::{
format::{
Account, BasicAuthCredential, Credential, EditableField, FieldType, Header, Item, ItemType,
Account as CxpAccount, BasicAuthCredential, Credential, EditableField, FieldType, Header,
Item, ItemType,
},
B64Url,
};
use uuid::Uuid;

use crate::{Cipher, CipherType, Login};

mod error;
pub use error::CxpError;

pub(crate) fn build_cxf(ciphers: Vec<Cipher>) -> Result<String, CxpError> {
#[derive(Debug)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct Account {
id: Uuid,
email: String,
name: Option<String>,
}

pub(crate) fn build_cxf(account: Account, ciphers: Vec<Cipher>) -> Result<String, CxpError> {
let items: Vec<Item> = ciphers.into_iter().map(|cipher| cipher.into()).collect();

let header = Header {
version: 0,
exporter: "Bitwarden".to_string(),
timestamp: Utc::now().timestamp() as u64,
accounts: vec![Account {
id: todo!(),
user_name: todo!(),
email: todo!(),
full_name: todo!(),
icon: todo!(),
collections: todo!(),
accounts: vec![CxpAccount {
id: account.id.as_bytes().as_slice().into(),
user_name: "".to_owned(),
email: account.email,
full_name: account.name,
icon: None,
collections: vec![],
items,
extensions: todo!(),
extensions: None,
}],
};

Expand Down
13 changes: 10 additions & 3 deletions crates/bitwarden-exporters/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use bitwarden_crypto::KeyDecryptable;
use bitwarden_vault::{Cipher, CipherView, Collection, Folder, FolderView};

use crate::{
csv::export_csv, cxp::build_cxf, encrypted_json::export_encrypted_json, json::export_json,
csv::export_csv,
cxp::{build_cxf, Account},
encrypted_json::export_encrypted_json,
json::export_json,
ExportError, ExportFormat,
};

Expand Down Expand Up @@ -42,12 +45,16 @@ pub(crate) fn export_organization_vault(
todo!();
}

pub(crate) fn export_cxf(client: &Client, ciphers: Vec<Cipher>) -> Result<String, ExportError> {
pub(crate) fn export_cxf(
client: &Client,
account: Account,
ciphers: Vec<Cipher>,
) -> Result<String, ExportError> {
let enc = client.internal.get_encryption_settings()?;
let key = enc.get_key(&None)?;

Check warning on line 54 in crates/bitwarden-exporters/src/export.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-exporters/src/export.rs#L48-L54

Added lines #L48 - L54 were not covered by tests

let ciphers: Vec<CipherView> = ciphers.decrypt_with_key(key)?;
let ciphers: Vec<crate::Cipher> = ciphers.into_iter().flat_map(|c| c.try_into()).collect();

Ok(build_cxf(ciphers)?)
Ok(build_cxf(account, ciphers)?)
}

Check warning on line 60 in crates/bitwarden-exporters/src/export.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-exporters/src/export.rs#L56-L60

Added lines #L56 - L60 were not covered by tests
3 changes: 3 additions & 0 deletions crates/bitwarden-exporters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ use uuid::Uuid;

#[cfg(feature = "uniffi")]
uniffi::setup_scaffolding!();
#[cfg(feature = "uniffi")]
mod uniffi_support;

mod client_exporter;
mod csv;
mod cxp;
pub use cxp::Account;
mod encrypted_json;
mod json;
mod models;
Expand Down
3 changes: 3 additions & 0 deletions crates/bitwarden-exporters/src/uniffi_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use uuid::Uuid;

uniffi::ffi_converter_forward!(Uuid, bitwarden_core::UniFfiTag, crate::UniFfiTag);
6 changes: 3 additions & 3 deletions crates/bitwarden-uniffi/src/tool/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use bitwarden_exporters::{ClientExportersExt, ExportFormat};
use bitwarden_exporters::{Account, ClientExportersExt, ExportFormat};
use bitwarden_generators::{
ClientGeneratorExt, PassphraseGeneratorRequest, PasswordGeneratorRequest,
UsernameGeneratorRequest,
Expand Down Expand Up @@ -91,12 +91,12 @@ impl ClientExporters {
///
/// For use with Apple using [ASCredentialExportManager](https://developer.apple.com/documentation/authenticationservices/ascredentialexportmanager).
/// Ideally the output should be immediately deserialized to [ASExportedCredentialData](https://developer.apple.com/documentation/authenticationservices/asexportedcredentialdata).
pub fn export_cxf(&self, ciphers: Vec<Cipher>) -> Result<String> {
pub fn export_cxf(&self, account: Account, ciphers: Vec<Cipher>) -> Result<String> {
Ok(self
.0
.0
.exporters()
.export_cxf(ciphers)
.export_cxf(account, ciphers)
.map_err(Error::ExportError)?)
}

Check warning on line 101 in crates/bitwarden-uniffi/src/tool/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/tool/mod.rs#L94-L101

Added lines #L94 - L101 were not covered by tests
}

0 comments on commit 27d8d4b

Please sign in to comment.