Skip to main content

bitwarden_generators/
generator_client.rs

1use bitwarden_core::Client;
2#[cfg(feature = "wasm")]
3use wasm_bindgen::prelude::*;
4
5use crate::{
6    PassphraseError, PassphraseGeneratorRequest, PasswordError, PasswordGeneratorRequest,
7    PasswordRulesError, UsernameError, UsernameGeneratorRequest, passphrase::passphrase,
8    password::password, passwordrules::parse_password_rules, username::username,
9};
10
11#[allow(missing_docs)]
12#[cfg_attr(feature = "wasm", wasm_bindgen)]
13pub struct GeneratorClient {
14    client: Client,
15}
16
17#[cfg_attr(feature = "wasm", wasm_bindgen)]
18impl GeneratorClient {
19    fn new(client: Client) -> Self {
20        Self { client }
21    }
22
23    /// Generates a random password.
24    ///
25    /// The character sets and password length can be customized using the `input` parameter.
26    ///
27    /// # Examples
28    ///
29    /// ```
30    /// use bitwarden_core::Client;
31    /// use bitwarden_generators::{GeneratorClientsExt, PassphraseError, PasswordGeneratorRequest};
32    ///
33    /// async fn test() -> Result<(), PassphraseError> {
34    ///     let input = PasswordGeneratorRequest {
35    ///         lowercase: true,
36    ///         uppercase: true,
37    ///         numbers: true,
38    ///         length: 20,
39    ///         ..Default::default()
40    ///     };
41    ///     let password = Client::new(None).generator().password(input).unwrap();
42    ///     println!("{}", password);
43    ///     Ok(())
44    /// }
45    /// ```
46    pub fn password(&self, input: PasswordGeneratorRequest) -> Result<String, PasswordError> {
47        password(input)
48    }
49
50    /// Parses an HTML `passwordrules` attribute string into a [`PasswordGeneratorRequest`].
51    ///
52    /// The returned request can be passed to [`GeneratorClient::password`] to produce a
53    /// password that satisfies the website's declared constraints.
54    pub fn password_rules(
55        &self,
56        rules: String,
57    ) -> Result<PasswordGeneratorRequest, PasswordRulesError> {
58        parse_password_rules(&rules)
59    }
60
61    /// Generates a random passphrase.
62    /// A passphrase is a combination of random words separated by a character.
63    /// An example of passphrase is `correct horse battery staple`.
64    ///
65    /// The number of words and their case, the word separator, and the inclusion of
66    /// a number in the passphrase can be customized using the `input` parameter.
67    ///
68    /// # Examples
69    ///
70    /// ```
71    /// use bitwarden_core::Client;
72    /// use bitwarden_generators::{GeneratorClientsExt, PassphraseError, PassphraseGeneratorRequest};
73    ///
74    /// async fn test() -> Result<(), PassphraseError> {
75    ///     let input = PassphraseGeneratorRequest {
76    ///         num_words: 4,
77    ///         ..Default::default()
78    ///     };
79    ///     let passphrase = Client::new(None).generator().passphrase(input).unwrap();
80    ///     println!("{}", passphrase);
81    ///     Ok(())
82    /// }
83    /// ```
84    pub fn passphrase(&self, input: PassphraseGeneratorRequest) -> Result<String, PassphraseError> {
85        passphrase(input)
86    }
87}
88
89impl GeneratorClient {
90    /// Generates a random username.
91    /// There are different username generation strategies, which can be customized using the
92    /// `input` parameter.
93    ///
94    /// Note that most generation strategies will be executed on the client side, but `Forwarded`
95    /// will use third-party services, which may require a specific setup or API key.
96    ///
97    /// ```
98    /// use bitwarden_core::Client;
99    /// use bitwarden_generators::{GeneratorClientsExt, UsernameError, UsernameGeneratorRequest};
100    ///
101    /// async fn test() -> Result<(), UsernameError> {
102    ///     let input = UsernameGeneratorRequest::Word {
103    ///         capitalize: true,
104    ///         include_number: true,
105    ///     };
106    ///     let username = Client::new(None).generator().username(input).await.unwrap();
107    ///     println!("{}", username);
108    ///     Ok(())
109    /// }
110    /// ```
111    pub async fn username(&self, input: UsernameGeneratorRequest) -> Result<String, UsernameError> {
112        username(input, self.client.internal.get_http_client()).await
113    }
114}
115
116#[allow(missing_docs)]
117pub trait GeneratorClientsExt {
118    fn generator(&self) -> GeneratorClient;
119}
120
121impl GeneratorClientsExt for Client {
122    fn generator(&self) -> GeneratorClient {
123        GeneratorClient::new(self.clone())
124    }
125}