Skip to content

Commit 841c409

Browse files
committed
Corrections
1 parent f27947e commit 841c409

File tree

2 files changed

+56
-53
lines changed

2 files changed

+56
-53
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-256", 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>

default-views/account/register.hbs

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

0 commit comments

Comments
 (0)