Skip to content

Commit e35abfb

Browse files
authored
Merge pull request #845 from mintunitish/develop
Password Validator
2 parents 41a7c96 + 3115330 commit e35abfb

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

default-views/account/register-form.hbs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="col-md-6">
33
<div class="panel panel-default">
44
<div class="panel-body">
5-
<form method="post" action="/api/accounts/new">
5+
<form method="post" action="/api/accounts/new" onsubmit="return validatePasswordBeforeSubmit(e)">
66
{{> shared/error}}
77

88
<div class="form-group">
@@ -19,6 +19,7 @@
1919
<input type="checkbox" id="showPassword"/> Show Password
2020
</label>
2121
</div>
22+
<span id="passwordHelp" class="text-danger"></span>
2223
</div>
2324

2425
<div class="form-group">
@@ -66,6 +67,11 @@
6667
</div>
6768

6869

70+
<script src="https://raw.githubusercontent.com/nowsecure/owasp-password-strength-test/master/owasp-password-strength-test.js"
71+
defer></script>
72+
<script>
73+
74+
</script>
6975
<script>
7076
(function () {
7177
'use strict'
@@ -75,4 +81,53 @@
7581
password.type = password.type === 'password' ? 'text' : 'password'
7682
})
7783
})()
84+
85+
function validatePasswordBeforeSubmit (e) {
86+
e.preventDefault();
87+
const pwdErrorDiv = document.getElementById('passwordHelp');
88+
let pw = document.getElementById('password').value;
89+
let owaspCheck = owaspPasswordStrengthTest.test(pw)
90+
if (owaspCheck.strong === true) {
91+
pwdErrorDiv.innerText = '';
92+
sha1(pw).then((digest) => {
93+
const preFix = digest.slice(0, 5);
94+
const url = 'https://api.pwnedpasswords.com/range/';
95+
fetch(url+preFix).then(
96+
response => response.text()
97+
).then(
98+
data => {
99+
if (data.indexOf(digest) !== -1) {
100+
pwdErrorDiv.innerText = 'This password was exposed in a data breach. Please use a more secure alternative one!';
101+
return false;
102+
}
103+
}
104+
)
105+
});
106+
}
107+
else {
108+
pwdErrorDiv.innerText = owaspCheck.requiredTestErrors[0]
109+
return false;
110+
}
111+
return true;
112+
}
113+
114+
function sha1(str) {
115+
let buffer = new TextEncoder("utf-8").encode(str);
116+
return crypto.subtle.digest("SHA-1", buffer).then(function (hash) {
117+
return hex(hash);
118+
});
119+
}
120+
121+
function hex(buffer) {
122+
let hexCodes = [];
123+
let view = new DataView(buffer);
124+
for (let i = 0; i < view.byteLength; i += 4) {
125+
let value = view.getUint32(i);
126+
let stringValue = value.toString(16);
127+
const padding = '00000000';
128+
let paddedValue = (padding + stringValue).slice(-padding.length);
129+
hexCodes.push(paddedValue);
130+
}
131+
return hexCodes.join("");
132+
}
78133
</script>

0 commit comments

Comments
 (0)