Skip to content

Commit a0bca6b

Browse files
committed
restructured cipher client to sub folders
1 parent 2b8491a commit a0bca6b

File tree

6 files changed

+136
-127
lines changed

6 files changed

+136
-127
lines changed

crates/bitwarden-vault/src/cipher/create.rs renamed to crates/bitwarden-vault/src/cipher/cipher_client/create.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use tsify::Tsify;
1313
#[cfg(feature = "wasm")]
1414
use wasm_bindgen::prelude::*;
1515

16+
use super::CiphersClient;
1617
use crate::{Cipher, CipherView, VaultParseError};
1718

1819
#[allow(missing_docs)]
@@ -104,7 +105,7 @@ impl IdentifyKey<SymmetricKeyId> for CipherAddEditRequest {
104105
}
105106
}
106107

107-
pub(super) async fn create_cipher<R: Repository<Cipher> + ?Sized>(
108+
async fn create_cipher<R: Repository<Cipher> + ?Sized>(
108109
key_store: &KeyStore<KeyIds>,
109110
api_config: &bitwarden_api_api::apis::configuration::Configuration,
110111
repository: &R,
@@ -124,6 +125,22 @@ pub(super) async fn create_cipher<R: Repository<Cipher> + ?Sized>(
124125
Ok(key_store.decrypt(&cipher)?)
125126
}
126127

128+
impl CiphersClient {
129+
/// Create a new [Cipher] and save it to the server.
130+
pub async fn create(
131+
&self,
132+
mut request: CipherAddEditRequest,
133+
) -> Result<CipherView, CreateCipherError> {
134+
let key_store = self.client.internal.get_key_store();
135+
let config = self.client.internal.get_api_configurations().await;
136+
let repository = self.get_repository()?;
137+
138+
request.encrypted_for = self.client.internal.get_user_id();
139+
140+
create_cipher(key_store, &config.api, repository.as_ref(), request).await
141+
}
142+
}
143+
127144
#[cfg(test)]
128145
mod tests {
129146
use bitwarden_api_api::models::CipherResponseModel;

crates/bitwarden-vault/src/cipher/edit.rs renamed to crates/bitwarden-vault/src/cipher/cipher_client/edit.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ use bitwarden_error::bitwarden_error;
55
use bitwarden_state::repository::{Repository, RepositoryError};
66
use thiserror::Error;
77

8-
use crate::{create::CipherAddEditRequest, Cipher, CipherView, ItemNotFoundError, VaultParseError};
8+
use super::CiphersClient;
9+
use crate::{
10+
cipher_client::create::CipherAddEditRequest, Cipher, CipherView, ItemNotFoundError,
11+
VaultParseError,
12+
};
913

1014
#[allow(missing_docs)]
1115
#[bitwarden_error(flat)]
@@ -27,7 +31,7 @@ pub enum EditCipherError {
2731
Uuid(#[from] uuid::Error),
2832
}
2933

30-
pub(super) async fn edit_cipher<R: Repository<Cipher> + ?Sized>(
34+
async fn edit_cipher<R: Repository<Cipher> + ?Sized>(
3135
key_store: &KeyStore<KeyIds>,
3236
api_config: &bitwarden_api_api::apis::configuration::Configuration,
3337
repository: &R,
@@ -58,6 +62,30 @@ pub(super) async fn edit_cipher<R: Repository<Cipher> + ?Sized>(
5862
Ok(key_store.decrypt(&cipher)?)
5963
}
6064

65+
impl CiphersClient {
66+
/// Edit an existing [Cipher] and save it to the server.
67+
pub async fn edit(
68+
&self,
69+
cipher_id: &str,
70+
mut request: CipherAddEditRequest,
71+
) -> Result<CipherView, EditCipherError> {
72+
let key_store = self.client.internal.get_key_store();
73+
let config = self.client.internal.get_api_configurations().await;
74+
let repository = self.get_repository()?;
75+
76+
request.encrypted_for = self.client.internal.get_user_id();
77+
78+
edit_cipher(
79+
key_store,
80+
&config.api,
81+
repository.as_ref(),
82+
cipher_id,
83+
request,
84+
)
85+
.await
86+
}
87+
}
88+
6189
#[cfg(test)]
6290
mod tests {
6391
use bitwarden_api_api::{
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use bitwarden_core::key_management::KeyIds;
2+
use bitwarden_crypto::{CryptoError, KeyStore};
3+
use bitwarden_error::bitwarden_error;
4+
use bitwarden_state::repository::{Repository, RepositoryError};
5+
use thiserror::Error;
6+
7+
use super::CiphersClient;
8+
use crate::{cipher::cipher::DecryptCipherListResult, Cipher, CipherView, ItemNotFoundError};
9+
10+
#[allow(missing_docs)]
11+
#[bitwarden_error(flat)]
12+
#[derive(Debug, Error)]
13+
pub enum GetCipherError {
14+
#[error(transparent)]
15+
ItemNotFound(#[from] ItemNotFoundError),
16+
#[error(transparent)]
17+
Crypto(#[from] CryptoError),
18+
#[error(transparent)]
19+
RepositoryError(#[from] RepositoryError),
20+
}
21+
22+
async fn get_cipher(
23+
store: &KeyStore<KeyIds>,
24+
repository: &dyn Repository<Cipher>,
25+
id: &str,
26+
) -> Result<CipherView, GetCipherError> {
27+
let cipher = repository
28+
.get(id.to_string())
29+
.await?
30+
.ok_or(ItemNotFoundError)?;
31+
32+
Ok(store.decrypt(&cipher)?)
33+
}
34+
35+
async fn list_ciphers(
36+
store: &KeyStore<KeyIds>,
37+
repository: &dyn Repository<Cipher>,
38+
) -> Result<Vec<CipherView>, GetCipherError> {
39+
let ciphers = repository.list().await?;
40+
let views = store.decrypt_list(&ciphers)?;
41+
Ok(views)
42+
}
43+
44+
async fn list_ciphers_with_failures(
45+
store: &KeyStore<KeyIds>,
46+
repository: &dyn Repository<Cipher>,
47+
) -> Result<DecryptCipherListResult, GetCipherError> {
48+
let ciphers = repository.list().await?;
49+
let (successes, failures) = store.decrypt_list_with_failures(&ciphers);
50+
Ok(DecryptCipherListResult {
51+
successes,
52+
failures: failures.into_iter().cloned().collect(),
53+
})
54+
}
55+
56+
impl CiphersClient {
57+
/// Get all ciphers from state and decrypt them to a list of [CipherView].
58+
pub async fn list(&self) -> Result<Vec<CipherView>, GetCipherError> {
59+
let key_store = self.client.internal.get_key_store();
60+
let repository = self.get_repository()?;
61+
62+
list_ciphers(key_store, repository.as_ref()).await
63+
}
64+
65+
/// Get all ciphers from state and decrypt them, returning both successes and failures.
66+
/// This method will not fail when some ciphers fail to decrypt, allowing for graceful
67+
/// handling of corrupted or problematic cipher data.
68+
pub async fn list_with_failures(&self) -> Result<DecryptCipherListResult, GetCipherError> {
69+
let key_store = self.client.internal.get_key_store();
70+
let repository = self.get_repository()?;
71+
72+
list_ciphers_with_failures(key_store, repository.as_ref()).await
73+
}
74+
75+
/// Get [Cipher] by ID from state and decrypt it to a [CipherView].
76+
pub async fn get(&self, cipher_id: &str) -> Result<CipherView, GetCipherError> {
77+
let key_store = self.client.internal.get_key_store();
78+
let repository = self.get_repository()?;
79+
80+
get_cipher(key_store, repository.as_ref(), cipher_id).await
81+
}
82+
}

crates/bitwarden-vault/src/cipher/cipher_client.rs renamed to crates/bitwarden-vault/src/cipher/cipher_client/mod.rs

Lines changed: 6 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ use wasm_bindgen::prelude::*;
1010

1111
use super::EncryptionContext;
1212
use crate::{
13-
cipher::cipher::DecryptCipherListResult,
14-
create::{create_cipher, CipherAddEditRequest, CreateCipherError},
15-
edit::{edit_cipher, EditCipherError},
16-
get_list::{get_cipher, list_ciphers, list_ciphers_with_failures, GetCipherError},
17-
Cipher, CipherError, CipherListView, CipherView, DecryptError, EncryptError,
18-
Fido2CredentialFullView,
13+
cipher::cipher::DecryptCipherListResult, Cipher, CipherError, CipherListView, CipherView,
14+
DecryptError, EncryptError, Fido2CredentialFullView,
1915
};
2016

17+
pub mod create;
18+
pub mod edit;
19+
pub mod get;
20+
2121
#[allow(missing_docs)]
2222
#[cfg_attr(feature = "wasm", wasm_bindgen)]
2323
pub struct CiphersClient {
@@ -181,68 +181,6 @@ impl CiphersClient {
181181
let decrypted_key = cipher_view.decrypt_fido2_private_key(&mut key_store.context())?;
182182
Ok(decrypted_key)
183183
}
184-
185-
/// Get all ciphers from state and decrypt them to a list of [CipherView].
186-
pub async fn list(&self) -> Result<Vec<CipherView>, GetCipherError> {
187-
let key_store = self.client.internal.get_key_store();
188-
let repository = self.get_repository()?;
189-
190-
list_ciphers(key_store, repository.as_ref()).await
191-
}
192-
193-
/// Get all ciphers from state and decrypt them, returning both successes and failures.
194-
/// This method will not fail when some ciphers fail to decrypt, allowing for graceful
195-
/// handling of corrupted or problematic cipher data.
196-
pub async fn list_with_failures(&self) -> Result<DecryptCipherListResult, GetCipherError> {
197-
let key_store = self.client.internal.get_key_store();
198-
let repository = self.get_repository()?;
199-
200-
list_ciphers_with_failures(key_store, repository.as_ref()).await
201-
}
202-
203-
/// Get [Cipher] by ID from state and decrypt it to a [CipherView].
204-
pub async fn get(&self, cipher_id: &str) -> Result<CipherView, GetCipherError> {
205-
let key_store = self.client.internal.get_key_store();
206-
let repository = self.get_repository()?;
207-
208-
get_cipher(key_store, repository.as_ref(), cipher_id).await
209-
}
210-
211-
/// Create a new [Cipher] and save it to the server.
212-
pub async fn create(
213-
&self,
214-
mut request: CipherAddEditRequest,
215-
) -> Result<CipherView, CreateCipherError> {
216-
let key_store = self.client.internal.get_key_store();
217-
let config = self.client.internal.get_api_configurations().await;
218-
let repository = self.get_repository()?;
219-
220-
request.encrypted_for = self.client.internal.get_user_id();
221-
222-
create_cipher(key_store, &config.api, repository.as_ref(), request).await
223-
}
224-
225-
/// Edit an existing [Cipher] and save it to the server.
226-
pub async fn edit(
227-
&self,
228-
cipher_id: &str,
229-
mut request: CipherAddEditRequest,
230-
) -> Result<CipherView, EditCipherError> {
231-
let key_store = self.client.internal.get_key_store();
232-
let config = self.client.internal.get_api_configurations().await;
233-
let repository = self.get_repository()?;
234-
235-
request.encrypted_for = self.client.internal.get_user_id();
236-
237-
edit_cipher(
238-
key_store,
239-
&config.api,
240-
repository.as_ref(),
241-
cipher_id,
242-
request,
243-
)
244-
.await
245-
}
246184
}
247185

248186
impl CiphersClient {

crates/bitwarden-vault/src/cipher/get_list.rs

Lines changed: 0 additions & 53 deletions
This file was deleted.

crates/bitwarden-vault/src/cipher/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ pub(crate) mod card;
55
pub(crate) mod cipher;
66
pub(crate) mod cipher_client;
77
pub(crate) mod cipher_permissions;
8-
pub(crate) mod create;
9-
pub(crate) mod edit;
108
pub(crate) mod field;
11-
pub(crate) mod get_list;
129
pub(crate) mod identity;
1310
pub(crate) mod linked_id;
1411
pub(crate) mod local_data;

0 commit comments

Comments
 (0)