Account creation and login now works
This commit is contained in:
parent
cf8116b218
commit
5e01e5d459
4 changed files with 320 additions and 42 deletions
150
main.js
150
main.js
|
|
@ -1,4 +1,6 @@
|
|||
var url = `${window.location.protocol}//${window.location.hostname}`;
|
||||
console.log(window.location.protocol);
|
||||
|
||||
var url = `${window.location.protocol}//${window.location.hostname}/_larpix`;
|
||||
var params = new URLSearchParams(window.location.search);
|
||||
|
||||
const collapseDmsBtn = document.getElementById("collapse-dms");
|
||||
|
|
@ -17,16 +19,25 @@ function delay(time) {
|
|||
return new Promise(resolve => setTimeout(resolve, time));
|
||||
}
|
||||
async function packetEncPass(pass, key, username) {
|
||||
return await encryptWithNonce(pass, key, getNonce(username, key));
|
||||
}
|
||||
|
||||
async function getNonce(username, key) {
|
||||
|
||||
let nonce;
|
||||
let fetchRes = await fetchAsync(`${url}/nextnonce?u=${username}`);
|
||||
let fetchRes = await (await fetch(`${url}/nextnonce?u=${username}`)).text();
|
||||
console.log(key, fetchRes);
|
||||
try {
|
||||
nonce = await decryptString(fetchRes, key);
|
||||
}
|
||||
catch(err) {
|
||||
nonce = await decryptString(fetchRes, "");
|
||||
}
|
||||
return nonce;
|
||||
}
|
||||
|
||||
return await encryptString(pass, nonce + key);
|
||||
async function encryptWithNonce(value, key, nonce) {
|
||||
return await encryptString(value, nonce + key);
|
||||
}
|
||||
|
||||
async function calcCommunicationKeyClient(p, g, pubServer) {
|
||||
|
|
@ -59,8 +70,12 @@ function keyDataFromServerJson(jsonFromServer) {
|
|||
return [p, g, pubServer, data.idKey]
|
||||
}
|
||||
|
||||
const base64ToUint8 = (base64) => Uint8Array.from(atob(base64), c => c.charCodeAt(0));
|
||||
const uint8ToBase64 = (uint8) => fixBase64Padding(btoa(String.fromCharCode(...uint8)));
|
||||
function base64ToUint8(base64) {
|
||||
return Uint8Array.from(atob(base64), c => c.charCodeAt(0));
|
||||
}
|
||||
function uint8ToBase64(uint8) {
|
||||
return fixBase64Padding(btoa(String.fromCharCode(...uint8)));
|
||||
}
|
||||
|
||||
async function encrypt(plainText, keyBytes) {
|
||||
const iv = window.crypto.getRandomValues(new Uint8Array(16));
|
||||
|
|
@ -184,49 +199,84 @@ async function decryptString(base64Text, passphrase) {
|
|||
}
|
||||
|
||||
|
||||
async function fetchpost(url, value) {
|
||||
async function fetchPost(url, value) {
|
||||
let response = await fetch(url, {
|
||||
method: "POST",
|
||||
credentials: "omit",
|
||||
body: value,
|
||||
headers: {
|
||||
"secret": passwordHash
|
||||
"secret": await encryptWithNonce(passwordHash, passwordHash, await getNonce(username, passwordHash))
|
||||
}
|
||||
});
|
||||
let data = await response.text();
|
||||
return data;
|
||||
}
|
||||
|
||||
async function fetchAsync(url, sendSecret) {
|
||||
let response;
|
||||
if (sendSecret) {
|
||||
response = await fetch(url, {
|
||||
method: "GET", credentials: "omit", headers: {
|
||||
"secret": passwordHash
|
||||
}
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
response = await fetch(url, {method: "GET", credentials: "omit"});
|
||||
}
|
||||
async function fetchPostEnc(url, value) {
|
||||
let nonce = await getNonce(username, passwordHash);
|
||||
let response = await fetch(url, {
|
||||
method: "POST",
|
||||
credentials: "omit",
|
||||
body: await encryptWithNonce(value, passwordHash, nonce),
|
||||
headers: {
|
||||
"secret": await encryptWithNonce(passwordHash, passwordHash, nonce)
|
||||
}
|
||||
});
|
||||
let data = await response.text();
|
||||
return data;
|
||||
}
|
||||
|
||||
async function fetchAsync(url) {
|
||||
let response = await fetch(url, {
|
||||
method: "GET",
|
||||
credentials: "omit",
|
||||
headers: {
|
||||
"secret": await encryptWithNonce(passwordHash, passwordHash, await getNonce(username, passwordHash))
|
||||
}
|
||||
});
|
||||
|
||||
let data = await response.text();
|
||||
return data;
|
||||
}
|
||||
|
||||
async function Auth(username, password) {
|
||||
|
||||
let passwordHash = await hashSHA3_512(password);
|
||||
console.log(username, password, passwordHash);
|
||||
let response = await fetch(`${url}/auth?u=${username}`, {
|
||||
method: "GET",
|
||||
credentials: "omit",
|
||||
headers: {
|
||||
"secret": await encryptWithNonce(passwordHash, passwordHash, await getNonce(username, passwordHash))
|
||||
}
|
||||
});
|
||||
|
||||
let data = await response.text();
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
async function fetchEncrypted(request, body)
|
||||
{
|
||||
return await decryptString(
|
||||
await fetchpost(`${url}/encryptedrequest?u=${username}`, await packetEncPass(
|
||||
let nonce = await getNonce(username, passwordHash);
|
||||
|
||||
let response = await fetch(`${url}/encryptedrequest?u=${username}`, {
|
||||
method: "POST",
|
||||
credentials: "omit",
|
||||
body: await encryptWithNonce(
|
||||
|
||||
JSON.stringify({
|
||||
string1: request,
|
||||
string2: body
|
||||
})
|
||||
|
||||
, passwordHash, username))
|
||||
|
||||
, passwordHash);
|
||||
, passwordHash, nonce),
|
||||
headers: {
|
||||
"secret": await encryptWithNonce(passwordHash, passwordHash, nonce)
|
||||
}
|
||||
});
|
||||
let data = await response.text();
|
||||
return data;
|
||||
}
|
||||
|
||||
function power(base, exponent, mod) {
|
||||
|
|
@ -254,9 +304,59 @@ function fixBase64Padding(base64String) {
|
|||
return str;
|
||||
}
|
||||
|
||||
function showNotification(message, type = 'info', duration = 3500) {
|
||||
let container = document.getElementById('notification-container');
|
||||
if (!container) {
|
||||
container = document.createElement('div');
|
||||
container.id = 'notification-container';
|
||||
document.body.appendChild(container);
|
||||
}
|
||||
|
||||
const notif = document.createElement('div');
|
||||
notif.className = `notification ${type}`;
|
||||
notif.textContent = message;
|
||||
|
||||
container.appendChild(notif);
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
requestAnimationFrame(() => {
|
||||
notif.classList.add('show');
|
||||
});
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
notif.classList.remove('show');
|
||||
|
||||
notif.addEventListener('transitionend', () => {
|
||||
notif.remove();
|
||||
});
|
||||
}, duration);
|
||||
}
|
||||
|
||||
|
||||
async function hashSHA3_512(input) {
|
||||
const encoder = new TextEncoder();
|
||||
const data = encoder.encode(input);
|
||||
const hashBuffer = await crypto.subtle.digest('SHA-512', data); //-3 kiedys xddddddddddddddddd
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
||||
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
||||
return hashHex;
|
||||
}
|
||||
|
||||
|
||||
|
||||
var password = "";
|
||||
var username = "";
|
||||
var passwordHash = "";
|
||||
var host = "";
|
||||
async function mainJS()
|
||||
{
|
||||
username = localStorage.getItem('username');
|
||||
password = localStorage.getItem('password');
|
||||
host = localStorage.getItem('host');
|
||||
passwordHash = await hashSHA3_512(password);
|
||||
}
|
||||
mainJS();
|
||||
|
||||
collapseDmsBtn.addEventListener("click", () => {
|
||||
collapseDmsBtn.classList.toggle("collapsed");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue