Skip to content

Commit

Permalink
Migrate remaining sync to try from
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Oct 20, 2023
1 parent 68c5747 commit 4c5bb78
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 200 deletions.
28 changes: 12 additions & 16 deletions crates/bitwarden/src/platform/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,33 +93,29 @@ impl SyncResponse {
profile: ProfileResponse::process_response(profile, enc)?,
folders: response
.folders
.ok_or(Error::MissingFields)?
.into_iter()
.map(|f| f.into())
.collect(),
.map(|f| f.into_iter().map(|f| f.try_into()).collect())
.transpose()?
.ok_or(Error::MissingFields)?,
collections: response
.collections
.ok_or(Error::MissingFields)?
.into_iter()
.map(|c| c.into())
.collect(),
.map(|c| c.into_iter().map(|c| c.try_into()).collect())
.transpose()?
.ok_or(Error::MissingFields)?,
ciphers: ciphers
.into_iter()
.map(|c| c.try_into())
.collect::<Result<Vec<Cipher>>>()?,
domains: response.domains.map(|d| (*d).try_into()).transpose()?,
policies: response
.policies
.ok_or(Error::MissingFields)?
.into_iter()
.map(|p| p.try_into())
.collect::<Result<Vec<Policy>>>()?,
.map(|p| p.into_iter().map(|p| p.try_into()).collect())
.transpose()?
.ok_or(Error::MissingFields)?,
sends: response
.sends
.ok_or(Error::MissingFields)?
.into_iter()
.map(|s| s.into())
.collect(),
.map(|s| s.into_iter().map(|s| s.try_into()).collect())
.transpose()?
.ok_or(Error::MissingFields)?,
})
}
}
Expand Down
25 changes: 12 additions & 13 deletions crates/bitwarden/src/vault/cipher/attachment.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::str::FromStr;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::{
client::encryption_settings::EncryptionSettings,
crypto::{Decryptable, EncString, Encryptable},
error::Result,
error::{Error, Result},
};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
Expand Down Expand Up @@ -55,26 +53,27 @@ impl Decryptable<AttachmentView> for Attachment {
url: self.url.clone(),
size: self.size.clone(),
size_name: self.size_name.clone(),
file_name: self.file_name.decrypt(enc, org_id).unwrap(),
file_name: self.file_name.decrypt(enc, org_id)?,
key: self
.key
.as_ref()
.map(|key| enc.decrypt_bytes(key, org_id).unwrap()),
.map(|key| enc.decrypt_bytes(key, org_id))
.transpose()?,
})
}
}

impl From<bitwarden_api_api::models::AttachmentResponseModel> for Attachment {
fn from(attachment: bitwarden_api_api::models::AttachmentResponseModel) -> Self {
Self {
impl TryFrom<bitwarden_api_api::models::AttachmentResponseModel> for Attachment {
type Error = Error;

fn try_from(attachment: bitwarden_api_api::models::AttachmentResponseModel) -> Result<Self> {
Ok(Self {
id: attachment.id,
url: attachment.url,
size: attachment.size.map(|s| s.to_string()),
size_name: attachment.size_name,
file_name: attachment
.file_name
.map(|s| EncString::from_str(&s).unwrap()),
key: attachment.key.map(|s| EncString::from_str(&s).unwrap()),
}
file_name: EncString::try_from(attachment.file_name)?,
key: EncString::try_from(attachment.key)?,
})
}
}
28 changes: 13 additions & 15 deletions crates/bitwarden/src/vault/cipher/card.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::FromStr;

use bitwarden_api_api::models::CipherCardModel;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand All @@ -8,7 +6,7 @@ use uuid::Uuid;
use crate::{
client::encryption_settings::EncryptionSettings,
crypto::{Decryptable, EncString, Encryptable},
error::Result,
error::{Error, Result},
};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
Expand Down Expand Up @@ -61,17 +59,17 @@ impl Decryptable<CardView> for Card {
}
}

impl From<CipherCardModel> for Card {
fn from(card: CipherCardModel) -> Self {
Self {
cardholder_name: card
.cardholder_name
.map(|s| EncString::from_str(&s).unwrap()),
exp_month: card.exp_month.map(|s| EncString::from_str(&s).unwrap()),
exp_year: card.exp_year.map(|s| EncString::from_str(&s).unwrap()),
code: card.code.map(|s| EncString::from_str(&s).unwrap()),
brand: card.brand.map(|s| EncString::from_str(&s).unwrap()),
number: card.number.map(|s| EncString::from_str(&s).unwrap()),
}
impl TryFrom<CipherCardModel> for Card {
type Error = Error;

fn try_from(card: CipherCardModel) -> Result<Self> {
Ok(Self {
cardholder_name: EncString::try_from(card.cardholder_name)?,
exp_month: EncString::try_from(card.exp_month)?,
exp_year: EncString::try_from(card.exp_year)?,
code: EncString::try_from(card.code)?,
brand: EncString::try_from(card.brand)?,
number: EncString::try_from(card.number)?,
})
}
}
24 changes: 11 additions & 13 deletions crates/bitwarden/src/vault/cipher/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ impl TryFrom<CipherDetailsResponseModel> for Cipher {
notes: EncString::try_from(cipher.notes)?,
r#type: cipher.r#type.ok_or(Error::MissingFields)?.into(),
login: cipher.login.map(|l| (*l).try_into()).transpose()?,
identity: cipher.identity.map(|i| (*i).into()),
card: cipher.card.map(|c| (*c).into()),
secure_note: cipher.secure_note.map(|s| (*s).into()),
identity: cipher.identity.map(|i| (*i).try_into()).transpose()?,
card: cipher.card.map(|c| (*c).try_into()).transpose()?,
secure_note: cipher.secure_note.map(|s| (*s).try_into()).transpose()?,
favorite: cipher.favorite.unwrap_or(false),
reprompt: cipher
.reprompt
Expand All @@ -313,20 +313,18 @@ impl TryFrom<CipherDetailsResponseModel> for Cipher {
edit: cipher.edit.unwrap_or(true),
view_password: cipher.view_password.unwrap_or(true),
local_data: None, // Not sent from server
attachments: Some(
cipher
.attachments
.unwrap_or_default()
.into_iter()
.map(|a| a.into())
.collect(),
),
attachments: cipher
.attachments
.map(|a| a.into_iter().map(|a| a.try_into()).collect())
.transpose()?,
fields: cipher
.fields
.map(|f| f.into_iter().map(|f| f.into()).collect()),
.map(|f| f.into_iter().map(|f| f.try_into()).collect())
.transpose()?,
password_history: cipher
.password_history
.map(|p| p.into_iter().map(|p| p.into()).collect()),
.map(|p| p.into_iter().map(|p| p.try_into()).collect())
.transpose()?,
creation_date: cipher.creation_date.ok_or(Error::MissingFields)?.parse()?,
deleted_date: cipher.deleted_date.map(|d| d.parse()).transpose()?,
revision_date: cipher.revision_date.ok_or(Error::MissingFields)?.parse()?,
Expand Down
25 changes: 14 additions & 11 deletions crates/bitwarden/src/vault/cipher/field.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::FromStr;

use bitwarden_api_api::models::CipherFieldModel;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand All @@ -10,7 +8,7 @@ use super::linked_id::LinkedIdType;
use crate::{
client::encryption_settings::EncryptionSettings,
crypto::{Decryptable, EncString, Encryptable},
error::Result,
error::{Error, Result},
};

#[derive(Clone, Copy, Serialize_repr, Deserialize_repr, Debug, JsonSchema)]
Expand Down Expand Up @@ -67,14 +65,19 @@ impl Decryptable<FieldView> for Field {
}
}

impl From<CipherFieldModel> for Field {
fn from(model: CipherFieldModel) -> Self {
Self {
name: model.name.map(|s| EncString::from_str(&s).unwrap()),
value: model.value.map(|s| EncString::from_str(&s).unwrap()),
r#type: model.r#type.map(|t| t.into()).unwrap(),
linked_id: model.linked_id.map(|id| (id as u32).into()),
}
impl TryFrom<CipherFieldModel> for Field {
type Error = Error;

fn try_from(model: CipherFieldModel) -> Result<Self> {
Ok(Self {
name: EncString::try_from(model.name)?,
value: EncString::try_from(model.value)?,
r#type: model.r#type.map(|t| t.into()).ok_or(Error::MissingFields)?,
linked_id: model
.linked_id
.map(|id| (id as u32).try_into())
.transpose()?,
})
}
}

Expand Down
60 changes: 25 additions & 35 deletions crates/bitwarden/src/vault/cipher/identity.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::FromStr;

use bitwarden_api_api::models::CipherIdentityModel;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand All @@ -8,7 +6,7 @@ use uuid::Uuid;
use crate::{
client::encryption_settings::EncryptionSettings,
crypto::{Decryptable, EncString, Encryptable},
error::Result,
error::{Error, Result},
};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
Expand Down Expand Up @@ -109,37 +107,29 @@ impl Decryptable<IdentityView> for Identity {
}
}

impl From<CipherIdentityModel> for Identity {
fn from(identity: CipherIdentityModel) -> Self {
Self {
title: identity.title.map(|s| EncString::from_str(&s).unwrap()),
first_name: identity
.first_name
.map(|s| EncString::from_str(&s).unwrap()),
middle_name: identity
.middle_name
.map(|s| EncString::from_str(&s).unwrap()),
last_name: identity.last_name.map(|s| EncString::from_str(&s).unwrap()),
address1: identity.address1.map(|s| EncString::from_str(&s).unwrap()),
address2: identity.address2.map(|s| EncString::from_str(&s).unwrap()),
address3: identity.address3.map(|s| EncString::from_str(&s).unwrap()),
city: identity.city.map(|s| EncString::from_str(&s).unwrap()),
state: identity.state.map(|s| EncString::from_str(&s).unwrap()),
postal_code: identity
.postal_code
.map(|s| EncString::from_str(&s).unwrap()),
country: identity.country.map(|s| EncString::from_str(&s).unwrap()),
company: identity.company.map(|s| EncString::from_str(&s).unwrap()),
email: identity.email.map(|s| EncString::from_str(&s).unwrap()),
phone: identity.phone.map(|s| EncString::from_str(&s).unwrap()),
ssn: identity.ssn.map(|s| EncString::from_str(&s).unwrap()),
username: identity.username.map(|s| EncString::from_str(&s).unwrap()),
passport_number: identity
.passport_number
.map(|s| EncString::from_str(&s).unwrap()),
license_number: identity
.license_number
.map(|s| EncString::from_str(&s).unwrap()),
}
impl TryFrom<CipherIdentityModel> for Identity {
type Error = Error;

fn try_from(identity: CipherIdentityModel) -> Result<Self> {
Ok(Self {
title: EncString::try_from(identity.title)?,
first_name: EncString::try_from(identity.first_name)?,
middle_name: EncString::try_from(identity.middle_name)?,
last_name: EncString::try_from(identity.last_name)?,
address1: EncString::try_from(identity.address1)?,
address2: EncString::try_from(identity.address2)?,
address3: EncString::try_from(identity.address3)?,
city: EncString::try_from(identity.city)?,
state: EncString::try_from(identity.state)?,
postal_code: EncString::try_from(identity.postal_code)?,
country: EncString::try_from(identity.country)?,
company: EncString::try_from(identity.company)?,
email: EncString::try_from(identity.email)?,
phone: EncString::try_from(identity.phone)?,
ssn: EncString::try_from(identity.ssn)?,
username: EncString::try_from(identity.username)?,
passport_number: EncString::try_from(identity.passport_number)?,
license_number: EncString::try_from(identity.license_number)?,
})
}
}
63 changes: 33 additions & 30 deletions crates/bitwarden/src/vault/cipher/linked_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum LinkedIdType {
Identity(IdentityLinkedIdType),
}

use crate::error::{Error, Result};
#[cfg(feature = "mobile")]
use crate::UniffiCustomTypeConverter;
#[cfg(feature = "mobile")]
Expand Down Expand Up @@ -73,37 +74,39 @@ pub enum IdentityLinkedIdType {
FullName = 418,
}

impl From<u32> for LinkedIdType {
fn from(value: u32) -> Self {
impl TryFrom<u32> for LinkedIdType {
type Error = Error;

fn try_from(value: u32) -> Result<Self> {
match value {
100 => LinkedIdType::Login(LoginLinkedIdType::Username),
101 => LinkedIdType::Login(LoginLinkedIdType::Password),
300 => LinkedIdType::Card(CardLinkedIdType::CardholderName),
301 => LinkedIdType::Card(CardLinkedIdType::ExpMonth),
302 => LinkedIdType::Card(CardLinkedIdType::ExpYear),
303 => LinkedIdType::Card(CardLinkedIdType::Code),
304 => LinkedIdType::Card(CardLinkedIdType::Brand),
305 => LinkedIdType::Card(CardLinkedIdType::Number),
400 => LinkedIdType::Identity(IdentityLinkedIdType::Title),
401 => LinkedIdType::Identity(IdentityLinkedIdType::MiddleName),
402 => LinkedIdType::Identity(IdentityLinkedIdType::Address1),
403 => LinkedIdType::Identity(IdentityLinkedIdType::Address2),
404 => LinkedIdType::Identity(IdentityLinkedIdType::Address3),
405 => LinkedIdType::Identity(IdentityLinkedIdType::City),
406 => LinkedIdType::Identity(IdentityLinkedIdType::State),
407 => LinkedIdType::Identity(IdentityLinkedIdType::PostalCode),
408 => LinkedIdType::Identity(IdentityLinkedIdType::Country),
409 => LinkedIdType::Identity(IdentityLinkedIdType::Company),
410 => LinkedIdType::Identity(IdentityLinkedIdType::Email),
411 => LinkedIdType::Identity(IdentityLinkedIdType::Phone),
412 => LinkedIdType::Identity(IdentityLinkedIdType::Ssn),
413 => LinkedIdType::Identity(IdentityLinkedIdType::Username),
414 => LinkedIdType::Identity(IdentityLinkedIdType::PassportNumber),
415 => LinkedIdType::Identity(IdentityLinkedIdType::LicenseNumber),
416 => LinkedIdType::Identity(IdentityLinkedIdType::FirstName),
417 => LinkedIdType::Identity(IdentityLinkedIdType::LastName),
418 => LinkedIdType::Identity(IdentityLinkedIdType::FullName),
_ => panic!("Invalid LinkedIdType value: {}", value),
100 => Ok(LinkedIdType::Login(LoginLinkedIdType::Username)),
101 => Ok(LinkedIdType::Login(LoginLinkedIdType::Password)),
300 => Ok(LinkedIdType::Card(CardLinkedIdType::CardholderName)),
301 => Ok(LinkedIdType::Card(CardLinkedIdType::ExpMonth)),
302 => Ok(LinkedIdType::Card(CardLinkedIdType::ExpYear)),
303 => Ok(LinkedIdType::Card(CardLinkedIdType::Code)),
304 => Ok(LinkedIdType::Card(CardLinkedIdType::Brand)),
305 => Ok(LinkedIdType::Card(CardLinkedIdType::Number)),
400 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::Title)),
401 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::MiddleName)),
402 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::Address1)),
403 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::Address2)),
404 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::Address3)),
405 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::City)),
406 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::State)),
407 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::PostalCode)),
408 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::Country)),
409 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::Company)),
410 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::Email)),
411 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::Phone)),
412 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::Ssn)),
413 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::Username)),
414 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::PassportNumber)),
415 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::LicenseNumber)),
416 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::FirstName)),
417 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::LastName)),
418 => Ok(LinkedIdType::Identity(IdentityLinkedIdType::FullName)),
_ => Err(Error::MissingFields),
}
}
}
Expand Down
Loading

0 comments on commit 4c5bb78

Please sign in to comment.