Skip to content

Commit f9c5198

Browse files
authored
Refactor password strength JS (hestiacp#3459)
1 parent 48ccfd9 commit f9c5198

File tree

7 files changed

+66
-164
lines changed

7 files changed

+66
-164
lines changed

web/js/events.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,21 @@ const VE = {
245245
document.body.appendChild(dialog);
246246
dialog.showModal();
247247
},
248+
recalculatePasswordStrength: (input) => {
249+
const password = input.value;
250+
const meter = input.parentNode.querySelector('.js-password-meter');
251+
if (meter) {
252+
// TODO: Switch to zxcvbn or something when we can load modules
253+
const validations = [
254+
password.length >= 8, // Min length of 8
255+
password.search(/[a-z]/) > -1, // Contains 1 lowercase letter
256+
password.search(/[A-Z]/) > -1, // Contains 1 uppercase letter
257+
password.search(/[0-9]/) > -1, // Contains 1 number
258+
];
259+
const strength = validations.reduce((acc, cur) => acc + cur, 0);
260+
meter.value = strength;
261+
}
262+
},
248263
warn: (msg) => {
249264
alert('WARNING: ' + msg);
250265
},

web/js/pages/add_db.js

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,36 +60,12 @@ App.Listeners.DB.keypress_db_databasename = function () {
6060
});
6161
};
6262

63-
App.Actions.DB.update_password_meter = function () {
64-
var password = $('input[name="v_password"]').val();
65-
var min_small = new RegExp(/^(?=.*[a-z]).+$/);
66-
var min_cap = new RegExp(/^(?=.*[A-Z]).+$/);
67-
var min_num = new RegExp(/^(?=.*\d).+$/);
68-
var min_length = 8;
69-
var score = 0;
70-
71-
if (password.length >= min_length) {
72-
score = score + 1;
73-
}
74-
if (min_small.test(password)) {
75-
score = score + 1;
76-
}
77-
if (min_cap.test(password)) {
78-
score = score + 1;
79-
}
80-
if (min_num.test(password)) {
81-
score = score + 1;
82-
}
83-
$('.js-password-meter').val(score);
84-
};
85-
8663
App.Listeners.DB.keypress_v_password = function () {
8764
var ref = $('input[name="v_password"]');
8865
ref.bind('keypress input', function (evt) {
8966
clearTimeout(window.frp_usr_tmt);
9067
window.frp_usr_tmt = setTimeout(function () {
91-
var elm = $(evt.target);
92-
App.Actions.DB.update_password_meter(elm, $(elm).val());
68+
VE.helpers.recalculatePasswordStrength(evt.target);
9369
}, 100);
9470
});
9571
};
@@ -103,6 +79,9 @@ App.Listeners.DB.keypress_db_username();
10379
App.Listeners.DB.keypress_db_databasename();
10480

10581
applyRandomPassword = function (min_length = 16) {
106-
$('input[name=v_password]').val(randomString(min_length));
107-
App.Actions.DB.update_password_meter();
82+
const passwordInput = document.querySelector('input[name=v_password]');
83+
if (passwordInput) {
84+
passwordInput.value = randomString(min_length);
85+
VE.helpers.recalculatePasswordStrength(passwordInput);
86+
}
10887
};

web/js/pages/add_mail_acc.js

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -73,50 +73,32 @@ $('form[name="v_quota"]').on('submit', function () {
7373
});
7474
});
7575

76-
App.Actions.MAIL_ACC.update_password_meter = function () {
77-
var password = $('input[name="v_password"]').val();
78-
var min_small = new RegExp(/^(?=.*[a-z]).+$/);
79-
var min_cap = new RegExp(/^(?=.*[A-Z]).+$/);
80-
var min_num = new RegExp(/^(?=.*\d).+$/);
81-
var min_length = 8;
82-
var score = 0;
83-
84-
if (password.length >= min_length) {
85-
score = score + 1;
86-
}
87-
if (min_small.test(password)) {
88-
score = score + 1;
89-
}
90-
if (min_cap.test(password)) {
91-
score = score + 1;
92-
}
93-
if (min_num.test(password)) {
94-
score = score + 1;
95-
}
96-
$('.js-password-meter').val(score);
97-
};
98-
9976
App.Listeners.MAIL_ACC.keypress_v_password = function () {
10077
var ref = $('input[name="v_password"]');
10178
ref.bind('keypress input', function (evt) {
10279
clearTimeout(window.frp_usr_tmt);
10380
window.frp_usr_tmt = setTimeout(function () {
104-
var elm = $(evt.target);
105-
App.Actions.MAIL_ACC.update_password_meter(elm, $(elm).val());
81+
VE.helpers.recalculatePasswordStrength(evt.target);
10682
}, 100);
10783
});
10884
};
10985

11086
App.Listeners.MAIL_ACC.keypress_v_password();
11187

11288
applyRandomPassword = function (min_length = 16) {
113-
var randomPassword = randomString(min_length);
114-
$('input[name=v_password]').val(randomPassword);
115-
if ($('input[name=v_password]').attr('type') == 'text')
116-
$('.js-password-output').text(randomPassword);
117-
else $('.js-password-output').text(Array(randomPassword.length + 1).join('*'));
118-
App.Actions.MAIL_ACC.update_password_meter();
119-
generate_mail_credentials();
89+
const randomPassword = randomString(min_length);
90+
const passwordInput = document.querySelector('input[name=v_password]');
91+
if (passwordInput) {
92+
passwordInput.value = randomPassword;
93+
VE.helpers.recalculatePasswordStrength(passwordInput);
94+
const passwordOutput = document.querySelector('.js-password-output');
95+
if (passwordInput.getAttribute('type') === 'text' && passwordOutput) {
96+
passwordOutput.textContent = randomPassword;
97+
} else {
98+
passwordOutput.textContent = Array(randomPassword.length + 1).join('*');
99+
}
100+
generate_mail_credentials();
101+
}
120102
};
121103

122104
generate_mail_credentials = function () {

web/js/pages/add_user.js

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,19 @@ $(function () {
1414
});
1515

1616
applyRandomPassword = function (min_length = 16) {
17-
$('input[name=v_password]').val(randomString(min_length));
18-
App.Actions.WEB.update_password_meter();
19-
};
20-
21-
App.Actions.WEB.update_password_meter = function () {
22-
var password = $('input[name="v_password"]').val();
23-
var min_small = new RegExp(/^(?=.*[a-z]).+$/);
24-
var min_cap = new RegExp(/^(?=.*[A-Z]).+$/);
25-
var min_num = new RegExp(/^(?=.*\d).+$/);
26-
var min_length = 8;
27-
var score = 0;
28-
29-
if (password.length >= min_length) {
30-
score = score + 1;
31-
}
32-
if (min_small.test(password)) {
33-
score = score + 1;
34-
}
35-
if (min_cap.test(password)) {
36-
score = score + 1;
37-
}
38-
if (min_num.test(password)) {
39-
score = score + 1;
17+
const passwordInput = document.querySelector('input[name=v_password]');
18+
if (passwordInput) {
19+
passwordInput.value = randomString(min_length);
20+
VE.helpers.recalculatePasswordStrength(passwordInput);
4021
}
41-
$('.js-password-meter').val(score);
4222
};
4323

4424
App.Listeners.WEB.keypress_v_password = function () {
4525
var ref = $('input[name="v_password"]');
4626
ref.bind('keypress input', function (evt) {
4727
clearTimeout(window.frp_usr_tmt);
4828
window.frp_usr_tmt = setTimeout(function () {
49-
var elm = $(evt.target);
50-
App.Actions.WEB.update_password_meter(elm, $(elm).val());
29+
VE.helpers.recalculatePasswordStrength(evt.target);
5130
}, 100);
5231
});
5332
};

web/js/pages/edit_db.js

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,36 +67,12 @@ App.Listeners.DB.keypress_db_databasename = function () {
6767
});
6868
};
6969

70-
App.Actions.DB.update_password_meter = function () {
71-
var password = $('input[name="v_password"]').val();
72-
var min_small = new RegExp(/^(?=.*[a-z]).+$/);
73-
var min_cap = new RegExp(/^(?=.*[A-Z]).+$/);
74-
var min_num = new RegExp(/^(?=.*\d).+$/);
75-
var min_length = 8;
76-
var score = 0;
77-
78-
if (password.length >= min_length) {
79-
score = score + 1;
80-
}
81-
if (min_small.test(password)) {
82-
score = score + 1;
83-
}
84-
if (min_cap.test(password)) {
85-
score = score + 1;
86-
}
87-
if (min_num.test(password)) {
88-
score = score + 1;
89-
}
90-
$('.js-password-meter').val(score);
91-
};
92-
9370
App.Listeners.DB.keypress_v_password = function () {
9471
var ref = $('input[name="v_password"]');
9572
ref.bind('keypress input', function (evt) {
9673
clearTimeout(window.frp_usr_tmt);
9774
window.frp_usr_tmt = setTimeout(function () {
98-
var elm = $(evt.target);
99-
App.Actions.DB.update_password_meter(elm, $(elm).val());
75+
VE.helpers.recalculatePasswordStrength(evt.target);
10076
}, 100);
10177
});
10278
};
@@ -110,6 +86,9 @@ App.Listeners.DB.keypress_db_username();
11086
App.Listeners.DB.keypress_db_databasename();
11187

11288
applyRandomPassword = function (min_length = 16) {
113-
$('input[name=v_password]').val(randomString(min_length));
114-
App.Actions.DB.update_password_meter();
89+
const passwordInput = document.querySelector('input[name=v_password]');
90+
if (passwordInput) {
91+
passwordInput.value = randomString(min_length);
92+
VE.helpers.recalculatePasswordStrength(passwordInput);
93+
}
11594
};

web/js/pages/edit_mail_acc.js

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -53,36 +53,12 @@ App.Listeners.MAIL_ACC.init = function () {
5353
App.Listeners.MAIL_ACC.init();
5454
App.Listeners.MAIL_ACC.checkbox_unlimited_feature();
5555

56-
App.Actions.MAIL_ACC.update_password_meter = function () {
57-
var password = $('input[name="v_password"]').val();
58-
var min_small = new RegExp(/^(?=.*[a-z]).+$/);
59-
var min_cap = new RegExp(/^(?=.*[A-Z]).+$/);
60-
var min_num = new RegExp(/^(?=.*\d).+$/);
61-
var min_length = 8;
62-
var score = 0;
63-
64-
if (password.length >= min_length) {
65-
score = score + 1;
66-
}
67-
if (min_small.test(password)) {
68-
score = score + 1;
69-
}
70-
if (min_cap.test(password)) {
71-
score = score + 1;
72-
}
73-
if (min_num.test(password)) {
74-
score = score + 1;
75-
}
76-
$('.js-password-meter').val(score);
77-
};
78-
7956
App.Listeners.MAIL_ACC.keypress_v_password = function () {
8057
var ref = $('input[name="v_password"]');
8158
ref.bind('keypress input', function (evt) {
8259
clearTimeout(window.frp_usr_tmt);
8360
window.frp_usr_tmt = setTimeout(function () {
84-
var elm = $(evt.target);
85-
App.Actions.MAIL_ACC.update_password_meter(elm, $(elm).val());
61+
VE.helpers.recalculatePasswordStrength(evt.target);
8662
}, 100);
8763
});
8864
};
@@ -101,13 +77,19 @@ $('#v_blackhole').on('click', function () {
10177
App.Listeners.MAIL_ACC.keypress_v_password();
10278

10379
applyRandomPassword = function (min_length = 16) {
104-
var randomPassword = randomString(min_length);
105-
$('input[name=v_password]').val(randomPassword);
106-
if ($('input[name=v_password]').attr('type') == 'text')
107-
$('.js-password-output').text(randomPassword);
108-
else $('.js-password-output').text(Array(randomPassword.length + 1).join('*'));
109-
App.Actions.MAIL_ACC.update_password_meter();
110-
generate_mail_credentials();
80+
const randomPassword = randomString(min_length);
81+
const passwordInput = document.querySelector('input[name=v_password]');
82+
if (passwordInput) {
83+
passwordInput.value = randomPassword;
84+
VE.helpers.recalculatePasswordStrength(passwordInput);
85+
const passwordOutput = document.querySelector('.js-password-output');
86+
if (passwordInput.getAttribute('type') === 'text' && passwordOutput) {
87+
passwordOutput.textContent = randomPassword;
88+
} else {
89+
passwordOutput.textContent = Array(randomPassword.length + 1).join('*');
90+
}
91+
generate_mail_credentials();
92+
}
11193
};
11294

11395
generate_mail_credentials = function () {

web/js/pages/edit_user.js

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
11
applyRandomPassword = function (min_length = 16) {
2-
$('input[name=v_password]').val(randomString(min_length));
3-
App.Actions.WEB.update_password_meter();
4-
};
5-
6-
App.Actions.WEB.update_password_meter = () => {
7-
/**
8-
* @type string
9-
*/
10-
const password = document.querySelector('input[name=v_password]').value;
11-
12-
const validations = [
13-
password.length >= 8, // Min length of 8
14-
password.search(/[a-z]/) > -1, // Contains 1 lowercase letter
15-
password.search(/[A-Z]/) > -1, // Contains 1 uppercase letter
16-
password.search(/[0-9]/) > -1, // Contains 1 number
17-
];
18-
const strength = validations.reduce((acc, cur) => acc + cur, 0);
19-
20-
document.querySelector('.js-password-meter').value = strength;
2+
const passwordInput = document.querySelector('input[name=v_password]');
3+
if (passwordInput) {
4+
passwordInput.value = randomString(min_length);
5+
VE.helpers.recalculatePasswordStrength(passwordInput);
6+
}
217
};
228

239
App.Listeners.WEB.keypress_v_password = () => {
2410
const updateTimeout = (evt) => {
2511
clearTimeout(window.frp_usr_tmt);
2612
window.frp_usr_tmt = setTimeout(() => {
27-
App.Actions.WEB.update_password_meter(evt.target, evt.target.value);
13+
VE.helpers.recalculatePasswordStrength(evt.target);
2814
}, 100);
2915
};
3016

0 commit comments

Comments
 (0)