relay requesting (and maybe verifying) to the user

This commit is contained in:
2025-04-30 18:03:56 +03:00
parent 991a4f29a6
commit 92fd491194
22 changed files with 968 additions and 222 deletions

View File

@@ -1,25 +1,73 @@
window.onload = async () => {
let id = (await (await fetch("/api/captcha", {method: "POST"})).json()).id
if (id == undefined) {
alert("Service is unavailable now. Please, try again later.");
return;
const check_solution = async (solution) => {
const body = {
"TotalSum": "78278",
"FnNumber": "9960440301173139",
"ReceiptOperationType": "1",
"DocNumber": "35704",
"DocFiscalSign": "4149689833",
"Captcha": solution,
"DocDateTime": "2022-09-21T20:28:00.000Z"
}
console.log(id);
fetch(`/api/captcha/${id}`).then(response => response.blob())
.then(blob => {
const url = URL.createObjectURL(blob);
document.getElementById("captcha_image").src = url;
}
);
const result = await fetch("https://check.ofd.ru/Document/FetchReceiptFromFns", {
method: "POST",
headers: {
"Content-Type": "application/json;charset=utf-8",
// "Origin": "https://check.ofd.ru"
},
body: JSON.stringify(body),
});
return result.status != 400;
}
const get_cookie = (name) => {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
if (cookie.startsWith(name + '=')) {
const value = cookie.substring(name.length + 1);
console.log(value);
}
}
}
const blobToBase64 = (blob) => {
return new Promise((resolve, _) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
}
window.onload = async () => {
console.log("koka: " + get_cookie("JWT"));
console.log("all: " + document.cookie);
if (!document.cookie.includes('JWT')) {
document.location.href = "/login";
}
const response = await fetch("https://check.ofd.ru/api/captcha/common/img");
captcha = await response.blob();
const url = URL.createObjectURL(captcha);
document.getElementById("captcha_image").src = url;
console.log(captcha.type)
const form = document.getElementById("captchaForm");
const inputField = document.getElementById("captcha");
form.addEventListener('submit', async (e) => {
e.preventDefault();
// if (!await check_solution(inputField.value)) {
// alert("Капча решена неверно")
// returnl
// }
const response = await fetch(`/api/captcha/${id}`, {method: "PATCH",headers: {'Content-Type': 'application/json'}, body: JSON.stringify({"solution": inputField.value})});
const response = await fetch(`/api/captcha/submit`, {method: "POST", headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ "image": await blobToBase64(captcha), "solution": inputField.value})});
if (response.status == 200) {
inputField.value = "";
window.location.reload();
};
})
} else {
response_json = await response.json()
alert(response_json.message)
}
});
};

48
public/js/login.js Normal file
View File

@@ -0,0 +1,48 @@
const set_cookie = (name, value, days) => {
const expires = new Date(Date.now() + days * 86400 * 1000).toUTCString();
document.cookie = `${name}=${value}; expires=${expires}; path=/;`;
}
window.onload = async () => {
alert("This service requests a captcha from https://ofd.ru and sends an example receipt to it to check the correctness of the captcha. If you are not okay with making such requests, please leave the site immediately");
if (document.cookie.includes('JWT')) {
document.location = '/';
}
const form = document.getElementById("loginForm");
const username = document.getElementById("username");
const password = document.getElementById("password");
form.addEventListener('submit', async (e) => {
e.preventDefault();
const response = await fetch(
`/api/user/login`, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"username": username.value,
"password": password.value
})
});
switch (response.status) {
case 403:
alert("Incorrect password");
password.value = "";
break;
case 200:
response_json = await response.json()
set_cookie("JWT", response_json["token"], 365);
window.location.href = '/';
break;
case 404:
alert("No such user exists");
username.value = "";
break;
default:
alert("Unknown server error. Please, conact the developer");
console.log(response);
}
});
};