Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-14808] Add uris to ListView #29

Merged
merged 3 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@
#[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 @@
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 @@
.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 @@
key: cipher.key,
name: "My test login".to_string(),
subtitle: "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
24 changes: 22 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 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 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
dani-garcia marked this conversation as resolved.
Show resolved Hide resolved
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct LoginUriView {
Expand Down Expand Up @@ -268,6 +268,16 @@
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,
/// The TOTP key is not decrypted. Useable as is with [`crate::generate_totp_cipher_view`].
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 +326,16 @@
}
}

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(),
subtitle: "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