Skip to main content

bitwarden_uniffi/tool/
mod.rs

1use bitwarden_collections::collection::Collection;
2use bitwarden_exporters::{Account, ExportFormat};
3use bitwarden_generators::{
4    PassphraseGeneratorRequest, PasswordGeneratorRequest, UsernameGeneratorRequest,
5};
6use bitwarden_importers::{ImportOptions, ImportSummary};
7use bitwarden_vault::{Cipher, Folder};
8
9use crate::error::Result;
10
11mod sends;
12pub use sends::SendClient;
13
14mod ssh;
15pub use ssh::SshClient;
16
17#[derive(uniffi::Object)]
18pub struct GeneratorClients(pub(crate) bitwarden_generators::GeneratorClient);
19
20#[uniffi::export(async_runtime = "tokio")]
21impl GeneratorClients {
22    /// Generate Password
23    pub fn password(&self, settings: PasswordGeneratorRequest) -> Result<String> {
24        Ok(self.0.password(settings)?)
25    }
26
27    /// Generate Passphrase
28    pub fn passphrase(&self, settings: PassphraseGeneratorRequest) -> Result<String> {
29        Ok(self.0.passphrase(settings)?)
30    }
31
32    /// Parses an HTML `passwordrules` attribute string into a [`PasswordGeneratorRequest`].
33    pub fn password_rules(&self, rules: String) -> Result<PasswordGeneratorRequest> {
34        Ok(self.0.password_rules(rules)?)
35    }
36
37    /// Generate Username
38    pub async fn username(&self, settings: UsernameGeneratorRequest) -> Result<String> {
39        Ok(self.0.username(settings).await?)
40    }
41}
42
43#[derive(uniffi::Object)]
44pub struct ExporterClient(pub(crate) bitwarden_exporters::ExporterClient);
45
46#[uniffi::export(async_runtime = "tokio")]
47impl ExporterClient {
48    /// Export user vault
49    pub async fn export_vault(
50        &self,
51        folders: Vec<Folder>,
52        ciphers: Vec<Cipher>,
53        format: ExportFormat,
54    ) -> Result<String> {
55        Ok(self.0.export_vault(folders, ciphers, format).await?)
56    }
57
58    /// Export organization vault
59    pub fn export_organization_vault(
60        &self,
61        collections: Vec<Collection>,
62        ciphers: Vec<Cipher>,
63        format: ExportFormat,
64    ) -> Result<String> {
65        Ok(self
66            .0
67            .export_organization_vault(collections, ciphers, format)?)
68    }
69
70    /// Credential Exchange Format (CXF)
71    ///
72    /// *Warning:* Expect this API to be unstable, and it will change in the future.
73    ///
74    /// For use with Apple using [ASCredentialExportManager](https://developer.apple.com/documentation/authenticationservices/ascredentialexportmanager).
75    /// Ideally the output should be immediately deserialized to [ASImportableAccount](https://developer.apple.com/documentation/authenticationservices/asimportableaccount).
76    pub fn export_cxf(&self, account: Account, ciphers: Vec<Cipher>) -> Result<String> {
77        Ok(self.0.export_cxf(account, ciphers)?)
78    }
79
80    /// Credential Exchange Format (CXF)
81    ///
82    /// *Warning:* Expect this API to be unstable, and it will change in the future.
83    ///
84    /// For use with Apple using [ASCredentialExportManager](https://developer.apple.com/documentation/authenticationservices/ascredentialexportmanager).
85    /// Ideally the input should be immediately serialized from [ASImportableAccount](https://developer.apple.com/documentation/authenticationservices/asimportableaccount).
86    pub fn import_cxf(&self, payload: String) -> Result<Vec<Cipher>> {
87        Ok(self.0.import_cxf(payload)?)
88    }
89}
90
91#[derive(uniffi::Object)]
92pub struct ImporterClient(pub(crate) bitwarden_importers::ImporterClient);
93
94#[uniffi::export(async_runtime = "tokio")]
95impl ImporterClient {
96    /// Import a KeePass KDBX (`.kdbx`) database and submit it to the server.
97    pub async fn import_kdbx(
98        &self,
99        file: Vec<u8>,
100        password: Option<String>,
101        key_file: Option<Vec<u8>>,
102        options: ImportOptions,
103    ) -> Result<ImportSummary> {
104        Ok(self
105            .0
106            .import_kdbx(file, password, key_file, options)
107            .await?)
108    }
109}