add return values to backend docstring,

fix return values of all backends
This commit is contained in:
Arthur Lu 2024-06-04 23:09:55 +00:00
parent b12f38e608
commit 01f55aa0cb
6 changed files with 64 additions and 83 deletions

View File

@ -30,12 +30,13 @@ class BACKEND {
* Opens a session with the backend and creates session tokens if needed * Opens a session with the backend and creates session tokens if needed
* @param {{id: string, realm: string}} user object containing username and password fields * @param {{id: string, realm: string}} user object containing username and password fields
* @param {string} password * @param {string} password
* @returns {{ok: boolean, status: number, cookies: {name: string, value: string}[]}} response like object with list of session token objects with token name and value * @returns {{ok: boolean, status: number, message: string, cookies: {name: string, value: string}[]}} response like object with list of session token objects with token name and value
*/ */
openSession (user, password) { openSession (user, password) {
return { return {
ok: true, ok: true,
status: 200, status: 200,
message: "",
cookies: [] cookies: []
}; };
} }
@ -63,6 +64,7 @@ class USER_BACKEND extends BACKEND {
* @param {{id: string, realm: string}} user * @param {{id: string, realm: string}} user
* @param {Object} attributes user attributes * @param {Object} attributes user attributes
* @param {Object} params authentication params, usually req.cookies * @param {Object} params authentication params, usually req.cookies
* @returns {{ok: boolean, status: number, message: string}} error object or null
*/ */
addUser (user, attributes, params = null) {} addUser (user, attributes, params = null) {}
@ -70,6 +72,7 @@ class USER_BACKEND extends BACKEND {
* Get user from backend * Get user from backend
* @param {{id: string, realm: string}} user * @param {{id: string, realm: string}} user
* @param {Object} params authentication params, usually req.cookies * @param {Object} params authentication params, usually req.cookies
* @returns {Object} containing user data from this backend, null if user does not exist
*/ */
getUser (user, params = null) {} getUser (user, params = null) {}
@ -78,6 +81,7 @@ class USER_BACKEND extends BACKEND {
* @param {{id: string, realm: string}} user * @param {{id: string, realm: string}} user
* @param {Object} attributes new user attributes to modify * @param {Object} attributes new user attributes to modify
* @param {Object} params authentication params, usually req.cookies * @param {Object} params authentication params, usually req.cookies
* @returns {{ok: boolean, status: number, message: string}} error object or null
*/ */
setUser (user, attributes, params = null) {} setUser (user, attributes, params = null) {}
@ -85,6 +89,7 @@ class USER_BACKEND extends BACKEND {
* Delete user from backend * Delete user from backend
* @param {{id: string, realm: string}} user * @param {{id: string, realm: string}} user
* @param {Object} params authentication params, usually req.cookies * @param {Object} params authentication params, usually req.cookies
* @returns {{ok: boolean, status: number, message: string}} error object or null
*/ */
deluser (user, params = null) {} deluser (user, params = null) {}
@ -93,6 +98,7 @@ class USER_BACKEND extends BACKEND {
* @param {{id: string}} group * @param {{id: string}} group
* @param {Object} attributes group attributes * @param {Object} attributes group attributes
* @param {Object} params authentication params, usually req.cookies * @param {Object} params authentication params, usually req.cookies
* @returns {{ok: boolean, status: number, message: string}} error object or null
*/ */
addGroup (group, attributes, params = null) {} addGroup (group, attributes, params = null) {}
@ -100,6 +106,7 @@ class USER_BACKEND extends BACKEND {
* Get group from backend * Get group from backend
* @param {{id: string}} group * @param {{id: string}} group
* @param {Object} params authentication params, usually req.cookies * @param {Object} params authentication params, usually req.cookies
* @returns {Object} containing group data from this backend, null if user does not exist
*/ */
getGroup (group, params = null) {} getGroup (group, params = null) {}
@ -108,6 +115,7 @@ class USER_BACKEND extends BACKEND {
* @param {{id: string}} group * @param {{id: string}} group
* @param {Object} attributes new group attributes to modify * @param {Object} attributes new group attributes to modify
* @param {Object} params authentication params, usually req.cookies * @param {Object} params authentication params, usually req.cookies
* @returns {{ok: boolean, status: number, message: string}} error object or null
*/ */
setGroup (group, attributes, params = null) {} setGroup (group, attributes, params = null) {}
@ -115,6 +123,7 @@ class USER_BACKEND extends BACKEND {
* Delete group from backend * Delete group from backend
* @param {{id: string}} group * @param {{id: string}} group
* @param {Object} params authentication params, usually req.cookies * @param {Object} params authentication params, usually req.cookies
* @returns {{ok: boolean, status: number, message: string}} error object or null
*/ */
delGroup (group, params = null) {} delGroup (group, params = null) {}
@ -123,6 +132,7 @@ class USER_BACKEND extends BACKEND {
* @param {{id: string, realm: string}} user * @param {{id: string, realm: string}} user
* @param {{id: string}} group * @param {{id: string}} group
* @param {Object} params authentication params, usually req.cookies * @param {Object} params authentication params, usually req.cookies
* @returns {{ok: boolean, status: number, message: string}} error object or null
*/ */
addUserToGroup (user, group, params = null) {} addUserToGroup (user, group, params = null) {}
@ -131,6 +141,7 @@ class USER_BACKEND extends BACKEND {
* @param {{id: string, realm: string}} user * @param {{id: string, realm: string}} user
* @param {{id: string}} group * @param {{id: string}} group
* @param {Object} params authentication params, usually req.cookies * @param {Object} params authentication params, usually req.cookies
* @returns {{ok: boolean, status: number, message: string}} error object or null
*/ */
delUserFromGroup (user, group, params = null) {} delUserFromGroup (user, group, params = null) {}
} }
@ -153,7 +164,6 @@ export class AUTH_BACKEND extends USER_BACKEND {}
/** /**
* Interface combining all user backends into a single interface * Interface combining all user backends into a single interface
* Calling methods will also call sub handler methods * Calling methods will also call sub handler methods
* Also handles refreshing proxmox handler
*/ */
class USER_BACKEND_MANAGER extends USER_BACKEND { class USER_BACKEND_MANAGER extends USER_BACKEND {
#config = null; #config = null;
@ -167,23 +177,12 @@ class USER_BACKEND_MANAGER extends USER_BACKEND {
return this.#config[user.realm]; return this.#config[user.realm];
} }
/**
* Add user to backend
* @param {{id: string, realm: string}} user
* @param {Object} attributes user attributes
* @param {Object} params authentication params, usually req.cookies
*/
addUser (user, attributes, params = null) {} addUser (user, attributes, params = null) {}
/**
* Get user from backend
* @param {{id: string, realm: string}} user
* @param {Object} params authentication params, usually req.cookies
*/
async getUser (user, params = null) { async getUser (user, params = null) {
let userData = {}; let userData = {};
for (const backend of this.#config[user.realm]) { for (const backend of this.#config[user.realm]) {
let backendData = await global.backends[backend].getUser(user, params) const backendData = await global.backends[backend].getUser(user, params);
if (backendData) { if (backendData) {
userData = { ...backendData, ...userData }; userData = { ...backendData, ...userData };
} }
@ -191,21 +190,14 @@ class USER_BACKEND_MANAGER extends USER_BACKEND {
return userData; return userData;
} }
/**
* Modify user in backend
* @param {{id: string, realm: string}} user
* @param {Object} attributes new user attributes to modify
* @param {Object} params authentication params, usually req.cookies
*/
async setUser (user, attributes, params = null) { async setUser (user, attributes, params = null) {
const results = { const results = {
ok: true, ok: true,
status: 200, status: 200,
log: [] message: ""
}; };
for (const backend of this.#config[user.realm]) { for (const backend of this.#config[user.realm]) {
const r = await global.backends[backend].setUser(user, attributes, params); const r = await global.backends[backend].setUser(user, attributes, params);
results.log.push(backend)
if (!r) { if (!r) {
results.ok = false; results.ok = false;
results.status = 500; results.status = 500;
@ -215,56 +207,17 @@ class USER_BACKEND_MANAGER extends USER_BACKEND {
return results; return results;
} }
/**
* Delete user from backend
* @param {{id: string, realm: string}} user
* @param {Object} params authentication params, usually req.cookies
*/
deluser (user, params = null) {} deluser (user, params = null) {}
/**
* Add group to backend
* @param {{id: string}} group
* @param {Object} attributes group attributes
* @param {Object} params authentication params, usually req.cookies
*/
addGroup (group, attributes, params = null) {} addGroup (group, attributes, params = null) {}
/**
* Get group from backend
* @param {{id: string}} group
* @param {Object} params authentication params, usually req.cookies
*/
getGroup (group, params = null) {} getGroup (group, params = null) {}
/**
* Modify group in backend
* @param {{id: string}} group
* @param {Object} attributes new group attributes to modify
* @param {Object} params authentication params, usually req.cookies
*/
setGroup (group, attributes, params = null) {} setGroup (group, attributes, params = null) {}
/**
* Delete group from backend
* @param {{id: string}} group
* @param {Object} params authentication params, usually req.cookies
*/
delGroup (group, params = null) {} delGroup (group, params = null) {}
/**
* Add user to group
* @param {{id: string, realm: string}} user
* @param {{id: string}} group
* @param {Object} params authentication params, usually req.cookies
*/
addUserToGroup (user, group, params = null) {} addUserToGroup (user, group, params = null) {}
/**
* Remove user from group
* @param {{id: string, realm: string}} user
* @param {{id: string}} group
* @param {Object} params authentication params, usually req.cookies
*/
delUserFromGroup (user, group, params = null) {} delUserFromGroup (user, group, params = null) {}
} }

View File

@ -37,9 +37,19 @@ export default class LocalDB extends DB_BACKEND {
addUser (user, attributes, params = null) { addUser (user, attributes, params = null) {
const username = `${user.id}@${user.realm}`; const username = `${user.id}@${user.realm}`;
attributes = attributes || this.#defaultuser; if (this.#data.users[username]) { // user already exists
this.#data.users[username] = attributes; return {
this.#save(); ok: false,
status: 1,
message: "User already exists"
};
}
else {
attributes = attributes || this.#defaultuser;
this.#data.users[username] = attributes;
this.#save();
return null;
}
} }
getUser (user, params = null) { getUser (user, params = null) {

View File

@ -15,7 +15,7 @@ export default class PAASLDAP extends AUTH_BACKEND {
* @param {*} path HTTP path, prepended with the paas-LDAP API base url * @param {*} path HTTP path, prepended with the paas-LDAP API base url
* @param {*} method HTTP method * @param {*} method HTTP method
* @param {*} body body parameters and data to be sent. Optional. * @param {*} body body parameters and data to be sent. Optional.
* @returns {Object} HTTP response object or HTTP error object. * @returns {Object} HTTP response object
*/ */
async #request (path, method, auth = null, body = null) { async #request (path, method, auth = null, body = null) {
const url = `${this.#url}${path}`; const url = `${this.#url}${path}`;
@ -39,12 +39,9 @@ export default class PAASLDAP extends AUTH_BACKEND {
return result; return result;
} }
catch (error) { catch (error) {
error.ok = false; const result = error.response;
error.status = 500; result.ok = result.status === 200;
error.data = { return result;
error: error.code
};
return error;
} }
} }
@ -60,25 +57,44 @@ export default class PAASLDAP extends AUTH_BACKEND {
return { return {
ok: true, ok: true,
status: result.status, status: result.status,
message: "",
cookies cookies
}; };
} }
else { else {
return result; return {
ok: false,
status: result.status,
message: result.data.error.message,
cookies: []
};
} }
} }
async addUser (user, attributes, params = null) { async addUser (user, attributes, params = null) {
return await this.#request(`/users/${user.id}`, "POST", params, attributes); const res = await this.#request(`/users/${user.id}`, "POST", params, attributes);
if (res.ok) { // if ok, return null
return null;
}
else { // if not ok, return error obj
return {
ok: res.ok,
status: res.status,
message: res.ok ? "" : res.data.error.message
};
}
} }
async getUser (user, params = null) { async getUser (user, params = null) {
const res = await this.#request(`/users/${user.id}`, "GET", params); if (!params) { // params required, do nothing if params are missing
if (res.ok) { return null;
return res.data;
} }
else { const res = await this.#request(`/users/${user.id}`, "GET", params);
return false; if (res.ok) { // if ok, return user data
return res.data.user;
}
else { // else return null
return null;
} }
} }

View File

@ -17,7 +17,12 @@ export default class PVE extends PVE_BACKEND {
const credentials = { username: `${user.id}@${user.realm}`, password }; const credentials = { username: `${user.id}@${user.realm}`, password };
const response = await global.pve.requestPVE("/access/ticket", "POST", null, credentials); const response = await global.pve.requestPVE("/access/ticket", "POST", null, credentials);
if (!(response.status === 200)) { if (!(response.status === 200)) {
return response; return {
ok: false,
status: response.status,
message: "Authorization failed",
cookies: []
};
} }
const ticket = response.data.data.ticket; const ticket = response.data.data.ticket;
const csrftoken = response.data.data.CSRFPreventionToken; const csrftoken = response.data.data.CSRFPreventionToken;

View File

@ -64,10 +64,6 @@ router.post("/ticket", async (req, res) => {
const userObj = global.utils.getUserObjFromUsername(params.username); const userObj = global.utils.getUserObjFromUsername(params.username);
let backends = global.userManager.getBackendsByUser(userObj); let backends = global.userManager.getBackendsByUser(userObj);
backends = backends.concat(["pve"]); backends = backends.concat(["pve"]);
// const backends = [global.pve, global.db];
// if (userRealm in global.auth) {
// backends.push(global.auth[userRealm]);
// }
const cm = new CookieFetcher(); const cm = new CookieFetcher();
const success = await cm.fetchBackends(backends, userObj, params.password); const success = await cm.fetchBackends(backends, userObj, params.password);
if (!success) { if (!success) {

View File

@ -37,6 +37,7 @@ export async function checkAuth (cookies, res, vmpath = null) {
res.status(401).send({ auth, path: vmpath ? `${vmpath}/config` : "/version", error: "User token did not pass authentication check." }); res.status(401).send({ auth, path: vmpath ? `${vmpath}/config` : "/version", error: "User token did not pass authentication check." });
res.end(); res.end();
} }
return auth; return auth;
} }