Files
clients/apps/cli/src/tools/generate.command.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

112 lines
4.0 KiB
TypeScript
Raw Normal View History

import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
import { DefaultPassphraseGenerationOptions } from "@bitwarden/common/tools/generator/passphrase";
import {
DefaultPasswordGenerationOptions,
PasswordGenerationServiceAbstraction,
} from "@bitwarden/common/tools/generator/password";
import { PasswordGeneratorOptions } from "@bitwarden/common/tools/generator/password/password-generator-options";
import { Response } from "../models/response";
import { StringResponse } from "../models/response/string.response";
2022-01-19 10:45:14 -05:00
import { CliUtils } from "../utils";
export class GenerateCommand {
constructor(
private passwordGenerationService: PasswordGenerationServiceAbstraction,
private stateService: StateService,
) {}
2022-01-19 10:45:14 -05:00
async run(cmdOptions: Record<string, any>): Promise<Response> {
const normalizedOptions = new Options(cmdOptions);
const options: PasswordGeneratorOptions = {
2022-01-19 10:45:14 -05:00
uppercase: normalizedOptions.uppercase,
lowercase: normalizedOptions.lowercase,
number: normalizedOptions.number,
special: normalizedOptions.special,
length: normalizedOptions.length,
type: normalizedOptions.type,
wordSeparator: normalizedOptions.separator,
numWords: normalizedOptions.words,
capitalize: normalizedOptions.capitalize,
includeNumber: normalizedOptions.includeNumber,
minNumber: normalizedOptions.minNumber,
minSpecial: normalizedOptions.minSpecial,
ambiguous: normalizedOptions.ambiguous,
2018-10-08 22:57:36 -04:00
};
const enforcedOptions = (await this.stateService.getIsAuthenticated())
? (await this.passwordGenerationService.enforcePasswordGeneratorPoliciesOnOptions(options))[0]
: options;
const password = await this.passwordGenerationService.generatePassword(enforcedOptions);
const res = new StringResponse(password);
return Response.success(res);
2021-12-20 18:04:00 +01:00
}
}
2022-01-19 10:45:14 -05:00
class Options {
uppercase: boolean;
lowercase: boolean;
number: boolean;
special: boolean;
length: number;
type: "passphrase" | "password";
separator: string;
words: number;
capitalize: boolean;
includeNumber: boolean;
minNumber: number;
minSpecial: number;
ambiguous: boolean;
2022-01-19 10:45:14 -05:00
constructor(passedOptions: Record<string, any>) {
this.uppercase = CliUtils.convertBooleanOption(passedOptions?.uppercase);
this.lowercase = CliUtils.convertBooleanOption(passedOptions?.lowercase);
this.number = CliUtils.convertBooleanOption(passedOptions?.number);
this.special = CliUtils.convertBooleanOption(passedOptions?.special);
this.capitalize = CliUtils.convertBooleanOption(passedOptions?.capitalize);
this.includeNumber = CliUtils.convertBooleanOption(passedOptions?.includeNumber);
this.ambiguous = CliUtils.convertBooleanOption(passedOptions?.ambiguous);
this.length = CliUtils.convertNumberOption(
passedOptions?.length,
DefaultPasswordGenerationOptions.length,
);
this.type = passedOptions?.passphrase ? "passphrase" : "password";
this.separator = CliUtils.convertStringOption(
passedOptions?.separator,
DefaultPassphraseGenerationOptions.wordSeparator,
);
this.words = CliUtils.convertNumberOption(
passedOptions?.words,
DefaultPassphraseGenerationOptions.numWords,
);
this.minNumber = CliUtils.convertNumberOption(
passedOptions?.minNumber,
DefaultPasswordGenerationOptions.minNumber,
);
this.minSpecial = CliUtils.convertNumberOption(
passedOptions?.minSpecial,
DefaultPasswordGenerationOptions.minSpecial,
);
2022-01-19 10:45:14 -05:00
if (!this.uppercase && !this.lowercase && !this.special && !this.number) {
this.lowercase = true;
this.uppercase = true;
this.number = true;
}
if (this.length < 5) {
this.length = 5;
}
if (this.words < 3) {
this.words = 3;
}
if (this.separator === "space") {
this.separator = " ";
} else if (this.separator === "empty") {
this.separator = "";
2022-01-19 10:45:14 -05:00
} else if (this.separator != null && this.separator.length > 1) {
this.separator = this.separator[0];
}
}
}