Skip to content

Commit

Permalink
Async XHR for login form (FreshRSS#7023)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkarex authored Nov 26, 2024
1 parent 51133a5 commit 4baed12
Showing 1 changed file with 37 additions and 28 deletions.
65 changes: 37 additions & 28 deletions p/scripts/extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,44 +39,53 @@ function init_crypto_form() {
}

crypto_form.onsubmit = function (e) {
if (submit_button) {
submit_button.disabled = true;
e.preventDefault();

if (!submit_button) {
return false;
}
submit_button.disabled = true;

if (document.getElementById('challenge').value) {
// Already computed
return true;
}
let success = false;

const req = new XMLHttpRequest();
req.open('GET', './?c=javascript&a=nonce&user=' + document.getElementById('username').value, false);
req.open('GET', './?c=javascript&a=nonce&user=' + document.getElementById('username').value, true);

req.onerror = function () {
openNotification('Communication error!', 'bad');
submit_button.disabled = false;
};
req.send();
if (req.status == 200) {
const json = xmlHttpRequestJson(req);
if (!json.salt1 || !json.nonce) {
openNotification('Invalid user!', 'bad');
} else {
try {
const strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function');
const s = dcodeIO.bcrypt.hashSync(document.getElementById('passwordPlain').value, json.salt1);
const c = dcodeIO.bcrypt.hashSync(json.nonce + s, strong ? dcodeIO.bcrypt.genSaltSync(4) : poormanSalt());
document.getElementById('challenge').value = c;
if (!s || !c) {
openNotification('Crypto error!', 'bad');
} else {
success = true;

req.onload = function () {
if (req.status == 200) {
const json = xmlHttpRequestJson(req);
if (!json.salt1 || !json.nonce) {
openNotification('Invalid user!', 'bad');
} else {
try {
const strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function');
const s = dcodeIO.bcrypt.hashSync(document.getElementById('passwordPlain').value, json.salt1);
const c = dcodeIO.bcrypt.hashSync(json.nonce + s, strong ? dcodeIO.bcrypt.genSaltSync(4) : poormanSalt());
document.getElementById('challenge').value = c;
if (!s || !c) {
openNotification('Crypto error!', 'bad');
} else {
crypto_form.removeEventListener('submit', crypto_form.onsubmit);
crypto_form.submit();
}
} catch (ex) {
openNotification('Crypto exception! ' + ex, 'bad');
}
} catch (ex) {
openNotification('Crypto exception! ' + ex, 'bad');
}
} else {
req.onerror();
}
} else {
req.onerror();
}
};

if (submit_button) {
submit_button.disabled = false;
}
return success;
req.send();
};
}
// </crypto form (Web login)>
Expand Down

0 comments on commit 4baed12

Please sign in to comment.