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