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_crypto_cipher_suite::CryptoCipherSuiteClientExt as _;
14use bitwarden_exporters::ExporterClientExt as _;
15use bitwarden_generators::GeneratorClientsExt as _;
16use bitwarden_importers::ImporterClientExt as _;
17use bitwarden_organization_invite_link::InviteLinkClientExt as _;
18use bitwarden_policies::PoliciesClientExt as _;
19use bitwarden_send::SendClientExt as _;
20use bitwarden_sync::SyncClientExt as _;
21use bitwarden_unlock::UnlockClientExt as _;
22use bitwarden_user_crypto_management::UserCryptoManagementClientExt;
23use bitwarden_vault::{FolderSyncHandler, VaultClientExt as _};
24
25#[cfg(feature = "uniffi")]
26uniffi::setup_scaffolding!();
27
28pub mod clients {
30 pub use bitwarden_auth::AuthClient;
31 pub use bitwarden_core::key_management::CryptoClient;
32 pub use bitwarden_crypto_cipher_suite::CryptoCipherSuiteClient;
33 pub use bitwarden_exporters::ExporterClient;
34 pub use bitwarden_generators::GeneratorClient;
35 pub use bitwarden_importers::ImporterClient;
36 pub use bitwarden_organization_invite_link::InviteLinkClient;
37 pub use bitwarden_policies::PolicyClient;
38 pub use bitwarden_send::SendClient;
39 pub use bitwarden_sync::SyncClient;
40 pub use bitwarden_unlock::UnlockClient;
41 pub use bitwarden_vault::VaultClient;
42}
43#[cfg(feature = "bitwarden-license")]
44pub use commercial::CommercialPasswordManagerClient;
45
46mod builder;
47pub mod migrations;
48pub use bitwarden_core::{RehydrationError, SaveStateData};
49pub use bitwarden_unlock::{SessionKey, UnlockError, UnlockMethod};
50pub use builder::PasswordManagerClientBuilder;
51
52pub struct PasswordManagerClient(pub bitwarden_core::Client);
54
55impl PasswordManagerClient {
56 pub fn new(settings: Option<bitwarden_core::ClientSettings>) -> Self {
58 let mut builder = PasswordManagerClientBuilder::new();
59 if let Some(s) = settings {
60 builder = builder.with_settings(s);
61 }
62 builder.build()
63 }
64
65 pub fn builder() -> PasswordManagerClientBuilder {
67 PasswordManagerClientBuilder::new()
68 }
69
70 pub fn new_with_client_tokens(
72 settings: Option<bitwarden_core::ClientSettings>,
73 tokens: Arc<dyn ClientManagedTokens>,
74 ) -> Self {
75 Self(bitwarden_core::Client::new_with_token_handler(
76 settings,
77 ClientManagedTokenHandler::new(tokens),
78 ))
79 }
80
81 pub fn new_with_sync(settings: Option<bitwarden_core::ClientSettings>) -> Self {
86 let client = Self::new(settings);
87
88 client
89 .sync()
90 .register_sync_handler(Arc::new(FolderSyncHandler::from_client(&client.0)));
91
92 client
95 }
96
97 pub fn platform(&self) -> bitwarden_core::platform::PlatformClient {
99 self.0.platform()
100 }
101
102 pub fn auth(&self) -> bitwarden_auth::AuthClient {
104 self.0.auth_new()
105 }
106
107 #[cfg(feature = "bitwarden-license")]
109 pub fn commercial(&self) -> CommercialPasswordManagerClient {
110 CommercialPasswordManagerClient::new(self.0.clone())
111 }
112
113 pub fn crypto(&self) -> bitwarden_core::key_management::CryptoClient {
115 self.0.crypto()
116 }
117
118 pub fn crypto_cipher_suite(&self) -> bitwarden_crypto_cipher_suite::CryptoCipherSuiteClient {
120 self.0.crypto_cipher_suite()
121 }
122
123 pub fn flags(&self) -> bitwarden_core::FlagsClient {
125 self.0.flags()
126 }
127
128 pub fn user_crypto_management(
130 &self,
131 ) -> bitwarden_user_crypto_management::UserCryptoManagementClient {
132 self.0.user_crypto_management()
133 }
134
135 pub fn vault(&self) -> bitwarden_vault::VaultClient {
137 self.0.vault()
138 }
139
140 pub fn exporters(&self) -> bitwarden_exporters::ExporterClient {
142 self.0.exporters()
143 }
144
145 pub fn importers(&self) -> bitwarden_importers::ImporterClient {
147 self.0.importers()
148 }
149
150 pub fn generator(&self) -> bitwarden_generators::GeneratorClient {
152 self.0.generator()
153 }
154
155 pub fn sends(&self) -> bitwarden_send::SendClient {
157 self.0.sends()
158 }
159
160 pub fn policies(&self) -> bitwarden_policies::PolicyClient {
162 self.0.policies()
163 }
164
165 pub fn invite_link(&self) -> bitwarden_organization_invite_link::InviteLinkClient {
167 self.0.invite_link()
168 }
169
170 pub fn sync(&self) -> bitwarden_sync::SyncClient {
172 self.0.sync()
173 }
174
175 pub fn is_unlocked(&self) -> bool {
177 use bitwarden_core::key_management::SymmetricKeySlotId;
178 self.0
179 .internal
180 .get_key_store()
181 .context()
182 .has_symmetric_key(SymmetricKeySlotId::User)
183 }
184
185 pub fn unlock(&self) -> bitwarden_unlock::UnlockClient {
187 self.0.unlock()
188 }
189
190 pub async fn save_to_state(
194 data: SaveStateData,
195 reg: &bitwarden_state::registry::StateRegistry,
196 ) -> Result<(), RehydrationError> {
197 bitwarden_core::Client::save_to_state(data, reg).await
198 }
199
200 pub async fn load_from_state(
204 token_handler: std::sync::Arc<dyn bitwarden_core::auth::auth_tokens::TokenHandler>,
205 registry: bitwarden_state::registry::StateRegistry,
206 ) -> Result<Self, RehydrationError> {
207 let client = bitwarden_core::Client::load_from_state(token_handler, registry).await?;
208 Ok(PasswordManagerClient(client))
209 }
210}
211
212#[cfg(test)]
213mod tests {
214 use std::sync::Arc;
215
216 use super::*;
217
218 #[test]
219 fn new_with_server_communication_config_constructs() {
220 struct MockCookieProvider;
221
222 #[async_trait::async_trait]
223 impl bitwarden_server_communication_config::CookieProvider for MockCookieProvider {
224 async fn cookies(&self, _hostname: &str) -> Vec<(String, String)> {
225 vec![]
226 }
227
228 async fn acquire_cookie(
229 &self,
230 _hostname: &str,
231 ) -> Result<(), bitwarden_server_communication_config::AcquireCookieError> {
232 Ok(())
233 }
234
235 async fn needs_bootstrap(&self, _hostname: &str) -> bool {
236 false
237 }
238 }
239
240 let _client = PasswordManagerClient::builder()
241 .with_server_communication_config(Arc::new(MockCookieProvider))
242 .build();
243 }
244}