forked from splitbrain/dokuwiki-plugin-passpolicy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
121 lines (105 loc) · 3.52 KB
/
script.js
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
jQuery(function () {
var $passfield = jQuery('form input[type=password][name=pass], ' +
'form input[type=password][name=newpass], ' +
'#add_userpass, #modify_userpass');
if (!$passfield.length) return;
/**
* Scores a password's strength on an open scale
*
* @author Toms Baugis
* @link http://stackoverflow.com/a/11268104
* @param pass string
* @return int
*/
function scorePassword(pass) {
var score = 0;
if (!pass)
return score;
// award every unique letter until 5 repetitions
var letters = {};
for (var i = 0; i < pass.length; i++) {
letters[pass[i]] = (letters[pass[i]] || 0) + 1;
score += 5.0 / letters[pass[i]];
}
// bonus points for mixing it up
var variations = {
digits: /\d/.test(pass),
lower: /[a-z]/.test(pass),
upper: /[A-Z]/.test(pass),
nonWords: /\W/.test(pass)
};
var variationCount = 0;
for (var check in variations) {
variationCount += (variations[check]) ? 1 : 0;
}
score += (variationCount - 1) * 10;
return parseInt(score);
}
/**
* check policy
*
* @param $field object jQuery object of the password field
* @param indicator DomObject where the output should go
*/
function checkpolicy($field, indicator) {
var pass = $field.val();
var user = jQuery('form input[type=text][name=login]').val();
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
{
call: 'plugin_passpolicy',
pass: pass,
user: user
},
function (response) {
if (response === '1') {
scoreit($field, indicator, true);
} else {
scoreit($field, indicator, false);
}
}
);
}
/**
* Apply scoring
*
* @param {object} $field jQuery object of the password field
* @param {Node} indicator where the output should go
* @param {bool} policy true if the policy is met
*/
function scoreit($field, indicator, policy) {
var score = scorePassword($field.val());
if (score > 80) {
indicator.innerHTML = LANG.plugins.passpolicy.strength3;
indicator.className = 'passpolicy_strength3';
} else if (score >= 60) {
indicator.innerHTML = LANG.plugins.passpolicy.strength2;
indicator.className = 'passpolicy_strength2';
} else if (score >= 30) {
indicator.innerHTML = LANG.plugins.passpolicy.strength1;
indicator.className = 'passpolicy_strength1';
} else {
indicator.innerHTML = LANG.plugins.passpolicy.strength0;
indicator.className = 'passpolicy_strength0';
}
if (!policy) {
indicator.innerHTML += LANG.plugins.passpolicy.nopolicy;
indicator.className = 'passpolicy_strength0';
}
}
/**
* Attach strength tester at the found password fields
*/
$passfield.each(function () {
var $field = jQuery(this);
var indicator = document.createElement('p');
indicator.className = 'passpolicy__indicator';
$field.after(indicator);
$field.keyup(function () {
checkpolicy($field, indicator)
});
$field.blur(function () {
checkpolicy($field, indicator)
});
});
});