bitwarden_vault/cipher/attachment_client/
mod.rs1use std::{path::Path, sync::Arc};
2
3use bitwarden_core::{FromClient, client::ApiConfigurations, key_management::KeySlotIds};
4use bitwarden_crypto::{EncString, KeyStore};
5use bitwarden_error::bitwarden_error;
6use bitwarden_state::repository::Repository;
7use thiserror::Error;
8
9use crate::{
10 Attachment, AttachmentEncryptResult, AttachmentFile, AttachmentFileView, AttachmentView,
11 Cipher, DecryptError, EncryptError,
12};
13
14mod admin;
15mod create;
16mod delete;
17mod download_url;
18mod renew;
19mod upgrade;
20
21pub use admin::{
22 AttachmentAdminClient, CipherAdminGetAttachmentDownloadUrlError, CreateAttachmentAdminError,
23 DeleteAttachmentAdminError,
24};
25pub use create::{
26 AttachmentFileUploadType, CipherCreateAttachmentError, CreateAttachmentRequest,
27 CreatedAttachment,
28};
29pub use delete::CipherDeleteAttachmentError;
30pub use download_url::CipherGetAttachmentDownloadUrlError;
31pub use renew::CipherRenewFileUploadUrlError;
32pub use upgrade::CipherUpgradeAttachmentError;
33
34#[allow(missing_docs)]
36#[bitwarden_error(flat)]
37#[derive(Debug, Error)]
38pub enum EncryptFileError {
39 #[error(transparent)]
40 Encrypt(#[from] EncryptError),
41 #[error(transparent)]
42 Io(#[from] std::io::Error),
43}
44
45#[allow(missing_docs)]
47#[bitwarden_error(flat)]
48#[derive(Debug, Error)]
49pub enum DecryptFileError {
50 #[error(transparent)]
51 Decrypt(#[from] DecryptError),
52 #[error(transparent)]
53 Io(#[from] std::io::Error),
54}
55
56#[bitwarden_ffi::wasm_object]
58#[derive(FromClient)]
59pub struct AttachmentsClient {
60 pub(crate) key_store: KeyStore<KeySlotIds>,
61 pub(crate) api_configurations: Arc<ApiConfigurations>,
62 pub(crate) repository: Option<Arc<dyn Repository<Cipher>>>,
63}
64
65#[bitwarden_ffi::wasm_export]
66impl AttachmentsClient {
67 pub fn admin(&self) -> AttachmentAdminClient {
70 AttachmentAdminClient {
71 api_configurations: self.api_configurations.clone(),
72 }
73 }
74
75 #[allow(missing_docs)]
76 pub fn decrypt_buffer(
77 &self,
78 cipher: Cipher,
79 attachment: AttachmentView,
80 encrypted_buffer: &[u8],
81 ) -> Result<Vec<u8>, DecryptError> {
82 Ok(self.key_store.decrypt(&AttachmentFile {
83 cipher,
84 attachment,
85 contents: EncString::from_buffer(encrypted_buffer)?,
86 })?)
87 }
88}
89
90impl AttachmentsClient {
91 #[allow(missing_docs)]
92 pub fn encrypt_buffer(
93 &self,
94 cipher: Cipher,
95 attachment: AttachmentView,
96 buffer: &[u8],
97 ) -> Result<AttachmentEncryptResult, EncryptError> {
98 Ok(self.key_store.encrypt(AttachmentFileView {
99 cipher,
100 attachment,
101 contents: buffer,
102 })?)
103 }
104
105 #[allow(missing_docs)]
106 pub fn encrypt_file(
107 &self,
108 cipher: Cipher,
109 attachment: AttachmentView,
110 decrypted_file_path: &Path,
111 encrypted_file_path: &Path,
112 ) -> Result<Attachment, EncryptFileError> {
113 let data = std::fs::read(decrypted_file_path)?;
114 let AttachmentEncryptResult {
115 attachment,
116 contents,
117 } = self.encrypt_buffer(cipher, attachment, &data)?;
118 std::fs::write(encrypted_file_path, contents)?;
119 Ok(attachment)
120 }
121
122 #[allow(missing_docs)]
123 pub fn decrypt_file(
124 &self,
125 cipher: Cipher,
126 attachment: AttachmentView,
127 encrypted_file_path: &Path,
128 decrypted_file_path: &Path,
129 ) -> Result<(), DecryptFileError> {
130 let data = std::fs::read(encrypted_file_path)?;
131 let decrypted = self.decrypt_buffer(cipher, attachment, &data)?;
132 std::fs::write(decrypted_file_path, decrypted)?;
133 Ok(())
134 }
135}