This commit is contained in:
maru21 2023-11-04 18:55:55 +01:00
parent 1f008a22a8
commit 1ac7eb8bf4
4 changed files with 77 additions and 62 deletions

View File

@ -298,6 +298,18 @@ function resetPage() {
sidebarDiv.replaceWith(activeState.orgPage.sidebar);
}
function wrongPwAlert() {
let wrongPWAlert = document.getElementById("wrongPWAlert");
wrongPWAlert.style.display = "block";
wrongPWAlert.addEventListener("click", (e) => {
if (e.target && e.target.tagName === "A") {
clearStorage();
document.getElementById("wrongPWAlert").innerHTML =
"<p>all files cleared - set new password</p>";
}
});
}
export {
hideMenus,
showMenu,
@ -311,4 +323,5 @@ export {
resetNavBar,
printVersion,
resetPage,
wrongPwAlert
};

View File

@ -33,6 +33,7 @@ window.activeState = {
notifierPause: 1,
persistentStorage: "false",
enablePell: "false",
debug: "false"
},
templates: [],
templateObjectsPurified: [],

View File

@ -1,7 +1,8 @@
import { clearStorage, createStorageObj, retrieveData } from "./storage.js";
import { createStorageObj, retrieveData } from "./storage.js";
import sha256 from "./sha256.min.js";
import XORCipher from "./xorc.js";
import getBrowserFingerprint from "./identify.js"
import { wrongPwAlert } from "./evts.js";
export const passwordHash = {
name: cyrb53("m21_"+getBrowserFingerprint( { hardwareOnly: true } )),
@ -13,32 +14,25 @@ export const passwordHash = {
},
set: function (pw) {
if (pw == "") return;
activeState.sessionToken = XORCipher.encode(this.name, pw);
activeState.sessionToken = XORCipher.encode(this.name, sha256(pw));
},
initHash: function () {
//check if cookie exists
if (getCookie(sha256(this.name)) != null) {
if (getCookie(sha256(this.name)) != "") {
this.set(XORCipher.decode(sha256(this.name), getCookie(sha256(this.name))));
if (getCookie(this.name) != null) {
if (getCookie(this.name) != "") {
this.set(XORCipher.decode(this.name, getCookie(this.name)));
}
}
let verifiedStatus = false;
let tF = retrieveData("templateFiles");
if (tF != null) {verifiedStatus = true}
if (verifiedStatus == true) {
if (retrieveData("templateFiles") != null) {
//set user id
activeState.userId = getUsrId();
setCookie(sha256(this.name), XORCipher.encode(sha256(this.name), this), 10);
activeState.userId = passwordHash.name;
setCookie(this.name, XORCipher.encode(this.name, this), 10);
}
},
verify: function () {
if (passwordHash == "") return false;
let verifiedStatus = false;
let tF = retrieveData("templateFiles");
if (tF != null) verifiedStatus = true
return verifiedStatus;
return (retrieveData("templateFiles") != null) ? true : false;
}
}
@ -47,7 +41,7 @@ function setPassword() {
let pw = sanitize(x.elements[0].value);
if (pw != "" || pw !== "undefined") {
passwordHash.set(sha256(pw));
passwordHash.set(pw);
let tF = retrieveData("templateFiles");
if (tF == null) {
wrongPwAlert();
@ -63,25 +57,13 @@ function setPassword() {
if (tF == null || tF.length == 0) {
activeState.settings.persistentStorage = "false";
}
activeState.userId = passwordHash.name;
document.getElementById("login").style.display = "none";
setCookie(sha256(passwordHash.name), XORCipher.encode(sha256(passwordHash.name), passwordHash), 10)
setCookie(passwordHash.name, XORCipher.encode(passwordHash.name, passwordHash), 10)
}
}
function wrongPwAlert() {
let wrongPWAlert = document.getElementById("wrongPWAlert");
wrongPWAlert.style.display = "block";
wrongPWAlert.addEventListener("click", (e) => {
if (e.target && e.target.tagName === "A") {
clearStorage();
document.getElementById("wrongPWAlert").innerHTML =
"<p>all files cleared - set new password</p>";
}
});
}
function cyrb53(str, seed = 21) {
export function cyrb53(str, seed = 21) {
let h1 = 0xdeadbeef ^ seed,
h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
@ -96,11 +78,6 @@ function cyrb53(str, seed = 21) {
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
}
function getUsrId() {
const fingerprint = getBrowserFingerprint( { hardwareOnly: true } );
return cyrb53(fingerprint + passwordHash);
}
function setCookie(cname, cvalue, exdays) {
const d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
@ -124,9 +101,9 @@ function getCookie(cname) {
}
export function logout() {
let id = sha256(passwordHash.name);
let id = passwordHash.name;
activeState.sessionToken = "";
setCookie(sha256(passwordHash.name), "", 10);
setCookie(passwordHash.name, "", 10);
document.cookie = id + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
document.getElementById("passwordField").value = "";
document.getElementById("login").style.display = "block";

View File

@ -1,12 +1,21 @@
import XORCipher from "./xorc.js";
import sha256 from "./sha256.min.js";
import { getCurrentDate, passwordHash, sanitize } from "./scripts.js";
import { cyrb53, getCurrentDate, passwordHash, sanitize } from "./scripts.js";
const store = {
getItem: function (key) {return getStor().getItem(sha256(key+activeState.userId))},
setItem: function (key, data) {getStor().setItem(sha256(key+activeState.userId), data)},
removeItem: function (key) {getStor().removeItem(sha256(key+activeState.userId))},
clear: function () {getStor().clear()},
getItem: function (key) {
return debug("GET", key, getStor().getItem(sha256(key + activeState.userId)));
},
setItem: function (key, data) {
debug("SET", key, "setItem: "+data);
getStor().setItem(sha256(key + activeState.userId), data);
},
removeItem: function (key) {
getStor().removeItem(sha256(key + activeState.userId));
},
clear: function () {
getStor().clear();
},
};
const tempStore = {
@ -32,6 +41,17 @@ function getStor() {
}
}
function debug(mode, key, data) {
if (activeState.settings.debug == "false") return data;
console.log({
mode: mode,
key: key,
data: data.substring(0,10),
persistent: activeState.settings.persistentStorage
});
return data;
}
function createStorageObj() {
let x = document.getElementById("mainFormObj");
let dataArray = [];
@ -75,9 +95,9 @@ function storeData(name, data) {
name = "userInput";
}
let lT = activeState.loadedTemplate;
let key = sha256(name + "_m21_" + lT);
let key = name + "_m21_" + lT;
if (name == "templateFiles") {
key = sha256(name + "_m21_" + activeState.userId);
key = name + "_m21_" + activeState.userId;
}
store.setItem(key, obfuscate(data));
}
@ -99,12 +119,12 @@ function retrieveData(name, template = "none") {
let key;
if (template == "none") {
let lT = activeState.loadedTemplate;
key = sha256(name + "_m21_" + lT);
key = name + "_m21_" + lT;
if (name == "templateFiles") {
key = sha256(name + "_m21_"+activeState.userId);
key = name + "_m21_" + activeState.userId;
}
} else {
key = sha256(name + "_m21_" + template);
key = name + "_m21_" + template;
}
cdata = store.getItem(key);
@ -127,13 +147,13 @@ function clearData(name, template = "none") {
let key;
if (template == "none") {
lT = activeState.loadedTemplate;
key = sha256(name + "_m21_" + lT);
key = name + "_m21_" + lT;
if (name == "templateFiles") {
key = sha256(name + "_m21_"+activeState.userId);
key = name + "_m21_" + activeState.userId;
}
} else {
lT = template;
key = sha256(name + "_m21_" + template);
key = name + "_m21_" + template;
}
store.removeItem(key);
}
@ -151,7 +171,7 @@ function getFileName(ref = "none") {
const metadata = {
ts_create: getCurrentDate(),
ts_save: "",
id: sha256(currentFileName),
id: cyrb53(currentFileName),
};
if (tF.length != 0) {
@ -178,11 +198,11 @@ function getFileName(ref = "none") {
}
function obfuscate(data, mode = true) {
if (data == null | data == "") return "";
if ((data == null) | (data == "")) return "";
if (mode) {
return XORCipher.encode(sha256(passwordHash), data);
return XORCipher.encode(passwordHash, data);
} else {
return XORCipher.decode(sha256(passwordHash), data);
return XORCipher.decode(passwordHash, data);
}
}
@ -235,14 +255,14 @@ function importBookShelf() {
let templateFilesArray = [];
for (let file of mainArray) {
if (file.name == "hash") continue;
store.setItem(sha256(file.name), file.data);
store.setItem(file.name, file.data);
templateFilesArray.push({
fileName: file.name.split("_m21_")[0],
template: file.name.split("_m21_")[1],
});
}
store.setItem(
sha256("templateFiles_m21_"+activeState.userId),
"templateFiles_m21_" + activeState.userId,
obfuscate(JSON.stringify(templateFilesArray))
);
}
@ -257,17 +277,21 @@ function importBookShelf() {
}
function storeSettings(data, get = false) {
let key = sha256("settings_m21_"+activeState.userId);
let key = "settings_m21_" + activeState.userId;
if (get) {
let cdata = "";
if (data == "getInit") {activeState.settings.persistentStorage = "true"}
if (data == "getInit") {
activeState.settings.persistentStorage = "true";
}
try {
cdata = JSON.parse(obfuscate(store.getItem(key), false));
} catch (e) {
cdata = "";
}
if (data == "getInit") {activeState.settings.persistentStorage = "false"};
if (data == "getInit") {
activeState.settings.persistentStorage = "false";
}
return cdata;
} else {
store.setItem(key, obfuscate(JSON.stringify(data)));
@ -287,5 +311,5 @@ export {
createBookShelf,
importBookShelf,
storeSettings,
clearStorage
clearStorage,
};