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