author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 18 | be944660c56a |
permissions | -rw-r--r-- |
9 | 1 |
/** |
2 |
* @output wp-admin/js/password-strength-meter.js |
|
3 |
*/ |
|
4 |
||
5 | 5 |
/* global zxcvbn */ |
0 | 6 |
window.wp = window.wp || {}; |
7 |
||
8 |
(function($){ |
|
16 | 9 |
var __ = wp.i18n.__, |
10 |
sprintf = wp.i18n.sprintf; |
|
9 | 11 |
|
12 |
/** |
|
13 |
* Contains functions to determine the password strength. |
|
14 |
* |
|
15 |
* @since 3.7.0 |
|
16 |
* |
|
17 |
* @namespace |
|
18 |
*/ |
|
0 | 19 |
wp.passwordStrength = { |
20 |
/** |
|
9 | 21 |
* Determines the strength of a given password. |
22 |
* |
|
23 |
* Compares first password to the password confirmation. |
|
24 |
* |
|
25 |
* @since 3.7.0 |
|
0 | 26 |
* |
16 | 27 |
* @param {string} password1 The subject password. |
28 |
* @param {Array} disallowedList An array of words that will lower the entropy of |
|
29 |
* the password. |
|
30 |
* @param {string} password2 The password confirmation. |
|
9 | 31 |
* |
16 | 32 |
* @return {number} The password strength score. |
0 | 33 |
*/ |
16 | 34 |
meter : function( password1, disallowedList, password2 ) { |
18 | 35 |
if ( ! Array.isArray( disallowedList ) ) |
16 | 36 |
disallowedList = [ disallowedList.toString() ]; |
0 | 37 |
|
38 |
if (password1 != password2 && password2 && password2.length > 0) |
|
39 |
return 5; |
|
40 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
41 |
if ( 'undefined' === typeof window.zxcvbn ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
42 |
// Password strength unknown. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
43 |
return -1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
|
16 | 46 |
var result = zxcvbn( password1, disallowedList ); |
0 | 47 |
return result.score; |
48 |
}, |
|
49 |
||
50 |
/** |
|
9 | 51 |
* Builds an array of words that should be penalized. |
0 | 52 |
* |
9 | 53 |
* Certain words need to be penalized because it would lower the entropy of a |
16 | 54 |
* password if they were used. The disallowedList is based on user input fields such |
9 | 55 |
* as username, first name, email etc. |
56 |
* |
|
57 |
* @since 3.7.0 |
|
16 | 58 |
* @deprecated 5.5.0 Use {@see 'userInputDisallowedList()'} instead. |
9 | 59 |
* |
16 | 60 |
* @return {string[]} The array of words to be disallowed. |
0 | 61 |
*/ |
62 |
userInputBlacklist : function() { |
|
16 | 63 |
window.console.log( |
64 |
sprintf( |
|
65 |
/* translators: 1: Deprecated function name, 2: Version number, 3: Alternative function name. */ |
|
66 |
__( '%1$s is deprecated since version %2$s! Use %3$s instead. Please consider writing more inclusive code.' ), |
|
67 |
'wp.passwordStrength.userInputBlacklist()', |
|
68 |
'5.5.0', |
|
69 |
'wp.passwordStrength.userInputDisallowedList()' |
|
70 |
) |
|
71 |
); |
|
72 |
||
73 |
return wp.passwordStrength.userInputDisallowedList(); |
|
74 |
}, |
|
75 |
||
76 |
/** |
|
77 |
* Builds an array of words that should be penalized. |
|
78 |
* |
|
79 |
* Certain words need to be penalized because it would lower the entropy of a |
|
80 |
* password if they were used. The disallowed list is based on user input fields such |
|
81 |
* as username, first name, email etc. |
|
82 |
* |
|
83 |
* @since 5.5.0 |
|
84 |
* |
|
85 |
* @return {string[]} The array of words to be disallowed. |
|
86 |
*/ |
|
87 |
userInputDisallowedList : function() { |
|
0 | 88 |
var i, userInputFieldsLength, rawValuesLength, currentField, |
89 |
rawValues = [], |
|
16 | 90 |
disallowedList = [], |
0 | 91 |
userInputFields = [ 'user_login', 'first_name', 'last_name', 'nickname', 'display_name', 'email', 'url', 'description', 'weblog_title', 'admin_email' ]; |
92 |
||
16 | 93 |
// Collect all the strings we want to disallow. |
0 | 94 |
rawValues.push( document.title ); |
95 |
rawValues.push( document.URL ); |
|
96 |
||
97 |
userInputFieldsLength = userInputFields.length; |
|
98 |
for ( i = 0; i < userInputFieldsLength; i++ ) { |
|
99 |
currentField = $( '#' + userInputFields[ i ] ); |
|
100 |
||
5 | 101 |
if ( 0 === currentField.length ) { |
0 | 102 |
continue; |
103 |
} |
|
104 |
||
105 |
rawValues.push( currentField[0].defaultValue ); |
|
106 |
rawValues.push( currentField.val() ); |
|
107 |
} |
|
108 |
||
9 | 109 |
/* |
110 |
* Strip out non-alphanumeric characters and convert each word to an |
|
111 |
* individual entry. |
|
112 |
*/ |
|
0 | 113 |
rawValuesLength = rawValues.length; |
114 |
for ( i = 0; i < rawValuesLength; i++ ) { |
|
115 |
if ( rawValues[ i ] ) { |
|
16 | 116 |
disallowedList = disallowedList.concat( rawValues[ i ].replace( /\W/g, ' ' ).split( ' ' ) ); |
0 | 117 |
} |
118 |
} |
|
119 |
||
9 | 120 |
/* |
121 |
* Remove empty values, short words and duplicates. Short words are likely to |
|
122 |
* cause many false positives. |
|
123 |
*/ |
|
16 | 124 |
disallowedList = $.grep( disallowedList, function( value, key ) { |
5 | 125 |
if ( '' === value || 4 > value.length ) { |
0 | 126 |
return false; |
127 |
} |
|
128 |
||
16 | 129 |
return $.inArray( value, disallowedList ) === key; |
0 | 130 |
}); |
131 |
||
16 | 132 |
return disallowedList; |
0 | 133 |
} |
5 | 134 |
}; |
0 | 135 |
|
9 | 136 |
// Backward compatibility. |
137 |
||
138 |
/** |
|
139 |
* Password strength meter function. |
|
140 |
* |
|
141 |
* @since 2.5.0 |
|
142 |
* @deprecated 3.7.0 Use wp.passwordStrength.meter instead. |
|
143 |
* |
|
144 |
* @global |
|
145 |
* |
|
146 |
* @type {wp.passwordStrength.meter} |
|
147 |
*/ |
|
148 |
window.passwordStrength = wp.passwordStrength.meter; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
})(jQuery); |