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