From 139bc324e832a6161bea1c45df3a36317c2ad71c Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Tue, 14 Feb 2023 05:21:45 +0000 Subject: [PATCH] fix user name by adding username cookie --- scripts/elements.js | 4 ++-- scripts/login.js | 2 +- scripts/utils.js | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/scripts/elements.js b/scripts/elements.js index 879488a..e49dd4f 100644 --- a/scripts/elements.js +++ b/scripts/elements.js @@ -1,4 +1,4 @@ -import {requestPVE, goToPage, instances} from "./utils.js"; +import {requestPVE, goToPage, instances, getCookie} from "./utils.js"; export class Instance extends HTMLElement { constructor () { @@ -131,7 +131,7 @@ export class Instance extends HTMLElement { handleConsoleButton () { if (this.status === "running") { - goToPage("pve-xtermjs/index.html", {type: this.type, vmid: this.vmid, name: this.name, node: this.node.name, user: "alu@ldap", url: "pve.tronnet.net/api2/json"}); + goToPage("pve-xtermjs/index.html", {type: this.type, vmid: this.vmid, name: this.name, node: this.node.name, user: getCookie("username"), url: "pve.tronnet.net/api2/json"}); } } } diff --git a/scripts/login.js b/scripts/login.js index 51ffeb3..1957f49 100644 --- a/scripts/login.js +++ b/scripts/login.js @@ -14,7 +14,7 @@ function init (){ try { status.innerText = "Authenticating..."; let ticket = await requestTicket(formData.get("username"), formData.get("password")); - setTicket(ticket.data.ticket, ticket.data.CSRFPreventionToken); + setTicket(ticket.data.ticket, ticket.data.CSRFPreventionToken, formData.get("username")); status.innerText = "Authentication successful!" status.style.color = "#00ff00"; goToPage("index.html"); diff --git a/scripts/utils.js b/scripts/utils.js index 34f779f..53d27ba 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -59,7 +59,7 @@ export const instances = { } } -function getCookie(cname) { +export function getCookie(cname) { let name = cname + "="; let decodedCookie = decodeURIComponent(document.cookie); let ca = decodedCookie.split(";"); @@ -81,11 +81,12 @@ export async function requestTicket (username, password) { return response; } -export function setTicket (ticket, csrf) { +export function setTicket (ticket, csrf, username) { let d = new Date(); d.setTime(d.getTime() + (2*60*60*1000)); document.cookie = `PVEAuthCookie=${ticket}; path=/; expires=${d.toUTCString()}; domain=.tronnet.net`; document.cookie = `CSRFPreventionToken=${csrf}; path=/; expires=${d.toUTCString()}; domain=.tronnet.net;` + document.cookie = `username=${username}@ldap; path=/; expires=${d.toUTCString()}; domain=.tronnet.net;` } export async function requestPVE (path, method, body = null) {