|
| 1 | +// Taken from django-password-strength, with changes to use the bower-managed zxcvbn.js The |
| 2 | +// bower-managed zxcvbn.js is kept up-to-date to a larger extent than the copy packaged with |
| 3 | +// the django-password-strength component. |
| 4 | +(function($, window, document, undefined){ |
| 5 | + window.djangoPasswordStrength = { |
| 6 | + config: { |
| 7 | + passwordClass: 'password_strength', |
| 8 | + confirmationClass: 'password_confirmation' |
| 9 | + }, |
| 10 | + |
| 11 | + init: function (config) { |
| 12 | + var self = this; |
| 13 | + // Setup configuration |
| 14 | + if ($.isPlainObject(config)) { |
| 15 | + $.extend(self.config, config); |
| 16 | + } |
| 17 | + |
| 18 | + self.initListeners(); |
| 19 | + }, |
| 20 | + |
| 21 | + initListeners: function() { |
| 22 | + var self = this; |
| 23 | + var body = $('body'); |
| 24 | + |
| 25 | + $('.' + self.config.passwordClass).on('keyup', function() { |
| 26 | + var password_strength_bar = $(this).parent().find('.password_strength_bar'); |
| 27 | + var password_strength_info = $(this).parent().find('.password_strength_info'); |
| 28 | + var password_strength_offline_info = $(this).parent().parent().parent().find('.password_strength_offline_info'); |
| 29 | + |
| 30 | + if( $(this).val() ) { |
| 31 | + var result = zxcvbn( $(this).val() ); |
| 32 | + |
| 33 | + if( result.score < 3 ) { |
| 34 | + password_strength_bar.removeClass('progress-bar-success').addClass('progress-bar-warning'); |
| 35 | + password_strength_info.find('.label').removeClass('hidden'); |
| 36 | + } else { |
| 37 | + password_strength_bar.removeClass('progress-bar-warning').addClass('progress-bar-success'); |
| 38 | + password_strength_info.find('.label').addClass('hidden'); |
| 39 | + } |
| 40 | + |
| 41 | + password_strength_bar.width( ((result.score+1)/5)*100 + '%' ).attr('aria-valuenow', result.score + 1); |
| 42 | + // henrik@levkowetz.com -- this is the only changed line: |
| 43 | + password_strength_info.find('.password_strength_time').html(result.crack_times_display.online_no_throttling_10_per_second); |
| 44 | + password_strength_info.removeClass('hidden'); |
| 45 | + |
| 46 | + password_strength_offline_info.find('.password_strength_time').html(result.crack_times_display.offline_slow_hashing_1e4_per_second); |
| 47 | + password_strength_offline_info.removeClass('hidden'); |
| 48 | + } else { |
| 49 | + password_strength_bar.removeClass('progress-bar-success').addClass('progress-bar-warning'); |
| 50 | + password_strength_bar.width( '0%' ).attr('aria-valuenow', 0); |
| 51 | + password_strength_info.addClass('hidden'); |
| 52 | + } |
| 53 | + self.match_passwords($(this)); |
| 54 | + }); |
| 55 | + |
| 56 | + var timer = null; |
| 57 | + $('.' + self.config.confirmationClass).on('keyup', function() { |
| 58 | + var password_field; |
| 59 | + var confirm_with = $(this).data('confirm-with'); |
| 60 | + |
| 61 | + if( confirm_with ) { |
| 62 | + password_field = $('#' + confirm_with); |
| 63 | + } else { |
| 64 | + password_field = $('.' + self.config.passwordClass); |
| 65 | + } |
| 66 | + |
| 67 | + if (timer !== null) clearTimeout(timer); |
| 68 | + |
| 69 | + timer = setTimeout(function(){ |
| 70 | + self.match_passwords(password_field); |
| 71 | + }, 400); |
| 72 | + }); |
| 73 | + }, |
| 74 | + |
| 75 | + display_time: function(seconds) { |
| 76 | + var minute = 60; |
| 77 | + var hour = minute * 60; |
| 78 | + var day = hour * 24; |
| 79 | + var month = day * 31; |
| 80 | + var year = month * 12; |
| 81 | + var century = year * 100; |
| 82 | + |
| 83 | + // Provide fake gettext for when it is not available |
| 84 | + if( typeof gettext !== 'function' ) { gettext = function(text) { return text; }; }; |
| 85 | + |
| 86 | + if( seconds < minute ) return gettext('only an instant'); |
| 87 | + if( seconds < hour) return (1 + Math.ceil(seconds / minute)) + ' ' + gettext('minutes'); |
| 88 | + if( seconds < day) return (1 + Math.ceil(seconds / hour)) + ' ' + gettext('hours'); |
| 89 | + if( seconds < month) return (1 + Math.ceil(seconds / day)) + ' ' + gettext('days'); |
| 90 | + if( seconds < year) return (1 + Math.ceil(seconds / month)) + ' ' + gettext('months'); |
| 91 | + if( seconds < century) return (1 + Math.ceil(seconds / year)) + ' ' + gettext('years'); |
| 92 | + |
| 93 | + return gettext('centuries'); |
| 94 | + }, |
| 95 | + |
| 96 | + match_passwords: function(password_field, confirmation_fields) { |
| 97 | + var self = this; |
| 98 | + // Optional parameter: if no specific confirmation field is given, check all |
| 99 | + if( confirmation_fields === undefined ) { confirmation_fields = $('.' + self.config.confirmationClass) } |
| 100 | + if( confirmation_fields === undefined ) { return; } |
| 101 | + |
| 102 | + var password = password_field.val(); |
| 103 | + |
| 104 | + confirmation_fields.each(function(index, confirm_field) { |
| 105 | + var confirm_value = $(confirm_field).val(); |
| 106 | + var confirm_with = $(confirm_field).data('confirm-with'); |
| 107 | + |
| 108 | + if( confirm_with && confirm_with == password_field.attr('id')) { |
| 109 | + if( confirm_value && password ) { |
| 110 | + if (confirm_value === password) { |
| 111 | + $(confirm_field).parent().find('.password_strength_info').addClass('hidden'); |
| 112 | + } else { |
| 113 | + $(confirm_field).parent().find('.password_strength_info').removeClass('hidden'); |
| 114 | + } |
| 115 | + } else { |
| 116 | + $(confirm_field).parent().find('.password_strength_info').addClass('hidden'); |
| 117 | + } |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + // If a password field other than our own has been used, add the listener here |
| 122 | + if( !password_field.hasClass(self.config.passwordClass) && !password_field.data('password-listener') ) { |
| 123 | + password_field.on('keyup', function() { |
| 124 | + self.match_passwords($(this)); |
| 125 | + }); |
| 126 | + password_field.data('password-listener', true); |
| 127 | + } |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + // Call the init for backwards compatibility |
| 132 | + djangoPasswordStrength.init(); |
| 133 | + |
| 134 | +})(jQuery, window, document); |
0 commit comments