1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-27 12:36:14 +01:00

Add common password requirements option to passphrase generator (#43)

* Added commont password requirements option to passphrase generator

* Processed PR comments
This commit is contained in:
SvenvDam 2019-06-06 20:06:38 +02:00 committed by Kyle Spearrin
parent f4f90f83cd
commit a017f72506

View File

@ -25,6 +25,8 @@ const DefaultOptions = {
type: 'password',
numWords: 3,
wordSeparator: '-',
capitalize: false,
includeNumber: false,
};
const Keys = {
@ -171,12 +173,29 @@ export class PasswordGenerationService implements PasswordGenerationServiceAbstr
o.wordSeparator = ' ';
}
if (o.capitalize == null) {
o.addCommonRequirements = false;
}
if (o.includeNumber == null) {
o.includeNumber = false;
}
const listLength = EEFLongWordList.length - 1;
const wordList = new Array(o.numWords);
let wordList = new Array(o.numWords);
for (let i = 0; i < o.numWords; i++) {
const wordIndex = await this.cryptoService.randomNumber(0, listLength);
wordList[i] = EEFLongWordList[wordIndex];
if (o.capitalize) {
wordList[i] = this.capitalize(EEFLongWordList[wordIndex]);
} else {
wordList[i] = EEFLongWordList[wordIndex];
}
}
if (o.includeNumber) {
return await this.insertNumber(wordList.join(o.wordSeparator));
}
return wordList.join(o.wordSeparator);
}
@ -256,6 +275,16 @@ export class PasswordGenerationService implements PasswordGenerationServiceAbstr
return result;
}
private capitalize(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
private async insertNumber(word: string) {
const charIndex = await this.cryptoService.randomNumber(0, word.length - 1);
const addedNumber = await this.cryptoService.randomNumber(0, 9);
return word.substring(0, charIndex) + addedNumber + word.substring(charIndex, word.length);
}
private async encryptHistory(history: GeneratedPasswordHistory[]): Promise<GeneratedPasswordHistory[]> {
if (history == null || history.length === 0) {
return Promise.resolve([]);