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 tool;
21mod uniffi_support;
22#[allow(missing_docs)]
23pub mod vault;
24
25#[cfg(target_os = "android")]
26mod android_support;
27
28use crypto::CryptoClient;
29use error::{Error, Result};
30pub use log_callback::LogCallback;
31use platform::PlatformClient;
32pub use platform::{
33 AcquiredCookie, BootstrapConfig, ServerCommunicationConfig, ServerCommunicationConfigClient,
34 ServerCommunicationConfigRepository, SsoCookieVendorConfig,
35};
36use tool::{ExporterClient, GeneratorClients, SendClient, SshClient};
37use vault::VaultClient;
38
39#[allow(missing_docs)]
40#[derive(uniffi::Object)]
41pub struct Client(pub(crate) bitwarden_pm::PasswordManagerClient);
42
43#[uniffi::export(async_runtime = "tokio")]
44impl Client {
45 #[uniffi::constructor]
47 pub fn new(
48 token_provider: Arc<dyn ClientManagedTokens>,
49 settings: Option<ClientSettings>,
50 ) -> Self {
51 init_logger(None);
52 setup_error_converter();
53
54 #[cfg(target_os = "android")]
55 android_support::init();
56
57 Self(bitwarden_pm::PasswordManagerClient::new_with_client_tokens(
58 settings,
59 token_provider,
60 ))
61 }
62
63 pub fn crypto(&self) -> CryptoClient {
65 CryptoClient(self.0.crypto())
66 }
67
68 pub fn vault(&self) -> VaultClient {
70 VaultClient(self.0.vault())
71 }
72
73 #[allow(missing_docs)]
74 pub fn platform(&self) -> PlatformClient {
75 PlatformClient(self.0.0.clone())
76 }
77
78 pub fn generators(&self) -> GeneratorClients {
80 GeneratorClients(self.0.generator())
81 }
82
83 pub fn exporters(&self) -> ExporterClient {
85 ExporterClient(self.0.exporters())
86 }
87
88 pub fn sends(&self) -> SendClient {
90 SendClient(self.0.sends())
91 }
92
93 pub fn ssh(&self) -> SshClient {
95 SshClient()
96 }
97
98 pub fn auth(&self) -> AuthClient {
100 AuthClient(self.0.0.clone())
101 }
102
103 pub fn echo(&self, msg: String) -> String {
105 msg
106 }
107
108 pub async fn http_get(&self, url: String) -> Result<String> {
110 let client = self.0.0.internal.get_http_client();
111 let res = client
112 .get(&url)
113 .send()
114 .await
115 .map_err(|e| Error::Api(e.into()))?;
116
117 res.text().await.map_err(|e| Error::Api(e.into()))
118 }
119}
120
121static INIT: Once = Once::new();
122
123#[uniffi::export]
145pub fn init_logger(callback: Option<Arc<dyn LogCallback>>) {
146 use tracing_subscriber::{EnvFilter, layer::SubscriberExt as _, util::SubscriberInitExt as _};
147
148 INIT.call_once(|| {
149 let filter = EnvFilter::builder()
154 .with_default_directive(
155 option_env!("RUST_LOG")
156 .unwrap_or("info")
157 .parse()
158 .expect("should provide valid log level at compile time."),
159 )
160 .from_env_lossy();
161
162 let fmtlayer = tracing_subscriber::fmt::layer()
163 .with_ansi(true)
164 .with_file(true)
165 .with_line_number(true)
166 .with_target(true)
167 .pretty();
168
169 let registry = tracing_subscriber::registry().with(fmtlayer).with(filter);
171
172 let callback_layer = callback.map(log_callback::CallbackLayer::new);
175 let registry = registry.with(callback_layer);
176 #[cfg(target_os = "ios")]
177 {
178 const TAG: &str = "com.8bit.bitwarden";
179 registry
180 .with(tracing_oslog::OsLogger::new(TAG, "default"))
181 .init();
182 }
183
184 #[cfg(target_os = "android")]
185 {
186 const TAG: &str = "com.bitwarden.sdk";
187 registry
188 .with(
189 tracing_android::layer(TAG)
190 .expect("initialization of android logcat tracing layer"),
191 )
192 .init();
193 }
194
195 #[cfg(not(any(target_os = "android", target_os = "ios")))]
196 {
197 registry.init();
198 }
199 #[cfg(feature = "dangerous-crypto-debug")]
200 tracing::warn!(
201 "Dangerous crypto debug features are enabled. THIS MUST NOT BE USED IN PRODUCTION BUILDS!!"
202 );
203 });
204}
205
206fn setup_error_converter() {
209 bitwarden_uniffi_error::set_error_to_uniffi_error(|e| {
210 crate::error::BitwardenError::Conversion(e.to_string()).into()
211 });
212}
213#[cfg(test)]
214mod tests {
215 use std::sync::Mutex;
216
217 use super::*;
218 #[derive(Debug)]
220 struct MockTokenProvider;
221
222 #[async_trait::async_trait]
223 impl ClientManagedTokens for MockTokenProvider {
224 async fn get_access_token(&self) -> Option<String> {
225 Some("mock_token".to_string())
226 }
227 }
228 struct TestLogCallback {
230 logs: Arc<Mutex<Vec<(String, String, String)>>>,
231 }
232 impl LogCallback for TestLogCallback {
233 fn on_log(&self, level: String, target: String, message: String) -> Result<()> {
234 self.logs
235 .lock()
236 .expect("Failed to lock logs mutex")
237 .push((level, target, message));
238 Ok(())
239 }
240 }
241
242 #[test]
247 fn test_callback_receives_logs() {
248 let logs = Arc::new(Mutex::new(Vec::new()));
249 let callback = Arc::new(TestLogCallback { logs: logs.clone() });
250
251 init_logger(Some(callback));
253
254 let _client = Client::new(Arc::new(MockTokenProvider), None);
256
257 tracing::info!("test message from SDK");
259
260 let captured = logs.lock().expect("Failed to lock logs mutex");
262 assert!(!captured.is_empty(), "Callback should receive logs");
263
264 let test_log = captured
266 .iter()
267 .find(|(_, _, msg)| msg.contains("test message"))
268 .expect("Should find our test log message");
269
270 assert_eq!(test_log.0, "INFO");
271 assert!(test_log.2.contains("test message"));
272 }
273}