Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 1 | // Example starter JavaScript for disabling form submissions if there are invalid fields |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 2 | (function() { |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 3 | 'use strict'; |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 4 | window.addEventListener('load', function() { |
Marc Kupietz | 87a1431 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 5 | update_total_due(); |
Marc Kupietz | 11de6cb | 2023-03-10 06:46:32 +0100 | [diff] [blame] | 6 | update_paper_id_field(); |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 7 | // Fetch all the forms we want to apply custom Bootstrap validation styles to |
| 8 | var forms = document.getElementsByClassName('needs-validation'); |
| 9 | // Loop over them and prevent submission |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 10 | var validation = Array.prototype.filter.call(forms, function(form) { |
| 11 | form.addEventListener('submit', function(event) { |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 12 | if (form.checkValidity() === false) { |
| 13 | event.preventDefault(); |
| 14 | event.stopPropagation(); |
| 15 | } |
| 16 | form.classList.add('was-validated'); |
| 17 | }, false); |
| 18 | }); |
| 19 | }, false); |
| 20 | })(); |
| 21 | |
Marc Kupietz | 87a1431 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 22 | function update_total_due() { |
Marc Kupietz | 79c2b92 | 2023-03-08 09:29:11 +0100 | [diff] [blame] | 23 | var costs = 0; |
| 24 | |
| 25 | |
| 26 | if ($('#participate').is(":checked")) { |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 27 | costs += parseInt($("#conference_fee").text(), 10); |
Marc Kupietz | 79c2b92 | 2023-03-08 09:29:11 +0100 | [diff] [blame] | 28 | if ($('#student').is(":checked")) { |
| 29 | costs -= parseInt($("#student_discount").text(), 10); |
| 30 | } |
| 31 | if ($('#conference_dinner').is(":checked")) { |
| 32 | costs += parseInt($("#conference_dinner_price").text(), 10); |
| 33 | } |
| 34 | } |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 35 | $("#total_due").val(costs.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })); |
Marc Kupietz | 79c2b92 | 2023-03-08 09:29:11 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | function update_paper_id_field() { |
| 39 | var paper_id = $("#paper_id"); |
| 40 | if ($('#author').is(":checked")) { |
| 41 | paper_id.removeAttr('disabled'); |
| 42 | paper_id.attr('placeholder', '0000'); |
| 43 | paper_id.attr('pattern', '\\d{4}'); |
| 44 | paper_id.attr('required', 'required'); |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 45 | //if (paper_id.attr('oldval').match(/^\\d{4}$/)) { |
| 46 | // paper_id.val(paper_id.attr('oldval')); |
| 47 | //} |
Marc Kupietz | 79c2b92 | 2023-03-08 09:29:11 +0100 | [diff] [blame] | 48 | } else { |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 49 | paper_id.attr('oldval', paper_id.val()); |
Marc Kupietz | 79c2b92 | 2023-03-08 09:29:11 +0100 | [diff] [blame] | 50 | paper_id.val(''); |
| 51 | paper_id.attr('disabled', 'disabled'); |
| 52 | paper_id.removeAttr('placeholder'); |
| 53 | paper_id.removeAttr('pattern'); |
| 54 | paper_id.removeAttr('required'); |
| 55 | } |
| 56 | } |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 57 | |
| 58 | function check_password_match() { |
| 59 | pass = $("#pw1").val(); |
| 60 | pass2 = $("#pw2").val(); |
| 61 | |
| 62 | if (pass2.length > 0) { |
| 63 | $("#pwconfirm")[0].classList.add('was-validated') |
| 64 | if (pass != pass2) { |
| 65 | $("#pw2")[0].setCustomValidity("Passwords do not match"); |
| 66 | } else { |
| 67 | $("#pw2")[0].setCustomValidity(""); // is valid |
| 68 | } |
| 69 | } else { |
| 70 | $("#pwconfirm")[0].classList.remove('was-validated') |
| 71 | } |
| 72 | passUpdated(); |
| 73 | } |
| 74 | |
| 75 | TOO_SHORT = 'Password too short, still %% characters needed'; |
| 76 | TOO_LONG = 'Password too long, please remove %% characters'; |
| 77 | INVALID_CHARS = 'Password contains invalid characters'; |
| 78 | QUAL_NONE = 'Password is very weak' |
| 79 | QUAL_LOW = 'Password is weak'; |
| 80 | QUAL_MEDIUM = 'Password is average' |
| 81 | QUAL_GOOD = 'Password is good'; |
| 82 | QUAL_STRONG = 'Password is strong'; |
| 83 | REP_OK = 'Repetition ok'; |
| 84 | REP_NE = 'Passwords not identical'; |
| 85 | PWNED = 'Password found in public password list'; |
| 86 | |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 87 | String.prototype.strReverse = function() { |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 88 | var newstring = ''; |
| 89 | for (var s = 0; s < this.length; s++) |
| 90 | newstring = this.charAt(s) + newstring; |
| 91 | return newstring; |
| 92 | }; |
| 93 | |
| 94 | function isPasswordPwned(pass, callback) { |
| 95 | const sha1Hash = CryptoJS.enc.Hex.stringify(CryptoJS.SHA1(pass)).toUpperCase(); |
| 96 | const passwordChunk1 = sha1Hash.substring(0, 5); |
| 97 | const passwordChunk2 = sha1Hash.substring(5); |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 98 | $.get('https://api.pwnedpasswords.com/range/' + passwordChunk1).done(function(data, status) { |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 99 | var isPwned = false; |
| 100 | if (status == "success") { |
| 101 | if (data && data.length) { |
| 102 | const chunks = data.split('\r\n'); |
| 103 | const matches = chunks.filter(s => s.includes(passwordChunk2)); |
| 104 | |
| 105 | if (matches.length) { |
| 106 | isPwned = true |
| 107 | } |
| 108 | } |
| 109 | callback(isPwned) |
| 110 | } |
| 111 | }); |
| 112 | } |
| 113 | |
| 114 | //var checkTimer; |
| 115 | |
| 116 | function passUpdated() { |
| 117 | var nScore = 0; |
| 118 | var message = ''; |
| 119 | |
| 120 | var pass = $('#pw1').val(); |
| 121 | var pass2 = $('#pw2').val(); |
| 122 | |
| 123 | //clearTimeout(checkTimer); |
| 124 | |
| 125 | try { |
| 126 | if (!pass) |
| 127 | throw ''; |
| 128 | |
| 129 | if (pass.match(/[^a-zA-Z0-9!@#$%()_+=:;",.?/-]/)) |
| 130 | throw INVALID_CHARS; |
| 131 | |
| 132 | var nLength = pass.length; |
| 133 | if (nLength < 8) |
| 134 | throw TOO_SHORT.replace('%%', 8 - nLength); |
| 135 | if (nLength > 20) |
| 136 | throw TOO_LONG.replace('%%', nLength - 20); |
| 137 | |
| 138 | nScore = 4 * nLength; |
| 139 | |
| 140 | // check for upper-/lowercase, numeric and special chars pattern matches |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 141 | var nAlphaUC = 0, |
| 142 | nAlphaLC = 0, |
| 143 | nNumber = 0, |
| 144 | nSpecial = 0; |
| 145 | var nMidChar = 0, |
| 146 | nRepChar = 0, |
| 147 | nRepInc = 0; |
| 148 | var nConsecAlphaUC = 0, |
| 149 | nConsecAlphaLC = 0, |
| 150 | nConsecNumber = 0; |
| 151 | var nTmpAlphaUC = '', |
| 152 | nTmpAlphaLC = '', |
| 153 | nTmpNumber = ''; |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 154 | for (var i = 0; i < nLength; i++) { |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 155 | if (pass[i].match(/[A-Z]/g)) { // uppercase characters |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 156 | if (nTmpAlphaUC !== '' && (nTmpAlphaUC + 1) == i) { |
| 157 | nConsecAlphaUC++; |
| 158 | } |
| 159 | nTmpAlphaUC = i; |
| 160 | nAlphaUC++; |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 161 | } else if (pass[i].match(/[a-z]/g)) { // lowercase characters |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 162 | if (nTmpAlphaLC !== '' && (nTmpAlphaLC + 1) == i) { |
| 163 | nConsecAlphaLC++; |
| 164 | } |
| 165 | nTmpAlphaLC = i; |
| 166 | nAlphaLC++; |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 167 | } else if (pass[i].match(/[0-9]/g)) { // numbers |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 168 | if (i > 0 && i < (nLength - 1)) { |
| 169 | nMidChar++; |
| 170 | } |
| 171 | if (nTmpNumber !== '' && (nTmpNumber + 1) == i) { |
| 172 | nConsecNumber++; |
| 173 | } |
| 174 | nTmpNumber = i; |
| 175 | nNumber++; |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 176 | } else { // special characters |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 177 | if (i > 0 && i < (nLength - 1)) { |
| 178 | nMidChar++; |
| 179 | } |
| 180 | nSpecial++; |
| 181 | } |
| 182 | |
| 183 | // check for repeated characters |
| 184 | var bCharExists = false; |
| 185 | for (var j = 0; j < nLength; j++) { |
| 186 | if (pass[i] == pass[j] && i != j) { |
| 187 | bCharExists = true; |
| 188 | nRepInc += Math.abs(nLength / (j - i)); |
| 189 | } |
| 190 | } |
| 191 | if (bCharExists) { |
| 192 | nRepChar++; |
| 193 | var nUnqChar = nLength - nRepChar; |
| 194 | nRepInc = (nUnqChar) ? Math.ceil(nRepInc / nUnqChar) : Math.ceil(nRepInc); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // check for sequential alpha string patterns (forward and reverse) |
| 199 | var sAlphas = "abcdefghijklmnopqrstuvwxyz"; |
| 200 | var nSeqAlpha = 0; |
| 201 | for (var i = 0; i < 23; i++) { |
| 202 | var sFwd = sAlphas.substring(i, i + 3); |
| 203 | var sRev = sFwd.strReverse(); |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 204 | if (pass.toLowerCase().indexOf(sFwd) != -1 || |
| 205 | pass.toLowerCase().indexOf(sRev) != -1) |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 206 | nSeqAlpha++; |
| 207 | } |
| 208 | |
| 209 | // check for sequential numeric string patterns (forward and reverse) |
| 210 | var sNumerics = "01234567890"; |
| 211 | var nSeqNumber = 0; |
| 212 | for (var i = 0; i < 8; i++) { |
| 213 | var sFwd = sNumerics.substring(i, i + 3); |
| 214 | var sRev = sFwd.strReverse(); |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 215 | if (pass.toLowerCase().indexOf(sFwd) != -1 || |
| 216 | pass.toLowerCase().indexOf(sRev) != -1) |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 217 | nSeqNumber++; |
| 218 | } |
| 219 | |
| 220 | // general point assignment |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 221 | if (nAlphaUC > 0 && nAlphaUC < nLength) // uppercase characters |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 222 | nScore += 2 * (nLength - nAlphaUC); |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 223 | if (nAlphaLC > 0 && nAlphaLC < nLength) // lowercase characters |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 224 | nScore += 2 * (nLength - nAlphaLC); |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 225 | if (nNumber > 0 && nNumber < nLength) // numbers |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 226 | nScore += 2 * nNumber; |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 227 | if (nSpecial > 0) // special characters |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 228 | nScore += 4 * nSpecial; |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 229 | if (nMidChar > 0) // mid numbers/special characters |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 230 | nScore += 2 * nMidChar; |
| 231 | |
| 232 | // point deductions for poor practices |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 233 | if ((nAlphaLC > 0 || nAlphaUC > 0) && |
| 234 | nSpecial === 0 && nNumber === 0) // characters only |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 235 | nScore -= nLength; |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 236 | if (nAlphaLC === 0 && nAlphaUC === 0 && |
| 237 | nSpecial === 0 && nNumber > 0) // numbers only |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 238 | nScore -= nLength; |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 239 | if (nRepChar > 0) // same character exists more than once |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 240 | nScore -= nRepInc; |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 241 | if (nConsecAlphaUC > 0) // consecutive uppercase letters exist |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 242 | nScore -= 2 * nConsecAlphaUC; |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 243 | if (nConsecAlphaLC > 0) // consecutive lowercase letters exist |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 244 | nScore -= 2 * nConsecAlphaLC; |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 245 | if (nConsecNumber > 0) // consecutive numbers exist |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 246 | nScore -= 2 * nConsecNumber; |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 247 | if (nSeqAlpha > 0) // sequential alpha strings exist (3 chars or more) |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 248 | nScore -= 3 * nSeqAlpha; |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 249 | if (nSeqNumber > 0) // sequential numeric strings exist (3 chars or more) |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 250 | nScore -= 3 * nSeqNumber; |
| 251 | |
| 252 | // determine if mandatory requirements have been met |
| 253 | var arrChars = [nAlphaUC, nAlphaLC, nNumber, nSpecial]; |
| 254 | var nReqChar = 0; |
| 255 | for (var i = 0; i < arrChars.length; i++) { |
| 256 | if (arrChars[i]) { |
| 257 | nReqChar++; |
| 258 | } |
| 259 | } |
| 260 | if (nReqChar >= arrChars.length) |
| 261 | nScore += 2 * nReqChar; |
| 262 | else if (nReqChar < arrChars.length - 1) |
| 263 | nScore -= 2 * nReqChar; |
| 264 | |
| 265 | // limit points to 3..100 |
| 266 | nScore = Math.max(3, Math.min(nScore, 100)); |
| 267 | |
| 268 | // set message according to points |
| 269 | if (nScore >= 80) |
| 270 | message = QUAL_STRONG; |
| 271 | else if (nScore >= 60) |
| 272 | message = QUAL_GOOD; |
| 273 | else if (nScore >= 40) |
| 274 | message = QUAL_MEDIUM; |
| 275 | else if (nScore >= 10) |
| 276 | message = QUAL_LOW; |
| 277 | else |
| 278 | message = QUAL_NONE; |
| 279 | } catch (error) { |
| 280 | nScore = 3; |
| 281 | message = error; |
| 282 | } |
| 283 | /* |
| 284 | if (pass.length > 0 && pass2.length > 0) { |
| 285 | message+=' / '; |
| 286 | message+=(pass == pass2) ? REP_OK : REP_NE; |
| 287 | } |
| 288 | */ |
| 289 | $('#pwqinfo').val(message); |
| 290 | |
| 291 | var progress = $('#pwqbar'); |
| 292 | progress.width(nScore + '%'); |
| 293 | progress.attr('aria-valuenow', nScore); |
| 294 | if (nScore >= 60) |
| 295 | progress.removeClass('bg-danger bg-warning').addClass('bg-success'); |
| 296 | else if (nScore >= 40) |
| 297 | progress.removeClass('bg-danger bg-success').addClass('bg-warning'); |
| 298 | else |
| 299 | progress.removeClass('bg-warning bg-success').addClass('bg-danger'); |
| 300 | |
| 301 | if (nScore >= 40) { |
| 302 | $("#password-div")[0].classList.add('was-validated'); |
| 303 | $('#btn_change').prop('disabled', pass !== pass2); |
| 304 | $("#pw1")[0].setCustomValidity(""); |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 305 | isPasswordPwned(pass, function(isPwned) { |
Marc Kupietz | 484ec8e | 2023-02-25 11:23:07 +0100 | [diff] [blame] | 306 | if (isPwned) { |
| 307 | $('#pwqinfo').val(PWNED); |
| 308 | progress.removeClass().addClass('low'); |
| 309 | progress.val(3); |
| 310 | $("#pw1")[0].setCustomValidity("Password is pwned"); |
| 311 | } |
| 312 | }); |
| 313 | } else { |
| 314 | $("#password-div")[0].classList.add('was-validated'); |
| 315 | $('#btn_change').prop('disabled', 1); |
| 316 | $("#pw1")[0].setCustomValidity("Password is to week"); |
| 317 | } |
| 318 | |
Marc Kupietz | 216ff53 | 2023-03-10 13:54:23 +0100 | [diff] [blame^] | 319 | } |