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