This commit is contained in:
Tben
2023-05-06 12:14:09 +01:00
parent 47f6e8331e
commit f8633b839e
7 changed files with 418 additions and 0 deletions
+88
View File
@@ -0,0 +1,88 @@
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const https = require("https");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* ==============================================================================
* Main Function
* ==============================================================================
* @param {String} key - API Key
* @param {String} database - Target Database
* @param {String | Object} payload - SQL query String or Request Object. Eg. {
action: "insert | update | delete",
data: {
user_id: user.id,
user_first_name: user.first_name,
user_last_name: user.last_name,
},
table: "posts",
}
*/
module.exports = async function ({ key, payload, database }) {
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
https
.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/add-user`,
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("error", (err) => {
reject(err);
});
}
)
.write({
payload,
database,
})
.end();
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
};
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
+143
View File
@@ -0,0 +1,143 @@
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const https = require("https");
const encrypt = require("../functions/encrypt");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* ==============================================================================
* Main Function
* ==============================================================================
* @param {String} key - API Key
* @param {String} database - Target Database
* @param {Object} payload - SQL query String or Request Object. Eg. {
action: "insert | update | delete",
data: {
user_id: user.id,
user_first_name: user.first_name,
user_last_name: user.last_name,
},
table: "posts",
}
* @param {Object} response - Http response object
* @param {String} encryptionKey - Encryption Key
* @param {String} encryptionSalt - Encryption Salt
*/
module.exports = async function ({ key, payload, database, response, encryptionKey, encryptionSalt }) {
/**
* Check Encryption Keys
*
* @description Check Encryption Keys
*/
if (!encryptionKey?.match(/./))
return {
success: false,
payload: null,
msg: "Encryption Key Required",
};
if (!encryptionSalt?.match(/./))
return {
success: false,
payload: null,
msg: "Encryption Salt Required",
};
if (encryptionKey.length < 24)
return {
success: false,
payload: null,
msg: "Encryption Key must be at least 24 characters",
};
if (encryptionSalt.length < 8)
return {
success: false,
payload: null,
msg: "Encryption Salt must be at least 8 characters",
};
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
https
.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/login-user`,
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("error", (err) => {
reject(err);
});
}
)
.write({
payload,
database,
})
.end();
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
if (httpResponse?.success) {
let encryptedPayload = encrypt({
data: JSON.stringify(httpResponse.payload),
encryptionKey,
encryptionSalt,
});
response.setHeader("Set-Cookie", [`datasquirelAuthKey=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, `csrf=${httpResponse.csrf};samesite=strict;path=/;HttpOnly=true`]);
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
};
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
+40
View File
@@ -0,0 +1,40 @@
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* ==============================================================================
* Main Function
* ==============================================================================
* @param {Object} response - Http response object
*/
module.exports = async function ({ response }) {
/**
* Check Encryption Keys
*
* @description Check Encryption Keys
*/
response.setHeader("Set-Cookie", ["datasquirelAuthKey=none;max-age=0", "usertype=none;max-age=0", `refresh_properties=1;Max-Age=7000`]);
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return {
success: true,
payload: "User Logged Out",
};
};
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
+97
View File
@@ -0,0 +1,97 @@
/**
* ==============================================================================
* 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 = async function ({ request, encryptionKey, encryptionSalt }) {
/**
* 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 (csrf && !req.headers["x-csrf-auth"]?.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,
};
};
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */