Skip to main content

bitwarden_sm/
client.rs

1//! Client for the Bitwarden Secrets Manager API
2
3use std::sync::Arc;
4
5use bitwarden_auth::token_management::SecretsManagerTokenHandler;
6pub use bitwarden_core::ClientSettings;
7use bitwarden_core::{OrganizationId, auth::auth_client::AuthClient};
8use bitwarden_generators::GeneratorClientsExt;
9
10use crate::{ProjectsClient, SecretsClient};
11
12/// The main struct for interacting with the Secrets Manager service through the SM SDK.
13#[derive(Clone)]
14pub struct SecretsManagerClient {
15    client: bitwarden_core::Client,
16    token_handler: Arc<SecretsManagerTokenHandler>,
17}
18
19impl SecretsManagerClient {
20    /// Create a new SecretsManagerClient
21    pub fn new(settings: Option<ClientSettings>) -> Self {
22        let token_handler = Arc::new(SecretsManagerTokenHandler::default());
23        Self {
24            client: bitwarden_core::Client::new_with_token_handler(settings, token_handler.clone()),
25            token_handler,
26        }
27    }
28
29    /// Get access to the Projects API
30    pub fn projects(&self) -> ProjectsClient {
31        ProjectsClient::new(self.clone())
32    }
33
34    /// Get access to the Secrets API
35    pub fn secrets(&self) -> SecretsClient {
36        SecretsClient::new(self.clone())
37    }
38
39    /// Get access to the Auth API
40    pub fn auth(&self) -> AuthClient {
41        self.client.auth()
42    }
43
44    /// Get access to the Generators API
45    pub fn generator(&self) -> bitwarden_generators::GeneratorClient {
46        self.client.generator()
47    }
48
49    /// Get the Organization ID for the access token
50    pub fn get_access_token_organization(&self) -> Option<OrganizationId> {
51        self.token_handler.get_access_token_organization()
52    }
53
54    pub(crate) fn client(&self) -> &bitwarden_core::Client {
55        &self.client
56    }
57}