Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
172 changes: 170 additions & 2 deletions crates/bitwarden-vault/src/cipher/cipher.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use bitwarden_api_api::models::{CipherDetailsResponseModel, CipherResponseModel};
use bitwarden_api_api::{
apis::ciphers_api::{PutShareError, PutShareManyError},
models::{
CipherDetailsResponseModel, CipherRequestModel, CipherResponseModel,
CipherWithIdRequestModel,
},
};
use bitwarden_collections::collection::CollectionId;
use bitwarden_core::{
MissingFieldError, OrganizationId, UserId,
Expand All @@ -10,8 +16,9 @@ use bitwarden_crypto::{
PrimitiveEncryptable,
};
use bitwarden_error::bitwarden_error;
use bitwarden_state::repository::RepositoryError;
use bitwarden_uuid::uuid_newtype;
use chrono::{DateTime, Utc};
use chrono::{DateTime, SecondsFormat, Utc};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use thiserror::Error;
Expand Down Expand Up @@ -46,10 +53,20 @@ pub enum CipherError {
Crypto(#[from] CryptoError),
#[error(transparent)]
Encrypt(#[from] EncryptError),
#[error(transparent)]
VaultParse(#[from] VaultParseError),
#[error(
"This cipher contains attachments without keys. Those attachments will need to be reuploaded to complete the operation"
)]
AttachmentsWithoutKeys,
#[error("This cipher cannot be moved to the specified organization")]
OrganizationAlreadySet,
#[error(transparent)]
PutShare(#[from] bitwarden_api_api::apis::Error<PutShareError>),
#[error(transparent)]
PutShareMany(#[from] bitwarden_api_api::apis::Error<PutShareManyError>),
#[error(transparent)]
Repository(#[from] RepositoryError),
}

/// Helper trait for operations on cipher types.
Expand Down Expand Up @@ -101,6 +118,148 @@ pub struct EncryptionContext {
pub cipher: Cipher,
}

impl TryFrom<EncryptionContext> for CipherWithIdRequestModel {
type Error = CipherError;
fn try_from(
EncryptionContext {
cipher,
encrypted_for,
}: EncryptionContext,
) -> Result<Self, Self::Error> {
Ok(Self {
id: require!(cipher.id).into(),
encrypted_for: Some(encrypted_for.into()),
r#type: Some(cipher.r#type.into()),
organization_id: cipher.organization_id.map(|o| o.to_string()),
folder_id: cipher.folder_id.as_ref().map(ToString::to_string),
favorite: cipher.favorite.into(),
reprompt: Some(cipher.reprompt.into()),
key: cipher.key.map(|k| k.to_string()),
name: cipher.name.to_string(),
notes: cipher.notes.map(|n| n.to_string()),
fields: Some(
cipher
.fields
.into_iter()
.flatten()
.map(Into::into)
.collect(),
),
password_history: Some(
cipher
.password_history
.into_iter()
.flatten()
.map(Into::into)
.collect(),
),
attachments: None,
attachments2: Some(
cipher
.attachments
.into_iter()
.flatten()
.filter_map(|a| {
a.id.map(|id| {
(
id,
bitwarden_api_api::models::CipherAttachmentModel {
file_name: a.file_name.map(|n| n.to_string()),
key: a.key.map(|k| k.to_string()),
},
)
})
})
.collect(),
),
login: cipher.login.map(|l| Box::new(l.into())),
card: cipher.card.map(|c| Box::new(c.into())),
identity: cipher.identity.map(|i| Box::new(i.into())),
secure_note: cipher.secure_note.map(|s| Box::new(s.into())),
ssh_key: cipher.ssh_key.map(|s| Box::new(s.into())),
data: None, // TODO: Consume this instead of the individual fields above.
last_known_revision_date: Some(
cipher
.revision_date
.to_rfc3339_opts(SecondsFormat::Millis, true),
),
archived_date: cipher
.archived_date
.map(|d| d.to_rfc3339_opts(SecondsFormat::Millis, true)),
})
}
}

impl From<EncryptionContext> for CipherRequestModel {
fn from(
EncryptionContext {
cipher,
encrypted_for,
}: EncryptionContext,
) -> Self {
Self {
encrypted_for: Some(encrypted_for.into()),
r#type: Some(cipher.r#type.into()),
organization_id: cipher.organization_id.map(|o| o.to_string()),
folder_id: cipher.folder_id.as_ref().map(ToString::to_string),
favorite: cipher.favorite.into(),
reprompt: Some(cipher.reprompt.into()),
key: cipher.key.map(|k| k.to_string()),
name: cipher.name.to_string(),
notes: cipher.notes.map(|n| n.to_string()),
fields: Some(
cipher
.fields
.into_iter()
.flatten()
.map(Into::into)
.collect(),
),
password_history: Some(
cipher
.password_history
.into_iter()
.flatten()
.map(Into::into)
.collect(),
),
attachments: None,
attachments2: Some(
cipher
.attachments
.into_iter()
.flatten()
.filter_map(|a| {
a.id.map(|id| {
(
id,
bitwarden_api_api::models::CipherAttachmentModel {
file_name: a.file_name.map(|n| n.to_string()),
key: a.key.map(|k| k.to_string()),
},
)
})
})
.collect(),
),
login: cipher.login.map(|l| Box::new(l.into())),
card: cipher.card.map(|c| Box::new(c.into())),
identity: cipher.identity.map(|i| Box::new(i.into())),
secure_note: cipher.secure_note.map(|s| Box::new(s.into())),
ssh_key: cipher.ssh_key.map(|s| Box::new(s.into())),
data: None, // TODO: Consume this instead of the individual fields above.
last_known_revision_date: Some(
cipher
.revision_date
.to_rfc3339_opts(SecondsFormat::Millis, true),
),
archived_date: cipher
.archived_date
.map(|d| d.to_rfc3339_opts(SecondsFormat::Millis, true)),
}
}
}

#[allow(missing_docs)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
Expand Down Expand Up @@ -681,6 +840,15 @@ impl Decryptable<KeyIds, SymmetricKeyId, CipherListView> for Cipher {
}
}

#[cfg(feature = "wasm")]
impl wasm_bindgen::__rt::VectorIntoJsValue for Cipher {
fn vector_into_jsvalue(
vector: wasm_bindgen::__rt::std::boxed::Box<[Self]>,
) -> wasm_bindgen::JsValue {
wasm_bindgen::__rt::js_value_vector_into_jsvalue(vector)
}
}

#[cfg(feature = "wasm")]
impl wasm_bindgen::__rt::VectorIntoJsValue for CipherView {
fn vector_into_jsvalue(
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-vault/src/cipher/cipher_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{
mod create;
mod edit;
mod get;
mod share_cipher;

#[allow(missing_docs)]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
Expand Down
Loading
Loading