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