forked from ietf-tools/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_strength.js
More file actions
189 lines (162 loc) · 7.76 KB
/
password_strength.js
File metadata and controls
189 lines (162 loc) · 7.76 KB
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
// Taken from django-password-strength, with changes to use the bower-managed zxcvbn.js The
// bower-managed zxcvbn.js is kept up-to-date to a larger extent than the copy packaged with
// the django-password-strength component.
(function ($, window, document, undefined) {
window.djangoPasswordStrength = {
config: {
passwordClass: 'password_strength',
confirmationClass: 'password_confirmation'
},
init: function (config) {
var self = this;
// Setup configuration
if ($.isPlainObject(config)) {
$.extend(self.config, config);
}
// Fix the initial widget for bootstrap 5
var widget = $("." + self.config.passwordClass)
.closest("form");
widget
.find(".hidden")
.addClass("d-none")
.removeClass("hidden");
widget
.find(".label")
.addClass("badge rounded-pill")
.removeClass("label");
widget
.find(".label-danger")
.addClass("bg-danger")
.removeClass("label-danger");
widget
.find(".text-muted")
.addClass("form-text")
.removeClass("text-muted");
self.initListeners();
},
initListeners: function () {
var self = this;
$('.' + self.config.passwordClass)
.on('keyup', function () {
var password_strength_bar = $(this)
.parent()
.find('.password_strength_bar');
var password_strength_info = $(this)
.parent()
.find('.password_strength_info');
var password_strength_offline_info = $(this)
.parent()
.parent()
.parent()
.find('.password_strength_offline_info');
if ($(this)
.val()) {
var result = zxcvbn($(this)
.val());
if (result.score < 3) {
password_strength_bar.removeClass('bg-success')
.addClass('bg-warning');
password_strength_info.find('.badge')
.removeClass('d-none');
} else {
password_strength_bar.removeClass('bg-warning')
.addClass('bg-success');
password_strength_info.find('.badge')
.addClass('d-none');
}
password_strength_bar.width(((result.score + 1) / 5) * 100 + '%')
.attr('aria-valuenow', result.score + 1);
// henrik@levkowetz.com -- this is the only changed line:
password_strength_info.find('.password_strength_time')
.html(result.crack_times_display.online_no_throttling_10_per_second);
password_strength_info.removeClass('d-none');
password_strength_offline_info.find('.password_strength_time')
.html(result.crack_times_display.offline_slow_hashing_1e4_per_second);
password_strength_offline_info.removeClass('d-none');
} else {
password_strength_bar.removeClass('bg-success')
.addClass('bg-warning');
password_strength_bar.width('0%')
.attr('aria-valuenow', 0);
password_strength_info.addClass('d-none');
}
self.match_passwords($(this));
});
var timer = null;
$('.' + self.config.confirmationClass)
.on('keyup', function () {
var password_field;
var confirm_with = $(this)
.data('confirm-with');
if (confirm_with) {
password_field = $('#' + confirm_with);
} else {
password_field = $('.' + self.config.passwordClass);
}
if (timer !== null) clearTimeout(timer);
timer = setTimeout(function () {
self.match_passwords(password_field);
}, 400);
});
},
display_time: function (seconds) {
var minute = 60;
var hour = minute * 60;
var day = hour * 24;
var month = day * 31;
var year = month * 12;
var century = year * 100;
// Provide fake gettext for when it is not available
if (typeof gettext !== 'function') { gettext = function (text) { return text; }; }
if (seconds < minute) return gettext('only an instant');
if (seconds < hour) return (1 + Math.ceil(seconds / minute)) + ' ' + gettext('minutes');
if (seconds < day) return (1 + Math.ceil(seconds / hour)) + ' ' + gettext('hours');
if (seconds < month) return (1 + Math.ceil(seconds / day)) + ' ' + gettext('days');
if (seconds < year) return (1 + Math.ceil(seconds / month)) + ' ' + gettext('months');
if (seconds < century) return (1 + Math.ceil(seconds / year)) + ' ' + gettext('years');
return gettext('centuries');
},
match_passwords: function (password_field, confirmation_fields) {
var self = this;
// Optional parameter: if no specific confirmation field is given, check all
if (confirmation_fields === undefined) { confirmation_fields = $('.' + self.config.confirmationClass); }
if (confirmation_fields === undefined) { return; }
var password = password_field.val();
confirmation_fields.each(function (index, confirm_field) {
var confirm_value = $(confirm_field)
.val();
var confirm_with = $(confirm_field)
.data('confirm-with');
if (confirm_with && confirm_with == password_field.attr('id')) {
if (confirm_value && password) {
if (confirm_value === password) {
$(confirm_field)
.parent()
.find('.password_strength_info')
.addClass('d-none');
} else {
$(confirm_field)
.parent()
.find('.password_strength_info')
.removeClass('d-none');
}
} else {
$(confirm_field)
.parent()
.find('.password_strength_info')
.addClass('d-none');
}
}
});
// If a password field other than our own has been used, add the listener here
if (!password_field.hasClass(self.config.passwordClass) && !password_field.data('password-listener')) {
password_field.on('keyup', function () {
self.match_passwords($(this));
});
password_field.data('password-listener', true);
}
}
};
// Call the init for backwards compatibility
djangoPasswordStrength.init();
})(jQuery, window, document);