refactor: update key handling
This commit is contained in:
parent
017a71e942
commit
76fabec96a
|
@ -1,34 +1,32 @@
|
||||||
import {
|
import {
|
||||||
addContextToErr,
|
addContextToErr,
|
||||||
bufToStr,
|
|
||||||
downloadObject,
|
downloadObject,
|
||||||
Err,
|
Err,
|
||||||
getActivePortals,
|
getActivePortals,
|
||||||
hexToBuf,
|
|
||||||
} from "@lumeweb/libweb";
|
} from "@lumeweb/libweb";
|
||||||
import { sendAuthUpdate } from "./util.js";
|
import { log, logErr, sendAuthUpdate } from "./util.js";
|
||||||
import { log, logErr } from "@lumeweb/libkernel/kernel";
|
|
||||||
import {
|
import {
|
||||||
defaultKernelLink,
|
defaultKernelLink,
|
||||||
setBootloaderPortals,
|
setBootloaderPortals,
|
||||||
setKernelLoaded,
|
setKernelLoaded,
|
||||||
setLoginComplete,
|
setLoginComplete,
|
||||||
|
setUserKey,
|
||||||
} from "./vars.js";
|
} from "./vars.js";
|
||||||
|
import { getStoredUserKey } from "./storage.js";
|
||||||
|
|
||||||
export function boot() {
|
export function boot() {
|
||||||
let userKeyString = window.localStorage.getItem("key");
|
let userKey;
|
||||||
if (userKeyString === null) {
|
|
||||||
sendAuthUpdate();
|
try {
|
||||||
return;
|
userKey = getStoredUserKey();
|
||||||
}
|
} catch (e) {
|
||||||
let [decodedSeed, errHTB] = hexToBuf(userKeyString);
|
logErr(addContextToErr(e, "user key could not be fetched"));
|
||||||
if (errHTB !== null) {
|
|
||||||
logErr(addContextToErr(errHTB, "seed could not be decoded from hex"));
|
|
||||||
sendAuthUpdate();
|
sendAuthUpdate();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
log("user is already logged in, attempting to load kernel");
|
log("user is already logged in, attempting to load kernel");
|
||||||
|
setUserKey(userKey as Uint8Array);
|
||||||
setLoginComplete(true);
|
setLoginComplete(true);
|
||||||
sendAuthUpdate();
|
sendAuthUpdate();
|
||||||
loadKernel();
|
loadKernel();
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { addContextToErr, hexToBuf } from "@lumeweb/libweb";
|
import { addContextToErr, bytesToHex, hexToBytes } from "@lumeweb/libweb";
|
||||||
import {
|
import {
|
||||||
getLoginComplete,
|
getLoginComplete,
|
||||||
getLogoutComplete,
|
getLogoutComplete,
|
||||||
|
@ -23,18 +23,22 @@ function handleStorage(event: StorageEvent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.key === "key" && !getLoginComplete()) {
|
if (event.key === "key" && !getLoginComplete()) {
|
||||||
let userKey = window.localStorage.getItem("key");
|
let userKey;
|
||||||
|
|
||||||
|
try {
|
||||||
|
userKey = getStoredUserKey();
|
||||||
|
} catch (e) {
|
||||||
|
logErr(addContextToErr(e, "user key could not be fetched"));
|
||||||
|
sendAuthUpdate();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (userKey === null) {
|
if (userKey === null) {
|
||||||
sendAuthUpdate();
|
sendAuthUpdate();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let [decodedKey, errHTB] = hexToBuf(userKey);
|
|
||||||
if (errHTB !== null) {
|
setUserKey(userKey);
|
||||||
logErr(addContextToErr(errHTB, "seed could not be decoded from hex"));
|
|
||||||
sendAuthUpdate();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setUserKey(decodedKey);
|
|
||||||
|
|
||||||
log("user is now logged in, attempting to load kernel");
|
log("user is now logged in, attempting to load kernel");
|
||||||
setLoginComplete(true);
|
setLoginComplete(true);
|
||||||
|
@ -47,5 +51,22 @@ function handleStorage(event: StorageEvent) {
|
||||||
log("attempting to do a page reload");
|
log("attempting to do a page reload");
|
||||||
reloadKernel();
|
reloadKernel();
|
||||||
}
|
}
|
||||||
|
window.addEventListener("storage", handleStorage);
|
||||||
|
|
||||||
window.addEventListener("storage", (event) => handleStorage(event));
|
export function saveUserKey(key: Uint8Array) {
|
||||||
|
window.localStorage.setItem("key", bytesToHex(key));
|
||||||
|
const event = new StorageEvent("storage", {
|
||||||
|
key: "key",
|
||||||
|
});
|
||||||
|
window.dispatchEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getStoredUserKey() {
|
||||||
|
const key = window.localStorage.getItem("key");
|
||||||
|
|
||||||
|
if (key) {
|
||||||
|
return hexToBytes(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
Reference in New Issue