OpenAuth_server/views/src/login/login.js

167 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-11-06 19:48:50 +00:00
import sha from "sha512";
2020-08-07 14:16:39 +00:00
import { setCookie, getCookie } from "cookie";
import "inputs";
const loader = document.getElementById("loader");
const container = document.getElementById("container");
const usernameinput = document.getElementById("username");
const usernamegroup = document.getElementById("usernamegroup");
const uerrorfield = document.getElementById("uerrorfield");
const passwordinput = document.getElementById("password");
const passwordgroup = document.getElementById("passwordgroup");
const perrorfield = document.getElementById("perrorfield");
const nextbutton = document.getElementById("nextbutton");
const loginbutton = document.getElementById("loginbutton");
2018-11-06 19:48:50 +00:00
let username;
let salt;
2020-08-07 14:16:39 +00:00
usernameinput.focus();
2018-11-06 19:48:50 +00:00
const loading = () => {
container.style.filter = "blur(2px)";
loader.style.display = "";
2020-08-07 14:16:39 +00:00
};
2018-11-06 19:48:50 +00:00
const loading_fin = () => {
2020-08-07 14:16:39 +00:00
container.style.filter = "";
2018-11-06 19:48:50 +00:00
loader.style.display = "none";
2020-08-07 14:16:39 +00:00
};
2018-11-06 19:48:50 +00:00
loading_fin();
usernameinput.onkeydown = (e) => {
var keycode = e.keyCode ? e.keyCode : e.which;
if (keycode === 13) nextbutton.click();
clearError(uerrorfield);
2020-08-07 14:16:39 +00:00
};
2018-11-06 19:48:50 +00:00
nextbutton.onclick = async () => {
loading();
username = usernameinput.value;
try {
2020-08-07 14:16:39 +00:00
let res = await fetch(
"/api/user/login?type=username&username=" + username,
{
method: "POST",
2018-11-06 19:48:50 +00:00
}
2020-08-07 14:16:39 +00:00
)
.then((e) => {
if (e.status !== 200) throw new Error(e.statusText);
return e.json();
})
.then((data) => {
if (data.error) {
return Promise.reject(new Error(data.error));
}
return data;
});
2018-11-06 19:48:50 +00:00
salt = res.salt;
2020-08-07 14:16:39 +00:00
usernamegroup.classList.add("invisible");
passwordgroup.classList.remove("invisible");
passwordinput.focus();
2018-11-06 19:48:50 +00:00
} catch (e) {
2020-08-07 14:16:39 +00:00
showError(uerrorfield, e.message);
2018-11-06 19:48:50 +00:00
}
2020-08-07 14:16:39 +00:00
loading_fin();
};
2018-11-06 19:48:50 +00:00
passwordinput.onkeydown = (e) => {
var keycode = e.keyCode ? e.keyCode : e.which;
if (keycode === 13) loginbutton.click();
clearError(perrorfield);
2020-08-07 14:16:39 +00:00
};
2018-11-06 19:48:50 +00:00
loginbutton.onclick = async () => {
loading();
let pw = sha(salt + passwordinput.value);
try {
2020-08-07 14:16:39 +00:00
let { login, special, tfa } = await fetch(
"/api/user/login?type=password",
{
method: "POST",
body: JSON.stringify({
username: usernameinput.value,
password: pw,
}),
headers: {
"content-type": "application/json",
},
2018-11-06 19:48:50 +00:00
}
2020-08-07 14:16:39 +00:00
)
.then((e) => {
if (e.status !== 200) throw new Error(e.statusText);
return e.json();
})
.then((data) => {
if (data.error) {
return Promise.reject(new Error(data.error));
}
return data;
});
2018-11-06 19:48:50 +00:00
2019-03-13 01:06:09 +00:00
setCookie("login", login.token, new Date(login.expires).toUTCString());
2020-08-07 14:16:39 +00:00
setCookie(
"special",
special.token,
new Date(special.expires).toUTCString()
);
let d = new Date();
d.setTime(d.getTime() + 30 * 24 * 60 * 60 * 1000); //Keep the username 30 days
2018-11-06 19:48:50 +00:00
setCookie("username", username, d.toUTCString());
let url = new URL(window.location.href);
2020-08-07 14:16:39 +00:00
let state = url.searchParams.get("state");
let red = "/";
2019-03-13 01:06:09 +00:00
if (tfa) twofactor(tfa);
else {
if (state) {
2020-08-07 14:16:39 +00:00
let base64 = url.searchParams.get("base64");
if (base64) red = atob(state);
else red = state;
2019-03-13 01:06:09 +00:00
}
window.location.href = red;
2018-11-06 19:48:50 +00:00
}
} catch (e) {
passwordinput.value = "";
showError(perrorfield, e.message);
}
loading_fin();
2020-08-07 14:16:39 +00:00
};
2018-11-06 19:48:50 +00:00
function clearError(field) {
field.innerText = "";
2020-08-07 14:16:39 +00:00
field.classList.add("invisible");
2018-11-06 19:48:50 +00:00
}
function showError(field, error) {
field.innerText = error;
2020-08-07 14:16:39 +00:00
field.classList.remove("invisible");
2018-11-06 19:48:50 +00:00
}
2020-08-07 14:16:39 +00:00
username = getCookie("username");
2018-11-06 19:48:50 +00:00
if (username) {
usernameinput.value = username;
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
usernameinput.dispatchEvent(evt);
2019-03-13 01:06:09 +00:00
}
function twofactor(tfa) {
let list = tfa
2020-08-07 14:16:39 +00:00
.map((entry) => {
2019-03-13 01:06:09 +00:00
switch (entry) {
case 0: // OTC
return "Authenticator App";
case 1: // BACKUP
return "Backup Key";
}
return undefined;
})
2020-08-07 14:16:39 +00:00
.filter((e) => e !== undefined)
2019-03-13 01:06:09 +00:00
.reduce((p, c) => p + `<li>${c}</li>`, "");
let tfl = document.getElementById("tflist");
tfl.innerHTML = list;
2020-08-07 14:16:39 +00:00
}