basic auth / session stuff working

This commit is contained in:
Samuel Lorch 2023-03-11 00:18:16 +01:00
parent b7daba6040
commit b80bc4d32f
6 changed files with 144 additions and 23 deletions

View file

@ -3,7 +3,7 @@ import IDashboard from '~icons/ri/dashboard-2-line';
import IRule from '~icons/material-symbols/rule-folder-outline-sharp';
import IAddress from '~icons/eos-icons/ip';
import { authenticate, checkAuthentication, setup } from "./api";
import { authenticate, logout, checkAuthentication, setup } from "./api";
enum NavState { Open, Reduced, Collapsed };
const NavStateCount = 3;
@ -31,17 +31,18 @@ async function tryLogin() {
console.info("authentication error");
} else {
// TODO Check for MFA here
authState = 1;
authState = AuthState.Authenticated;
}
}
async function tryLogout() {
authState = 0;
logout();
authState = AuthState.Unauthenticated;
}
function deAuthenticatedCallback() {
console.info("Unauthenticated");
authState = 0;
authState = AuthState.Unauthenticated;
}
onMounted(async() => {
@ -49,7 +50,7 @@ onMounted(async() => {
let res = await checkAuthentication();
authState = res.auth;
loginDisabled = false;
if (authState > 0) {
if (authState === AuthState.Authenticated) {
console.info("Already Authenticated ", authState);
}
else console.info("Check Authentication error",res.error);

View file

@ -1,7 +1,8 @@
import { RequestManager, HTTPTransport, WebSocketTransport, Client } from "@open-rpc/client-js";
import axios from "axios";
const httpTransport = new HTTPTransport("http://"+ window.location.host +"/api");
const socktransport = new WebSocketTransport("ws://"+ window.location.host + "/ws/api");
const manager = new RequestManager([socktransport, httpTransport], () => crypto.randomUUID());
// const socktransport = new WebSocketTransport("ws://"+ window.location.host + "/ws/api");
const manager = new RequestManager([httpTransport], () => crypto.randomUUID());
const client = new Client(manager);
let deAuthenticatedCallback;
@ -30,23 +31,31 @@ export async function authenticate(username: string, password: string): Promise<
}
}
export async function logout(): Promise<any> {
const pResponse = axios.post("/logout", null, {timeout: 10100});
try {
const response = await pResponse;
return { data: response.data, error: null};
} catch (error) {
return { data: null, error: error};
}
}
export async function checkAuthentication() {
const res = await apiCall("session-check", {});
if (res.error == "HTTP: Your Session cookie is invalid") return {auth: 0, error: null};
if (res.error == "HTTP: Your Session Requires TFA") return {auth: 1, error: null};
else if (res.error) return {auth: 0, error: res.error};
else {
/* TODO add commit_hash storing
const pResponse = axios.post("/session", null, {timeout: 10100});
try {
const response = await pResponse;
const last_hash = window.localStorage.getItem("commit_hash");
if (last_hash) {
if (last_hash !== res.data.commit_hash) {
if (last_hash !== response.data.commit_hash) {
console.log("Detected New Backend Version, Reloading...");
window.localStorage.removeItem("commit_hash");
window.location.reload(true);
window.location.reload();
}
} else window.localStorage.setItem("commit_hash", res.data.commit_hash);
*/
} else window.localStorage.setItem("commit_hash", response.data.commit_hash);
return {auth: 2, error: null};
} catch (error) {
return {auth: 0, error: error};
}
}