datasquirel/users/validate-token.js

77 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2023-09-21 14:00:04 +00:00
// @ts-check
const http = require("http");
2024-12-06 10:31:24 +00:00
const decrypt = require("../package-shared/functions/dsql/decrypt");
2023-09-21 14:00:04 +00:00
/**
* Validate Token
2024-12-06 11:55:03 +00:00
* ======================================
2023-09-21 14:00:04 +00:00
* @description This Function takes in a encrypted token and returns a user object
*
* @param {Object} params - Arg
* @param {string} params.token - Encrypted Token
* @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-10-19 16:45:42 +00:00
* @returns { import("../package-shared/types").DATASQUIREL_LoggedInUser | null}
2023-09-21 14:00:04 +00:00
*/
function validateToken({ token, encryptionKey, encryptionSalt }) {
try {
/**
* Grab the payload
*
* @description Grab the payload
*/
const key = token;
/**
* Grab the payload
*
* @description Grab the payload
*/
let userPayload = decrypt({
encryptedString: key,
encryptionKey,
encryptionSalt,
});
/**
* Grab the payload
*
* @description Grab the payload
*/
if (!userPayload) {
return null;
}
/**
* Grab the payload
*
* @description Grab the payload
*/
let userObject = JSON.parse(userPayload);
if (!userObject.csrf_k) {
return null;
}
/**
* Return User Object
*
* @description Return User Object
*/
return userObject;
} catch (error) {
/**
* Return User Object
*
* @description Return User Object
*/
return null;
}
}
module.exports = validateToken;