Updates
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
||||
APILoginFunctionReturn,
|
||||
DATASQUIREL_LoggedInUser,
|
||||
} from "../../../types";
|
||||
import grabDbFullName from "../../../utils/grab-db-full-name";
|
||||
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler";
|
||||
import hashPassword from "../../dsql/hashPassword";
|
||||
|
||||
@@ -23,8 +24,10 @@ export default async function apiLoginUser({
|
||||
skipPassword,
|
||||
social,
|
||||
useLocal,
|
||||
dbUserId,
|
||||
debug,
|
||||
}: APILoginFunctionParams): Promise<APILoginFunctionReturn> {
|
||||
const dbFullName = database.replace(/[^a-z0-9_]/g, "");
|
||||
const dbFullName = grabDbFullName({ dbName: database, userId: dbUserId });
|
||||
|
||||
/**
|
||||
* Check input validity
|
||||
@@ -54,13 +57,23 @@ export default async function apiLoginUser({
|
||||
})
|
||||
: null;
|
||||
|
||||
if (debug) {
|
||||
console.log("apiLoginUser:database:", dbFullName);
|
||||
console.log("apiLoginUser:Finding User ...");
|
||||
}
|
||||
|
||||
let foundUser = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM ${dbFullName}.users WHERE email = ? OR username = ?`,
|
||||
queryValuesArray: [email, username],
|
||||
database: dbFullName,
|
||||
useLocal,
|
||||
debug,
|
||||
});
|
||||
|
||||
if (debug) {
|
||||
console.log("apiLoginUser:foundUser:", foundUser);
|
||||
}
|
||||
|
||||
if ((!foundUser || !foundUser[0]) && !social)
|
||||
return {
|
||||
success: false,
|
||||
@@ -70,9 +83,20 @@ export default async function apiLoginUser({
|
||||
|
||||
let isPasswordCorrect = false;
|
||||
|
||||
if (debug) {
|
||||
console.log("apiLoginUser:isPasswordCorrect:", isPasswordCorrect);
|
||||
}
|
||||
|
||||
if (foundUser?.[0] && !email_login && skipPassword) {
|
||||
isPasswordCorrect = true;
|
||||
} else if (foundUser?.[0] && !email_login) {
|
||||
if (debug) {
|
||||
console.log("apiLoginUser:hashedPassword:", hashedPassword);
|
||||
console.log(
|
||||
"apiLoginUser:foundUser[0].password:",
|
||||
foundUser[0].password
|
||||
);
|
||||
}
|
||||
isPasswordCorrect = hashedPassword === foundUser[0].password;
|
||||
} else if (
|
||||
foundUser &&
|
||||
@@ -81,8 +105,11 @@ export default async function apiLoginUser({
|
||||
email_login_code &&
|
||||
email_login_field
|
||||
) {
|
||||
/** @type {string} */
|
||||
const tempCode = foundUser[0][email_login_field];
|
||||
const tempCode: string = foundUser[0][email_login_field];
|
||||
|
||||
if (debug) {
|
||||
console.log("apiLoginUser:tempCode:", tempCode);
|
||||
}
|
||||
|
||||
if (!tempCode) throw new Error("No code Found!");
|
||||
|
||||
@@ -106,6 +133,11 @@ export default async function apiLoginUser({
|
||||
};
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
console.log("apiLoginUser:isPasswordCorrect:", isPasswordCorrect);
|
||||
console.log("apiLoginUser:email_login:", email_login);
|
||||
}
|
||||
|
||||
if (isPasswordCorrect && email_login) {
|
||||
const resetTempCode = await varDatabaseDbHandler({
|
||||
queryString: `UPDATE ${dbFullName}.users SET ${email_login_field} = '' WHERE email = ? OR username = ?`,
|
||||
@@ -139,6 +171,11 @@ export default async function apiLoginUser({
|
||||
date: Date.now(),
|
||||
};
|
||||
|
||||
if (debug) {
|
||||
console.log("apiLoginUser:userPayload:", userPayload);
|
||||
console.log("apiLoginUser:Sending Response Object ...");
|
||||
}
|
||||
|
||||
const resposeObject: APILoginFunctionReturn = {
|
||||
success: true,
|
||||
msg: "Login Successful",
|
||||
|
||||
@@ -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}`;
|
||||
}
|
||||
+77
@@ -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;
|
||||
@@ -1,5 +1,7 @@
|
||||
import fs from "fs";
|
||||
import serverError from "./serverError";
|
||||
import mysql from "serverless-mysql";
|
||||
import grabDSQLConnection from "../../utils/grab-dsql-connection";
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
@@ -12,6 +14,8 @@ export default async function dbHandler(...args: any[]) {
|
||||
"utf8"
|
||||
);
|
||||
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
|
||||
let results;
|
||||
|
||||
/**
|
||||
@@ -20,10 +24,8 @@ export default async function dbHandler(...args: any[]) {
|
||||
* @description Fetch data from db if no cache
|
||||
*/
|
||||
try {
|
||||
const connection = global.DSQL_DB_CONN;
|
||||
|
||||
results = await new Promise((resolve, reject) => {
|
||||
connection.query(
|
||||
CONNECTION.query(
|
||||
...args,
|
||||
(error: any, result: any, fields: any) => {
|
||||
if (error) {
|
||||
@@ -34,8 +36,6 @@ export default async function dbHandler(...args: any[]) {
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
await connection.end();
|
||||
} catch (error: any) {
|
||||
fs.appendFileSync(
|
||||
"./.tmp/dbErrorLogs.txt",
|
||||
@@ -49,6 +49,8 @@ export default async function dbHandler(...args: any[]) {
|
||||
component: "dbHandler",
|
||||
message: error.message,
|
||||
});
|
||||
} finally {
|
||||
await CONNECTION?.end();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import parseDbResults from "./parseDbResults";
|
||||
import serverError from "./serverError";
|
||||
import DB_HANDLER from "../../utils/backend/global-db/DB_HANDLER";
|
||||
import DSQL_USER_DB_HANDLER from "../../utils/backend/global-db/DSQL_USER_DB_HANDLER";
|
||||
import LOCAL_DB_HANDLER from "../../utils/backend/global-db/LOCAL_DB_HANDLER";
|
||||
import grabDSQLConnection from "../../utils/grab-dsql-connection";
|
||||
import connDbHandler from "../../utils/db/conn-db-handler";
|
||||
|
||||
type Param = {
|
||||
queryString: string;
|
||||
@@ -10,6 +9,7 @@ type Param = {
|
||||
database?: string;
|
||||
tableSchema?: import("../../types").DSQL_TableSchemaType;
|
||||
useLocal?: boolean;
|
||||
debug?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -21,66 +21,27 @@ export default async function varDatabaseDbHandler({
|
||||
database,
|
||||
tableSchema,
|
||||
useLocal,
|
||||
debug,
|
||||
}: Param): Promise<any> {
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
*/
|
||||
const isMaster = useLocal
|
||||
? true
|
||||
: database?.match(/^datasquirel$/)
|
||||
? true
|
||||
: false;
|
||||
let CONNECTION = grabDSQLConnection({ fa: true });
|
||||
if (useLocal) CONNECTION = grabDSQLConnection({ local: true });
|
||||
if (database?.match(/^datasquirel$/)) CONNECTION = grabDSQLConnection();
|
||||
|
||||
const FINAL_DB_HANDLER: any = useLocal
|
||||
? LOCAL_DB_HANDLER
|
||||
: isMaster
|
||||
? DB_HANDLER
|
||||
: DSQL_USER_DB_HANDLER;
|
||||
if (debug) {
|
||||
console.log(`varDatabaseDbHandler:query:`, queryString);
|
||||
console.log(`varDatabaseDbHandler:values:`, queryValuesArray);
|
||||
}
|
||||
|
||||
let results;
|
||||
let results = await connDbHandler(
|
||||
CONNECTION,
|
||||
queryString,
|
||||
queryValuesArray
|
||||
);
|
||||
|
||||
/**
|
||||
* Fetch from db
|
||||
*
|
||||
* @description Fetch data from db if no cache
|
||||
*/
|
||||
try {
|
||||
if (
|
||||
queryString &&
|
||||
queryValuesArray &&
|
||||
Array.isArray(queryValuesArray) &&
|
||||
queryValuesArray[0]
|
||||
) {
|
||||
results = isMaster
|
||||
? await FINAL_DB_HANDLER(queryString, queryValuesArray)
|
||||
: await FINAL_DB_HANDLER({
|
||||
paradigm: "Full Access",
|
||||
queryString,
|
||||
queryValues: queryValuesArray,
|
||||
});
|
||||
} else {
|
||||
results = isMaster
|
||||
? await FINAL_DB_HANDLER(queryString)
|
||||
: await FINAL_DB_HANDLER({
|
||||
paradigm: "Full Access",
|
||||
queryString,
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log(`varDatabaseDbHandler Error: ${error.message}`);
|
||||
serverError({
|
||||
component: "varDatabaseDbHandler/lines-29-32",
|
||||
message: error.message,
|
||||
});
|
||||
if (debug) {
|
||||
console.log(`varDatabaseDbHandler:results:`, results);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return results
|
||||
*
|
||||
* @description Return results add to cache if "req" param is passed
|
||||
*/
|
||||
if (results && tableSchema) {
|
||||
try {
|
||||
const unparsedResults = results;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user