Skip to content

Commit

Permalink
Add uris to ListView
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Nov 11, 2024
1 parent edee180 commit cc0ddc9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
27 changes: 12 additions & 15 deletions crates/bitwarden-vault/src/cipher/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use uuid::Uuid;
use super::{
attachment, card, field, identity,
local_data::{LocalData, LocalDataView},
login::LoginListView,
secure_note, ssh_key,
};
use crate::{
Expand Down Expand Up @@ -133,10 +134,7 @@ pub struct CipherView {
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
pub enum CipherListViewType {
Login {
has_fido2: bool,
totp: Option<EncString>,
},
Login(LoginListView),
SecureNote,
Card,
Identity,
Expand Down Expand Up @@ -182,10 +180,11 @@ impl CipherListView {
let cipher_key = Cipher::get_cipher_key(key, &self.key)?;
let key = cipher_key.as_ref().unwrap_or(key);

let totp = if let CipherListViewType::Login { totp, .. } = self.r#type {
totp.decrypt_with_key(key)?
} else {
None
let totp = match self.r#type {
CipherListViewType::Login(LoginListView { totp, .. }) => {
totp.map(|t| t.decrypt_with_key(key)).transpose()?
}
_ => None,

Check warning on line 187 in crates/bitwarden-vault/src/cipher/cipher.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-vault/src/cipher/cipher.rs#L187

Added line #L187 was not covered by tests
};

Ok(totp)
Expand Down Expand Up @@ -562,10 +561,7 @@ impl KeyDecryptable<SymmetricCryptoKey, CipherListView> for Cipher {
.login
.as_ref()
.ok_or(CryptoError::MissingField("login"))?;
CipherListViewType::Login {
has_fido2: login.fido2_credentials.is_some(),
totp: login.totp.clone(),
}
CipherListViewType::Login(login.decrypt_with_key(key)?)
}
CipherType::SecureNote => CipherListViewType::SecureNote,
CipherType::Card => CipherListViewType::Card,
Expand Down Expand Up @@ -803,10 +799,11 @@ mod tests {
key: cipher.key,
name: "My test login".to_string(),
sub_title: "test_username".to_string(),
r#type: CipherListViewType::Login {
r#type: CipherListViewType::Login(LoginListView {
has_fido2: true,
totp: cipher.login.as_ref().unwrap().totp.clone()
},
totp: cipher.login.as_ref().unwrap().totp.clone(),
uris: None,
}),
favorite: cipher.favorite,
reprompt: cipher.reprompt,
edit: cipher.edit,
Expand Down
23 changes: 21 additions & 2 deletions crates/bitwarden-vault/src/cipher/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde_repr::{Deserialize_repr, Serialize_repr};

use crate::VaultParseError;

#[derive(Clone, Copy, Serialize_repr, Deserialize_repr, Debug, JsonSchema)]
#[derive(Clone, Copy, Serialize_repr, Deserialize_repr, Debug, JsonSchema, PartialEq)]

Check warning on line 14 in crates/bitwarden-vault/src/cipher/login.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-vault/src/cipher/login.rs#L14

Added line #L14 was not covered by tests
#[repr(u8)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
Expand All @@ -33,7 +33,7 @@ pub struct LoginUri {
pub uri_checksum: Option<EncString>,
}

#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone)]
#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone, PartialEq)]

Check warning on line 36 in crates/bitwarden-vault/src/cipher/login.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-vault/src/cipher/login.rs#L36

Added line #L36 was not covered by tests
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct LoginUriView {
Expand Down Expand Up @@ -268,6 +268,15 @@ pub struct LoginView {
pub fido2_credentials: Option<Vec<Fido2Credential>>,
}

#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone, PartialEq)]

Check warning on line 271 in crates/bitwarden-vault/src/cipher/login.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-vault/src/cipher/login.rs#L271

Added line #L271 was not covered by tests
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct LoginListView {
pub has_fido2: bool,
pub totp: Option<EncString>,
pub uris: Option<Vec<LoginUriView>>,
}

impl KeyEncryptable<SymmetricCryptoKey, LoginUri> for LoginUriView {
fn encrypt_with_key(self, key: &SymmetricCryptoKey) -> Result<LoginUri, CryptoError> {
Ok(LoginUri {
Expand Down Expand Up @@ -316,6 +325,16 @@ impl KeyDecryptable<SymmetricCryptoKey, LoginView> for Login {
}
}

impl KeyDecryptable<SymmetricCryptoKey, LoginListView> for Login {
fn decrypt_with_key(&self, key: &SymmetricCryptoKey) -> Result<LoginListView, CryptoError> {
Ok(LoginListView {
has_fido2: self.fido2_credentials.is_some(),
totp: self.totp.clone(),
uris: self.uris.decrypt_with_key(key).ok().flatten(),
})
}
}

impl KeyEncryptable<SymmetricCryptoKey, Fido2Credential> for Fido2CredentialView {
fn encrypt_with_key(self, key: &SymmetricCryptoKey) -> Result<Fido2Credential, CryptoError> {
Ok(Fido2Credential {
Expand Down
7 changes: 4 additions & 3 deletions crates/bitwarden-vault/src/totp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ mod tests {
use uuid::Uuid;

use super::*;
use crate::{cipher::cipher::CipherListViewType, CipherRepromptType};
use crate::{cipher::cipher::CipherListViewType, login::LoginListView, CipherRepromptType};

#[test]
fn test_decode_b32() {
Expand Down Expand Up @@ -377,10 +377,11 @@ mod tests {
key: None,
name: "My test login".to_string(),
sub_title: "test_username".to_string(),
r#type: CipherListViewType::Login {
r#type: CipherListViewType::Login(LoginListView{
has_fido2: true,
totp: Some("2.hqdioUAc81FsKQmO1XuLQg==|oDRdsJrQjoFu9NrFVy8tcJBAFKBx95gHaXZnWdXbKpsxWnOr2sKipIG43pKKUFuq|3gKZMiboceIB5SLVOULKg2iuyu6xzos22dfJbvx0EHk=".parse().unwrap()),
},
uris: None,
}),
favorite: false,
reprompt: CipherRepromptType::None,
edit: true,
Expand Down

0 comments on commit cc0ddc9

Please sign in to comment.