Updates
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user