bitwarden_send/
send_client.rs1use std::{path::Path, sync::Arc};
2
3use bitwarden_core::Client;
4use bitwarden_crypto::{
5 Decryptable, EncString, IdentifyKey, OctetStreamBytes, PrimitiveEncryptable,
6};
7use bitwarden_state::repository::{Repository, RepositoryError};
8use thiserror::Error;
9#[cfg(feature = "wasm")]
10use wasm_bindgen::prelude::*;
11
12use crate::{Send, SendListView, SendView};
13
14#[allow(missing_docs)]
16#[derive(Debug, Error)]
17#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
18pub enum SendEncryptError {
19 #[error(transparent)]
20 Crypto(#[from] bitwarden_crypto::CryptoError),
21}
22
23#[allow(missing_docs)]
25#[derive(Debug, Error)]
26#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
27pub enum SendDecryptError {
28 #[error(transparent)]
29 Crypto(#[from] bitwarden_crypto::CryptoError),
30}
31
32#[allow(missing_docs)]
34#[derive(Debug, Error)]
35#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
36pub enum SendEncryptFileError {
37 #[error(transparent)]
38 Encrypt(#[from] SendEncryptError),
39 #[error(transparent)]
40 Io(#[from] std::io::Error),
41}
42
43#[allow(missing_docs)]
45#[derive(Debug, Error)]
46#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
47pub enum SendDecryptFileError {
48 #[error(transparent)]
49 Decrypt(#[from] SendDecryptError),
50 #[error(transparent)]
51 Io(#[from] std::io::Error),
52}
53
54#[allow(missing_docs)]
55#[cfg_attr(feature = "wasm", wasm_bindgen)]
56pub struct SendClient {
57 pub(crate) client: Client,
58}
59
60impl SendClient {
61 fn new(client: Client) -> Self {
62 Self { client }
63 }
64
65 #[allow(missing_docs)]
66 pub fn decrypt(&self, send: Send) -> Result<SendView, SendDecryptError> {
67 let key_store = self.client.internal.get_key_store();
68 let send_view = key_store.decrypt(&send)?;
69 Ok(send_view)
70 }
71
72 #[allow(missing_docs)]
73 pub fn decrypt_list(&self, sends: Vec<Send>) -> Result<Vec<SendListView>, SendDecryptError> {
74 let key_store = self.client.internal.get_key_store();
75 let send_views = key_store.decrypt_list(&sends)?;
76 Ok(send_views)
77 }
78
79 #[allow(missing_docs)]
80 pub fn decrypt_file(
81 &self,
82 send: Send,
83 encrypted_file_path: &Path,
84 decrypted_file_path: &Path,
85 ) -> Result<(), SendDecryptFileError> {
86 let data = std::fs::read(encrypted_file_path)?;
87 let decrypted = self.decrypt_buffer(send, &data)?;
88 std::fs::write(decrypted_file_path, decrypted)?;
89 Ok(())
90 }
91
92 #[allow(missing_docs)]
93 pub fn decrypt_buffer(
94 &self,
95 send: Send,
96 encrypted_buffer: &[u8],
97 ) -> Result<Vec<u8>, SendDecryptError> {
98 let key_store = self.client.internal.get_key_store();
99 let mut ctx = key_store.context();
100
101 let key = Send::get_key(&mut ctx, &send.key, send.key_identifier())?;
102
103 let buf = EncString::from_buffer(encrypted_buffer)?;
104 Ok(buf.decrypt(&mut ctx, key)?)
105 }
106
107 #[allow(missing_docs)]
108 pub fn encrypt(&self, send_view: SendView) -> Result<Send, SendEncryptError> {
109 let key_store = self.client.internal.get_key_store();
110
111 let send = key_store.encrypt(send_view)?;
112
113 Ok(send)
114 }
115
116 #[allow(missing_docs)]
117 pub fn encrypt_file(
118 &self,
119 send: Send,
120 decrypted_file_path: &Path,
121 encrypted_file_path: &Path,
122 ) -> Result<(), SendEncryptFileError> {
123 let data = std::fs::read(decrypted_file_path)?;
124 let encrypted = self.encrypt_buffer(send, &data)?;
125 std::fs::write(encrypted_file_path, encrypted)?;
126 Ok(())
127 }
128
129 #[allow(missing_docs)]
130 pub fn encrypt_buffer(&self, send: Send, buffer: &[u8]) -> Result<Vec<u8>, SendEncryptError> {
131 let key_store = self.client.internal.get_key_store();
132 let mut ctx = key_store.context();
133
134 let key = Send::get_key(&mut ctx, &send.key, send.key_identifier())?;
135
136 let encrypted = OctetStreamBytes::from(buffer).encrypt(&mut ctx, key)?;
137 Ok(encrypted.to_buffer()?)
138 }
139}
140
141impl SendClient {
142 pub(crate) fn get_repository(&self) -> Result<Arc<dyn Repository<Send>>, RepositoryError> {
143 Ok(self.client.platform().state().get::<Send>()?)
144 }
145}
146
147#[allow(missing_docs)]
148pub trait SendClientExt {
149 fn sends(&self) -> SendClient;
150}
151
152impl SendClientExt for Client {
153 fn sends(&self) -> SendClient {
154 SendClient::new(self.clone())
155 }
156}