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
+27 -6
View File
@@ -6,6 +6,7 @@
* ==============================================================================
*/
const https = require("https");
const http = require("http");
const path = require("path");
const fs = require("fs");
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
*/
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} */
let dbSchema;
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"));
} catch (error) {}
@@ -82,7 +103,7 @@ async function addUser({ key, payload, database }) {
database,
});
const httpsRequest = https.request(
const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request(
{
method: "POST",
headers: {
@@ -90,8 +111,8 @@ async function addUser({ key, payload, database }) {
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
port: localHostPort || 443,
hostname: localHost || "datasquirel.com",
path: `/api/user/add-user`,
},
+45 -8
View File
@@ -4,6 +4,7 @@
* ==============================================================================
*/
const https = require("https");
const http = require("http");
const getLocalUser = require("../engine/user/get-user");
/** ****************************************************************************** */
@@ -48,9 +49,25 @@ 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 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({
userId,
@@ -58,19 +75,39 @@ async function getUser({ key, userId, database, fields }) {
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
*
* @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} */
let dbSchema;
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"));
} catch (error) {}
@@ -91,7 +128,7 @@ async function getUser({ key, userId, database, fields }) {
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
const httpsRequest = https.request(
const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request(
{
method: "POST",
headers: {
@@ -99,8 +136,8 @@ async function getUser({ key, userId, database, fields }) {
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
port: localHostPort || 443,
hostname: localHost || "datasquirel.com",
path: `/api/user/get-user`,
},
+42 -8
View File
@@ -47,7 +47,19 @@ const loginLocalUser = require("../engine/user/login-user");
*
* @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
*
@@ -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
*/
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} */
let dbSchema;
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"));
} catch (error) {}
@@ -126,7 +154,9 @@ async function loginUser({ key, payload, database, additionalFields, response, e
additionalFields,
});
const httpsRequest = https.request(
const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{
method: "POST",
headers: {
@@ -134,8 +164,8 @@ async function loginUser({ key, payload, database, additionalFields, response, e
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
port: localHostPort || 443,
hostname: localHost || "datasquirel.com",
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 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`,
]);
}
/** ********************************************** */
+43 -8
View File
@@ -47,12 +47,25 @@ const localReauthUser = require("../engine/user/reauth-user");
*
* @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
*
* @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({
database,
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
*/
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} */
let dbSchema;
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"));
} catch (error) {}
@@ -112,7 +141,9 @@ async function reauthUser({ key, database, response, request, level, encryptionK
additionalFields,
});
const httpsRequest = https.request(
const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{
method: "POST",
headers: {
@@ -120,8 +151,8 @@ async function reauthUser({ key, database, response, request, level, encryptionK
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
port: localHostPort || 443,
hostname: localHost || "datasquirel.com",
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 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`,
]);
}
/** ********************************************** */
+46 -8
View File
@@ -47,12 +47,27 @@ const localGithubAuth = require("../../engine/user/social/github-auth");
*
* @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
*
* @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(/ /)) {
return {
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
*/
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} */
let dbSchema;
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"));
} catch (error) {}
@@ -164,7 +195,9 @@ async function githubAuth({ key, code, email, database, clientId, clientSecret,
additionalFields,
});
const httpsRequest = https.request(
const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{
method: "POST",
headers: {
@@ -172,8 +205,8 @@ async function githubAuth({ key, code, email, database, clientId, clientSecret,
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
port: localHostPort || 443,
hostname: localHost || "datasquirel.com",
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 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=/`,
]);
}
////////////////////////////////////////
+44 -8
View File
@@ -45,7 +45,20 @@ const localGoogleAuth = require("../../engine/user/social/google-auth");
*
* @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
*
@@ -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
*/
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} */
let dbSchema;
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"));
} catch (error) {}
@@ -162,7 +191,9 @@ async function googleAuth({ key, token, database, clientId, response, encryption
additionalFields,
});
const httpsRequest = https.request(
const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{
method: "POST",
headers: {
@@ -170,8 +201,8 @@ async function googleAuth({ key, token, database, clientId, response, encryption
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
port: localHostPort || 443,
hostname: localHost || "datasquirel.com",
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 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=/`,
]);
}
////////////////////////////////////////
+27 -6
View File
@@ -5,6 +5,7 @@
* Imports
* ==============================================================================
*/
const http = require("http");
const https = require("https");
const path = require("path");
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
*/
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} */
let dbSchema;
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"));
} catch (error) {}
@@ -74,7 +95,7 @@ async function updateUser({ key, payload, database }) {
database,
});
const httpsRequest = https.request(
const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request(
{
method: "POST",
headers: {
@@ -82,8 +103,8 @@ async function updateUser({ key, payload, database }) {
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
port: localHostPort || 443,
hostname: localHost || "datasquirel.com",
path: `/api/user/update-user`,
},