bitwarden_vault/cipher/cipher_client/admin/
get.rs

1use bitwarden_api_api::models::CipherMiniDetailsResponseModelListResponseModel;
2use bitwarden_core::{ApiError, OrganizationId, key_management::KeyIds};
3use bitwarden_crypto::{CryptoError, KeyStore};
4use bitwarden_error::bitwarden_error;
5use thiserror::Error;
6
7use crate::{
8    VaultParseError,
9    cipher::cipher::{DecryptCipherListResult, PartialCipher},
10    cipher_client::admin::CipherAdminClient,
11};
12
13#[allow(missing_docs)]
14#[bitwarden_error(flat)]
15#[derive(Debug, Error)]
16pub enum GetOrganizationCiphersAdminError {
17    #[error(transparent)]
18    Crypto(#[from] CryptoError),
19    #[error(transparent)]
20    VaultParse(#[from] VaultParseError),
21    #[error(transparent)]
22    Api(#[from] ApiError),
23}
24
25/// Get all ciphers for an organization.
26pub async fn list_org_ciphers(
27    org_id: OrganizationId,
28    include_member_items: bool,
29    api_client: &bitwarden_api_api::apis::ApiClient,
30    key_store: &KeyStore<KeyIds>,
31) -> Result<DecryptCipherListResult, GetOrganizationCiphersAdminError> {
32    let api = api_client.ciphers_api();
33    let response: CipherMiniDetailsResponseModelListResponseModel = api
34        .get_organization_ciphers(Some(org_id.into()), Some(include_member_items))
35        .await
36        .map_err(ApiError::from)?;
37    let ciphers = response
38        .data
39        .into_iter()
40        .flatten()
41        .map(|model| model.merge_with_cipher(None))
42        .collect::<Result<Vec<_>, _>>()?;
43
44    let (successes, failures) = key_store.decrypt_list_with_failures(&ciphers);
45    Ok(DecryptCipherListResult {
46        successes,
47        failures: failures.into_iter().cloned().collect(),
48    })
49}
50
51impl CipherAdminClient {
52    pub async fn list_org_ciphers(
53        &self,
54        org_id: OrganizationId,
55        include_member_items: bool,
56    ) -> Result<DecryptCipherListResult, GetOrganizationCiphersAdminError> {
57        list_org_ciphers(
58            org_id,
59            include_member_items,
60            &self
61                .client
62                .internal
63                .get_api_configurations()
64                .await
65                .api_client,
66            self.client.internal.get_key_store(),
67        )
68        .await
69    }
70}