blob: 7ed582c616fe3eb910fde44fe0f10c34f2b0fb4e [file] [log] [blame]
Marc Kupietz484ec8e2023-02-25 11:23:07 +01001// Example starter JavaScript for disabling form submissions if there are invalid fields
Marc Kupietz216ff532023-03-10 13:54:23 +01002(function() {
Marc Kupietz484ec8e2023-02-25 11:23:07 +01003 'use strict';
Marc Kupietz216ff532023-03-10 13:54:23 +01004 window.addEventListener('load', function() {
Marc Kupietzc4f6f752023-03-21 17:56:34 +01005 const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
6 const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
Marc Kupietz87a14312023-03-09 20:39:31 +01007 update_total_due();
Marc Kupietz11de6cb2023-03-10 06:46:32 +01008 update_paper_id_field();
Marc Kupietz484ec8e2023-02-25 11:23:07 +01009 // 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 Kupietz216ff532023-03-10 13:54:23 +010012 var validation = Array.prototype.filter.call(forms, function(form) {
13 form.addEventListener('submit', function(event) {
Marc Kupietz484ec8e2023-02-25 11:23:07 +010014 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 Kupietz87a14312023-03-09 20:39:31 +010024function update_total_due() {
Marc Kupietz79c2b922023-03-08 09:29:11 +010025 var costs = 0;
26
27
28 if ($('#participate').is(":checked")) {
Marc Kupietz216ff532023-03-10 13:54:23 +010029 costs += parseInt($("#conference_fee").text(), 10);
Marc Kupietz79c2b922023-03-08 09:29:11 +010030 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 Kupietz8701abd2023-03-16 17:33:43 +010036 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 Kupietz79c2b922023-03-08 09:29:11 +010042 }
Marc Kupietz216ff532023-03-10 13:54:23 +010043 $("#total_due").val(costs.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }));
Marc Kupietz79c2b922023-03-08 09:29:11 +010044}
45
46function update_paper_id_field() {
47 var paper_id = $("#paper_id");
48 if ($('#author').is(":checked")) {
49 paper_id.removeAttr('disabled');
Marc Kupietz18f2f5a2023-03-16 17:35:19 +010050 paper_id.attr('pattern', '\\d{1,4}');
Marc Kupietz79c2b922023-03-08 09:29:11 +010051 paper_id.attr('required', 'required');
Marc Kupietzf577e672023-03-21 17:54:33 +010052 val = paper_id.val();
53 if (!x.match(/^\d{1,4}$/)) {
54 paper_id.val('');
55 }
Marc Kupietz79c2b922023-03-08 09:29:11 +010056 } else {
Marc Kupietz79c2b922023-03-08 09:29:11 +010057 paper_id.attr('disabled', 'disabled');
Marc Kupietzf577e672023-03-21 17:54:33 +010058 paper_id.val('');
Marc Kupietz79c2b922023-03-08 09:29:11 +010059 paper_id.removeAttr('placeholder');
60 paper_id.removeAttr('pattern');
61 paper_id.removeAttr('required');
62 }
63}
Marc Kupietz484ec8e2023-02-25 11:23:07 +010064
65function 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
82TOO_SHORT = 'Password too short, still %% characters needed';
83TOO_LONG = 'Password too long, please remove %% characters';
84INVALID_CHARS = 'Password contains invalid characters';
85QUAL_NONE = 'Password is very weak'
86QUAL_LOW = 'Password is weak';
87QUAL_MEDIUM = 'Password is average'
88QUAL_GOOD = 'Password is good';
89QUAL_STRONG = 'Password is strong';
90REP_OK = 'Repetition ok';
91REP_NE = 'Passwords not identical';
92PWNED = 'Password found in public password list';
93
Marc Kupietz216ff532023-03-10 13:54:23 +010094String.prototype.strReverse = function() {
Marc Kupietz484ec8e2023-02-25 11:23:07 +010095 var newstring = '';
96 for (var s = 0; s < this.length; s++)
97 newstring = this.charAt(s) + newstring;
98 return newstring;
99};
100
101function 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 Kupietz216ff532023-03-10 13:54:23 +0100105 $.get('https://api.pwnedpasswords.com/range/' + passwordChunk1).done(function(data, status) {
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100106 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
123function 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 Kupietz216ff532023-03-10 13:54:23 +0100148 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 Kupietz484ec8e2023-02-25 11:23:07 +0100161 for (var i = 0; i < nLength; i++) {
Marc Kupietz216ff532023-03-10 13:54:23 +0100162 if (pass[i].match(/[A-Z]/g)) { // uppercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100163 if (nTmpAlphaUC !== '' && (nTmpAlphaUC + 1) == i) {
164 nConsecAlphaUC++;
165 }
166 nTmpAlphaUC = i;
167 nAlphaUC++;
Marc Kupietz216ff532023-03-10 13:54:23 +0100168 } else if (pass[i].match(/[a-z]/g)) { // lowercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100169 if (nTmpAlphaLC !== '' && (nTmpAlphaLC + 1) == i) {
170 nConsecAlphaLC++;
171 }
172 nTmpAlphaLC = i;
173 nAlphaLC++;
Marc Kupietz216ff532023-03-10 13:54:23 +0100174 } else if (pass[i].match(/[0-9]/g)) { // numbers
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100175 if (i > 0 && i < (nLength - 1)) {
176 nMidChar++;
177 }
178 if (nTmpNumber !== '' && (nTmpNumber + 1) == i) {
179 nConsecNumber++;
180 }
181 nTmpNumber = i;
182 nNumber++;
Marc Kupietz216ff532023-03-10 13:54:23 +0100183 } else { // special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100184 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 Kupietz216ff532023-03-10 13:54:23 +0100211 if (pass.toLowerCase().indexOf(sFwd) != -1 ||
212 pass.toLowerCase().indexOf(sRev) != -1)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100213 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 Kupietz216ff532023-03-10 13:54:23 +0100222 if (pass.toLowerCase().indexOf(sFwd) != -1 ||
223 pass.toLowerCase().indexOf(sRev) != -1)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100224 nSeqNumber++;
225 }
226
227 // general point assignment
Marc Kupietz216ff532023-03-10 13:54:23 +0100228 if (nAlphaUC > 0 && nAlphaUC < nLength) // uppercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100229 nScore += 2 * (nLength - nAlphaUC);
Marc Kupietz216ff532023-03-10 13:54:23 +0100230 if (nAlphaLC > 0 && nAlphaLC < nLength) // lowercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100231 nScore += 2 * (nLength - nAlphaLC);
Marc Kupietz216ff532023-03-10 13:54:23 +0100232 if (nNumber > 0 && nNumber < nLength) // numbers
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100233 nScore += 2 * nNumber;
Marc Kupietz216ff532023-03-10 13:54:23 +0100234 if (nSpecial > 0) // special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100235 nScore += 4 * nSpecial;
Marc Kupietz216ff532023-03-10 13:54:23 +0100236 if (nMidChar > 0) // mid numbers/special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100237 nScore += 2 * nMidChar;
238
239 // point deductions for poor practices
Marc Kupietz216ff532023-03-10 13:54:23 +0100240 if ((nAlphaLC > 0 || nAlphaUC > 0) &&
241 nSpecial === 0 && nNumber === 0) // characters only
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100242 nScore -= nLength;
Marc Kupietz216ff532023-03-10 13:54:23 +0100243 if (nAlphaLC === 0 && nAlphaUC === 0 &&
244 nSpecial === 0 && nNumber > 0) // numbers only
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100245 nScore -= nLength;
Marc Kupietz216ff532023-03-10 13:54:23 +0100246 if (nRepChar > 0) // same character exists more than once
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100247 nScore -= nRepInc;
Marc Kupietz216ff532023-03-10 13:54:23 +0100248 if (nConsecAlphaUC > 0) // consecutive uppercase letters exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100249 nScore -= 2 * nConsecAlphaUC;
Marc Kupietz216ff532023-03-10 13:54:23 +0100250 if (nConsecAlphaLC > 0) // consecutive lowercase letters exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100251 nScore -= 2 * nConsecAlphaLC;
Marc Kupietz216ff532023-03-10 13:54:23 +0100252 if (nConsecNumber > 0) // consecutive numbers exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100253 nScore -= 2 * nConsecNumber;
Marc Kupietz216ff532023-03-10 13:54:23 +0100254 if (nSeqAlpha > 0) // sequential alpha strings exist (3 chars or more)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100255 nScore -= 3 * nSeqAlpha;
Marc Kupietz216ff532023-03-10 13:54:23 +0100256 if (nSeqNumber > 0) // sequential numeric strings exist (3 chars or more)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100257 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 Kupietz216ff532023-03-10 13:54:23 +0100312 isPasswordPwned(pass, function(isPwned) {
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100313 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 Kupietz216ff532023-03-10 13:54:23 +0100326}