2017-06-22 22:08:43 +02:00
|
|
|
var common = (function () {
|
2012-10-18 20:29:16 +02:00
|
|
|
|
2017-06-22 22:08:43 +02:00
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
exports.status_classes = 'alert-error alert-success alert-info';
|
|
|
|
|
|
|
|
exports.autofocus = function (selector) {
|
2012-08-29 17:45:15 +02:00
|
|
|
$(function () {
|
2017-07-08 17:43:42 +02:00
|
|
|
$(selector).focus();
|
2012-08-29 17:45:15 +02:00
|
|
|
});
|
2017-06-22 22:08:43 +02:00
|
|
|
};
|
2013-04-03 22:30:36 +02:00
|
|
|
|
2013-04-08 20:21:20 +02:00
|
|
|
// Return a boolean indicating whether the password is acceptable.
|
|
|
|
// Also updates a Bootstrap progress bar control (a jQuery object)
|
|
|
|
// if provided.
|
2013-04-03 22:30:36 +02:00
|
|
|
//
|
|
|
|
// Assumes that zxcvbn.js has been loaded.
|
|
|
|
//
|
|
|
|
// This is in common.js because we want to use it from the signup page
|
|
|
|
// and also from the in-app password change interface.
|
2017-06-22 22:08:43 +02:00
|
|
|
exports.password_quality = function (password, bar, password_field) {
|
2013-04-04 00:55:36 +02:00
|
|
|
// We load zxcvbn.js asynchronously, so the variable might not be set.
|
2013-08-01 17:47:48 +02:00
|
|
|
if (typeof zxcvbn === 'undefined') {
|
2013-04-04 00:55:36 +02:00
|
|
|
return undefined;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-04-04 00:55:36 +02:00
|
|
|
|
2017-07-06 22:51:57 +02:00
|
|
|
var min_length = password_field.data('minLength');
|
|
|
|
var min_quality = password_field.data('minQuality');
|
2017-01-09 18:04:23 +01:00
|
|
|
|
2013-04-10 23:19:04 +02:00
|
|
|
// Consider the password acceptable if it's at least 6 characters.
|
2017-01-09 18:04:23 +01:00
|
|
|
var acceptable = password.length >= min_length;
|
2013-04-08 20:21:20 +02:00
|
|
|
|
2017-01-09 18:04:23 +01:00
|
|
|
// Compute a quality score in [0,1].
|
|
|
|
var result = zxcvbn(password);
|
|
|
|
var quality = Math.min(1,Math.log(1 + result.crack_times_seconds.
|
|
|
|
offline_slow_hashing_1e4_per_second) / 22);
|
2013-04-10 23:19:04 +02:00
|
|
|
|
2017-01-09 18:04:23 +01:00
|
|
|
// Even if zxcvbn loves your short password, the bar should be filled
|
|
|
|
// at most 1/3 of the way, because we won't accept it.
|
|
|
|
if (!acceptable) {
|
|
|
|
quality = Math.min(quality, 0.33);
|
2013-04-03 22:30:36 +02:00
|
|
|
|
2017-01-09 18:04:23 +01:00
|
|
|
// In case the quality is below the minimum, we should not accept the password
|
|
|
|
} else if (quality < min_quality) {
|
|
|
|
acceptable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bar !== undefined) {
|
2013-04-08 20:21:20 +02:00
|
|
|
// Display the password quality score on a progress bar
|
|
|
|
// which bottoms out at 10% so there's always something
|
|
|
|
// for the user to see.
|
2013-04-08 20:31:00 +02:00
|
|
|
bar.width(((90 * quality) + 10) + '%')
|
|
|
|
.removeClass('bar-success bar-danger')
|
|
|
|
.addClass(acceptable ? 'bar-success' : 'bar-danger');
|
2013-04-08 20:21:20 +02:00
|
|
|
}
|
2013-04-03 22:30:36 +02:00
|
|
|
|
2013-04-08 20:31:00 +02:00
|
|
|
return acceptable;
|
2017-06-22 22:08:43 +02:00
|
|
|
};
|
|
|
|
|
2017-06-29 16:26:48 +02:00
|
|
|
exports.password_warning = function (password, password_field) {
|
|
|
|
if (typeof zxcvbn === 'undefined') {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2017-07-06 22:51:57 +02:00
|
|
|
var min_length = password_field.data('minLength');
|
2017-06-29 16:26:48 +02:00
|
|
|
|
|
|
|
if (password.length < min_length) {
|
|
|
|
return i18n.t('Password should be at least __length__ characters long', {length: min_length});
|
|
|
|
}
|
|
|
|
return zxcvbn(password).feedback.warning || i18n.t("Password is too weak");
|
|
|
|
};
|
|
|
|
|
2017-06-22 22:08:43 +02:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
2016-12-04 08:59:56 +01:00
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
2017-06-22 22:08:43 +02:00
|
|
|
module.exports = common;
|
2016-12-04 08:59:56 +01:00
|
|
|
}
|