2023-09-21 14:00:04 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ==============================================================================
|
|
|
|
* Imports
|
|
|
|
* ==============================================================================
|
|
|
|
*/
|
|
|
|
const http = require("http");
|
|
|
|
const decrypt = require("../functions/decrypt");
|
|
|
|
const parseCookies = require("../utils/functions/parseCookies");
|
|
|
|
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate User from request
|
|
|
|
* ==============================================================================
|
|
|
|
* @description This Function takes in a request object and returns a user object
|
|
|
|
* with the user's data
|
|
|
|
*
|
|
|
|
* @param {Object} params - Arg
|
|
|
|
* @param {http.IncomingMessage} params.request - Http request object
|
|
|
|
* @param {string} params.encryptionKey - Encryption Key
|
|
|
|
* @param {string} params.encryptionSalt - Encryption Salt
|
|
|
|
* @param {("deep" | "normal")} [params.level] - Optional. "Deep" value indicates an extra layer of security
|
|
|
|
* @param {string} params.database - Database Name
|
2024-08-16 06:48:12 +00:00
|
|
|
* @param {string} [params.token] - access token to use instead of getting from cookie header
|
2023-09-21 14:00:04 +00:00
|
|
|
*
|
2024-10-18 05:45:25 +00:00
|
|
|
* @returns { import("@/package-shared/types").AuthenticatedUser }
|
2023-09-21 14:00:04 +00:00
|
|
|
*/
|
2024-08-16 06:48:12 +00:00
|
|
|
function userAuth({
|
|
|
|
request,
|
|
|
|
encryptionKey,
|
|
|
|
encryptionSalt,
|
|
|
|
level,
|
|
|
|
database,
|
|
|
|
token,
|
|
|
|
}) {
|
2023-09-21 14:00:04 +00:00
|
|
|
try {
|
|
|
|
/**
|
|
|
|
* Grab the payload
|
|
|
|
*
|
|
|
|
* @description Grab the payload
|
|
|
|
*/
|
|
|
|
const cookies = parseCookies({ request });
|
|
|
|
const dsqluid = cookies.dsqluid;
|
|
|
|
const authKeyName = `datasquirel_${dsqluid}_${database}_auth_key`;
|
|
|
|
const csrfName = `datasquirel_${dsqluid}_${database}_csrf`;
|
|
|
|
|
2024-08-16 06:48:12 +00:00
|
|
|
const key = token ? token : cookies[authKeyName];
|
2023-09-21 14:00:04 +00:00
|
|
|
const csrf = cookies[csrfName];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grab the payload
|
|
|
|
*
|
|
|
|
* @description Grab the payload
|
|
|
|
*/
|
|
|
|
let userPayload = decrypt({
|
|
|
|
encryptedString: key,
|
|
|
|
encryptionKey,
|
|
|
|
encryptionSalt,
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grab the payload
|
|
|
|
*
|
|
|
|
* @description Grab the payload
|
|
|
|
*/
|
|
|
|
if (!userPayload) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
payload: null,
|
|
|
|
msg: "Couldn't Decrypt cookie",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grab the payload
|
|
|
|
*
|
|
|
|
* @description Grab the payload
|
|
|
|
*/
|
|
|
|
let userObject = JSON.parse(userPayload);
|
|
|
|
|
|
|
|
if (!userObject.csrf_k) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
payload: null,
|
|
|
|
msg: "No CSRF_K in decrypted payload",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grab the payload
|
|
|
|
*
|
|
|
|
* @description Grab the payload
|
|
|
|
*/
|
2024-08-16 06:48:12 +00:00
|
|
|
if (
|
|
|
|
level?.match(/deep/i) &&
|
|
|
|
!csrf?.match(new RegExp(`${userObject.csrf_k}`))
|
|
|
|
) {
|
2023-09-21 14:00:04 +00:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
payload: null,
|
|
|
|
msg: "CSRF_K requested but does not match payload",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return User Object
|
|
|
|
*
|
|
|
|
* @description Return User Object
|
|
|
|
*/
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
payload: userObject,
|
|
|
|
};
|
2024-10-11 09:15:29 +00:00
|
|
|
} catch (/** @type {any} */ error) {
|
2023-09-21 14:00:04 +00:00
|
|
|
/**
|
|
|
|
* Return User Object
|
|
|
|
*
|
|
|
|
* @description Return User Object
|
|
|
|
*/
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
payload: null,
|
2024-10-11 09:15:29 +00:00
|
|
|
msg: error.message,
|
2023-09-21 14:00:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
|
|
|
|
module.exports = userAuth;
|