bitwarden_wasm_internal/
client.rs

1extern crate console_error_panic_hook;
2use std::{fmt::Display, sync::Arc};
3
4use bitwarden_core::ClientSettings;
5use bitwarden_error::bitwarden_error;
6use bitwarden_pm::{PasswordManagerClient, clients::*};
7use wasm_bindgen::prelude::*;
8
9use crate::platform::{
10    PlatformClient,
11    token_provider::{JsTokenProvider, WasmClientManagedTokens},
12};
13
14/// The main entry point for the Bitwarden SDK in WebAssembly environments
15#[wasm_bindgen]
16pub struct BitwardenClient(pub(crate) PasswordManagerClient);
17
18#[wasm_bindgen]
19impl BitwardenClient {
20    /// Initialize a new instance of the SDK client
21    #[wasm_bindgen(constructor)]
22    pub fn new(token_provider: JsTokenProvider, settings: Option<ClientSettings>) -> Self {
23        let tokens = Arc::new(WasmClientManagedTokens::new(token_provider));
24        Self(PasswordManagerClient::new_with_client_tokens(
25            settings, tokens,
26        ))
27    }
28
29    /// Test method, echoes back the input
30    pub fn echo(&self, msg: String) -> String {
31        msg
32    }
33
34    /// Returns the current SDK version
35    pub fn version(&self) -> String {
36        #[cfg(feature = "bitwarden-license")]
37        return format!("COMMERCIAL-{}", env!("SDK_VERSION"));
38        #[cfg(not(feature = "bitwarden-license"))]
39        return env!("SDK_VERSION").to_owned();
40    }
41
42    /// Test method, always throws an error
43    pub fn throw(&self, msg: String) -> Result<(), TestError> {
44        Err(TestError(msg))
45    }
46
47    /// Test method, calls http endpoint
48    pub async fn http_get(&self, url: String) -> Result<String, String> {
49        let client = self.0.0.internal.get_http_client();
50        let res = client.get(&url).send().await.map_err(|e| e.to_string())?;
51
52        res.text().await.map_err(|e| e.to_string())
53    }
54
55    /// Auth related operations.
56    pub fn auth(&self) -> AuthClient {
57        self.0.auth()
58    }
59
60    /// Bitwarden licensed operations.
61    #[cfg(feature = "bitwarden-license")]
62    pub fn commercial(&self) -> bitwarden_pm::CommercialPasswordManagerClient {
63        self.0.commercial()
64    }
65
66    /// Crypto related operations.
67    pub fn crypto(&self) -> CryptoClient {
68        self.0.0.crypto()
69    }
70
71    /// Vault item related operations.
72    pub fn vault(&self) -> VaultClient {
73        self.0.vault()
74    }
75
76    /// Constructs a specific client for platform-specific functionality
77    pub fn platform(&self) -> PlatformClient {
78        PlatformClient::new(self.0.0.clone())
79    }
80
81    /// Constructs a specific client for generating passwords and passphrases
82    pub fn generator(&self) -> GeneratorClient {
83        self.0.generator()
84    }
85
86    /// Exporter related operations.
87    pub fn exporters(&self) -> ExporterClient {
88        self.0.exporters()
89    }
90}
91
92#[bitwarden_error(basic)]
93pub struct TestError(String);
94
95impl Display for TestError {
96    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97        write!(f, "{}", self.0)
98    }
99}