|
27 | 27 | {{> account/register-form}} |
28 | 28 | {{/if}} |
29 | 29 | </div> |
30 | | -<script src="https://raw.githubusercontent.com/nowsecure/owasp-password-strength-test/master/owasp-password-strength-test.js" defer></script> |
| 30 | +<script src="https://raw.githubusercontent.com/nowsecure/owasp-password-strength-test/master/owasp-password-strength-test.js" |
| 31 | + defer></script> |
31 | 32 | <script> |
32 | | - function validatePassword () { |
| 33 | + function validatePasswordBeforeSubmit (e) { |
| 34 | + e.preventDefault(); |
| 35 | + const pwdErrorDiv = document.getElementById('passwordHelp'); |
33 | 36 | let pw = document.getElementById('password').value; |
34 | | - let owaspCheck = owaspPasswordStrengthTest.test(pw); |
35 | | - //check for errors and show |
36 | | - // call pwned api and check |
37 | | - //submit the form |
| 37 | + let owaspCheck = owaspPasswordStrengthTest.test(pw) |
| 38 | + if (owaspCheck.strong === true) { |
| 39 | + pwdErrorDiv.innerText = ''; |
| 40 | + sha1(pw).then((digest) => { |
| 41 | + const preFix = digest.slice(0, 5); |
| 42 | + const url = 'https://api.pwnedpasswords.com/range/'; |
| 43 | + fetch(url+preFix).then( |
| 44 | + response => response.text() |
| 45 | + ).then( |
| 46 | + data => { |
| 47 | + if (data.indexOf(digest) !== -1) { |
| 48 | + pwdErrorDiv.innerText = 'This password was exposed in a data breach. Please use a more secure alternative one!'; |
| 49 | + return false; |
| 50 | + } |
| 51 | + } |
| 52 | + ) |
| 53 | + }); |
| 54 | + } |
| 55 | + else { |
| 56 | + pwdErrorDiv.innerText = owaspCheck.requiredTestErrors[0] |
| 57 | + return false; |
| 58 | + } |
| 59 | + return true; |
| 60 | + } |
| 61 | +
|
| 62 | + function sha1(str) { |
| 63 | + let buffer = new TextEncoder("utf-8").encode(str); |
| 64 | + return crypto.subtle.digest("SHA-256", buffer).then(function (hash) { |
| 65 | + return hex(hash); |
| 66 | + }); |
| 67 | + } |
| 68 | +
|
| 69 | + function hex(buffer) { |
| 70 | + let hexCodes = []; |
| 71 | + let view = new DataView(buffer); |
| 72 | + for (let i = 0; i < view.byteLength; i += 4) { |
| 73 | + let value = view.getUint32(i); |
| 74 | + let stringValue = value.toString(16); |
| 75 | + const padding = '00000000'; |
| 76 | + let paddedValue = (padding + stringValue).slice(-padding.length); |
| 77 | + hexCodes.push(paddedValue); |
| 78 | + } |
| 79 | + return hexCodes.join(""); |
38 | 80 | } |
39 | 81 | </script> |
40 | 82 | </body> |
|
0 commit comments