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