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, 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 sends(&self) -> SendClient {
107 SendClient(self.0.sends())
108 }
109
110 pub fn ssh(&self) -> SshClient {
112 SshClient()
113 }
114
115 pub fn auth(&self) -> AuthClient {
117 AuthClient(self.0.0.clone())
118 }
119
120 pub fn policies(&self) -> policies::PoliciesClient {
122 use bitwarden_policies::PoliciesClientExt;
123 policies::PoliciesClient(self.0.0.policies())
124 }
125
126 pub fn echo(&self, msg: String) -> String {
128 msg
129 }
130
131 pub async fn http_get(&self, url: String) -> Result<String> {
133 let client = self.0.0.internal.get_http_client();
134 let res = client
135 .get(&url)
136 .send()
137 .await
138 .map_err(|e| Error::Api(e.into()))?;
139
140 res.text().await.map_err(|e| Error::Api(e.into()))
141 }
142}
143
144static INIT: Once = Once::new();
145
146#[derive(uniffi::Enum)]
148pub enum LogLevel {
149 Trace,
151 Debug,
153 Info,
155 Warn,
157 Error,
159}
160
161impl LogLevel {
162 fn as_str(&self) -> &'static str {
163 match self {
164 LogLevel::Trace => "trace",
165 LogLevel::Debug => "debug",
166 LogLevel::Info => "info",
167 LogLevel::Warn => "warn",
168 LogLevel::Error => "error",
169 }
170 }
171}
172
173#[uniffi::export]
197pub fn init_logger(callback: Option<Arc<dyn LogCallback>>, level: Option<LogLevel>) {
198 use tracing_subscriber::{EnvFilter, layer::SubscriberExt as _, util::SubscriberInitExt as _};
199
200 INIT.call_once(|| {
201 let level = level.as_ref().map(|l| l.as_str()).unwrap_or("info");
207 let filter = EnvFilter::builder()
208 .with_default_directive(
209 option_env!("RUST_LOG")
210 .unwrap_or(level)
211 .parse()
212 .expect("should provide valid log level at compile time."),
213 )
214 .from_env_lossy();
215
216 let fmtlayer = tracing_subscriber::fmt::layer()
217 .with_ansi(true)
218 .with_file(true)
219 .with_line_number(true)
220 .with_target(true)
221 .pretty();
222
223 let registry = tracing_subscriber::registry().with(fmtlayer).with(filter);
225
226 let callback_layer = callback.map(log_callback::CallbackLayer::new);
229 let registry = registry.with(callback_layer);
230 #[cfg(target_os = "ios")]
231 {
232 const TAG: &str = "com.8bit.bitwarden";
233 registry
234 .with(tracing_oslog::OsLogger::new(TAG, "default"))
235 .init();
236 }
237
238 #[cfg(target_os = "android")]
239 {
240 const TAG: &str = "com.bitwarden.sdk";
241 registry
242 .with(
243 tracing_android::layer(TAG)
244 .expect("initialization of android logcat tracing layer"),
245 )
246 .init();
247 }
248
249 #[cfg(not(any(target_os = "android", target_os = "ios")))]
250 {
251 registry.init();
252 }
253 #[cfg(feature = "dangerous-crypto-debug")]
254 tracing::warn!(
255 "Dangerous crypto debug features are enabled. THIS MUST NOT BE USED IN PRODUCTION BUILDS!!"
256 );
257 });
258}
259
260fn setup_error_converter() {
263 bitwarden_uniffi_error::set_error_to_uniffi_error(|e| {
264 crate::error::BitwardenError::Conversion(e.to_string()).into()
265 });
266}
267#[cfg(test)]
268mod tests {
269 use std::sync::Mutex;
270
271 use super::*;
272 #[derive(Debug)]
274 struct MockTokenProvider;
275
276 #[async_trait::async_trait]
277 impl ClientManagedTokens for MockTokenProvider {
278 async fn get_access_token(&self) -> Option<String> {
279 Some("mock_token".to_string())
280 }
281 }
282 struct TestLogCallback {
284 logs: Arc<Mutex<Vec<(String, String, String)>>>,
285 }
286 impl LogCallback for TestLogCallback {
287 fn on_log(&self, level: String, target: String, message: String) -> Result<()> {
288 self.logs
289 .lock()
290 .expect("Failed to lock logs mutex")
291 .push((level, target, message));
292 Ok(())
293 }
294 }
295
296 #[test]
301 fn test_callback_receives_logs() {
302 let logs = Arc::new(Mutex::new(Vec::new()));
303 let callback = Arc::new(TestLogCallback { logs: logs.clone() });
304
305 init_logger(Some(callback), None);
307
308 let _client = Client::new(Arc::new(MockTokenProvider), None);
310
311 tracing::info!("test message from SDK");
313
314 let captured = logs.lock().expect("Failed to lock logs mutex");
316 assert!(!captured.is_empty(), "Callback should receive logs");
317
318 let test_log = captured
320 .iter()
321 .find(|(_, _, msg)| msg.contains("test message"))
322 .expect("Should find our test log message");
323
324 assert_eq!(test_log.0, "INFO");
325 assert!(test_log.2.contains("test message"));
326 }
327}