Skip to main content

bitwarden_wasm_internal/
client.rs

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