This commit is contained in:
Benjamin Toby
2025-01-14 14:19:50 +01:00
parent 0731e8cb6c
commit 20754d5cae
23 changed files with 382 additions and 74 deletions
@@ -0,0 +1,11 @@
type Param = {
email: string;
encryptionKey?: string;
encryptionSalt?: string;
};
export type EncryptResetPasswordObject = {
email: string;
createdAt: number;
};
export default function encryptReserPasswordUrl({ email, encryptionKey, encryptionSalt, }: Param): void;
export {};
@@ -0,0 +1,22 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = encryptReserPasswordUrl;
const ejson_1 = __importDefault(require("../../../../../utils/ejson"));
const encrypt_1 = __importDefault(require("../../../../dsql/encrypt"));
function encryptReserPasswordUrl({ email, encryptionKey, encryptionSalt, }) {
const encryptObject = {
email,
createdAt: Date.now(),
};
const encryptStr = (0, encrypt_1.default)({
data: ejson_1.default.stringify(encryptObject),
encryptionKey,
encryptionSalt,
});
const defaultUrlOrigin = `https://datasquirel.com`;
let urlOrigin = process.env.DSQL_HOST || defaultUrlOrigin;
const url = `${defaultUrlOrigin}`;
}
@@ -0,0 +1,23 @@
type Return = {
success: boolean;
msg?: string;
error?: string;
};
type Param = {
key?: string;
database: string;
email: string;
encryptionKey?: string;
encryptionSalt?: string;
useLocal?: boolean;
debug?: boolean;
apiUserID?: string | number;
dbUserId?: string | number;
};
/**
* # API Login
*/
export default function apiSendResetPasswordLink({ database, email, apiUserID, dbUserId, debug, encryptionKey, encryptionSalt, key, useLocal, }: Param): Promise<Return>;
export type SendResetPasswordParam = Param;
export type SendResetPasswordReturn = Return;
export {};
@@ -0,0 +1,53 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = apiSendResetPasswordLink;
const grab_db_full_name_1 = __importDefault(require("../../../../utils/grab-db-full-name"));
const varDatabaseDbHandler_1 = __importDefault(require("../../../backend/varDatabaseDbHandler"));
/**
* # API Login
*/
function apiSendResetPasswordLink(_a) {
return __awaiter(this, arguments, void 0, function* ({ database, email, apiUserID, dbUserId, debug, encryptionKey, encryptionSalt, key, useLocal, }) {
const dbFullName = (0, grab_db_full_name_1.default)({ dbName: database, userId: dbUserId });
/**
* Check input validity
*
* @description Check input validity
*/
if (email === null || email === void 0 ? void 0 : email.match(/ /)) {
return {
success: false,
msg: "Invalid Email/Password format",
};
}
let foundUser = yield (0, varDatabaseDbHandler_1.default)({
queryString: `SELECT * FROM ${dbFullName}.users WHERE email = ? OR username = ?`,
queryValuesArray: [email, email],
database: dbFullName,
useLocal,
debug,
});
if (debug) {
console.log("apiSendResetPassword:foundUser:", foundUser);
}
const targetUser = foundUser === null || foundUser === void 0 ? void 0 : foundUser[0];
if (!targetUser)
return {
success: false,
msg: "No user found",
};
return { success: true };
});
}
+9 -10
View File
@@ -1,9 +1,13 @@
"use strict";
// @ts-check
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = decrypt;
const crypto_1 = require("crypto");
const buffer_1 = require("buffer");
const grab_keys_1 = __importDefault(require("../../utils/grab-keys"));
/**
* # Decrypt Function
*/
@@ -12,22 +16,17 @@ function decrypt({ encryptedString, encryptionKey, encryptionSalt, }) {
console.log("Encrypted string is invalid");
return encryptedString;
}
const finalEncryptionKey = encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
const finalEncryptionSalt = encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
const finalKeyLen = process.env.DSQL_ENCRYPTION_KEY_LENGTH
? Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH)
: 24;
if (!(finalEncryptionKey === null || finalEncryptionKey === void 0 ? void 0 : finalEncryptionKey.match(/.{8,}/))) {
const { key: encrptKey, salt, keyLen, algorithm, bufferAllocSize, } = (0, grab_keys_1.default)({ encryptionKey });
if (!(encrptKey === null || encrptKey === void 0 ? void 0 : encrptKey.match(/.{8,}/))) {
console.log("Decrption key is invalid");
return encryptedString;
}
if (!(finalEncryptionSalt === null || finalEncryptionSalt === void 0 ? void 0 : finalEncryptionSalt.match(/.{8,}/))) {
if (!(salt === null || salt === void 0 ? void 0 : salt.match(/.{8,}/))) {
console.log("Decrption salt is invalid");
return encryptedString;
}
const algorithm = "aes-192-cbc";
let key = (0, crypto_1.scryptSync)(finalEncryptionKey, finalEncryptionSalt, finalKeyLen);
let iv = buffer_1.Buffer.alloc(16, 0);
let key = (0, crypto_1.scryptSync)(encrptKey, salt, keyLen);
let iv = buffer_1.Buffer.alloc(bufferAllocSize, 0);
const decipher = (0, crypto_1.createDecipheriv)(algorithm, key, iv);
try {
let decrypted = decipher.update(encryptedString, "hex", "utf8");
+11 -13
View File
@@ -1,9 +1,13 @@
"use strict";
// @ts-check
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = encrypt;
const crypto_1 = require("crypto");
const buffer_1 = require("buffer");
const grab_keys_1 = __importDefault(require("../../utils/grab-keys"));
/**
* # Encrypt String
*/
@@ -12,31 +16,25 @@ function encrypt({ data, encryptionKey, encryptionSalt, }) {
console.log("Encryption string is invalid");
return data;
}
const finalEncryptionKey = encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
const finalEncryptionSalt = encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
const finalKeyLen = process.env.DSQL_ENCRYPTION_KEY_LENGTH
? Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH)
: 24;
if (!(finalEncryptionKey === null || finalEncryptionKey === void 0 ? void 0 : finalEncryptionKey.match(/.{8,}/))) {
const { key: encrptKey, salt, keyLen, algorithm, bufferAllocSize, } = (0, grab_keys_1.default)({ encryptionKey });
if (!(encrptKey === null || encrptKey === void 0 ? void 0 : encrptKey.match(/.{8,}/))) {
console.log("Encryption key is invalid");
return data;
}
if (!(finalEncryptionSalt === null || finalEncryptionSalt === void 0 ? void 0 : finalEncryptionSalt.match(/.{8,}/))) {
if (!(salt === null || salt === void 0 ? void 0 : salt.match(/.{8,}/))) {
console.log("Encryption salt is invalid");
return data;
}
const algorithm = "aes-192-cbc";
const password = finalEncryptionKey;
let key = (0, crypto_1.scryptSync)(password, finalEncryptionSalt, finalKeyLen);
let iv = buffer_1.Buffer.alloc(16, 0);
// @ts-ignore
const password = encrptKey;
let key = (0, crypto_1.scryptSync)(password, salt, keyLen);
let iv = buffer_1.Buffer.alloc(bufferAllocSize, 0);
const cipher = (0, crypto_1.createCipheriv)(algorithm, key, iv);
try {
let encrypted = cipher.update(data, "utf8", "hex");
encrypted += cipher.final("hex");
return encrypted;
}
catch ( /** @type {*} */error) {
catch (error) {
console.log("Error in encrypting =>", error.message);
return data;
}
+7 -3
View File
@@ -1,16 +1,20 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = hashPassword;
const crypto_1 = require("crypto");
const grab_keys_1 = __importDefault(require("../../utils/grab-keys"));
/**
* # Hash password Function
*/
function hashPassword({ password, encryptionKey, }) {
const finalEncryptionKey = encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
if (!(finalEncryptionKey === null || finalEncryptionKey === void 0 ? void 0 : finalEncryptionKey.match(/.{8,}/))) {
const { key } = (0, grab_keys_1.default)({ encryptionKey });
if (!(key === null || key === void 0 ? void 0 : key.match(/.{8,}/))) {
throw new Error("Encryption key is invalid");
}
const hmac = (0, crypto_1.createHmac)("sha512", finalEncryptionKey);
const hmac = (0, crypto_1.createHmac)("sha512", key);
hmac.update(password);
let hashed = hmac.digest("base64");
return hashed;
+19
View File
@@ -0,0 +1,19 @@
export type GrabEncryptionKeysParam = {
encryptionKey?: string;
encryptionSalt?: string;
apiKey?: string;
algorithm?: string;
bufferAllocSize?: number;
};
/**
* # Grab Encryption Keys
* @description Grab Required Encryption Keys
*/
export default function grabKeys(param?: GrabEncryptionKeysParam): {
key: string | undefined;
keyLen: number;
salt: string | undefined;
apiKey: string | undefined;
algorithm: string;
bufferAllocSize: number;
};
+29
View File
@@ -0,0 +1,29 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = grabKeys;
const numberfy_1 = __importDefault(require("./numberfy"));
/**
* # Grab Encryption Keys
* @description Grab Required Encryption Keys
*/
function grabKeys(param) {
return {
key: (param === null || param === void 0 ? void 0 : param.encryptionKey) || process.env.DSQL_ENCRYPTION_PASSWORD,
keyLen: process.env.DSQL_ENCRYPTION_KEY_LENGTH
? Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH)
: 24,
salt: (param === null || param === void 0 ? void 0 : param.encryptionSalt) || process.env.DSQL_ENCRYPTION_SALT,
apiKey: (param === null || param === void 0 ? void 0 : param.apiKey) || process.env.DSQL_API_KEY,
algorithm: (param === null || param === void 0 ? void 0 : param.algorithm) ||
process.env.DSQL_ENCRYPTION_ALGORITHM ||
"aes-192-cbc",
bufferAllocSize: (param === null || param === void 0 ? void 0 : param.bufferAllocSize) ||
(process.env.DSQL_ENCRYPTION_BUFFER_ALLOCATION_SIZE
? (0, numberfy_1.default)(process.env.DSQL_ENCRYPTION_BUFFER_ALLOCATION_SIZE)
: undefined) ||
16,
};
}
+1 -1
View File
@@ -7,4 +7,4 @@
* numberfy("123.456", 0) // 123
* numberfy("123.456", 3) // 123.456
*/
export default function numberfy(num: any, decimals: number): number;
export default function numberfy(num: any, decimals?: number): number;