227 lines
6.4 KiB
JavaScript
227 lines
6.4 KiB
JavaScript
import { createStorageObj, retrieveData } from "./storage.js";
|
|
import sha256 from "./sha256.min.js";
|
|
import XORCipher from "./xorc.js";
|
|
import getBrowserFingerprint from "./identify.js"
|
|
|
|
export const passwordHash = {
|
|
name: "anae3Iegbai1ahLu",
|
|
fp: getBrowserFingerprint( { hardwareOnly: true } ),
|
|
toString: () => {
|
|
let data;
|
|
try {
|
|
data = window.sessionStorage.getItem(sha256(passwordHash.name));
|
|
} catch (e) {
|
|
return "none";
|
|
}
|
|
if (data === null) return "none";
|
|
return XORCipher.decode(passwordHash.name, data);
|
|
},
|
|
getId: () => {
|
|
return passwordHash.fp;
|
|
},
|
|
set: (pw) => {
|
|
window.sessionStorage.setItem(sha256(passwordHash.name), XORCipher.encode(passwordHash.name, pw));
|
|
}
|
|
}
|
|
|
|
//export const passwordHash = {
|
|
// toString: () => {
|
|
// let fp = getBrowserFingerprint( { hardwareOnly: true } );
|
|
// let data;
|
|
// try {
|
|
// data = window.sessionStorage.getItem(sha256(fp));
|
|
// } catch (e) {
|
|
// return "none";
|
|
// }
|
|
// if (data === null) return "none";
|
|
// return XORCipher.decode(fp, data);
|
|
// },
|
|
//
|
|
// set: (pw) => {
|
|
// let fp = getBrowserFingerprint( { hardwareOnly: true } );
|
|
// window.sessionStorage.setItem(sha256(fp), XORCipher.encode(fp, pw));
|
|
// }
|
|
//}
|
|
|
|
|
|
|
|
function setPassword() {
|
|
let x = document.getElementById("loginForm");
|
|
let pw = sanitize(x.elements[0].value);
|
|
|
|
if (pw != "" || pw !== "undefined") {
|
|
let pwOld = pw;
|
|
passwordHash.set(sha256(pw));
|
|
let templateFiles = retrieveData("templateFiles");
|
|
if (templateFiles != "") {
|
|
try {
|
|
JSON.parse(templateFiles);
|
|
} catch (e) {
|
|
document.getElementById("wrongPWAlert").style.display = "block";
|
|
const alertTimeout = setTimeout(() => {
|
|
document.getElementById("wrongPWAlert").style.display = "none";
|
|
}, 5000);
|
|
passwordHash.set(pwOld);
|
|
x.elements[0].value = "";
|
|
return;
|
|
}
|
|
}
|
|
//user logged in
|
|
|
|
document.getElementById("login").style.display = "none";
|
|
window.sessionStorage.setItem(sha256("verified"), XORCipher.encode(sha256("passwordHash"), passwordHash));
|
|
setCookie(sha256("verified"), XORCipher.encode(sha256("passwordHash"), passwordHash), 10)
|
|
}
|
|
}
|
|
|
|
export function getUsrId() {
|
|
const fingerprint = getBrowserFingerprint( { hardwareOnly: true } );
|
|
return cyrb53(fingerprint + passwordHash);
|
|
}
|
|
|
|
export function sessionVerification() {
|
|
|
|
//check if cookie exists
|
|
if (getCookie(sha256("verified")) != null) {
|
|
passwordHash.set(XORCipher.decode(sha256("passwordHash"), getCookie(sha256("verified"))));
|
|
window.sessionStorage.setItem(sha256("verified"), XORCipher.encode(sha256("passwordHash"), passwordHash));
|
|
}
|
|
|
|
let verfiedStatus = window.sessionStorage.getItem(sha256("verified"));
|
|
let data;
|
|
try {
|
|
data = window.sessionStorage.getItem(sha256(passwordHash.name));
|
|
} catch (e) {
|
|
verfiedStatus = null;
|
|
}
|
|
if (data === null) verfiedStatus = null;
|
|
|
|
//if (verfiedStatus != data) verfiedStatus = null
|
|
let vsString;
|
|
let pnString;
|
|
try {
|
|
vsString = XORCipher.decode(sha256("passwordHash"), verfiedStatus);
|
|
pnString = XORCipher.decode(passwordHash.name, data);
|
|
if (vsString != pnString) verfiedStatus = null;
|
|
} catch (e) {
|
|
verfiedStatus = null;
|
|
}
|
|
|
|
let tF = retrieveData("templateFiles");
|
|
try {
|
|
tF = JSON.parse(tF);
|
|
} catch(e) {
|
|
//verfiedStatus = null;
|
|
}
|
|
|
|
return (verfiedStatus == null) ? false : true;
|
|
|
|
}
|
|
|
|
const cyrb53 = (str, seed = 21) => {
|
|
let h1 = 0xdeadbeef ^ seed,
|
|
h2 = 0x41c6ce57 ^ seed;
|
|
for (let i = 0, ch; i < str.length; i++) {
|
|
ch = str.charCodeAt(i);
|
|
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
}
|
|
|
|
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
|
|
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
};
|
|
|
|
function setCookie(cname, cvalue, exdays) {
|
|
const d = new Date();
|
|
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
|
|
let expires = "expires="+d.toUTCString();
|
|
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
|
|
}
|
|
|
|
export function getCookie(cname) {
|
|
let name = cname + "=";
|
|
let ca = document.cookie.split(';');
|
|
for(let i = 0; i < ca.length; i++) {
|
|
let c = ca[i];
|
|
while (c.charAt(0) == ' ') {
|
|
c = c.substring(1);
|
|
}
|
|
if (c.indexOf(name) == 0) {
|
|
return c.substring(name.length, c.length);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function logout() {
|
|
let id = sha256("verified");
|
|
window.sessionStorage.setItem(id, "");
|
|
document.cookie = id + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
|
sessionVerification();
|
|
document.getElementById("passwordField").value = "";
|
|
document.getElementById("login").style.display = "block";
|
|
}
|
|
|
|
|
|
|
|
export function sanitize(string) {
|
|
const map = {
|
|
'&': '_',
|
|
'<': '_',
|
|
'>': '_',
|
|
'"': '_',
|
|
"'": '_',
|
|
'/': '_',
|
|
'`': '_',
|
|
'=': '_'
|
|
};
|
|
const reg = /[&<>"'/]/ig;
|
|
return string.replace(reg, (match)=>(map[match]));
|
|
}
|
|
|
|
function isAlphaNumeric(str) {
|
|
var code, i, len;
|
|
|
|
for (i = 0, len = str.length; i < len; i++) {
|
|
code = str.charCodeAt(i);
|
|
if (!(code > 47 && code < 58) && // numeric (0-9)
|
|
!(code > 64 && code < 91) && // upper alpha (A-Z)
|
|
!(code > 96 && code < 123)) { // lower alpha (a-z)
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
};
|
|
|
|
export const inputRead = {
|
|
init: function () {
|
|
this.event = "";
|
|
this.inputString = "";
|
|
this.source = "";
|
|
this.inputContent = "";
|
|
},
|
|
read: function (event) {
|
|
this.event = event;
|
|
this.source = event.srcElement.id;
|
|
let key = (event.key !=undefined) ? event.key : "";
|
|
let contentElement = document.getElementById(this.source);
|
|
this.inputContent = (contentElement != undefined) ? contentElement.value + key : "";
|
|
|
|
if (this.inputContent == "" || key == "") return;
|
|
if (!isAlphaNumeric(key)) return;
|
|
if (key.length > 1) return;
|
|
if (activeState.activePage != "template") return;
|
|
|
|
document.getElementById("toggleFilesMenu").style.backgroundColor = "#c0392b";
|
|
const run = setTimeout(() => {
|
|
createStorageObj();
|
|
document.getElementById("toggleFilesMenu").style.backgroundColor = "#34495e"
|
|
}, 3000);
|
|
},
|
|
//filter words
|
|
}
|
|
|
|
export default setPassword;
|