1#![doc = include_str!("../README.md")]
2
3uniffi::setup_scaffolding!();
4
5use std::sync::{Arc, Once};
6
7use auth::AuthClient;
8use bitwarden_core::{ClientSettings, auth::ClientManagedTokens};
9
10#[allow(missing_docs)]
11pub mod auth;
12#[allow(missing_docs)]
13pub mod crypto;
14#[allow(missing_docs)]
15pub mod error;
16mod log_callback;
17#[allow(missing_docs)]
18pub mod platform;
19#[allow(missing_docs)]
20pub mod policies;
21#[allow(missing_docs)]
22pub mod tool;
23mod uniffi_support;
24#[allow(missing_docs)]
25pub mod vault;
26
27#[cfg(target_os = "android")]
28mod android_support;
29
30use crypto::CryptoClient;
31use error::{Error, Result};
32pub use log_callback::LogCallback;
33use platform::PlatformClient;
34pub use platform::{
35 AcquiredCookie, BootstrapConfig, ServerCommunicationConfig, ServerCommunicationConfigClient,
36 ServerCommunicationConfigRepository, SsoCookieVendorConfig,
37};
38use tool::{ExporterClient, GeneratorClients, ImporterClient, SendClient, SshClient};
39use vault::VaultClient;
40
41#[allow(missing_docs)]
42#[derive(uniffi::Object)]
43pub struct Client(pub(crate) bitwarden_pm::PasswordManagerClient);
44
45#[uniffi::export(async_runtime = "tokio")]
46impl Client {
47 #[uniffi::constructor]
49 pub fn new(
50 token_provider: Arc<dyn ClientManagedTokens>,
51 settings: Option<ClientSettings>,
52 ) -> Self {
53 init_logger(None, None);
54 setup_error_converter();
55
56 #[cfg(target_os = "android")]
57 android_support::init();
58
59 Self(bitwarden_pm::PasswordManagerClient::new_with_client_tokens(
60 settings,
61 token_provider,
62 ))
63 }
64
65 pub fn crypto(&self) -> CryptoClient {
67 CryptoClient(self.0.crypto())
68 }
69
70 pub fn km_state_bridge(
73 &self,
74 ) -> bitwarden_core::key_management::state_bridge::StateBridgeClient {
75 self.0.0.km_state_bridge()
76 }
77
78 pub fn user_crypto_management(
80 &self,
81 ) -> bitwarden_user_crypto_management::UserCryptoManagementClient {
82 self.0.user_crypto_management()
83 }
84
85 pub fn vault(&self) -> VaultClient {
87 VaultClient(self.0.vault())
88 }
89
90 #[allow(missing_docs)]
91 pub fn platform(&self) -> PlatformClient {
92 PlatformClient(self.0.0.clone())
93 }
94
95 pub fn generators(&self) -> GeneratorClients {
97 GeneratorClients(self.0.generator())
98 }
99
100 pub fn exporters(&self) -> ExporterClient {
102 ExporterClient(self.0.exporters())
103 }
104
105 pub fn importers(&self) -> ImporterClient {
107 ImporterClient(self.0.importers())
108 }
109
110 pub fn sends(&self) -> SendClient {
112 SendClient(self.0.sends())
113 }
114
115 pub fn ssh(&self) -> SshClient {
117 SshClient()
118 }
119
120 pub fn auth(&self) -> AuthClient {
122 AuthClient(self.0.0.clone())
123 }
124
125 pub fn policies(&self) -> policies::PoliciesClient {
127 use bitwarden_policies::PoliciesClientExt;
128 policies::PoliciesClient(self.0.0.policies())
129 }
130
131 pub fn echo(&self, msg: String) -> String {
133 msg
134 }
135
136 pub async fn http_get(&self, url: String) -> Result<String> {
138 let client = self.0.0.internal.get_http_client();
139 let res = client
140 .get(&url)
141 .send()
142 .await
143 .map_err(|e| Error::Api(e.into()))?;
144
145 res.text().await.map_err(|e| Error::Api(e.into()))
146 }
147}
148
149static INIT: Once = Once::new();
150
151#[derive(uniffi::Enum)]
153pub enum LogLevel {
154 Trace,
156 Debug,
158 Info,
160 Warn,
162 Error,
164}
165
166impl LogLevel {
167 fn as_str(&self) -> &'static str {
168 match self {
169 LogLevel::Trace => "trace",
170 LogLevel::Debug => "debug",
171 LogLevel::Info => "info",
172 LogLevel::Warn => "warn",
173 LogLevel::Error => "error",
174 }
175 }
176}
177
178#[uniffi::export]
202pub fn init_logger(callback: Option<Arc<dyn LogCallback>>, level: Option<LogLevel>) {
203 use tracing_subscriber::{EnvFilter, layer::SubscriberExt as _, util::SubscriberInitExt as _};
204
205 INIT.call_once(|| {
206 let level = level.as_ref().map(|l| l.as_str()).unwrap_or("info");
212 let filter = EnvFilter::builder()
213 .with_default_directive(
214 option_env!("RUST_LOG")
215 .unwrap_or(level)
216 .parse()
217 .expect("should provide valid log level at compile time."),
218 )
219 .from_env_lossy();
220
221 let fmtlayer = tracing_subscriber::fmt::layer()
222 .with_ansi(true)
223 .with_file(true)
224 .with_line_number(true)
225 .with_target(true)
226 .pretty();
227
228 let registry = tracing_subscriber::registry().with(fmtlayer).with(filter);
230
231 let callback_layer = callback.map(log_callback::CallbackLayer::new);
234 let registry = registry.with(callback_layer);
235 #[cfg(target_os = "ios")]
236 {
237 const TAG: &str = "com.8bit.bitwarden";
238 registry
239 .with(tracing_oslog::OsLogger::new(TAG, "default"))
240 .init();
241 }
242
243 #[cfg(target_os = "android")]
244 {
245 const TAG: &str = "com.bitwarden.sdk";
246 registry
247 .with(
248 tracing_android::layer(TAG)
249 .expect("initialization of android logcat tracing layer"),
250 )
251 .init();
252 }
253
254 #[cfg(not(any(target_os = "android", target_os = "ios")))]
255 {
256 registry.init();
257 }
258 #[cfg(feature = "dangerous-crypto-debug")]
259 tracing::warn!(
260 "Dangerous crypto debug features are enabled. THIS MUST NOT BE USED IN PRODUCTION BUILDS!!"
261 );
262 });
263}
264
265fn setup_error_converter() {
268 bitwarden_uniffi_error::set_error_to_uniffi_error(|e| {
269 crate::error::BitwardenError::Conversion(e.to_string()).into()
270 });
271}
272#[cfg(test)]
273mod tests {
274 use std::sync::Mutex;
275
276 use super::*;
277 #[derive(Debug)]
279 struct MockTokenProvider;
280
281 #[async_trait::async_trait]
282 impl ClientManagedTokens for MockTokenProvider {
283 async fn get_access_token(&self) -> Option<String> {
284 Some("mock_token".to_string())
285 }
286 }
287 struct TestLogCallback {
289 logs: Arc<Mutex<Vec<(String, String, String)>>>,
290 }
291 impl LogCallback for TestLogCallback {
292 fn on_log(&self, level: String, target: String, message: String) -> Result<()> {
293 self.logs
294 .lock()
295 .expect("Failed to lock logs mutex")
296 .push((level, target, message));
297 Ok(())
298 }
299 }
300
301 #[test]
306 fn test_callback_receives_logs() {
307 let logs = Arc::new(Mutex::new(Vec::new()));
308 let callback = Arc::new(TestLogCallback { logs: logs.clone() });
309
310 init_logger(Some(callback), None);
312
313 let _client = Client::new(Arc::new(MockTokenProvider), None);
315
316 tracing::info!("test message from SDK");
318
319 let captured = logs.lock().expect("Failed to lock logs mutex");
321 assert!(!captured.is_empty(), "Callback should receive logs");
322
323 let test_log = captured
325 .iter()
326 .find(|(_, _, msg)| msg.contains("test message"))
327 .expect("Should find our test log message");
328
329 assert_eq!(test_log.0, "INFO");
330 assert!(test_log.2.contains("test message"));
331 }
332}