Skip to main content

bitwarden_uniffi/
lib.rs

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    /// Initialize a new instance of the SDK client
48    #[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    /// Crypto operations
66    pub fn crypto(&self) -> CryptoClient {
67        CryptoClient(self.0.crypto())
68    }
69
70    /// Returns the key-management state bridge client used to register a
71    /// host-supplied storage implementation.
72    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    /// Returns the user-crypto-management sub-client (PIN settings, key rotation, etc).
79    pub fn user_crypto_management(
80        &self,
81    ) -> bitwarden_user_crypto_management::UserCryptoManagementClient {
82        self.0.user_crypto_management()
83    }
84
85    /// Vault item operations
86    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    /// Generator operations
96    pub fn generators(&self) -> GeneratorClients {
97        GeneratorClients(self.0.generator())
98    }
99
100    /// Exporters
101    pub fn exporters(&self) -> ExporterClient {
102        ExporterClient(self.0.exporters())
103    }
104
105    /// Importers
106    pub fn importers(&self) -> ImporterClient {
107        ImporterClient(self.0.importers())
108    }
109
110    /// Sends operations
111    pub fn sends(&self) -> SendClient {
112        SendClient(self.0.sends())
113    }
114
115    /// SSH operations
116    pub fn ssh(&self) -> SshClient {
117        SshClient()
118    }
119
120    /// Auth operations
121    pub fn auth(&self) -> AuthClient {
122        AuthClient(self.0.0.clone())
123    }
124
125    /// Policy operations
126    pub fn policies(&self) -> policies::PoliciesClient {
127        use bitwarden_policies::PoliciesClientExt;
128        policies::PoliciesClient(self.0.0.policies())
129    }
130
131    /// Test method, echoes back the input
132    pub fn echo(&self, msg: String) -> String {
133        msg
134    }
135
136    /// Test method, calls http endpoint
137    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/// Log level for SDK logging
152#[derive(uniffi::Enum)]
153pub enum LogLevel {
154    /// Most verbose: all trace, debug, info, warn, and error messages
155    Trace,
156    /// Verbose: debug, info, warn, and error messages
157    Debug,
158    /// Default: info, warn, and error messages
159    Info,
160    /// Only warn and error messages
161    Warn,
162    /// Only error messages
163    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/// Initialize the SDK logger
179///
180/// This function should be called once before creating any SDK clients.
181/// It initializes the tracing infrastructure for the SDK and optionally
182/// registers a callback to receive log events.
183///
184/// # Parameters
185/// - `callback`: Optional callback to receive SDK log events. Pass `None` to use only platform
186///   loggers (oslog on iOS, logcat on Android).
187/// - `level`: Optional log level. Defaults to `Info` if not specified. Can be overridden by
188///   `RUST_LOG` environment variable at runtime or compile time.
189///
190/// # Example
191/// ```kotlin
192/// // Initialize with callback and trace-level logging before creating clients
193/// initLogger(FlightRecorderCallback(), LogLevel.TRACE)
194/// val client = Client(tokenProvider, settings)
195/// ```
196///
197/// # Notes
198/// - This function can only be called once - subsequent calls are ignored
199/// - If not called explicitly, logging is auto-initialized when first client is created
200/// - Platform loggers (oslog/logcat) are always enabled regardless of callback
201#[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        // the log level prioritization is determined by:
207        //    1. if RUST_LOG is detected at runtime
208        //    2. if RUST_LOG is provided at compile time
209        //    3. the level parameter passed by the caller
210        //    4. default to INFO
211        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        // Build base registry once instead of duplicating per-platform
229        let registry = tracing_subscriber::registry().with(fmtlayer).with(filter);
230
231        // Conditionally add callback layer if provided
232        // Use Option to avoid type incompatibility between Some/None branches
233        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
265/// Setup the error converter to ensure conversion errors don't cause panics
266/// Check [`bitwarden_uniffi_error`] for more details
267fn 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    // Mock token provider for testing
278    #[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    /// Mock LogCallback implementation for testing
288    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    // Log callback unit tests only test happy path because running this with
302    // Once means we get one registered callback per test run. There are
303    // other tests written as integration tests in the /tests/ folder that
304    // assert more specific details.
305    #[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        // Initialize logger with callback before creating client
311        init_logger(Some(callback), None);
312
313        // Create client
314        let _client = Client::new(Arc::new(MockTokenProvider), None);
315
316        // Trigger a log
317        tracing::info!("test message from SDK");
318
319        // Verify callback received it
320        let captured = logs.lock().expect("Failed to lock logs mutex");
321        assert!(!captured.is_empty(), "Callback should receive logs");
322
323        // Find our specific test log (there may be other SDK logs during init)
324        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}