[PM-6755] Fix password generation defaults on CLI (#8308)

* Fix minSpecial for pwd generation being set to 1 instead of zero

* Use less magic numbers

---------

Co-authored-by: Daniel James Smith <djsmith85@users.noreply.github.com>
This commit is contained in:
Daniel James Smith 2024-03-19 17:20:57 +01:00 committed by GitHub
parent 61b3759736
commit ea0035f658
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 7 deletions

View File

@ -1,6 +1,9 @@
import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
import { DefaultPassphraseGenerationOptions } from "@bitwarden/common/tools/generator/passphrase"; import { DefaultPassphraseGenerationOptions } from "@bitwarden/common/tools/generator/passphrase";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/common/tools/generator/password"; import {
DefaultPasswordGenerationOptions,
PasswordGenerationServiceAbstraction,
} from "@bitwarden/common/tools/generator/password";
import { PasswordGeneratorOptions } from "@bitwarden/common/tools/generator/password/password-generator-options"; import { PasswordGeneratorOptions } from "@bitwarden/common/tools/generator/password/password-generator-options";
import { Response } from "../models/response"; import { Response } from "../models/response";
@ -64,7 +67,10 @@ class Options {
this.capitalize = CliUtils.convertBooleanOption(passedOptions?.capitalize); this.capitalize = CliUtils.convertBooleanOption(passedOptions?.capitalize);
this.includeNumber = CliUtils.convertBooleanOption(passedOptions?.includeNumber); this.includeNumber = CliUtils.convertBooleanOption(passedOptions?.includeNumber);
this.ambiguous = CliUtils.convertBooleanOption(passedOptions?.ambiguous); this.ambiguous = CliUtils.convertBooleanOption(passedOptions?.ambiguous);
this.length = CliUtils.convertNumberOption(passedOptions?.length, 14); this.length = CliUtils.convertNumberOption(
passedOptions?.length,
DefaultPasswordGenerationOptions.length,
);
this.type = passedOptions?.passphrase ? "passphrase" : "password"; this.type = passedOptions?.passphrase ? "passphrase" : "password";
this.separator = CliUtils.convertStringOption( this.separator = CliUtils.convertStringOption(
passedOptions?.separator, passedOptions?.separator,
@ -74,8 +80,14 @@ class Options {
passedOptions?.words, passedOptions?.words,
DefaultPassphraseGenerationOptions.numWords, DefaultPassphraseGenerationOptions.numWords,
); );
this.minNumber = CliUtils.convertNumberOption(passedOptions?.minNumber, 1); this.minNumber = CliUtils.convertNumberOption(
this.minSpecial = CliUtils.convertNumberOption(passedOptions?.minSpecial, 1); passedOptions?.minNumber,
DefaultPasswordGenerationOptions.minNumber,
);
this.minSpecial = CliUtils.convertNumberOption(
passedOptions?.minSpecial,
DefaultPasswordGenerationOptions.minSpecial,
);
if (!this.uppercase && !this.lowercase && !this.special && !this.number) { if (!this.uppercase && !this.lowercase && !this.special && !this.number) {
this.lowercase = true; this.lowercase = true;

View File

@ -78,6 +78,6 @@ export const DefaultPasswordGenerationOptions: Partial<PasswordGenerationOptions
lowercase: true, lowercase: true,
number: true, number: true,
minNumber: 1, minNumber: 1,
special: true, special: false,
minSpecial: 1, minSpecial: 0,
}); });

View File

@ -23,7 +23,7 @@ const DefaultOptions: PasswordGeneratorOptions = {
lowercase: true, lowercase: true,
minLowercase: 0, minLowercase: 0,
special: false, special: false,
minSpecial: 1, minSpecial: 0,
type: "password", type: "password",
numWords: 3, numWords: 3,
wordSeparator: "-", wordSeparator: "-",