Skip to main content

bitwarden_pm/
lib.rs

1#![doc = include_str!("../README.md")]
2
3#[cfg(feature = "bitwarden-license")]
4mod commercial;
5
6use std::sync::Arc;
7
8use bitwarden_auth::AuthClientExt as _;
9use bitwarden_core::{
10    FromClient,
11    auth::{ClientManagedTokenHandler, ClientManagedTokens},
12};
13use bitwarden_exporters::ExporterClientExt as _;
14use bitwarden_generators::GeneratorClientsExt as _;
15use bitwarden_send::SendClientExt as _;
16use bitwarden_sync::SyncClientExt as _;
17use bitwarden_user_crypto_management::UserCryptoManagementClientExt;
18use bitwarden_vault::{FolderSyncHandler, VaultClientExt as _};
19
20#[cfg(feature = "uniffi")]
21uniffi::setup_scaffolding!();
22
23/// Re-export subclients for easier access
24pub mod clients {
25    pub use bitwarden_auth::AuthClient;
26    pub use bitwarden_core::key_management::CryptoClient;
27    pub use bitwarden_exporters::ExporterClient;
28    pub use bitwarden_generators::GeneratorClient;
29    pub use bitwarden_send::SendClient;
30    pub use bitwarden_sync::SyncClient;
31    pub use bitwarden_vault::VaultClient;
32}
33#[cfg(feature = "bitwarden-license")]
34pub use commercial::CommercialPasswordManagerClient;
35
36mod builder;
37pub mod migrations;
38pub use builder::PasswordManagerClientBuilder;
39
40/// The main entry point for the Bitwarden Password Manager SDK
41pub struct PasswordManagerClient(pub bitwarden_core::Client);
42
43impl PasswordManagerClient {
44    /// Initialize a new instance of the SDK client
45    pub fn new(settings: Option<bitwarden_core::ClientSettings>) -> Self {
46        let mut builder = PasswordManagerClientBuilder::new();
47        if let Some(s) = settings {
48            builder = builder.with_settings(s);
49        }
50        builder.build()
51    }
52
53    /// Returns a [`PasswordManagerClientBuilder`] for constructing a new [`PasswordManagerClient`].
54    pub fn builder() -> PasswordManagerClientBuilder {
55        PasswordManagerClientBuilder::new()
56    }
57
58    /// Initialize a new instance of the SDK client with client-managed tokens
59    pub fn new_with_client_tokens(
60        settings: Option<bitwarden_core::ClientSettings>,
61        tokens: Arc<dyn ClientManagedTokens>,
62    ) -> Self {
63        Self(bitwarden_core::Client::new_with_token_handler(
64            settings,
65            ClientManagedTokenHandler::new(tokens),
66        ))
67    }
68
69    /// Initialize a new instance of the SDK client with SDK managed state and sync handlers
70    /// registered
71    ///
72    /// This will eventually replace `new` when the SDK fully owns sync on all clients.
73    pub fn new_with_sync(settings: Option<bitwarden_core::ClientSettings>) -> Self {
74        let client = Self::new(settings);
75
76        client
77            .sync()
78            .register_sync_handler(Arc::new(FolderSyncHandler::from_client(&client.0)));
79
80        // TODO: Add more sync handlers here!
81
82        client
83    }
84
85    /// Platform operations
86    pub fn platform(&self) -> bitwarden_core::platform::PlatformClient {
87        self.0.platform()
88    }
89
90    /// Auth operations
91    pub fn auth(&self) -> bitwarden_auth::AuthClient {
92        self.0.auth_new()
93    }
94
95    /// Bitwarden licensed operations
96    #[cfg(feature = "bitwarden-license")]
97    pub fn commercial(&self) -> CommercialPasswordManagerClient {
98        CommercialPasswordManagerClient::new(self.0.clone())
99    }
100
101    /// Crypto operations
102    pub fn crypto(&self) -> bitwarden_core::key_management::CryptoClient {
103        self.0.crypto()
104    }
105
106    /// Operations that manage the cryptographic machinery of a user account, including key-rotation
107    pub fn user_crypto_management(
108        &self,
109    ) -> bitwarden_user_crypto_management::UserCryptoManagementClient {
110        self.0.user_crypto_management()
111    }
112
113    /// Vault item operations
114    pub fn vault(&self) -> bitwarden_vault::VaultClient {
115        self.0.vault()
116    }
117
118    /// Exporter operations
119    pub fn exporters(&self) -> bitwarden_exporters::ExporterClient {
120        self.0.exporters()
121    }
122
123    /// Generator operations
124    pub fn generator(&self) -> bitwarden_generators::GeneratorClient {
125        self.0.generator()
126    }
127
128    /// Send operations
129    pub fn sends(&self) -> bitwarden_send::SendClient {
130        self.0.sends()
131    }
132
133    /// Sync operations
134    pub fn sync(&self) -> bitwarden_sync::SyncClient {
135        self.0.sync()
136    }
137}
138
139#[cfg(test)]
140mod tests {
141    use std::sync::Arc;
142
143    use super::*;
144
145    #[test]
146    fn new_with_server_communication_config_constructs() {
147        struct MockCookieProvider;
148
149        #[async_trait::async_trait]
150        impl bitwarden_server_communication_config::CookieProvider for MockCookieProvider {
151            async fn cookies(&self, _hostname: &str) -> Vec<(String, String)> {
152                vec![]
153            }
154
155            async fn acquire_cookie(
156                &self,
157                _hostname: &str,
158            ) -> Result<(), bitwarden_server_communication_config::AcquireCookieError> {
159                Ok(())
160            }
161
162            async fn needs_bootstrap(&self, _hostname: &str) -> bool {
163                false
164            }
165        }
166
167        let _client = PasswordManagerClient::builder()
168            .with_server_communication_config(Arc::new(MockCookieProvider))
169            .build();
170    }
171}