bitwarden_uniffi/
lib.rs

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