Skip to content

Commit 13ff4cc

Browse files
author
Nitish Kumar
committed
Password Validate
1 parent ad7726c commit 13ff4cc

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

default-views/account/register-form.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<form method="post" action="/api/accounts/new">
1+
<form method="post" action="/api/accounts/new" onsubmit="return validatePasswordBeforeSubmit(e)">
22
<div class="form-group">
33
{{#if error}}
44
<div class="row">

default-views/account/register.hbs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,56 @@
2727
{{> account/register-form}}
2828
{{/if}}
2929
</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>
3132
<script>
32-
function validatePassword () {
33+
function validatePasswordBeforeSubmit (e) {
34+
e.preventDefault();
35+
const pwdErrorDiv = document.getElementById('passwordHelp');
3336
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("");
3880
}
3981
</script>
4082
</body>

0 commit comments

Comments
 (0)