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_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#[wasm_bindgen]
27pub struct PasswordManagerClient(pub(crate) InnerPasswordManagerClient);
28
29#[wasm_bindgen]
30impl PasswordManagerClient {
31 #[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 pub fn echo(&self, msg: String) -> String {
42 msg
43 }
44
45 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 pub fn throw(&self, msg: String) -> Result<(), TestError> {
55 Err(TestError(msg))
56 }
57
58 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 pub fn auth(&self) -> AuthClient {
68 self.0.auth()
69 }
70
71 #[cfg(feature = "bitwarden-license")]
73 pub fn commercial(&self) -> bitwarden_pm::CommercialPasswordManagerClient {
74 self.0.commercial()
75 }
76
77 pub fn crypto(&self) -> CryptoClient {
79 self.0.0.crypto()
80 }
81
82 pub fn crypto_sync_handler(&self) -> CryptoSyncHandlerClient {
84 self.0.crypto_sync_handler()
85 }
86
87 pub fn km_state_bridge(&self) -> StateBridgeClient {
89 self.0.0.km_state_bridge()
90 }
91
92 pub fn user_crypto_management(&self) -> UserCryptoManagementClient {
94 self.0.0.user_crypto_management()
95 }
96
97 pub fn vault(&self) -> VaultClient {
99 self.0.vault()
100 }
101
102 pub fn platform(&self) -> PlatformClient {
104 PlatformClient::new(self.0.0.clone())
105 }
106
107 pub fn generator(&self) -> GeneratorClient {
109 self.0.generator()
110 }
111
112 pub fn exporters(&self) -> ExporterClient {
114 self.0.exporters()
115 }
116
117 pub fn importers(&self) -> ImporterClient {
119 self.0.importers()
120 }
121
122 pub fn policies(&self) -> PolicyClient {
124 self.0.policies()
125 }
126
127 pub fn sends(&self) -> SendClient {
129 self.0.sends()
130 }
131
132 pub fn invite_link(&self) -> InviteLinkClient {
134 self.0.invite_link()
135 }
136
137 pub fn crypto_cipher_suite(&self) -> CryptoCipherSuiteClient {
139 self.0.crypto_cipher_suite()
140 }
141
142 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}