This commit is contained in:
Benjamin Toby 2023-09-21 17:51:08 +01:00
parent 44b013147e
commit 069db84289
17 changed files with 414 additions and 91 deletions

View File

@ -4,6 +4,7 @@
* Imports * Imports
*/ */
const https = require("https"); const https = require("https");
const http = require("http");
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
@ -38,6 +39,10 @@ async function updateApiSchemaFromLocalDb() {
const dbSchema = JSON.parse(fs.readFileSync(dbSchemaPath, "utf8")); const dbSchema = JSON.parse(fs.readFileSync(dbSchemaPath, "utf8"));
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
/** /**
* Make https request * Make https request
* *
@ -63,7 +68,9 @@ async function updateApiSchemaFromLocalDb() {
const reqPayload = reqPayloadString; const reqPayload = reqPayloadString;
const httpsRequest = https.request( const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -71,8 +78,8 @@ async function updateApiSchemaFromLocalDb() {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: 443, port: localHostPort || 443,
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: `/api/query/update-schema-from-single-database`, path: `/api/query/update-schema-from-single-database`,
}, },

View File

@ -48,12 +48,22 @@ const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
* *
* @returns { Promise<FunctionReturn> } * @returns { Promise<FunctionReturn> }
*/ */
async function localGoogleAuth({ dbSchema, token, clientId, response, additionalFields }) { async function localGoogleAuth({
dbSchema,
token,
clientId,
response,
additionalFields,
}) {
/** /**
* Send Response * Send Response
* *
* @description Send a boolean response * @description Send a boolean response
*/ */
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
try { try {
/** /**
* Grab User data * Grab User data
@ -63,7 +73,7 @@ async function localGoogleAuth({ dbSchema, token, clientId, response, additional
*/ */
const payloadResponse = await httpsRequest({ const payloadResponse = await httpsRequest({
method: "POST", method: "POST",
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: "/user/grab-google-user-from-token", path: "/user/grab-google-user-from-token",
body: { body: {
token: token, token: token,
@ -114,7 +124,8 @@ async function localGoogleAuth({ dbSchema, token, clientId, response, additional
}; };
} }
const { given_name, family_name, email, sub, picture, email_verified } = payload; const { given_name, family_name, email, sub, picture, email_verified } =
payload;
const payloadObject = { const payloadObject = {
email: email || "", email: email || "",

View File

@ -3,6 +3,7 @@
* ============================================================================== * ==============================================================================
*/ */
const https = require("https"); const https = require("https");
const http = require("http");
////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////
@ -27,14 +28,18 @@ const https = require("https");
function httpsRequest({ url, method, hostname, path, href, headers, body }) { function httpsRequest({ url, method, hostname, path, href, headers, body }) {
const reqPayloadString = body ? JSON.stringify(body) : null; const reqPayloadString = body ? JSON.stringify(body) : null;
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
//////////////////////////////////////////////// ////////////////////////////////////////////////
//////////////////////////////////////////////// ////////////////////////////////////////////////
//////////////////////////////////////////////// ////////////////////////////////////////////////
let requestOptions = { let requestOptions = {
method: method, method: method,
hostname: hostname, hostname: localHost || hostname,
port: 443, port: localHostPort || 443,
headers: {}, headers: {},
}; };
@ -44,7 +49,9 @@ function httpsRequest({ url, method, hostname, path, href, headers, body }) {
if (headers) requestOptions.headers = headers; if (headers) requestOptions.headers = headers;
if (body) { if (body) {
requestOptions.headers["Content-Type"] = "application/json"; requestOptions.headers["Content-Type"] = "application/json";
requestOptions.headers["Content-Length"] = Buffer.from(reqPayloadString || "").length; requestOptions.headers["Content-Length"] = Buffer.from(
reqPayloadString || ""
).length;
} }
//////////////////////////////////////////////// ////////////////////////////////////////////////
@ -52,7 +59,7 @@ function httpsRequest({ url, method, hostname, path, href, headers, body }) {
//////////////////////////////////////////////// ////////////////////////////////////////////////
return new Promise((res, rej) => { return new Promise((res, rej) => {
const httpsRequest = https.request( const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request(
/* ====== Request Options object ====== */ /* ====== Request Options object ====== */
// @ts-ignore // @ts-ignore
url ? url : requestOptions, url ? url : requestOptions,

View File

@ -1,6 +1,6 @@
{ {
"name": "datasquirel", "name": "datasquirel",
"version": "1.9.2", "version": "1.9.3",
"description": "Cloud-based SQL data management tool", "description": "Cloud-based SQL data management tool",
"main": "index.js", "main": "index.js",
"bin": { "bin": {

View File

@ -6,6 +6,7 @@
* ============================================================================== * ==============================================================================
*/ */
const https = require("https"); const https = require("https");
const http = require("http");
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const localAddUser = require("../engine/user/add-user"); const localAddUser = require("../engine/user/add-user");
@ -50,14 +51,34 @@ async function addUser({ key, payload, database }) {
* *
* @description Look for local db settings in `.env` file and by pass the http request if available * @description Look for local db settings in `.env` file and by pass the http request if available
*/ */
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; 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(/./)) { const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
) {
/** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ /** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema; let dbSchema;
try { try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); const localDbSchemaPath = path.resolve(
process.cwd(),
"dsql.schema.json"
);
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {} } catch (error) {}
@ -82,7 +103,7 @@ async function addUser({ key, payload, database }) {
database, database,
}); });
const httpsRequest = https.request( const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -90,8 +111,8 @@ async function addUser({ key, payload, database }) {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: 443, port: localHostPort || 443,
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: `/api/user/add-user`, path: `/api/user/add-user`,
}, },

View File

@ -4,6 +4,7 @@
* ============================================================================== * ==============================================================================
*/ */
const https = require("https"); const https = require("https");
const http = require("http");
const getLocalUser = require("../engine/user/get-user"); const getLocalUser = require("../engine/user/get-user");
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -48,9 +49,25 @@ async function getUser({ key, userId, database, fields }) {
/** /**
* Initialize * 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 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 updatedFields =
fields && fields[0] ? [...defaultFields, ...fields] : defaultFields;
const reqPayload = JSON.stringify({ const reqPayload = JSON.stringify({
userId, userId,
@ -58,19 +75,39 @@ async function getUser({ key, userId, database, fields }) {
fields: [...new Set(updatedFields)], fields: [...new Set(updatedFields)],
}); });
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
/** /**
* Check for local DB settings * Check for local DB settings
* *
* @description Look for local db settings in `.env` file and by pass the http request if available * @description Look for local db settings in `.env` file and by pass the http request if available
*/ */
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; 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(/./)) { if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
) {
/** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ /** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema; let dbSchema;
try { try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); const localDbSchemaPath = path.resolve(
process.cwd(),
"dsql.schema.json"
);
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {} } catch (error) {}
@ -91,7 +128,7 @@ async function getUser({ key, userId, database, fields }) {
* @description make a request to datasquirel.com * @description make a request to datasquirel.com
*/ */
const httpResponse = await new Promise((resolve, reject) => { const httpResponse = await new Promise((resolve, reject) => {
const httpsRequest = https.request( const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -99,8 +136,8 @@ async function getUser({ key, userId, database, fields }) {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: 443, port: localHostPort || 443,
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: `/api/user/get-user`, path: `/api/user/get-user`,
}, },

View File

@ -47,7 +47,19 @@ const loginLocalUser = require("../engine/user/login-user");
* *
* @returns { Promise<AuthenticatedUser>} * @returns { Promise<AuthenticatedUser>}
*/ */
async function loginUser({ key, payload, database, additionalFields, response, encryptionKey, encryptionSalt }) { async function loginUser({
key,
payload,
database,
additionalFields,
response,
encryptionKey,
encryptionSalt,
}) {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
/** /**
* Check Encryption Keys * Check Encryption Keys
* *
@ -91,14 +103,30 @@ async function loginUser({ key, payload, database, additionalFields, response, e
* *
* @description Look for local db settings in `.env` file and by pass the http request if available * @description Look for local db settings in `.env` file and by pass the http request if available
*/ */
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; 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(/./)) { if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
) {
/** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ /** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema; let dbSchema;
try { try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); const localDbSchemaPath = path.resolve(
process.cwd(),
"dsql.schema.json"
);
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {} } catch (error) {}
@ -126,7 +154,9 @@ async function loginUser({ key, payload, database, additionalFields, response, e
additionalFields, additionalFields,
}); });
const httpsRequest = https.request( const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -134,8 +164,8 @@ async function loginUser({ key, payload, database, additionalFields, response, e
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: 443, port: localHostPort || 443,
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: `/api/user/login-user`, path: `/api/user/login-user`,
}, },
@ -187,7 +217,11 @@ async function loginUser({ key, payload, database, additionalFields, response, e
const authKeyName = `datasquirel_${userId}_${database}_auth_key`; const authKeyName = `datasquirel_${userId}_${database}_auth_key`;
const csrfName = `datasquirel_${userId}_${database}_csrf`; const csrfName = `datasquirel_${userId}_${database}_csrf`;
response.setHeader("Set-Cookie", [`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, `${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true`, `dsqluid=${userId};samesite=strict;path=/;HttpOnly=true`]); response.setHeader("Set-Cookie", [
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true`,
`dsqluid=${userId};samesite=strict;path=/;HttpOnly=true`,
]);
} }
/** ********************************************** */ /** ********************************************** */

View File

@ -47,12 +47,25 @@ const localReauthUser = require("../engine/user/reauth-user");
* *
* @returns { Promise<FunctionReturn> } * @returns { Promise<FunctionReturn> }
*/ */
async function reauthUser({ key, database, response, request, level, encryptionKey, encryptionSalt, additionalFields }) { async function reauthUser({
key,
database,
response,
request,
level,
encryptionKey,
encryptionSalt,
additionalFields,
}) {
/** /**
* Check Encryption Keys * Check Encryption Keys
* *
* @description Check Encryption Keys * @description Check Encryption Keys
*/ */
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const existingUser = userAuth({ const existingUser = userAuth({
database, database,
encryptionKey, encryptionKey,
@ -79,14 +92,30 @@ async function reauthUser({ key, database, response, request, level, encryptionK
* *
* @description Look for local db settings in `.env` file and by pass the http request if available * @description Look for local db settings in `.env` file and by pass the http request if available
*/ */
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; 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(/./)) { if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
) {
/** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ /** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema; let dbSchema;
try { try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); const localDbSchemaPath = path.resolve(
process.cwd(),
"dsql.schema.json"
);
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {} } catch (error) {}
@ -112,7 +141,9 @@ async function reauthUser({ key, database, response, request, level, encryptionK
additionalFields, additionalFields,
}); });
const httpsRequest = https.request( const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -120,8 +151,8 @@ async function reauthUser({ key, database, response, request, level, encryptionK
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: 443, port: localHostPort || 443,
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: `/api/user/reauth-user`, path: `/api/user/reauth-user`,
}, },
@ -173,7 +204,11 @@ async function reauthUser({ key, database, response, request, level, encryptionK
const authKeyName = `datasquirel_${userId}_${database}_auth_key`; const authKeyName = `datasquirel_${userId}_${database}_auth_key`;
const csrfName = `datasquirel_${userId}_${database}_csrf`; const csrfName = `datasquirel_${userId}_${database}_csrf`;
response.setHeader("Set-Cookie", [`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, `${csrfName}=${httpResponse.payload.csrf_k};samesite=strict;path=/;HttpOnly=true`, `dsqluid=${userId};samesite=strict;path=/;HttpOnly=true`]); response.setHeader("Set-Cookie", [
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
`${csrfName}=${httpResponse.payload.csrf_k};samesite=strict;path=/;HttpOnly=true`,
`dsqluid=${userId};samesite=strict;path=/;HttpOnly=true`,
]);
} }
/** ********************************************** */ /** ********************************************** */

View File

@ -47,12 +47,27 @@ const localGithubAuth = require("../../engine/user/social/github-auth");
* *
* @returns { Promise<FunctionReturn | undefined> } * @returns { Promise<FunctionReturn | undefined> }
*/ */
async function githubAuth({ key, code, email, database, clientId, clientSecret, response, encryptionKey, encryptionSalt, additionalFields }) { async function githubAuth({
key,
code,
email,
database,
clientId,
clientSecret,
response,
encryptionKey,
encryptionSalt,
additionalFields,
}) {
/** /**
* Check inputs * Check inputs
* *
* @description Check inputs * @description Check inputs
*/ */
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
if (!key || key?.match(/ /)) { if (!key || key?.match(/ /)) {
return { return {
success: false, success: false,
@ -123,14 +138,30 @@ async function githubAuth({ key, code, email, database, clientId, clientSecret,
* *
* @description Look for local db settings in `.env` file and by pass the http request if available * @description Look for local db settings in `.env` file and by pass the http request if available
*/ */
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; 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(/./)) { if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
) {
/** @type {import("../../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ /** @type {import("../../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema; let dbSchema;
try { try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); const localDbSchemaPath = path.resolve(
process.cwd(),
"dsql.schema.json"
);
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {} } catch (error) {}
@ -164,7 +195,9 @@ async function githubAuth({ key, code, email, database, clientId, clientSecret,
additionalFields, additionalFields,
}); });
const httpsRequest = https.request( const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -172,8 +205,8 @@ async function githubAuth({ key, code, email, database, clientId, clientSecret,
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: 443, port: localHostPort || 443,
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: `/api/user/github-login`, path: `/api/user/github-login`,
}, },
@ -234,7 +267,12 @@ async function githubAuth({ key, code, email, database, clientId, clientSecret,
const authKeyName = `datasquirel_${dsqlUserId}_${database}_auth_key`; const authKeyName = `datasquirel_${dsqlUserId}_${database}_auth_key`;
const csrfName = `datasquirel_${dsqlUserId}_${database}_csrf`; const csrfName = `datasquirel_${dsqlUserId}_${database}_csrf`;
response.setHeader("Set-Cookie", [`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, `${csrfName}=${user.csrf_k};samesite=strict;path=/;HttpOnly=true`, `dsqluid=${dsqlUserId};samesite=strict;path=/;HttpOnly=true`, `datasquirel_social_id=${user.social_id};samesite=strict;path=/`]); response.setHeader("Set-Cookie", [
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
`${csrfName}=${user.csrf_k};samesite=strict;path=/;HttpOnly=true`,
`dsqluid=${dsqlUserId};samesite=strict;path=/;HttpOnly=true`,
`datasquirel_social_id=${user.social_id};samesite=strict;path=/`,
]);
} }
//////////////////////////////////////// ////////////////////////////////////////

View File

@ -45,7 +45,20 @@ const localGoogleAuth = require("../../engine/user/social/google-auth");
* *
* @returns { Promise<FunctionReturn> } * @returns { Promise<FunctionReturn> }
*/ */
async function googleAuth({ key, token, database, clientId, response, encryptionKey, encryptionSalt, additionalFields }) { async function googleAuth({
key,
token,
database,
clientId,
response,
encryptionKey,
encryptionSalt,
additionalFields,
}) {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
/** /**
* Check inputs * Check inputs
* *
@ -121,14 +134,30 @@ async function googleAuth({ key, token, database, clientId, response, encryption
* *
* @description Look for local db settings in `.env` file and by pass the http request if available * @description Look for local db settings in `.env` file and by pass the http request if available
*/ */
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; 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(/./)) { if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
) {
/** @type {import("../../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ /** @type {import("../../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema; let dbSchema;
try { try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); const localDbSchemaPath = path.resolve(
process.cwd(),
"dsql.schema.json"
);
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {} } catch (error) {}
@ -162,7 +191,9 @@ async function googleAuth({ key, token, database, clientId, response, encryption
additionalFields, additionalFields,
}); });
const httpsRequest = https.request( const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -170,8 +201,8 @@ async function googleAuth({ key, token, database, clientId, response, encryption
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: 443, port: localHostPort || 443,
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: `/api/user/google-login`, path: `/api/user/google-login`,
}, },
@ -222,7 +253,12 @@ async function googleAuth({ key, token, database, clientId, response, encryption
const authKeyName = `datasquirel_${dsqlUserId}_${database}_auth_key`; const authKeyName = `datasquirel_${dsqlUserId}_${database}_auth_key`;
const csrfName = `datasquirel_${dsqlUserId}_${database}_csrf`; const csrfName = `datasquirel_${dsqlUserId}_${database}_csrf`;
response.setHeader("Set-Cookie", [`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, `${csrfName}=${user.csrf_k};samesite=strict;path=/;HttpOnly=true`, `dsqluid=${dsqlUserId};samesite=strict;path=/;HttpOnly=true`, `datasquirel_social_id=${user.social_id};samesite=strict;path=/`]); response.setHeader("Set-Cookie", [
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
`${csrfName}=${user.csrf_k};samesite=strict;path=/;HttpOnly=true`,
`dsqluid=${dsqlUserId};samesite=strict;path=/;HttpOnly=true`,
`datasquirel_social_id=${user.social_id};samesite=strict;path=/`,
]);
} }
//////////////////////////////////////// ////////////////////////////////////////

View File

@ -5,6 +5,7 @@
* Imports * Imports
* ============================================================================== * ==============================================================================
*/ */
const http = require("http");
const https = require("https"); const https = require("https");
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
@ -42,14 +43,34 @@ async function updateUser({ key, payload, database }) {
* *
* @description Look for local db settings in `.env` file and by pass the http request if available * @description Look for local db settings in `.env` file and by pass the http request if available
*/ */
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; 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(/./)) { const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
) {
/** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ /** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema; let dbSchema;
try { try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); const localDbSchemaPath = path.resolve(
process.cwd(),
"dsql.schema.json"
);
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {} } catch (error) {}
@ -74,7 +95,7 @@ async function updateUser({ key, payload, database }) {
database, database,
}); });
const httpsRequest = https.request( const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -82,8 +103,8 @@ async function updateUser({ key, payload, database }) {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: 443, port: localHostPort || 443,
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: `/api/user/update-user`, path: `/api/user/update-user`,
}, },

View File

@ -3,6 +3,7 @@
* Imports * Imports
* ============================================================================== * ==============================================================================
*/ */
const http = require("http");
const https = require("https"); const https = require("https");
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -35,6 +36,10 @@ const https = require("https");
* @returns { Promise<FunctionReturn> } - Image Url * @returns { Promise<FunctionReturn> } - Image Url
*/ */
async function uploadImage({ key, url }) { async function uploadImage({ key, url }) {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
try { try {
/** /**
* Make https request * Make https request
@ -44,7 +49,9 @@ async function uploadImage({ key, url }) {
const httpResponse = await new Promise((resolve, reject) => { const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({ url: url }); const reqPayload = JSON.stringify({ url: url });
const httpsRequest = https.request( const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -52,8 +59,8 @@ async function uploadImage({ key, url }) {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: 443, port: localHostPort || 443,
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: `/api/query/delete-file`, path: `/api/query/delete-file`,
}, },

View File

@ -5,6 +5,7 @@
* Imports * Imports
* ============================================================================== * ==============================================================================
*/ */
const http = require("http");
const https = require("https"); const https = require("https");
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -32,13 +33,17 @@ const https = require("https");
* @returns { Promise<GetSchemaReturn> } - Return Object * @returns { Promise<GetSchemaReturn> } - Return Object
*/ */
async function getSchema({ key, database }) { async function getSchema({ key, database }) {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
/** /**
* Make https request * Make https request
* *
* @description make a request to datasquirel.com * @description make a request to datasquirel.com
*/ */
const httpResponse = await new Promise((resolve, reject) => { const httpResponse = await new Promise((resolve, reject) => {
https (scheme?.match(/^http$/i) ? http : https)
.request( .request(
{ {
method: "GET", method: "GET",
@ -46,9 +51,11 @@ async function getSchema({ key, database }) {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: key, Authorization: key,
}, },
port: 443, port: localHostPort || 443,
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: "/api/query/get-schema" + (database ? `?database=${database}` : ""), path:
"/api/query/get-schema" +
(database ? `?database=${database}` : ""),
}, },
/** /**

View File

@ -5,6 +5,7 @@
* Imports * Imports
* ============================================================================== * ==============================================================================
*/ */
const http = require("http");
const https = require("https"); const https = require("https");
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
@ -40,19 +41,39 @@ const localGet = require("../engine/query/get");
* @returns { Promise<GetReturn> } - Return Object * @returns { Promise<GetReturn> } - Return Object
*/ */
async function get({ key, db, query, queryValues, tableName }) { async function get({ key, db, query, queryValues, tableName }) {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
/** /**
* Check for local DB settings * Check for local DB settings
* *
* @description Look for local db settings in `.env` file and by pass the http request if available * @description Look for local db settings in `.env` file and by pass the http request if available
*/ */
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; 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(/./)) { if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
) {
/** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ /** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema; let dbSchema;
try { try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); const localDbSchemaPath = path.resolve(
process.cwd(),
"dsql.schema.json"
);
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {} } catch (error) {}
@ -80,10 +101,12 @@ async function get({ key, db, query, queryValues, tableName }) {
.replace(/ /g, "+")}`; .replace(/ /g, "+")}`;
if (queryValues) { if (queryValues) {
path += `&queryValues=${JSON.stringify(queryValues)}${tableName ? `&tableName=${tableName}` : ""}`; path += `&queryValues=${JSON.stringify(queryValues)}${
tableName ? `&tableName=${tableName}` : ""
}`;
} }
https (scheme?.match(/^http$/i) ? http : https)
.request( .request(
{ {
method: "GET", method: "GET",
@ -91,8 +114,8 @@ async function get({ key, db, query, queryValues, tableName }) {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: key, Authorization: key,
}, },
port: 443, port: localHostPort || 443,
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: path, path: path,
}, },

View File

@ -3,6 +3,7 @@
/** /**
* Imports * Imports
*/ */
const http = require("http");
const https = require("https"); const https = require("https");
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
@ -52,19 +53,39 @@ const localPost = require("../engine/query/post");
* @returns { Promise<PostReturn> } - Return Object * @returns { Promise<PostReturn> } - Return Object
*/ */
async function post({ key, query, queryValues, database, tableName }) { async function post({ key, query, queryValues, database, tableName }) {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
/** /**
* Check for local DB settings * Check for local DB settings
* *
* @description Look for local db settings in `.env` file and by pass the http request if available * @description Look for local db settings in `.env` file and by pass the http request if available
*/ */
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; 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(/./)) { if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
) {
/** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ /** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema; let dbSchema;
try { try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); const localDbSchemaPath = path.resolve(
process.cwd(),
"dsql.schema.json"
);
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {} } catch (error) {}
@ -108,7 +129,7 @@ async function post({ key, query, queryValues, database, tableName }) {
const reqPayload = reqPayloadString; const reqPayload = reqPayloadString;
const httpsRequest = https.request( const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -116,8 +137,8 @@ async function post({ key, query, queryValues, database, tableName }) {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: 443, port: localHostPort || 443,
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: `/api/query/post`, path: `/api/query/post`,
}, },

View File

@ -1,8 +1,11 @@
// @ts-check
/** /**
* ============================================================================== * ==============================================================================
* Imports * Imports
* ============================================================================== * ==============================================================================
*/ */
const http = require("http");
const https = require("https"); const https = require("https");
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -17,7 +20,7 @@ const https = require("https");
* @property {boolean} success - Did the function run successfully? * @property {boolean} success - Did the function run successfully?
* @property {{ * @property {{
* urlPath: string, * urlPath: string,
* }} payload - Payload containing the url for the image and its thumbnail * } | null} payload - Payload containing the url for the image and its thumbnail
* @property {string} [msg] - An optional message * @property {string} [msg] - An optional message
*/ */
@ -40,6 +43,10 @@ const https = require("https");
* @returns { Promise<FunctionReturn> } - Return Object * @returns { Promise<FunctionReturn> } - Return Object
*/ */
async function uploadImage({ key, payload }) { async function uploadImage({ key, payload }) {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
try { try {
/** /**
* Make https request * Make https request
@ -49,7 +56,9 @@ async function uploadImage({ key, payload }) {
const httpResponse = await new Promise((resolve, reject) => { const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify(payload); const reqPayload = JSON.stringify(payload);
const httpsRequest = https.request( const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -57,8 +66,8 @@ async function uploadImage({ key, payload }) {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: 443, port: localHostPort || 443,
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: `/api/query/add-file`, path: `/api/query/add-file`,
}, },

View File

@ -1,8 +1,11 @@
// @ts-check
/** /**
* ============================================================================== * ==============================================================================
* Imports * Imports
* ============================================================================== * ==============================================================================
*/ */
const http = require("http");
const https = require("https"); const https = require("https");
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -18,7 +21,7 @@ const https = require("https");
* @property {{ * @property {{
* urlPath: string, * urlPath: string,
* urlThumbnailPath: string * urlThumbnailPath: string
* }} payload - Payload containing the url for the image and its thumbnail * } | null} payload - Payload containing the url for the image and its thumbnail
* @property {string} [msg] - An optional message * @property {string} [msg] - An optional message
*/ */
@ -42,6 +45,10 @@ const https = require("https");
* @returns { Promise<FunctionReturn> } - Return Object * @returns { Promise<FunctionReturn> } - Return Object
*/ */
async function uploadImage({ key, payload }) { async function uploadImage({ key, payload }) {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
try { try {
/** /**
* Make https request * Make https request
@ -51,7 +58,9 @@ async function uploadImage({ key, payload }) {
const httpResponse = await new Promise((resolve, reject) => { const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify(payload); const reqPayload = JSON.stringify(payload);
const httpsRequest = https.request( const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -59,8 +68,8 @@ async function uploadImage({ key, payload }) {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: 443, port: localHostPort || 443,
hostname: "datasquirel.com", hostname: localHost || "datasquirel.com",
path: `/api/query/add-image`, path: `/api/query/add-image`,
}, },