Skip to content

Commit

Permalink
Change input
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Dec 4, 2023
1 parent 97d83e5 commit 2a27f5f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
3 changes: 1 addition & 2 deletions crates/bitwarden/src/tool/generators/client_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ impl<'a> ClientGenerator<'a> {
/// }
/// ```
pub async fn passphrase(&self, input: PassphraseGeneratorRequest) -> Result<String> {
let options = input.validate_options()?;
Ok(passphrase(options))
passphrase(input)
}
}

Expand Down
26 changes: 11 additions & 15 deletions crates/bitwarden/src/tool/generators/passphrase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,18 @@ impl Default for PassphraseGeneratorRequest {
const MINIMUM_PASSPHRASE_NUM_WORDS: u8 = 3;
const MAXIMUM_PASSPHRASE_NUM_WORDS: u8 = 20;

// We don't want the validated struct to be accessible, yet at the same time it needs to be public
// to be used as a return type, so we define it in a private module to make it innaccessible.
mod private {
/// Represents a set of valid options to generate a passhprase with.
/// To get an instance of it, use [`PassphraseGeneratorRequest::validate_options`](PassphraseGeneratorRequest::validate_options)
pub struct ValidPassphraseGeneratorOptions {
pub(super) num_words: u8,
pub(super) word_separator: String,
pub(super) capitalize: bool,
pub(super) include_number: bool,
}
/// Represents a set of valid options to generate a passhprase with.
/// To get an instance of it, use [`PassphraseGeneratorRequest::validate_options`](PassphraseGeneratorRequest::validate_options)
struct ValidPassphraseGeneratorOptions {
pub(super) num_words: u8,
pub(super) word_separator: String,
pub(super) capitalize: bool,
pub(super) include_number: bool,
}
use private::ValidPassphraseGeneratorOptions;

impl PassphraseGeneratorRequest {
/// Validates the request and returns an immutable struct with valid options to use with the passphrase generator.
pub fn validate_options(self) -> Result<ValidPassphraseGeneratorOptions> {
fn validate_options(self) -> Result<ValidPassphraseGeneratorOptions> {
// TODO: Add password generator policy checks

if !(MINIMUM_PASSPHRASE_NUM_WORDS..=MAXIMUM_PASSPHRASE_NUM_WORDS).contains(&self.num_words)
Expand All @@ -79,8 +74,9 @@ impl PassphraseGeneratorRequest {
/// # Arguments:
/// * `options`: Valid parameters used to generate the passphrase. To create it, use
/// [`PassphraseGeneratorRequest::validate_options`](PassphraseGeneratorRequest::validate_options).
pub(super) fn passphrase(options: ValidPassphraseGeneratorOptions) -> String {
passphrase_with_rng(rand::thread_rng(), options)
pub(super) fn passphrase(request: PassphraseGeneratorRequest) -> Result<String> {
let options = request.validate_options()?;
Ok(passphrase_with_rng(rand::thread_rng(), options))
}

fn passphrase_with_rng(mut rng: impl RngCore, options: ValidPassphraseGeneratorOptions) -> String {
Expand Down

0 comments on commit 2a27f5f

Please sign in to comment.