Skip to main content

bitwarden_importers/
importer_client.rs

1use bitwarden_core::Client;
2#[cfg(feature = "wasm")]
3use wasm_bindgen::prelude::*;
4
5use crate::{
6    Credentials, ImportError, ImportOptions, ImportSummary, OnePasswordTwoFactorUi,
7    import::{import_kdbx, import_onepassword},
8};
9
10#[allow(missing_docs)]
11#[cfg_attr(feature = "wasm", wasm_bindgen)]
12pub struct ImporterClient {
13    client: Client,
14}
15
16#[cfg_attr(feature = "wasm", wasm_bindgen)]
17impl ImporterClient {
18    fn new(client: Client) -> Self {
19        Self { client }
20    }
21
22    /// Import a KeePass KDBX (`.kdbx`) database and submit it to the server.
23    ///
24    /// Parses and decrypts the raw `.kdbx` bytes (unlocked with the password and/or key file),
25    /// encrypts the entries for the user's personal vault or the given organization, and submits
26    /// them to the import endpoint. Returns the counts of what was imported. Inputs larger than
27    /// 10 MiB are rejected with `KdbxFileTooLarge`.
28    pub async fn import_kdbx(
29        &self,
30        file: Vec<u8>,
31        password: Option<String>,
32        key_file: Option<Vec<u8>>,
33        options: ImportOptions,
34    ) -> Result<ImportSummary, ImportError> {
35        import_kdbx(&self.client, file, password, key_file, options).await
36    }
37}
38
39// Separate from the block above: `wasm_bindgen` cannot export the `&dyn` two-factor callback.
40impl ImporterClient {
41    /// Import a 1Password account directly from the 1Password servers.
42    ///
43    /// Signs in with the email, master password and Secret Key in `credentials`, asking
44    /// `two_factor` for a code when the account requires one, downloads and decrypts every vault
45    /// the account can open, and submits the result to the import endpoint. Each vault becomes a
46    /// folder. Returns the counts of what was imported.
47    pub async fn import_onepassword(
48        &self,
49        credentials: Credentials,
50        two_factor: &dyn OnePasswordTwoFactorUi,
51        options: ImportOptions,
52    ) -> Result<ImportSummary, ImportError> {
53        import_onepassword(&self.client, credentials, two_factor, options).await
54    }
55}
56
57#[allow(missing_docs)]
58pub trait ImporterClientExt {
59    fn importers(&self) -> ImporterClient;
60}
61
62impl ImporterClientExt for Client {
63    fn importers(&self) -> ImporterClient {
64        ImporterClient::new(self.clone())
65    }
66}