From 9d8ddde81c497691945e074ba36c47b2e4070c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garci=CC=81a?= Date: Fri, 8 Dec 2023 15:36:12 +0100 Subject: [PATCH] Add user gen to uniffi, comments, and make username Send --- crates/bitwarden-uniffi/src/tool/mod.rs | 17 +++++++++++++++- .../src/tool/generators/client_generator.rs | 20 +++++++++++++++++++ .../bitwarden/src/tool/generators/username.rs | 8 ++++---- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/crates/bitwarden-uniffi/src/tool/mod.rs b/crates/bitwarden-uniffi/src/tool/mod.rs index 3243cceb6..282dbae69 100644 --- a/crates/bitwarden-uniffi/src/tool/mod.rs +++ b/crates/bitwarden-uniffi/src/tool/mod.rs @@ -1,7 +1,10 @@ use std::sync::Arc; use bitwarden::{ - tool::{ExportFormat, PassphraseGeneratorRequest, PasswordGeneratorRequest}, + tool::{ + ExportFormat, PassphraseGeneratorRequest, PasswordGeneratorRequest, + UsernameGeneratorRequest, + }, vault::{Cipher, Collection, Folder}, }; @@ -35,6 +38,18 @@ impl ClientGenerators { .passphrase(settings) .await?) } + + /// **API Draft:** Generate Username + pub async fn username(&self, settings: UsernameGeneratorRequest) -> Result { + Ok(self + .0 + .0 + .read() + .await + .generator() + .username(settings) + .await?) + } } #[derive(uniffi::Object)] diff --git a/crates/bitwarden/src/tool/generators/client_generator.rs b/crates/bitwarden/src/tool/generators/client_generator.rs index 23f7cca2c..d95ac1af3 100644 --- a/crates/bitwarden/src/tool/generators/client_generator.rs +++ b/crates/bitwarden/src/tool/generators/client_generator.rs @@ -63,6 +63,26 @@ impl<'a> ClientGenerator<'a> { 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::{Client, tool::{UsernameGeneratorRequest, UsernameGeneratorType}, error::Result}; + /// async fn test() -> Result<()> { + /// let input = UsernameGeneratorRequest { + /// r#type: UsernameGeneratorType::Word { + /// capitalize: Some(true), + /// include_number: Some(true), + /// } + /// }; + /// let username = Client::new(None).generator().username(input).await.unwrap(); + /// println!("{}", username); + /// Ok(()) + /// } + /// ``` pub async fn username(&self, input: UsernameGeneratorRequest) -> Result { username(input).await } diff --git a/crates/bitwarden/src/tool/generators/username.rs b/crates/bitwarden/src/tool/generators/username.rs index dfbed55b8..2f6a0b3b7 100644 --- a/crates/bitwarden/src/tool/generators/username.rs +++ b/crates/bitwarden/src/tool/generators/username.rs @@ -88,8 +88,8 @@ pub struct UsernameGeneratorRequest { } pub(super) async fn username(input: UsernameGeneratorRequest) -> Result { + use rand::thread_rng; use UsernameGeneratorType::*; - let mut rng = rand::thread_rng(); match input.r#type { Word { @@ -98,10 +98,10 @@ pub(super) async fn username(input: UsernameGeneratorRequest) -> Result } => { let capitalize = capitalize.unwrap_or(true); let include_number = include_number.unwrap_or(true); - Ok(username_word(&mut rng, capitalize, include_number)) + Ok(username_word(&mut thread_rng(), capitalize, include_number)) } - Subaddress { r#type, email } => Ok(username_subaddress(&mut rng, r#type, email)), - Catchall { r#type, domain } => Ok(username_catchall(&mut rng, r#type, domain)), + Subaddress { r#type, email } => Ok(username_subaddress(&mut thread_rng(), r#type, email)), + Catchall { r#type, domain } => Ok(username_catchall(&mut thread_rng(), r#type, domain)), Forwarded { service, website } => { use crate::tool::generators::username_forwarders::*; use ForwarderServiceType::*;