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, 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    /// Sends operations
106    pub fn sends(&self) -> SendClient {
107        SendClient(self.0.sends())
108    }
109
110    /// SSH operations
111    pub fn ssh(&self) -> SshClient {
112        SshClient()
113    }
114
115    /// Auth operations
116    pub fn auth(&self) -> AuthClient {
117        AuthClient(self.0.0.clone())
118    }
119
120    /// Policy operations
121    pub fn policies(&self) -> policies::PoliciesClient {
122        use bitwarden_policies::PoliciesClientExt;
123        policies::PoliciesClient(self.0.0.policies())
124    }
125
126    /// Test method, echoes back the input
127    pub fn echo(&self, msg: String) -> String {
128        msg
129    }
130
131    /// Test method, calls http endpoint
132    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/// Log level for SDK logging
147#[derive(uniffi::Enum)]
148pub enum LogLevel {
149    /// Most verbose: all trace, debug, info, warn, and error messages
150    Trace,
151    /// Verbose: debug, info, warn, and error messages
152    Debug,
153    /// Default: info, warn, and error messages
154    Info,
155    /// Only warn and error messages
156    Warn,
157    /// Only error messages
158    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/// Initialize the SDK logger
174///
175/// This function should be called once before creating any SDK clients.
176/// It initializes the tracing infrastructure for the SDK and optionally
177/// registers a callback to receive log events.
178///
179/// # Parameters
180/// - `callback`: Optional callback to receive SDK log events. Pass `None` to use only platform
181///   loggers (oslog on iOS, logcat on Android).
182/// - `level`: Optional log level. Defaults to `Info` if not specified. Can be overridden by
183///   `RUST_LOG` environment variable at runtime or compile time.
184///
185/// # Example
186/// ```kotlin
187/// // Initialize with callback and trace-level logging before creating clients
188/// initLogger(FlightRecorderCallback(), LogLevel.TRACE)
189/// val client = Client(tokenProvider, settings)
190/// ```
191///
192/// # Notes
193/// - This function can only be called once - subsequent calls are ignored
194/// - If not called explicitly, logging is auto-initialized when first client is created
195/// - Platform loggers (oslog/logcat) are always enabled regardless of callback
196#[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        // the log level prioritization is determined by:
202        //    1. if RUST_LOG is detected at runtime
203        //    2. if RUST_LOG is provided at compile time
204        //    3. the level parameter passed by the caller
205        //    4. default to INFO
206        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        // Build base registry once instead of duplicating per-platform
224        let registry = tracing_subscriber::registry().with(fmtlayer).with(filter);
225
226        // Conditionally add callback layer if provided
227        // Use Option to avoid type incompatibility between Some/None branches
228        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
260/// Setup the error converter to ensure conversion errors don't cause panics
261/// Check [`bitwarden_uniffi_error`] for more details
262fn 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    // Mock token provider for testing
273    #[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    /// Mock LogCallback implementation for testing
283    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    // Log callback unit tests only test happy path because running this with
297    // Once means we get one registered callback per test run. There are
298    // other tests written as integration tests in the /tests/ folder that
299    // assert more specific details.
300    #[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        // Initialize logger with callback before creating client
306        init_logger(Some(callback), None);
307
308        // Create client
309        let _client = Client::new(Arc::new(MockTokenProvider), None);
310
311        // Trigger a log
312        tracing::info!("test message from SDK");
313
314        // Verify callback received it
315        let captured = logs.lock().expect("Failed to lock logs mutex");
316        assert!(!captured.is_empty(), "Callback should receive logs");
317
318        // Find our specific test log (there may be other SDK logs during init)
319        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}