Updates
This commit is contained in:
parent
0731e8cb6c
commit
20754d5cae
9
dist/index.d.ts
vendored
9
dist/index.d.ts
vendored
@ -32,6 +32,9 @@ import trimSql from "./package-shared/utils/trim-sql";
|
||||
import parseCookies from "./package-shared/utils/backend/parseCookies";
|
||||
import httpRequest from "./package-shared/functions/backend/httpRequest";
|
||||
import connDbHandler from "./package-shared/utils/db/conn-db-handler";
|
||||
import encrypt from "./package-shared/functions/dsql/encrypt";
|
||||
import decrypt from "./package-shared/functions/dsql/decrypt";
|
||||
import hashPassword from "./package-shared/functions/dsql/hashPassword";
|
||||
/**
|
||||
* Main Export
|
||||
*/
|
||||
@ -105,9 +108,9 @@ declare const datasquirel: {
|
||||
};
|
||||
utils: {
|
||||
crypto: {
|
||||
encrypt: any;
|
||||
decrypt: any;
|
||||
hash: any;
|
||||
encrypt: typeof encrypt;
|
||||
decrypt: typeof decrypt;
|
||||
hashPassword: typeof hashPassword;
|
||||
};
|
||||
parseCookies: typeof parseCookies;
|
||||
httpRequest: typeof httpRequest;
|
||||
|
9
dist/index.js
vendored
9
dist/index.js
vendored
@ -32,6 +32,9 @@ const trim_sql_1 = __importDefault(require("./package-shared/utils/trim-sql"));
|
||||
const parseCookies_1 = __importDefault(require("./package-shared/utils/backend/parseCookies"));
|
||||
const httpRequest_1 = __importDefault(require("./package-shared/functions/backend/httpRequest"));
|
||||
const conn_db_handler_1 = __importDefault(require("./package-shared/utils/db/conn-db-handler"));
|
||||
const encrypt_1 = __importDefault(require("./package-shared/functions/dsql/encrypt"));
|
||||
const decrypt_1 = __importDefault(require("./package-shared/functions/dsql/decrypt"));
|
||||
const hashPassword_1 = __importDefault(require("./package-shared/functions/dsql/hashPassword"));
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
@ -86,9 +89,9 @@ const datasquirel = {
|
||||
sql,
|
||||
utils: {
|
||||
crypto: {
|
||||
encrypt: require("./package-shared/functions/dsql/encrypt"),
|
||||
decrypt: require("./package-shared/functions/dsql/decrypt"),
|
||||
hash: require("./package-shared/functions/dsql/hashPassword"),
|
||||
encrypt: encrypt_1.default,
|
||||
decrypt: decrypt_1.default,
|
||||
hashPassword: hashPassword_1.default,
|
||||
},
|
||||
parseCookies: parseCookies_1.default,
|
||||
httpRequest: httpRequest_1.default,
|
||||
|
11
dist/package-shared/functions/api/users/reset-password/(utils)/encrypt-url.d.ts
vendored
Normal file
11
dist/package-shared/functions/api/users/reset-password/(utils)/encrypt-url.d.ts
vendored
Normal file
@ -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 {};
|
22
dist/package-shared/functions/api/users/reset-password/(utils)/encrypt-url.js
vendored
Normal file
22
dist/package-shared/functions/api/users/reset-password/(utils)/encrypt-url.js
vendored
Normal file
@ -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}`;
|
||||
}
|
23
dist/package-shared/functions/api/users/reset-password/api-send-reset-password-link.d.ts
vendored
Normal file
23
dist/package-shared/functions/api/users/reset-password/api-send-reset-password-link.d.ts
vendored
Normal file
@ -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 {};
|
53
dist/package-shared/functions/api/users/reset-password/api-send-reset-password-link.js
vendored
Normal file
53
dist/package-shared/functions/api/users/reset-password/api-send-reset-password-link.js
vendored
Normal file
@ -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 };
|
||||
});
|
||||
}
|
19
dist/package-shared/functions/dsql/decrypt.js
vendored
19
dist/package-shared/functions/dsql/decrypt.js
vendored
@ -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");
|
||||
|
24
dist/package-shared/functions/dsql/encrypt.js
vendored
24
dist/package-shared/functions/dsql/encrypt.js
vendored
@ -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;
|
||||
}
|
||||
|
@ -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
dist/package-shared/utils/grab-keys.d.ts
vendored
Normal file
19
dist/package-shared/utils/grab-keys.d.ts
vendored
Normal 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
dist/package-shared/utils/grab-keys.js
vendored
Normal file
29
dist/package-shared/utils/grab-keys.js
vendored
Normal 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,
|
||||
};
|
||||
}
|
2
dist/package-shared/utils/numberfy.d.ts
vendored
2
dist/package-shared/utils/numberfy.d.ts
vendored
@ -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;
|
||||
|
2
dist/utils/get.js
vendored
2
dist/utils/get.js
vendored
@ -59,7 +59,6 @@ function get(_a) {
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
const httpResponse = yield new Promise((resolve, reject) => {
|
||||
/** @type {import("../package-shared/types").GetReqQueryObject} */
|
||||
const queryObject = {
|
||||
db: process.env.DSQL_API_DB_NAME || String(db),
|
||||
query: String(query.replace(/\n|\r|\n\r/g, "").replace(/ {2,}/g, " ")),
|
||||
@ -70,7 +69,6 @@ function get(_a) {
|
||||
};
|
||||
const queryString = (0, serialize_query_1.default)(Object.assign({}, queryObject));
|
||||
let path = `/api/query/${user_id || grabedHostNames.user_id}/get${queryString}`;
|
||||
/** @type {https.RequestOptions} */
|
||||
const requestObject = {
|
||||
method: "GET",
|
||||
headers: {
|
||||
|
9
index.ts
9
index.ts
@ -40,6 +40,9 @@ import trimSql from "./package-shared/utils/trim-sql";
|
||||
import parseCookies from "./package-shared/utils/backend/parseCookies";
|
||||
import httpRequest from "./package-shared/functions/backend/httpRequest";
|
||||
import connDbHandler from "./package-shared/utils/db/conn-db-handler";
|
||||
import encrypt from "./package-shared/functions/dsql/encrypt";
|
||||
import decrypt from "./package-shared/functions/dsql/decrypt";
|
||||
import hashPassword from "./package-shared/functions/dsql/hashPassword";
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
@ -99,9 +102,9 @@ const datasquirel = {
|
||||
sql,
|
||||
utils: {
|
||||
crypto: {
|
||||
encrypt: require("./package-shared/functions/dsql/encrypt"),
|
||||
decrypt: require("./package-shared/functions/dsql/decrypt"),
|
||||
hash: require("./package-shared/functions/dsql/hashPassword"),
|
||||
encrypt,
|
||||
decrypt,
|
||||
hashPassword,
|
||||
},
|
||||
parseCookies,
|
||||
httpRequest,
|
||||
|
@ -0,0 +1,35 @@
|
||||
import EJSON from "../../../../../utils/ejson";
|
||||
import encrypt from "../../../../dsql/encrypt";
|
||||
|
||||
type Param = {
|
||||
email: string;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
};
|
||||
|
||||
export type EncryptResetPasswordObject = {
|
||||
email: string;
|
||||
createdAt: number;
|
||||
};
|
||||
|
||||
export default function encryptReserPasswordUrl({
|
||||
email,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
}: Param) {
|
||||
const encryptObject: EncryptResetPasswordObject = {
|
||||
email,
|
||||
createdAt: Date.now(),
|
||||
};
|
||||
|
||||
const encryptStr = encrypt({
|
||||
data: EJSON.stringify(encryptObject) as string,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
});
|
||||
|
||||
const defaultUrlOrigin = `https://datasquirel.com`;
|
||||
let urlOrigin = process.env.DSQL_HOST || defaultUrlOrigin;
|
||||
|
||||
const url = `${defaultUrlOrigin}`;
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
import { DSQL_MYSQL_user_databases_Type } from "../../../../types";
|
||||
import grabDbFullName from "../../../../utils/grab-db-full-name";
|
||||
import varDatabaseDbHandler from "../../../backend/varDatabaseDbHandler";
|
||||
|
||||
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 async function apiSendResetPasswordLink({
|
||||
database,
|
||||
email,
|
||||
apiUserID,
|
||||
dbUserId,
|
||||
debug,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
key,
|
||||
useLocal,
|
||||
}: Param): Promise<Return> {
|
||||
const dbFullName = grabDbFullName({ dbName: database, userId: dbUserId });
|
||||
|
||||
/**
|
||||
* Check input validity
|
||||
*
|
||||
* @description Check input validity
|
||||
*/
|
||||
if (email?.match(/ /)) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Invalid Email/Password format",
|
||||
};
|
||||
}
|
||||
|
||||
let foundUser = await varDatabaseDbHandler({
|
||||
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?.[0] as
|
||||
| DSQL_MYSQL_user_databases_Type
|
||||
| undefined;
|
||||
|
||||
if (!targetUser)
|
||||
return {
|
||||
success: false,
|
||||
msg: "No user found",
|
||||
};
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export type SendResetPasswordParam = Param;
|
||||
export type SendResetPasswordReturn = Return;
|
@ -2,6 +2,7 @@
|
||||
|
||||
import { scryptSync, createDecipheriv } from "crypto";
|
||||
import { Buffer } from "buffer";
|
||||
import grabKeys from "../../utils/grab-keys";
|
||||
|
||||
type Param = {
|
||||
encryptedString: string;
|
||||
@ -22,28 +23,26 @@ export default function decrypt({
|
||||
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;
|
||||
const {
|
||||
key: encrptKey,
|
||||
salt,
|
||||
keyLen,
|
||||
algorithm,
|
||||
bufferAllocSize,
|
||||
} = grabKeys({ encryptionKey });
|
||||
|
||||
if (!finalEncryptionKey?.match(/.{8,}/)) {
|
||||
if (!encrptKey?.match(/.{8,}/)) {
|
||||
console.log("Decrption key is invalid");
|
||||
return encryptedString;
|
||||
}
|
||||
|
||||
if (!finalEncryptionSalt?.match(/.{8,}/)) {
|
||||
if (!salt?.match(/.{8,}/)) {
|
||||
console.log("Decrption salt is invalid");
|
||||
return encryptedString;
|
||||
}
|
||||
|
||||
const algorithm = "aes-192-cbc";
|
||||
|
||||
let key = scryptSync(finalEncryptionKey, finalEncryptionSalt, finalKeyLen);
|
||||
let iv = Buffer.alloc(16, 0);
|
||||
let key = scryptSync(encrptKey, salt, keyLen);
|
||||
let iv = Buffer.alloc(bufferAllocSize, 0);
|
||||
|
||||
const decipher = createDecipheriv(algorithm, key, iv);
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
import { scryptSync, createCipheriv } from "crypto";
|
||||
import { Buffer } from "buffer";
|
||||
import grabKeys from "../../utils/grab-keys";
|
||||
|
||||
type Param = {
|
||||
data: string;
|
||||
@ -22,36 +23,35 @@ export default function encrypt({
|
||||
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;
|
||||
const {
|
||||
key: encrptKey,
|
||||
salt,
|
||||
keyLen,
|
||||
algorithm,
|
||||
bufferAllocSize,
|
||||
} = grabKeys({ encryptionKey });
|
||||
|
||||
if (!finalEncryptionKey?.match(/.{8,}/)) {
|
||||
if (!encrptKey?.match(/.{8,}/)) {
|
||||
console.log("Encryption key is invalid");
|
||||
return data;
|
||||
}
|
||||
if (!finalEncryptionSalt?.match(/.{8,}/)) {
|
||||
if (!salt?.match(/.{8,}/)) {
|
||||
console.log("Encryption salt is invalid");
|
||||
return data;
|
||||
}
|
||||
|
||||
const algorithm = "aes-192-cbc";
|
||||
const password = finalEncryptionKey;
|
||||
const password = encrptKey;
|
||||
|
||||
let key = scryptSync(password, salt, keyLen);
|
||||
let iv = Buffer.alloc(bufferAllocSize, 0);
|
||||
|
||||
let key = scryptSync(password, finalEncryptionSalt, finalKeyLen);
|
||||
let iv = Buffer.alloc(16, 0);
|
||||
// @ts-ignore
|
||||
const cipher = createCipheriv(algorithm, key, iv);
|
||||
|
||||
try {
|
||||
let encrypted = cipher.update(data, "utf8", "hex");
|
||||
encrypted += cipher.final("hex");
|
||||
return encrypted;
|
||||
} catch (/** @type {*} */ error: any) {
|
||||
} catch (error: any) {
|
||||
console.log("Error in encrypting =>", error.message);
|
||||
return data;
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { createHmac } from "crypto";
|
||||
import grabKeys from "../../utils/grab-keys";
|
||||
|
||||
type Param = {
|
||||
password: string;
|
||||
@ -12,14 +13,13 @@ export default function hashPassword({
|
||||
password,
|
||||
encryptionKey,
|
||||
}: Param): string {
|
||||
const finalEncryptionKey =
|
||||
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
||||
const { key } = grabKeys({ encryptionKey });
|
||||
|
||||
if (!finalEncryptionKey?.match(/.{8,}/)) {
|
||||
if (!key?.match(/.{8,}/)) {
|
||||
throw new Error("Encryption key is invalid");
|
||||
}
|
||||
|
||||
const hmac = createHmac("sha512", finalEncryptionKey);
|
||||
const hmac = createHmac("sha512", key);
|
||||
hmac.update(password);
|
||||
let hashed = hmac.digest("base64");
|
||||
return hashed;
|
||||
|
34
package-shared/utils/grab-keys.ts
Normal file
34
package-shared/utils/grab-keys.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import numberfy from "./numberfy";
|
||||
|
||||
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) {
|
||||
return {
|
||||
key: param?.encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD,
|
||||
keyLen: process.env.DSQL_ENCRYPTION_KEY_LENGTH
|
||||
? Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH)
|
||||
: 24,
|
||||
salt: param?.encryptionSalt || process.env.DSQL_ENCRYPTION_SALT,
|
||||
apiKey: param?.apiKey || process.env.DSQL_API_KEY,
|
||||
algorithm:
|
||||
param?.algorithm ||
|
||||
process.env.DSQL_ENCRYPTION_ALGORITHM ||
|
||||
"aes-192-cbc",
|
||||
bufferAllocSize:
|
||||
param?.bufferAllocSize ||
|
||||
(process.env.DSQL_ENCRYPTION_BUFFER_ALLOCATION_SIZE
|
||||
? numberfy(process.env.DSQL_ENCRYPTION_BUFFER_ALLOCATION_SIZE)
|
||||
: undefined) ||
|
||||
16,
|
||||
};
|
||||
}
|
@ -7,7 +7,7 @@
|
||||
* 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 {
|
||||
try {
|
||||
const numberfiedNum = Number(num);
|
||||
if (typeof numberfiedNum !== "number") return 0;
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@moduletrace/datasquirel",
|
||||
"version": "3.5.2",
|
||||
"version": "3.5.3",
|
||||
"description": "Cloud-based SQL data management tool",
|
||||
"main": "dist/index.js",
|
||||
"bin": {
|
||||
|
@ -77,7 +77,6 @@ export default async function get({
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
const httpResponse = await new Promise((resolve, reject) => {
|
||||
/** @type {import("../package-shared/types").GetReqQueryObject} */
|
||||
const queryObject: import("../package-shared/types").GetReqQueryObject =
|
||||
{
|
||||
db: process.env.DSQL_API_DB_NAME || String(db),
|
||||
@ -96,7 +95,6 @@ export default async function get({
|
||||
user_id || grabedHostNames.user_id
|
||||
}/get${queryString}`;
|
||||
|
||||
/** @type {https.RequestOptions} */
|
||||
const requestObject: https.RequestOptions = {
|
||||
method: "GET",
|
||||
headers: {
|
||||
|
Loading…
Reference in New Issue
Block a user