bitwarden_wasm_internal/
client.rs1extern 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_managed_settings::ManagedSettingsClient;
8use bitwarden_pm::{PasswordManagerClient as InnerPasswordManagerClient, clients::*};
9use bitwarden_policies::PolicyClient;
10use bitwarden_user_crypto_management::{UserCryptoManagementClient, UserCryptoManagementClientExt};
11use wasm_bindgen::prelude::*;
12
13use crate::platform::{
14 PlatformClient,
15 token_provider::{JsTokenProvider, WasmClientManagedTokens},
16};
17
18#[wasm_bindgen(typescript_custom_section)]
19const TOKEN_CUSTOM_TS_TYPE: &'static str = r#"
20/**
21 * @deprecated Use PasswordManagerClient instead
22 */
23export type BitwardenClient = PasswordManagerClient;
24"#;
25
26#[wasm_bindgen]
28pub struct PasswordManagerClient(pub(crate) InnerPasswordManagerClient);
29
30#[wasm_bindgen]
31impl PasswordManagerClient {
32 #[wasm_bindgen(constructor)]
38 pub fn new(
39 token_provider: JsTokenProvider,
40 settings: Option<ClientSettings>,
41 managed_settings: &ManagedSettingsClient,
42 ) -> Self {
43 let tokens = Arc::new(WasmClientManagedTokens::new(token_provider));
44 Self(InnerPasswordManagerClient::new_with_client_tokens(
45 settings,
46 tokens,
47 managed_settings,
48 ))
49 }
50
51 pub fn managed_settings(&self) -> ManagedSettingsClient {
53 self.0.managed_settings()
54 }
55
56 pub fn echo(&self, msg: String) -> String {
58 msg
59 }
60
61 pub fn version(&self) -> String {
63 #[cfg(feature = "bitwarden-license")]
64 return format!("COMMERCIAL-{}", env!("SDK_VERSION"));
65 #[cfg(not(feature = "bitwarden-license"))]
66 return env!("SDK_VERSION").to_owned();
67 }
68
69 pub fn throw(&self, msg: String) -> Result<(), TestError> {
71 Err(TestError(msg))
72 }
73
74 pub async fn http_get(&self, url: String) -> Result<String, String> {
76 let client = self.0.0.internal.get_http_client();
77 let res = client.get(&url).send().await.map_err(|e| e.to_string())?;
78
79 res.text().await.map_err(|e| e.to_string())
80 }
81
82 pub fn auth(&self) -> AuthClient {
84 self.0.auth()
85 }
86
87 #[cfg(feature = "bitwarden-license")]
89 pub fn commercial(&self) -> bitwarden_pm::CommercialPasswordManagerClient {
90 self.0.commercial()
91 }
92
93 pub fn crypto(&self) -> CryptoClient {
95 self.0.0.crypto()
96 }
97
98 pub fn crypto_sync_handler(&self) -> CryptoSyncHandlerClient {
100 self.0.crypto_sync_handler()
101 }
102
103 pub fn km_state_bridge(&self) -> StateBridgeClient {
105 self.0.0.km_state_bridge()
106 }
107
108 pub fn user_crypto_management(&self) -> UserCryptoManagementClient {
110 self.0.0.user_crypto_management()
111 }
112
113 pub fn vault(&self) -> VaultClient {
115 self.0.vault()
116 }
117
118 pub fn collections(&self) -> CollectionsClient {
124 self.0.collections()
125 }
126
127 pub fn platform(&self) -> PlatformClient {
129 PlatformClient::new(self.0.0.clone())
130 }
131
132 pub fn generator(&self) -> GeneratorClient {
134 self.0.generator()
135 }
136
137 pub fn exporters(&self) -> ExporterClient {
139 self.0.exporters()
140 }
141
142 pub fn importers(&self) -> ImporterClient {
144 self.0.importers()
145 }
146
147 pub fn policies(&self) -> PolicyClient {
149 self.0.policies()
150 }
151
152 pub fn sends(&self) -> SendClient {
154 self.0.sends()
155 }
156
157 pub fn send_sync_handler(&self) -> SendSyncHandlerClient {
159 self.0.send_sync_handler()
160 }
161
162 pub fn invite_link(&self) -> InviteLinkClient {
164 self.0.invite_link()
165 }
166
167 pub fn organization_users_management(&self) -> OrganizationUsersManagementClient {
169 self.0.organization_users_management()
170 }
171
172 pub fn crypto_cipher_suite(&self) -> CryptoCipherSuiteClient {
174 self.0.crypto_cipher_suite()
175 }
176
177 pub fn gov_mode(&self) -> bool {
179 self.0.0.gov_mode()
180 }
181}
182
183#[bitwarden_error(basic)]
184pub struct TestError(String);
185
186impl Display for TestError {
187 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
188 write!(f, "{}", self.0)
189 }
190}