datasquirel/users/user-auth.js
2023-05-06 14:29:42 +01:00

118 lines
3.5 KiB
JavaScript

/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const decrypt = require("../functions/decrypt");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* ==============================================================================
* Main Function
* ==============================================================================
* @param {Object} request - Http request object
* @param {String} encryptionKey - Encryption Key
* @param {String} encryptionSalt - Encryption Salt
*/
module.exports = function ({ request, encryptionKey, encryptionSalt, level }) {
try {
/**
* Grab the payload
*
* @description Grab the payload
*/
const csrf = request.cookies.csrf;
/**
* Grab the payload
*
* @description Grab the payload
*/
let userPayload = decrypt({
encryptedString: request.cookies.datasquirelAuthKey,
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
*/
if (level?.match(/deep/i) && !csrf?.match(new RegExp(`${userObject.csrf_k}`))) {
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,
};
} catch (error) {
/**
* Return User Object
*
* @description Return User Object
*/
return {
success: false,
payload: null,
msg: error.message,
};
}
};
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */