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