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 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    /// Initialize a new instance of the SDK client
46    #[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    /// Crypto operations
64    pub fn crypto(&self) -> CryptoClient {
65        CryptoClient(self.0.crypto())
66    }
67
68    /// Vault item operations
69    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    /// Generator operations
79    pub fn generators(&self) -> GeneratorClients {
80        GeneratorClients(self.0.generator())
81    }
82
83    /// Exporters
84    pub fn exporters(&self) -> ExporterClient {
85        ExporterClient(self.0.exporters())
86    }
87
88    /// Sends operations
89    pub fn sends(&self) -> SendClient {
90        SendClient(self.0.sends())
91    }
92
93    /// SSH operations
94    pub fn ssh(&self) -> SshClient {
95        SshClient()
96    }
97
98    /// Auth operations
99    pub fn auth(&self) -> AuthClient {
100        AuthClient(self.0.0.clone())
101    }
102
103    /// Test method, echoes back the input
104    pub fn echo(&self, msg: String) -> String {
105        msg
106    }
107
108    /// Test method, calls http endpoint
109    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/// Initialize the SDK logger
124///
125/// This function should be called once before creating any SDK clients.
126/// It initializes the tracing infrastructure for the SDK and optionally
127/// registers a callback to receive log events.
128///
129/// # Parameters
130/// - `callback`: Optional callback to receive SDK log events. Pass `None` to use only platform
131///   loggers (oslog on iOS, logcat on Android).
132///
133/// # Example
134/// ```kotlin
135/// // Initialize with callback before creating clients
136/// initLogger(FlightRecorderCallback())
137/// val client = Client(tokenProvider, settings)
138/// ```
139///
140/// # Notes
141/// - This function can only be called once - subsequent calls are ignored
142/// - If not called explicitly, logging is auto-initialized when first client is created
143/// - Platform loggers (oslog/logcat) are always enabled regardless of callback
144#[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        // the log level prioritization is determined by:
150        //    1. if RUST_LOG is detected at runtime
151        //    2. if RUST_LOG is provided at compile time
152        //    3. default to INFO
153        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        // Build base registry once instead of duplicating per-platform
170        let registry = tracing_subscriber::registry().with(fmtlayer).with(filter);
171
172        // Conditionally add callback layer if provided
173        // Use Option to avoid type incompatibility between Some/None branches
174        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
206/// Setup the error converter to ensure conversion errors don't cause panics
207/// Check [`bitwarden_uniffi_error`] for more details
208fn 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    // Mock token provider for testing
219    #[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    /// Mock LogCallback implementation for testing
229    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    // Log callback unit tests only test happy path because running this with
243    // Once means we get one registered callback per test run. There are
244    // other tests written as integration tests in the /tests/ folder that
245    // assert more specific details.
246    #[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        // Initialize logger with callback before creating client
252        init_logger(Some(callback));
253
254        // Create client
255        let _client = Client::new(Arc::new(MockTokenProvider), None);
256
257        // Trigger a log
258        tracing::info!("test message from SDK");
259
260        // Verify callback received it
261        let captured = logs.lock().expect("Failed to lock logs mutex");
262        assert!(!captured.is_empty(), "Callback should receive logs");
263
264        // Find our specific test log (there may be other SDK logs during init)
265        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}