Skip to content

Commit

Permalink
[PM-14808] Add uris to ListView (#29)
Browse files Browse the repository at this point in the history
Adds the login URIs to `CipherListView` to make it useable for autofill.
  • Loading branch information
Hinton authored Nov 11, 2024
1 parent 2d677e8 commit a5f98db
Show file tree
Hide file tree
Showing 3 changed files with 38 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,
};

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(),
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 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)]
#[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)]
#[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 struct LoginView {
pub fido2_credentials: Option<Vec<Fido2Credential>>,
}

#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone, PartialEq)]
#[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, 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(),
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

0 comments on commit a5f98db

Please sign in to comment.