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

Add validation when a password is generated #2640

Merged
merged 5 commits into from
May 23, 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
4 changes: 2 additions & 2 deletions assets/js/lib/forms/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export const PASSWORD_POLICY_TEXT = (
<br />
- does not have 3 consecutive repeated numbers or letters (example: 111 or
aaa)
<br />- does not have 3 consecutive sequential numbers or letters (example:
123 or abc)
<br />- does not have 4 consecutive sequential numbers or letters (example:
1234, abcd or ABCD)
</div>
);
export const REQUIRED_FIELD_TEXT = 'Required field';
Expand Down
4 changes: 2 additions & 2 deletions assets/js/pages/Users/UserForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '@lib/forms';
import { getError } from '@lib/api/validationErrors';

import { generatePassword } from './generatePassword';
import { generateValidPassword } from './generatePassword';

const USER_ENABLED = 'Enabled';

Expand Down Expand Up @@ -115,7 +115,7 @@ function UserForm({
};

const onGeneratePassword = () => {
const newPassword = generatePassword();
const newPassword = generateValidPassword();
setPassword(newPassword);
setConfirmPassword(newPassword);
};
Expand Down
38 changes: 34 additions & 4 deletions assets/js/pages/Users/generatePassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,40 @@ const SPECIAL_CHARACTERS = '~!@#$%^&*()_+={}[]|:;<>,.?/-\'"`';
const PASSWORD_CHARACTERS =
DIGITS + UPPERCASE_LETTERS + LOWERCASE_LETTERS + SPECIAL_CHARACTERS;

export const generatePassword = (
length = 16,
characters = PASSWORD_CHARACTERS
) =>
const MAX_SEQUENTIAL_CHARS = 3;
const SEQUENCES = [DIGITS, UPPERCASE_LETTERS, LOWERCASE_LETTERS];

const hasValidPwdLength = (password, pwdLength = 8) =>
password.length >= pwdLength;

const hasNoRepetitiveCharacters = (password) => !/(.)\1{2,}/.test(password);

const hasNoSequentialCharacters = (password) =>
!SEQUENCES.some((seq) => {
const max = seq.length - MAX_SEQUENTIAL_CHARS;
return Array.from(Array(max).keys()).some((i) => {
const pattern = seq.slice(i, i + MAX_SEQUENTIAL_CHARS + 1);
return password.includes(pattern);
});
});

const generatePassword = (length = 16, characters = PASSWORD_CHARACTERS) =>
Array.from(crypto.getRandomValues(new Uint32Array(length)))
.map((char) => characters[char % characters.length])
.join('');

export const isValidPassword = (password) => {
if (
hasValidPwdLength(password) &&
hasNoRepetitiveCharacters(password) &&
hasNoSequentialCharacters(password)
) {
return true;
}
return false;
};

export const generateValidPassword = (length = 16) => {
const password = generatePassword(length);
return isValidPassword(password) ? password : generateValidPassword(length);
};
65 changes: 56 additions & 9 deletions assets/js/pages/Users/generatePassword.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,67 @@
import { generatePassword } from './generatePassword';
import { isValidPassword, generateValidPassword } from './generatePassword';

describe('generatePassword', () => {
test('generates a password of default length 16', () => {
const password = generatePassword();
describe('generateValidPassword', () => {
it('should generate a password with a default length of 16', () => {
const password = generateValidPassword();
expect(password).toHaveLength(16);
});

test('generates a password of specific length', () => {
it('should generate a password of specific length', () => {
const length = 20;
const password = generatePassword(length);
const password = generateValidPassword(length);
expect(password).toHaveLength(length);
});

test('generates passwords that vary', () => {
const firstPassword = generatePassword();
const secondPassword = generatePassword();
it('should generate passwords that vary', () => {
const firstPassword = generateValidPassword();
const secondPassword = generateValidPassword();
expect(firstPassword).not.toBe(secondPassword);
});
});

describe('isValidPassword', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put some new lines between describe and test entries

const passwordList = [
{
expectedResult: false,
password: 'short',
},
{
expectedResult: false,
password: 'passwordaaa',
},
{
expectedResult: false,
password: 'passwordBBB',
},
{
expectedResult: false,
password: 'password555',
},
{
expectedResult: false,
password: 'password1234',
},
{
expectedResult: false,
password: 'passwordefgh',
},
{
expectedResult: false,
password: 'passwordKLMNO',
},
{
expectedResult: true,
password: 'H^a*~wvFSv88*NRG',
},
];
it.each(passwordList)(
'should return false if the password is invalid',
({ expectedResult, password }) => {
expect(isValidPassword(password)).toBe(expectedResult);
}
);
it('should return true if the password is valid', () => {
const password = generateValidPassword();
expect(isValidPassword(password)).toBe(true);
});
});
2 changes: 1 addition & 1 deletion lib/trento/users/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Trento.Users.User do

defdelegate authorize(action, user, params), to: Trento.Users.Policy

@sequences ["01234567890", "abcdefghijklmnopqrstuvwxyz"]
@sequences ["01234567890", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
@max_sequential_chars 3

schema "users" do
Expand Down