Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cqi 147: strenghten password policy for generated passwords #102

Merged
merged 3 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/UserMasterPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const UserMasterPanel = (props) => {

const generatePassword = () => {
const passwordGeneratorOptions = modulesManager.getConf("fe-admin", "passwordGeneratorOptions", {
length: 10,
length: 12,
isNumberRequired: true,
isLowerCaseRequired: true,
isUpperCaseRequired: true,
Expand Down
25 changes: 18 additions & 7 deletions src/helpers/passwordGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const passwordGenerator = (options) => {
const lowercase = uppercase.toLowerCase();
const numbers = "0123456789";
const specialCharacters = "!@#$%^&*()_+-=[]{}|;:,.<>?";
const length = options?.length ?? 10;
const length = options?.length ?? 12;
const isNumberRequired = options?.isNumberRequired ?? true;
const isLowerCaseRequired = options?.isLowerCaseRequired ?? true;
const isUpperCaseRequired = options?.isUpperCaseRequired ?? true;
Expand All @@ -25,20 +25,31 @@ export const passwordGenerator = (options) => {

const categoriesArray = getCategoriesArray();

getSafeRandomNumberArray(length, categoriesArray.length).forEach((category) => {
password += getRandomOfType(categoriesArray[category]);
categoriesArray.forEach((category) => {
password += getRandomOfType(category);
});

for (let i = categoriesArray.length; i < length; i++) {
const randomCategory = categoriesArray[getSafeRandomNumberArray(1, categoriesArray.length)];
password += getRandomOfType(randomCategory);
}

password = shuffle(password);

return password;
};

function shuffle(str) {
return str.split('').sort(() => getSafeRandomNumberArray(1, 3) - 0.5).join('');
}

function getRandomOfType(charset) {
return charset.charAt(getSafeRandomNumberArray(1, charset.length));
}

function getSafeRandomNumberArray(length, modulo) {
// crypto.getRandomValues is coded in a way that is cryptographically secure
// do not use Math.Random to generate password
const seedArray = self.crypto.getRandomValues(new Uint32Array(length));
return Array.from(seedArray, (value) => value % modulo);
}

function getRandomOfType(charset) {
return charset.charAt(getSafeRandomNumberArray(1, charset.length));
}
Loading