Skip to content

Commit 9e3bcd9

Browse files
committed
Added js part for pwd_diff_namesurname option
1 parent f011ab3 commit 9e3bcd9

File tree

1 file changed

+64
-8
lines changed

1 file changed

+64
-8
lines changed

src/ppolicy/js/ppolicy.js

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
1+
let debounceTimeout;
2+
let storedLogin = "";
3+
let storedName = "";
4+
let storedSurname = "";
5+
const getNameApiUrl = "/rest/v1/getnamesurnamefromlogin.php";
6+
7+
const updateNameSurname = async () => {
8+
const response = await fetch(getNameApiUrl, {
9+
method: "POST",
10+
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
11+
body: new URLSearchParams({'login': storedLogin}).toString(),
12+
});
13+
const rawResponse = await response.text();
14+
const parsedResponse = JSON.parse(rawResponse);
15+
16+
if (response.ok) {
17+
storedName = parsedResponse['name'].toUpperCase();
18+
storedSurname = parsedResponse['surname'].toUpperCase();
19+
}
20+
else if (response.status === 404) {
21+
storedName = "";
22+
storedSurname = "";
23+
}
24+
else if (response.status === 400) {
25+
console.log("Error while trying to query " + getNameApiUrl + " api endpoint : invalid request");
26+
console.log(parsedResponse['message']);
27+
}
28+
else {
29+
console.log("Error while trying to query " + getNameApiUrl + " api endpoint : server error");
30+
console.log(parsedResponse['message']);
31+
}
32+
}
33+
34+
const doesPwdContainNameSurname = (password) => {
35+
const upperPwd = password.toUpperCase();
36+
return (
37+
(storedName !== "" && upperPwd.includes(storedName)) ||
38+
(storedSurname !== "" && upperPwd.includes(storedSurname))
39+
);
40+
}
41+
142
(function() {
243
var barWidth, bootstrapClasses, displayEntropyBar, displayEntropyBarMsg, ppolicyResults;
344

@@ -79,9 +120,9 @@
79120
// returns 2: 0
80121

81122
if (first === null ||
82-
second === null ||
83-
typeof first === 'undefined' ||
84-
typeof second === 'undefined') {
123+
second === null ||
124+
typeof first === 'undefined' ||
125+
typeof second === 'undefined') {
85126
return 0
86127
}
87128

@@ -120,9 +161,9 @@
120161

121162
if ((pos1 + max < firstLength) && (pos2 + max < secondLength)) {
122163
sum += similar_text(
123-
first.substr(pos1 + max, firstLength - pos1 - max),
124-
second.substr(pos2 + max,
125-
secondLength - pos2 - max))
164+
first.substr(pos1 + max, firstLength - pos1 - max),
165+
second.substr(pos2 + max,
166+
secondLength - pos2 - max))
126167
}
127168
}
128169

@@ -215,6 +256,21 @@
215256
}
216257
}
217258

259+
if (local_policy.pwd_diff_namesurname && local_policy.use_restapi) {
260+
setResult('ppolicy-pwd_diff_namesurname-feedback', "waiting");
261+
if(login !== storedLogin) {
262+
storedLogin = login;
263+
clearTimeout(debounceTimeout);
264+
debounceTimeout = setTimeout(async () => {
265+
await updateNameSurname();
266+
report( !doesPwdContainNameSurname(password), "ppolicy-pwd_diff_namesurname-feedback" );
267+
}, 1000);
268+
}
269+
else {
270+
report( !doesPwdContainNameSurname(password), "ppolicy-pwd_diff_namesurname-feedback" );
271+
}
272+
}
273+
218274
if (local_policy.pwd_diff_last_min_chars > 0) {
219275
if( $( "#oldpassword" ).length )
220276
{
@@ -259,8 +315,8 @@
259315
}
260316

261317
if ( local_policy.pwd_no_special_at_ends &&
262-
local_policy.pwd_no_special_at_ends == true &&
263-
local_policy.pwd_special_chars ) {
318+
local_policy.pwd_no_special_at_ends == true &&
319+
local_policy.pwd_special_chars ) {
264320
var re_start = new RegExp("^["+local_policy.pwd_special_chars+"]","");
265321
var re_end = new RegExp("["+local_policy.pwd_special_chars+"]$","");
266322
report( ( !password.match(re_start) && !password.match(re_end) ) , 'ppolicy-pwd_no_special_at_ends-feedback');

0 commit comments

Comments
 (0)