-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathlutecePassword.js
More file actions
156 lines (151 loc) · 6.88 KB
/
Copy pathlutecePassword.js
File metadata and controls
156 lines (151 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* A class for generating a new password and returning its complexity value
*/
export class LutecePassword {
/**
* Creates a new instance of LutecePassword.
* @param {NodeListOf<Element>} passwords - The list of password's input elements.
*/
constructor() {
this.lowercase = "abcdefghijklmnopqrstuvwxyz";
this.uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
this.numbers = "0123456789";
this.symbols = "!@#$%^&*()_+-=[]{}|;:,.<>?";
this.passwordInput = '#password';
this.progressBar = '#progress';
this.passTogglerBtn = '#lutece-password-toggler';
this.passTogglerIconOn = "ti-eye";
this.passTogglerIconOff = "ti-eye-off";
this.weakPasswords = ["123456", "123456789", "12345", "12345678", "1234", "111111", "1234567", "password"
, "123123", "qwerty", "abc123", "password1", "000000", "iloveyou", "123321", "qwertyuiop", "monkey", "dragon"
, "letmein", "football", "admin", "adminadmin", "welcome", "login", "princess", "solo", "passw0rd", "master"
, "sunshine", "shadow", "superman", "azerty", "azertyuiop", "qwerty123", "1q2w3e4r", "654321", "pokemon"
, "starwars", "batman", "trustno1", "hello", "freedom", "whatever", "baseball"];
}
/**
* Generates a new password that meets the following criteria:
* - Between 12 and 15 characters long.
* - Contains at least one lowercase letter, one uppercase letter, one number, and one symbol.
* - Uses the remaining characters from any of the character sets.
* @returns {string} The new password.
*/
generatePassword() {
let password = "";
let passwordLength = Math.floor(Math.random() * 4) + 12;
// Use at least one character from each character set
password += this.lowercase.charAt(Math.floor(Math.random() * this.lowercase.length));
password += this.uppercase.charAt(Math.floor(Math.random() * this.uppercase.length));
password += this.numbers.charAt(Math.floor(Math.random() * this.numbers.length));
password += this.symbols.charAt(Math.floor(Math.random() * this.symbols.length));
// Use remaining characters from any character set
let remainingLength = passwordLength - 4;
for (let i = 0; i < remainingLength; i++) {
let characterSet = Math.floor(Math.random() * 4);
switch (characterSet) {
case 0:
password += this.lowercase.charAt(Math.floor(Math.random() * this.lowercase.length));
break;
case 1:
password += this.uppercase.charAt(Math.floor(Math.random() * this.uppercase.length));
break;
case 2:
password += this.numbers.charAt(Math.floor(Math.random() * this.numbers.length));
break;
case 3:
password += this.symbols.charAt(Math.floor(Math.random() * this.symbols.length));
break;
}
}
return password;
}
/**
* Calculates the complexity of the given password and returns it as a percentage of the maximum score (100).
* The complexity is based on the following criteria:
* - The length of the password.
* - Whether the password contains at least one lowercase letter, one uppercase letter, one number, and one symbol.
* @param {string} password The password to check.
* @returns {number} The complexity value of the password, as a percentage of the maximum score (100).
*/
calcComplexity(password) {
if (this.weakPasswords.includes(password))
{
return 5;
}
let hasLowercase = /[a-z]/.test(password);
let hasUppercase = /[A-Z]/.test(password);
let hasNumber = /\d/.test(password);
let hasSymbol = /[^A-Za-z0-9]/.test(password);
let lengthScore = Math.min(password.length / 20 * 100, 50);
let complexityScore = 0;
if (hasLowercase && hasUppercase && hasNumber && hasSymbol) {
complexityScore = 50;
} else if ((hasLowercase || hasUppercase) && hasNumber && hasSymbol) {
complexityScore = 25;
} else if (hasLowercase && hasUppercase && hasNumber) {
complexityScore = 20;
} else if ((hasLowercase || hasUppercase) && hasNumber) {
complexityScore = 10;
} else if (hasLowercase && hasUppercase) {
complexityScore = 5;
}
return Math.round(lengthScore + complexityScore);
}
/**
* Set the complexity bar
* Show the complexity result in progress bar.
*/
getComplexity( ) {
const input = document.querySelector( this.passwordInput )
const progressBar = document.querySelector( this.progressBar )
const progressBarText = document.querySelector( this.progressBar + "-text" )
const progressBarStatus = document.querySelector( this.progressBar + "-status" )
const label = progressBar.labels?.[0] || null;
input.addEventListener('input', () => {
const complexityScore = this.calcComplexity(input.value);
progressBarText.textContent = Math.round(complexityScore) + "%";
progressBarStatus.textContent = (label?.textContent ?? '') + ' ' + Math.round(complexityScore) + "%";
progressBar.value = complexityScore;
progressBar.classList.remove('lutece-progress-danger', 'lutece-progress-warning', 'lutece-progress-success');
if( complexityScore < 50 ) {
progressBar.classList.add('lutece-progress-danger')
} else if ( complexityScore >= 50 && complexityScore < 100 ) {
progressBar.classList.add('lutece-progress-warning')
} else {
progressBar.classList.add('lutece-progress-success')
}
});
}
/**
* Initializes the password toggler.
*/
initPassToggler( ) {
const _passDefaultTogglerIcon = "ti-eye";
const btnToggler = document.querySelector( this.passTogglerBtn )
const inputToggler = document.querySelectorAll( this.passwordInput )
btnToggler.firstElementChild.classList.remove( _passDefaultTogglerIcon );
btnToggler.firstElementChild.classList.add( this.passTogglerIconOn );
btnToggler.addEventListener(
"click", ( event) => {
event.preventDefault();
inputToggler.forEach( input => {
this.showHidePassword( btnToggler, input );
});
});
}
/**
* Show / Hide password.
* @param {string} btn The button id that old the event.
* @param {string} field The password input to toggle.
*/
showHidePassword( btn, field ){
if ( field.type == 'password') {
field.setAttribute( 'type', 'text' );
btn.firstElementChild.classList.add( this.passTogglerIconOff );
btn.firstElementChild.classList.remove( this.passTogglerIconOn );
} else {
btn.firstElementChild.classList.remove( this.passTogglerIconOff );
btn.firstElementChild.classList.add( this.passTogglerIconOn );
field.setAttribute('type', 'password');
}
};
}