156 lines
5.3 KiB
JavaScript
156 lines
5.3 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.default = userAuth;
|
|
const decrypt_1 = __importDefault(require("../../functions/dsql/decrypt"));
|
|
const get_auth_cookie_names_1 = __importDefault(require("../../functions/backend/cookies/get-auth-cookie-names"));
|
|
const write_auth_files_1 = require("../../functions/backend/auth/write-auth-files");
|
|
const parseCookies_1 = __importDefault(require("../../utils/backend/parseCookies"));
|
|
const get_csrf_header_name_1 = __importDefault(require("../../actions/get-csrf-header-name"));
|
|
const grab_host_names_1 = __importDefault(require("../../utils/grab-host-names"));
|
|
const minuteInMilliseconds = 60000;
|
|
const hourInMilliseconds = minuteInMilliseconds * 60;
|
|
const dayInMilliseconds = hourInMilliseconds * 24;
|
|
const weekInMilliseconds = dayInMilliseconds * 7;
|
|
const monthInMilliseconds = dayInMilliseconds * 30;
|
|
const yearInMilliseconds = dayInMilliseconds * 365;
|
|
/**
|
|
* Authenticate User from request
|
|
* ==============================================================================
|
|
* @description This Function takes in a request object and returns a user object
|
|
* with the user's data
|
|
*/
|
|
function userAuth({ request, req, encryptionKey, encryptionSalt, level, database, dsqlUserId, encryptedUserString, expiry = weekInMilliseconds, cookieString, csrfHeaderName, debug, }) {
|
|
try {
|
|
const finalRequest = req || request;
|
|
const { user_id } = (0, grab_host_names_1.default)({ userId: dsqlUserId });
|
|
const cookies = (0, parseCookies_1.default)({
|
|
request: finalRequest,
|
|
cookieString,
|
|
});
|
|
if (debug) {
|
|
console.log("userAuth:cookies:", cookies);
|
|
}
|
|
const keyNames = (0, get_auth_cookie_names_1.default)({
|
|
userId: user_id,
|
|
database: database || process.env.DSQL_DB_NAME,
|
|
});
|
|
if (debug) {
|
|
console.log("userAuth:keyNames:", keyNames);
|
|
}
|
|
const key = encryptedUserString
|
|
? encryptedUserString
|
|
: cookies[keyNames.keyCookieName];
|
|
if (debug) {
|
|
console.log("userAuth:key:", key);
|
|
}
|
|
/**
|
|
* Grab the payload
|
|
*
|
|
* @description Grab the payload
|
|
*/
|
|
let userPayloadJSON = (0, decrypt_1.default)({
|
|
encryptedString: key,
|
|
encryptionKey,
|
|
encryptionSalt,
|
|
});
|
|
if (debug) {
|
|
console.log("userAuth:userPayloadJSON:", userPayloadJSON);
|
|
}
|
|
/**
|
|
* Grab the payload
|
|
*
|
|
* @description Grab the payload
|
|
*/
|
|
if (!userPayloadJSON) {
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "Couldn't Decrypt cookie",
|
|
};
|
|
}
|
|
/**
|
|
* Grab the payload
|
|
*
|
|
* @description Grab the payload
|
|
*/
|
|
/** @type {import("../../types").DATASQUIREL_LoggedInUser} */
|
|
let userObject = JSON.parse(userPayloadJSON);
|
|
if (debug) {
|
|
console.log("userAuth:userObject:", userObject);
|
|
}
|
|
if (!userObject.csrf_k) {
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "No CSRF_K in decrypted payload",
|
|
};
|
|
}
|
|
if (!(0, write_auth_files_1.checkAuthFile)(userObject.csrf_k)) {
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "Auth file doesn't exist",
|
|
};
|
|
}
|
|
/**
|
|
* Grab the payload
|
|
*
|
|
* @description Grab the payload
|
|
*/
|
|
if ((level === null || level === void 0 ? void 0 : level.match(/deep/i)) && finalRequest) {
|
|
const finalCsrfHeaderName = csrfHeaderName || (0, get_csrf_header_name_1.default)();
|
|
if (finalRequest.headers[finalCsrfHeaderName] !== userObject.csrf_k) {
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "CSRF_K mismatch",
|
|
};
|
|
}
|
|
}
|
|
const payloadCreationDate = Number(userObject.date);
|
|
if (Number.isNaN(payloadCreationDate) ||
|
|
typeof payloadCreationDate !== "number") {
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "Payload Creation Date is not a number",
|
|
};
|
|
}
|
|
const timeElapsed = Date.now() - payloadCreationDate;
|
|
const finalExpiry = process.env.DSQL_SESSION_EXPIRY_TIME
|
|
? Number(process.env.DSQL_SESSION_EXPIRY_TIME)
|
|
: expiry;
|
|
if (timeElapsed > finalExpiry) {
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "Session has expired",
|
|
};
|
|
}
|
|
/**
|
|
* 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,
|
|
};
|
|
}
|
|
}
|