This commit is contained in:
Tben 2023-08-12 17:35:59 +01:00
parent f0feaf508b
commit b5a4de3e8f
7 changed files with 34 additions and 8 deletions

View File

@ -24,6 +24,9 @@ module.exports = async function parseDbResults({ unparsedResults, tableSchema })
*/
let parsedResults = [];
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || "";
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
try {
/**
* Declare variables
@ -52,7 +55,7 @@ module.exports = async function parseDbResults({ unparsedResults, tableSchema })
if (resultFieldSchema?.encrypted) {
if (value?.match(/./)) {
result[resultFieldName] = decrypt(value);
result[resultFieldName] = decrypt({ encryptedString: value, encryptionKey, encryptionSalt });
}
}
}

View File

@ -26,8 +26,8 @@ const updateDbEntry = require("./updateDbEntry");
* @param {string} [params.duplicateColumnName] - Duplicate column name
* @param {string} [params.duplicateColumnValue] - Duplicate column value
* @param {boolean} [params.update] - Update this row if it exists
* @param {string} [params.encryptionKey] - Update this row if it exists
* @param {string} [params.encryptionSalt] - Update this row if it exists
* @param {string} params.encryptionKey - Update this row if it exists
* @param {string} params.encryptionSalt - Update this row if it exists
*
* @returns {Promise<object|null>}
*/
@ -63,6 +63,8 @@ async function addDbEntry({ dbContext, paradigm, dbFullName, tableName, data, ta
tableSchema,
identifierColumnName: duplicateColumnName,
identifierValue: duplicateColumnValue || "",
encryptionKey,
encryptionSalt,
});
}
}

View File

@ -33,6 +33,9 @@ async function runQuery({ dbFullName, query, readOnly, dbSchema, queryValuesArra
*
* @description Declare "results" variable
*/
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || "";
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
let result, error, tableSchema;
if (dbSchema) {
@ -78,6 +81,8 @@ async function runQuery({ dbFullName, query, readOnly, dbSchema, queryValuesArra
duplicateColumnName,
duplicateColumnValue,
tableSchema,
encryptionKey,
encryptionSalt,
});
if (!result?.insertId) {
@ -96,6 +101,8 @@ async function runQuery({ dbFullName, query, readOnly, dbSchema, queryValuesArra
identifierColumnName,
identifierValue,
tableSchema,
encryptionKey,
encryptionSalt,
});
break;

View File

@ -24,10 +24,12 @@ const dbHandler = require("../../engine/utils/dbHandler");
* @param {import("../../../types/database-schema.td").DSQL_TableSchemaType} [params.tableSchema] - Table schema
* @param {string} params.identifierColumnName - Update row identifier column name
* @param {string | number} params.identifierValue - Update row identifier column value
* @param {string} params.encryptionKey - Encryption key
* @param {string} params.encryptionSalt - Encryption salt
*
* @returns {Promise<object|null>}
*/
async function updateDbEntry({ dbContext, paradigm, dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue }) {
async function updateDbEntry({ dbContext, paradigm, dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue, encryptionKey, encryptionSalt }) {
/**
* Check if data is valid
*/
@ -47,9 +49,6 @@ async function updateDbEntry({ dbContext, paradigm, dbFullName, tableName, data,
let updateKeyValueArray = [];
let updateValues = [];
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY;
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT;
/**
* Declare variables
*

View File

@ -31,6 +31,10 @@ async function localAddUser({ payload, dbSchema }) {
* Initialize Variables
*/
const dbFullName = process.env.DSQL_DB_NAME || "";
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || "";
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
/**
* Hash Password
*
@ -105,6 +109,8 @@ async function localAddUser({ payload, dbSchema }) {
image: "/images/user_images/user-preset.png",
image_thumbnail: "/images/user_images/user-preset-thumbnail.png",
},
encryptionKey,
encryptionSalt,
});
if (addUser?.insertId) {

View File

@ -5,6 +5,15 @@ const decrypt = ({ encryptedString, encryptionKey, encryptionSalt }) => {
const algorithm = "aes-192-cbc";
const password = encryptionKey;
if (!encryptionKey?.match(/.{8,}/)) {
console.log("Decrption key is invalid");
return data;
}
if (!encryptionSalt?.match(/.{8,}/)) {
console.log("Decrption salt is invalid");
return data;
}
let key = scryptSync(password, encryptionSalt, 24);
let iv = Buffer.alloc(16, 0);
const decipher = createDecipheriv(algorithm, key, iv);

View File

@ -1,6 +1,6 @@
{
"name": "datasquirel",
"version": "1.6.1",
"version": "1.6.2",
"description": "Cloud-based SQL data management tool",
"main": "index.js",
"bin": {