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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/**
* @file
* User behaviors.
*/
(($, Drupal) => {
/**
* An object containing CSS classes used for password widget.
*
* @type {object}
* @prop {string} passwordParent - A CSS class for the parent element.
* @prop {string} passwordsMatch - A CSS class indicating password match.
* @prop {string} passwordsNotMatch - A CSS class indicating passwords
* doesn't match.
* @prop {string} passwordWeak - A CSS class indicating weak password
* strength.
* @prop {string} passwordFair - A CSS class indicating fair password
* strength.
* @prop {string} passwordGood - A CSS class indicating good password
* strength.
* @prop {string} passwordStrong - A CSS class indicating strong password
* strength.
* @prop {string} widgetInitial - Initial CSS class that should be removed
* on a state change.
* @prop {string} passwordEmpty - A CSS class indicating password has not
* been filled.
* @prop {string} passwordFilled - A CSS class indicating password has
* been filled.
* @prop {string} confirmEmpty - A CSS class indicating password
* confirmation has not been filled.
* @prop {string} confirmFilled - A CSS class indicating password
* confirmation has been filled.
*/
Drupal.user = {
password: {
css: {
passwordParent: 'password-parent',
passwordsMatch: 'ok',
passwordsNotMatch: 'error',
passwordWeak: 'is-weak',
passwordFair: 'is-fair',
passwordGood: 'is-good',
passwordStrong: 'is-strong',
widgetInitial: '',
passwordEmpty: '',
passwordFilled: '',
confirmEmpty: '',
confirmFilled: '',
},
},
};
/**
* Attach handlers to evaluate the strength of any password fields and to
* check that its confirmation is correct.
*
* @type {Drupal~behavior}
*
* @prop {Drupal~behaviorAttach} attach
* Attaches password strength indicator and other relevant validation to
* password fields.
*/
Drupal.behaviors.password = {
attach(context, settings) {
const cssClasses = Drupal.user.password.css;
$(context)
.find('input.js-password-field')
.once('password')
.each((index, value) => {
const $mainInput = $(value);
const $mainInputParent = $mainInput
.parent()
.addClass(cssClasses.passwordParent);
const $passwordWidget = $mainInput.closest(
'.js-form-type-password-confirm',
);
const $confirmInput = $passwordWidget.find(
'input.js-password-confirm',
);
const $passwordConfirmMessage = $(
Drupal.theme('passwordConfirmMessage', settings.password),
);
let $passwordMatchStatus = $passwordConfirmMessage
.find('[data-drupal-selector="password-match-status-text"]')
.first();
if ($passwordMatchStatus.length === 0) {
$passwordMatchStatus = $passwordConfirmMessage.find('span').first();
Drupal.deprecationError({
message:
'Returning <span> without data-drupal-selector="password-match-status-text" attribute is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. See https://www.drupal.org/node/3152101',
});
}
const $confirmInputParent = $confirmInput
.parent()
.addClass('confirm-parent')
.append($passwordConfirmMessage);
// List of classes to be removed from the strength bar on a state
// change.
const passwordStrengthBarClassesToRemove = [
cssClasses.passwordWeak || '',
cssClasses.passwordFair || '',
cssClasses.passwordGood || '',
cssClasses.passwordStrong || '',
]
.join(' ')
.trim();
// List of classes to be removed from the text wrapper on a state
// change.
const confirmTextWrapperClassesToRemove = [
cssClasses.passwordsMatch || '',
cssClasses.passwordsNotMatch || '',
]
.join(' ')
.trim();
// List of classes to be removed from the widget on a state change.
const widgetClassesToRemove = [
cssClasses.widgetInitial || '',
cssClasses.passwordEmpty || '',
cssClasses.passwordFilled || '',
cssClasses.confirmEmpty || '',
cssClasses.confirmFilled || '',
]
.join(' ')
.trim();
const password = {};
// If the password strength indicator is enabled, add its markup.
if (settings.password.showStrengthIndicator) {
const $passwordStrength = $(
Drupal.theme('passwordStrength', settings.password),
);
password.$strengthBar = $passwordStrength
.find('[data-drupal-selector="password-strength-indicator"]')
.first();
if (password.$strengthBar.length === 0) {
password.$strengthBar = $passwordStrength
.find('.js-password-strength__indicator')
.first();
Drupal.deprecationError({
message:
'The js-password-strength__indicator class is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Replace js-password-strength__indicator with a data-drupal-selector="password-strength-indicator" attribute. See https://www.drupal.org/node/3152101',
});
}
password.$strengthTextWrapper = $passwordStrength
.find('[data-drupal-selector="password-strength-text"]')
.first();
if (password.$strengthTextWrapper.length === 0) {
password.$strengthTextWrapper = $passwordStrength
.find('.js-password-strength__text')
.first();
Drupal.deprecationError({
message:
'The js-password-strength__text class is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Replace js-password-strength__text with a data-drupal-selector="password-strength-text" attribute. See https://www.drupal.org/node/3152101',
});
}
password.$suggestions = $(
Drupal.theme('passwordSuggestions', settings.password, []),
);
password.$suggestions.hide();
$mainInputParent.append($passwordStrength);
$confirmInputParent.after(password.$suggestions);
}
/**
* Adds classes to the widget indicating if the elements are filled.
*/
const addWidgetClasses = () => {
$passwordWidget
.addClass(
$mainInput.val()
? cssClasses.passwordFilled
: cssClasses.passwordEmpty,
)
.addClass(
$confirmInput.val()
? cssClasses.confirmFilled
: cssClasses.confirmEmpty,
);
};
/**
* Check that password and confirmation inputs match.
*
* @param {string} confirmInputVal
* The value of the confirm input.
*/
const passwordCheckMatch = (confirmInputVal) => {
const passwordsAreMatching = $mainInput.val() === confirmInputVal;
const confirmClass = passwordsAreMatching
? cssClasses.passwordsMatch
: cssClasses.passwordsNotMatch;
const confirmMessage = passwordsAreMatching
? settings.password.confirmSuccess
: settings.password.confirmFailure;
// Update the success message and set the class if needed.
if (
!$passwordMatchStatus.hasClass(confirmClass) ||
!$passwordMatchStatus.html() === confirmMessage
) {
if (confirmTextWrapperClassesToRemove) {
$passwordMatchStatus.removeClass(
confirmTextWrapperClassesToRemove,
);
}
$passwordMatchStatus.html(confirmMessage).addClass(confirmClass);
}
};
/**
* Checks the password strength.
*/
const passwordCheck = () => {
if (settings.password.showStrengthIndicator) {
// Evaluate the password strength.
const result = Drupal.evaluatePasswordStrength(
$mainInput.val(),
settings.password,
);
const $currentPasswordSuggestions = $(
Drupal.theme(
'passwordSuggestions',
settings.password,
result.messageTips,
),
);
// Update the suggestions for how to improve the password if needed.
if (
password.$suggestions.html() !==
$currentPasswordSuggestions.html()
) {
password.$suggestions.replaceWith($currentPasswordSuggestions);
password.$suggestions = $currentPasswordSuggestions.toggle(
// Only show the description box if a weakness exists in the
// password.
result.strength !== 100,
);
}
if (passwordStrengthBarClassesToRemove) {
password.$strengthBar.removeClass(
passwordStrengthBarClassesToRemove,
);
}
// Adjust the length of the strength indicator.
password.$strengthBar
.css('width', `${result.strength}%`)
.addClass(result.indicatorClass);
// Update the strength indication text.
password.$strengthTextWrapper.html(result.indicatorText);
}
// Check the value in the confirm input and show results.
if ($confirmInput.val()) {
passwordCheckMatch($confirmInput.val());
$passwordConfirmMessage.css({ visibility: 'visible' });
} else {
$passwordConfirmMessage.css({ visibility: 'hidden' });
}
if (widgetClassesToRemove) {
$passwordWidget.removeClass(widgetClassesToRemove);
addWidgetClasses();
}
};
if (widgetClassesToRemove) {
addWidgetClasses();
}
// Monitor input events.
$mainInput.on('input', passwordCheck);
$confirmInput.on('input', passwordCheck);
});
},
};
/**
* Evaluate the strength of a user's password.
*
* Returns the estimated strength and the relevant output message.
*
* @param {string} password
* The password to evaluate.
* @param {object} passwordSettings
* A password settings object containing the text to display and the CSS
* classes for each strength level.
*
* @return {object}
* An object containing strength, message, indicatorText and indicatorClass.
*/
Drupal.evaluatePasswordStrength = (password, passwordSettings) => {
password = password.trim();
let indicatorText;
let indicatorClass;
let weaknesses = 0;
let strength = 100;
let msg = [];
const hasLowercase = /[a-z]/.test(password);
const hasUppercase = /[A-Z]/.test(password);
const hasNumbers = /[0-9]/.test(password);
const hasPunctuation = /[^a-zA-Z0-9]/.test(password);
// If there is a username edit box on the page, compare password to that,
// otherwise use value from the database.
const $usernameBox = $('input.username');
const username =
$usernameBox.length > 0 ? $usernameBox.val() : passwordSettings.username;
// Lose 5 points for every character less than 12, plus a 30 point penalty.
if (password.length < 12) {
msg.push(passwordSettings.tooShort);
strength -= (12 - password.length) * 5 + 30;
}
// Count weaknesses.
if (!hasLowercase) {
msg.push(passwordSettings.addLowerCase);
weaknesses += 1;
}
if (!hasUppercase) {
msg.push(passwordSettings.addUpperCase);
weaknesses += 1;
}
if (!hasNumbers) {
msg.push(passwordSettings.addNumbers);
weaknesses += 1;
}
if (!hasPunctuation) {
msg.push(passwordSettings.addPunctuation);
weaknesses += 1;
}
// Apply penalty for each weakness (balanced against length penalty).
switch (weaknesses) {
case 1:
strength -= 12.5;
break;
case 2:
strength -= 25;
break;
case 3:
case 4:
strength -= 40;
break;
}
// Check if password is the same as the username.
if (password !== '' && password.toLowerCase() === username.toLowerCase()) {
msg.push(passwordSettings.sameAsUsername);
// Passwords the same as username are always very weak.
strength = 5;
}
const cssClasses = Drupal.user.password.css;
// Based on the strength, work out what text should be shown by the
// password strength meter.
if (strength < 60) {
indicatorText = passwordSettings.weak;
indicatorClass = cssClasses.passwordWeak;
} else if (strength < 70) {
indicatorText = passwordSettings.fair;
indicatorClass = cssClasses.passwordFair;
} else if (strength < 80) {
indicatorText = passwordSettings.good;
indicatorClass = cssClasses.passwordGood;
} else if (strength <= 100) {
indicatorText = passwordSettings.strong;
indicatorClass = cssClasses.passwordStrong;
}
// Assemble the final message while keeping the original message array.
const messageTips = msg;
msg = `${passwordSettings.hasWeaknesses}<ul><li>${msg.join(
'</li><li>',
)}</li></ul>`;
return Drupal.deprecatedProperty({
target: {
strength,
message: msg,
indicatorText,
indicatorClass,
messageTips,
},
deprecatedProperty: 'message',
message:
'The message property is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. The markup should be constructed using messageTips property and Drupal.theme.passwordSuggestions. See https://www.drupal.org/node/3130352',
});
};
})(jQuery, Drupal);
|