This commit is contained in:
Tben
2023-08-13 14:00:04 +01:00
parent 98c4c17eb3
commit 43287b0329
25 changed files with 2106 additions and 230 deletions
+34 -10
View File
@@ -4,6 +4,7 @@
* ==============================================================================
*/
const https = require("https");
const getLocalUser = require("../engine/user/get-user");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -44,6 +45,19 @@ const https = require("https");
* @returns { Promise<FunctionReturn>}
*/
async function getUser({ key, userId, database, fields }) {
/**
* Initialize
*/
const defaultFields = ["id", "first_name", "last_name", "email", "username", "image", "image_thumbnail", "verification_status", "date_created", "date_created_code", "date_created_timestamp", "date_updated", "date_updated_code", "date_updated_timestamp"];
const updatedFields = fields && fields[0] ? [...defaultFields, ...fields] : defaultFields;
const reqPayload = JSON.stringify({
userId,
database,
fields: [...new Set(updatedFields)],
});
/**
* Check for local DB settings
*
@@ -51,22 +65,32 @@ async function getUser({ key, userId, database, fields }) {
*/
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env;
if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) {
/** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json");
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
console.log("Reading from local database ...");
if (dbSchema) {
return await getLocalUser({
userId,
fields: [...new Set(updatedFields)],
dbSchema,
});
}
}
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
const defaultFields = ["id", "first_name", "last_name", "email", "username", "image", "image_thumbnail", "verification_status", "date_created", "date_created_code", "date_created_timestamp", "date_updated", "date_updated_code", "date_updated_timestamp"];
const updatedFields = fields && fields[0] ? [...defaultFields, ...fields] : defaultFields;
const reqPayload = JSON.stringify({
userId,
database,
fields: [...new Set(updatedFields)],
});
const httpsRequest = https.request(
{
method: "POST",
+73 -45
View File
@@ -7,7 +7,10 @@
*/
const http = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
const encrypt = require("../functions/encrypt");
const loginLocalUser = require("../engine/user/login-user");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -78,6 +81,11 @@ async function loginUser({ key, payload, database, additionalFields, response, e
msg: "Encryption Salt must be at least 8 characters",
};
/**
* Initialize HTTP response variable
*/
let httpResponse;
/**
* Check for local DB settings
*
@@ -85,58 +93,78 @@ async function loginUser({ key, payload, database, additionalFields, response, e
*/
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env;
/**
* Make https request
*
* @description make a request to datasquirel.com
*
* @type {{ success: boolean, payload: import("../types/user.td").DATASQUIREL_LoggedInUser | null, userId?: number, msg?: string }}
*/
const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
payload,
database,
additionalFields,
});
if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) {
/** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json");
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
console.log("Reading from local database ...");
if (dbSchema) {
httpResponse = await loginLocalUser({
payload,
additionalFields,
dbSchema,
});
}
} else {
/**
* Make https request
*
* @description make a request to datasquirel.com
*
* @type {{ success: boolean, payload: import("../types/user.td").DATASQUIREL_LoggedInUser | null, userId?: number, msg?: string }}
*/
httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
payload,
database,
additionalFields,
});
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/login-user`,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/login-user`,
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("error", (err) => {
reject(err);
});
}
);
response.on("error", (err) => {
reject(err);
});
}
);
httpsRequest.write(reqPayload);
httpsRequest.end();
});
httpsRequest.write(reqPayload);
httpsRequest.end();
});
}
/** ********************************************** */
/** ********************************************** */
+71 -43
View File
@@ -7,9 +7,12 @@
*/
const http = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
const encrypt = require("../functions/encrypt");
const userAuth = require("./user-auth");
const localReauthUser = require("../engine/user/reauth-user");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -66,6 +69,11 @@ async function reauthUser({ key, database, response, request, level, encryptionK
};
}
/**
* Initialize HTTP response variable
*/
let httpResponse;
/**
* Check for local DB settings
*
@@ -73,56 +81,76 @@ async function reauthUser({ key, database, response, request, level, encryptionK
*/
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env;
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
existingUser: existingUser.payload,
database,
additionalFields,
});
if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) {
/** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json");
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
console.log("Reading from local database ...");
if (dbSchema) {
httpResponse = await localReauthUser({
existingUser: existingUser.payload,
additionalFields,
dbSchema,
});
}
} else {
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
existingUser: existingUser.payload,
database,
additionalFields,
});
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/reauth-user`,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/reauth-user`,
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("error", (err) => {
reject(err);
});
}
);
response.on("error", (err) => {
reject(err);
});
}
);
httpsRequest.write(reqPayload);
httpsRequest.end();
});
httpsRequest.write(reqPayload);
httpsRequest.end();
});
}
/** ********************************************** */
/** ********************************************** */
+88 -60
View File
@@ -7,7 +7,10 @@
*/
const http = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
const encrypt = require("../../functions/encrypt");
const localGithubAuth = require("../../engine/user/social/github-auth");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -42,7 +45,7 @@ const encrypt = require("../../functions/encrypt");
* @param {string} params.encryptionSalt - Encryption salt
* @param {object} [params.additionalFields] - Additional Fields to be added to the user object
*
* @returns { Promise<FunctionReturn> }
* @returns { Promise<FunctionReturn | undefined> }
*/
async function githubAuth({ key, code, email, database, clientId, clientSecret, response, encryptionKey, encryptionSalt, additionalFields }) {
/**
@@ -110,6 +113,11 @@ async function githubAuth({ key, code, email, database, clientId, clientSecret,
////////////////////////////////////////
////////////////////////////////////////
/**
* Initialize HTTP response variable
*/
let httpResponse;
/**
* Check for local DB settings
*
@@ -117,73 +125,93 @@ async function githubAuth({ key, code, email, database, clientId, clientSecret,
*/
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) {
/** @type {import("../../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
/**
* Make https request
*
* @description make a request to datasquirel.com
* @type {FunctionReturn} - Https response object
*/
const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
code,
email,
clientId,
clientSecret,
database,
additionalFields,
});
try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json");
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
console.log("Reading from local database ...");
if (dbSchema) {
httpResponse = await localGithubAuth({
dbSchema: dbSchema,
code,
email: email || undefined,
clientId,
clientSecret,
additionalFields,
res: response,
});
}
} else {
/**
* Make https request
*
* @description make a request to datasquirel.com
* @type {FunctionReturn} - Https response object
*/
httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
code,
email,
clientId,
clientSecret,
database,
additionalFields,
});
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/github-login`,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/github-login`,
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
try {
resolve(JSON.parse(str));
} catch (error) {
console.log(error);
response.on("end", function () {
try {
resolve(JSON.parse(str));
} catch (error) {
console.log(error);
resolve({
success: false,
user: null,
msg: "Something went wrong",
});
}
});
resolve({
success: false,
user: null,
msg: "Something went wrong",
});
}
});
response.on("error", (err) => {
reject(err);
});
}
);
httpsRequest.write(reqPayload);
httpsRequest.end();
});
response.on("error", (err) => {
reject(err);
});
}
);
httpsRequest.write(reqPayload);
httpsRequest.end();
});
}
////////////////////////////////////////
////////////////////////////////////////
+78 -48
View File
@@ -7,7 +7,10 @@
*/
const http = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
const encrypt = require("../../functions/encrypt");
const localGoogleAuth = require("../../engine/user/social/google-auth");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -108,6 +111,11 @@ async function googleAuth({ key, token, database, clientId, response, encryption
////////////////////////////////////////
////////////////////////////////////////
/**
* Initialize HTTP response variable
*/
let httpResponse;
/**
* Check for local DB settings
*
@@ -115,61 +123,83 @@ async function googleAuth({ key, token, database, clientId, response, encryption
*/
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) {
/** @type {import("../../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
/**
* Make https request
*
* @description make a request to datasquirel.com
* @type {{ success: boolean, user: import("../../types/user.td").DATASQUIREL_LoggedInUser | null, msg?: string, dsqlUserId?: number } | null } - Https response object
*/
const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
token,
clientId,
database,
additionalFields,
});
try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json");
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
console.log("Reading from local database ...");
if (dbSchema) {
httpResponse = await localGoogleAuth({
dbSchema: dbSchema,
token,
clientId,
additionalFields,
response: response,
});
}
} else {
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Make https request
*
* @description make a request to datasquirel.com
* @type {{ success: boolean, user: import("../../types/user.td").DATASQUIREL_LoggedInUser | null, msg?: string, dsqlUserId?: number } | null } - Https response object
*/
httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
token,
clientId,
database,
additionalFields,
});
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/google-login`,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/google-login`,
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("error", (err) => {
reject(err);
});
}
);
httpsRequest.write(reqPayload);
httpsRequest.end();
});
response.on("error", (err) => {
reject(err);
});
}
);
httpsRequest.write(reqPayload);
httpsRequest.end();
});
}
////////////////////////////////////////
////////////////////////////////////////
+24
View File
@@ -1,9 +1,14 @@
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const https = require("https");
const path = require("path");
const fs = require("fs");
const localUpdateUser = require("../engine/user/update-user");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -39,6 +44,25 @@ async function updateUser({ key, payload, database }) {
*/
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env;
if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) {
/** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json");
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
console.log("Reading from local database ...");
if (dbSchema) {
return await localUpdateUser({
dbSchema: dbSchema,
payload: payload,
});
}
}
/**
* Make https request
*