2023-05-23 00:11:48 +00:00
|
|
|
import { getUsedResources, requestPVE } from "./pve.js";
|
2023-05-24 22:21:00 +00:00
|
|
|
import { db } from "./db.js";
|
2023-04-24 21:28:58 +00:00
|
|
|
|
2023-05-23 00:11:48 +00:00
|
|
|
export async function checkAuth(cookies, res, vmpath = null) {
|
|
|
|
let auth = false;
|
|
|
|
|
2023-05-24 22:21:00 +00:00
|
|
|
if (db.getUserConfig(cookies.username) === null) {
|
2023-05-23 00:11:48 +00:00
|
|
|
auth = false;
|
|
|
|
res.status(401).send({ auth: auth, path: vmpath ? `${vmpath}/config` : "/version", error: `user ${cookies.username} not found in localdb` });
|
|
|
|
res.end();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vmpath) {
|
|
|
|
let result = await requestPVE(`/${vmpath}/config`, "GET", cookies);
|
|
|
|
auth = result.status === 200;
|
|
|
|
}
|
|
|
|
else { // if no path is specified, then do a simple authentication
|
|
|
|
let result = await requestPVE("/version", "GET", cookies);
|
|
|
|
auth = result.status === 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!auth) {
|
|
|
|
res.status(401).send({ auth: auth, path: vmpath ? `${vmpath}/config` : "/version", error: `user token did not pass authentication check` });
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
return auth;
|
|
|
|
}
|
|
|
|
|
2023-05-24 22:21:00 +00:00
|
|
|
export async function getUserResources (req, username) {
|
|
|
|
let dbResources = db.getResourceConfig();
|
2023-04-24 21:28:58 +00:00
|
|
|
let used = await getUsedResources(req, dbResources);
|
2023-05-24 22:21:00 +00:00
|
|
|
let max = db.getUserConfig(username).resources.max;
|
2023-04-26 01:18:05 +00:00
|
|
|
let avail = {};
|
2023-04-24 21:28:58 +00:00
|
|
|
Object.keys(max).forEach((k) => {
|
|
|
|
avail[k] = max[k] - used[k];
|
|
|
|
});
|
2023-05-17 21:38:11 +00:00
|
|
|
return { used: used, max: max, avail: avail, units: dbResources };
|
2023-04-24 21:28:58 +00:00
|
|
|
}
|
|
|
|
|
2023-05-17 21:38:11 +00:00
|
|
|
export async function approveResources(req, username, request) {
|
2023-05-24 22:21:00 +00:00
|
|
|
let avail = (await getUserResources(req, username)).avail;
|
2023-05-24 23:10:05 +00:00
|
|
|
let approved = true;
|
2023-04-24 21:28:58 +00:00
|
|
|
Object.keys(request).forEach((key) => {
|
2023-05-24 22:21:00 +00:00
|
|
|
if (!(key in avail)) { // if requested resource is not in avail, block
|
2023-05-24 23:10:05 +00:00
|
|
|
approved = false;
|
2023-04-24 21:28:58 +00:00
|
|
|
}
|
2023-05-24 22:21:00 +00:00
|
|
|
else if (isNaN(avail[key]) || isNaN(request[key])) { // if either the requested or avail resource is NaN, block
|
2023-05-24 23:10:05 +00:00
|
|
|
approved = false;
|
2023-04-24 21:28:58 +00:00
|
|
|
}
|
2023-05-24 22:21:00 +00:00
|
|
|
else if (avail[key] - request[key] < 0) { // if the avail resources is less than the requested resources, block
|
2023-05-24 23:10:05 +00:00
|
|
|
approved = false;
|
2023-05-18 14:58:44 +00:00
|
|
|
}
|
2023-04-24 21:28:58 +00:00
|
|
|
});
|
2023-05-24 23:10:05 +00:00
|
|
|
return approved; // if all requested resources pass, allow
|
2023-04-26 01:18:05 +00:00
|
|
|
}
|