bitwarden_uniffi/
lib.rs

1#![doc = include_str!("../README.md")]
2
3uniffi::setup_scaffolding!();
4
5use auth::AuthClient;
6use bitwarden_core::ClientSettings;
7
8pub mod auth;
9pub mod crypto;
10mod error;
11pub mod platform;
12pub mod tool;
13mod uniffi_support;
14pub mod vault;
15
16#[cfg(target_os = "android")]
17mod android_support;
18
19use bitwarden_exporters::ExporterClientExt;
20use bitwarden_generators::GeneratorClientsExt;
21use bitwarden_send::SendClientExt;
22use bitwarden_vault::VaultClientExt;
23use crypto::CryptoClient;
24use error::{Error, Result};
25use platform::PlatformClient;
26use tool::{ExporterClient, GeneratorClients, SendClient, SshClient};
27use vault::VaultClient;
28
29#[derive(uniffi::Object)]
30pub struct Client(pub(crate) bitwarden_core::Client);
31
32#[uniffi::export(async_runtime = "tokio")]
33impl Client {
34    /// Initialize a new instance of the SDK client
35    #[uniffi::constructor]
36    pub fn new(settings: Option<ClientSettings>) -> Self {
37        init_logger();
38
39        #[cfg(target_os = "android")]
40        android_support::init();
41
42        Self(bitwarden_core::Client::new(settings))
43    }
44
45    /// Crypto operations
46    pub fn crypto(&self) -> CryptoClient {
47        CryptoClient(self.0.crypto())
48    }
49
50    /// Vault item operations
51    pub fn vault(&self) -> VaultClient {
52        VaultClient(self.0.vault())
53    }
54
55    pub fn platform(&self) -> PlatformClient {
56        PlatformClient(self.0.clone())
57    }
58
59    /// Generator operations
60    pub fn generators(&self) -> GeneratorClients {
61        GeneratorClients(self.0.generator())
62    }
63
64    /// Exporters
65    pub fn exporters(&self) -> ExporterClient {
66        ExporterClient(self.0.exporters())
67    }
68
69    /// Sends operations
70    pub fn sends(&self) -> SendClient {
71        SendClient(self.0.sends())
72    }
73
74    /// SSH operations
75    pub fn ssh(&self) -> SshClient {
76        SshClient()
77    }
78
79    /// Auth operations
80    pub fn auth(&self) -> AuthClient {
81        AuthClient(self.0.clone())
82    }
83
84    /// Test method, echoes back the input
85    pub fn echo(&self, msg: String) -> String {
86        msg
87    }
88
89    /// Test method, calls http endpoint
90    pub async fn http_get(&self, url: String) -> Result<String> {
91        let client = self.0.internal.get_http_client();
92        let res = client
93            .get(&url)
94            .send()
95            .await
96            .map_err(|e| Error::Api(e.into()))?;
97
98        Ok(res.text().await.map_err(|e| Error::Api(e.into()))?)
99    }
100}
101
102fn init_logger() {
103    #[cfg(not(any(target_os = "android", target_os = "ios")))]
104    let _ = env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
105        .try_init();
106
107    #[cfg(target_os = "ios")]
108    let _ = oslog::OsLogger::new("com.8bit.bitwarden")
109        .level_filter(log::LevelFilter::Info)
110        .init();
111
112    #[cfg(target_os = "android")]
113    android_logger::init_once(
114        android_logger::Config::default()
115            .with_tag("com.bitwarden.sdk")
116            .with_max_level(log::LevelFilter::Info),
117    );
118}