This commit is contained in:
Benjamin Toby
2024-12-09 12:45:39 +01:00
parent 7bd4b2fe65
commit 586e3cfa85
43 changed files with 216 additions and 506 deletions
+17 -13
View File
@@ -36,15 +36,21 @@ async function addUser({
*
* @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 } = process.env;
const {
DSQL_DB_HOST,
DSQL_DB_USERNAME,
DSQL_DB_PASSWORD,
DSQL_DB_NAME,
DSQL_API_USER_ID,
} = process.env;
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
DSQL_DB_PASSWORD?.match(/./) &&
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
@@ -59,15 +65,13 @@ async function addUser({
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
if (dbSchema) {
return await apiCreateUser({
database: DSQL_DB_NAME,
encryptionKey,
payload,
userId: apiUserId,
useLocal,
});
}
return await apiCreateUser({
database: DSQL_DB_NAME,
encryptionKey,
payload,
userId: apiUserId,
useLocal,
});
}
/**
+2 -2
View File
@@ -5,7 +5,7 @@ export = deleteUser;
*
* @param {object} params - API Key
* @param {String} [params.key] - API Key
* @param {String} params.database - Target Database
* @param {String} [params.database] - Target Database
* @param {String | number} params.deletedUserId - Target Database
* @param {boolean} [params.user_id] - User ID
* @param {boolean} [params.useLocal]
@@ -14,7 +14,7 @@ export = deleteUser;
*/
declare function deleteUser({ key, database, user_id, useLocal, deletedUserId }: {
key?: string;
database: string;
database?: string;
deletedUserId: string | number;
user_id?: boolean;
useLocal?: boolean;
+11 -12
View File
@@ -14,7 +14,7 @@ const apiDeleteUser = require("../package-shared/functions/api/users/api-delete-
*
* @param {object} params - API Key
* @param {String} [params.key] - API Key
* @param {String} params.database - Target Database
* @param {String} [params.database] - Target Database
* @param {String | number} params.deletedUserId - Target Database
* @param {boolean} [params.user_id] - User ID
* @param {boolean} [params.useLocal]
@@ -27,15 +27,16 @@ async function deleteUser({ key, database, user_id, useLocal, deletedUserId }) {
*
* @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 } = process.env;
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } =
process.env;
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
DSQL_DB_PASSWORD?.match(/./) &&
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
@@ -50,13 +51,11 @@ async function deleteUser({ key, database, user_id, useLocal, deletedUserId }) {
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
if (dbSchema) {
return await apiDeleteUser({
dbFullName: DSQL_DB_NAME,
useLocal,
deletedUserId,
});
}
return await apiDeleteUser({
dbFullName: DSQL_DB_NAME,
useLocal,
deletedUserId,
});
}
/**
+11 -12
View File
@@ -73,12 +73,13 @@ async function getUser({ key, userId, database, fields, user_id, useLocal }) {
*
* @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 } = process.env;
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } =
process.env;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
DSQL_DB_PASSWORD?.match(/./) &&
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
@@ -93,14 +94,12 @@ async function getUser({ key, userId, database, fields, user_id, useLocal }) {
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
if (dbSchema) {
return await apiGetUser({
userId,
fields: [...new Set(updatedFields)],
dbFullName: DSQL_DB_NAME,
useLocal,
});
}
return await apiGetUser({
userId,
fields: [...new Set(updatedFields)],
dbFullName: DSQL_DB_NAME,
useLocal,
});
}
/**
+4 -4
View File
@@ -10,7 +10,7 @@ export = loginUser;
* @param {{
* email?: string,
* username?: string,
* password: string,
* password?: string,
* }} params.payload Login Email/Username and Password
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
* @param {http.ServerResponse & Object<string, any>} [params.response] - Http response object
@@ -20,7 +20,7 @@ export = loginUser;
* @param {string} [params.email_login_code] - Email login code
* @param {string} [params.temp_code_field] - Database table field name for temporary code
* @param {boolean} [params.token] - Send access key as part of response body?
* @param {boolean} [params.user_id] - User ID
* @param {string | number} [params.user_id] - User ID
* @param {boolean} [params.skipPassword]
* @param {boolean} [params.useLocal]
* @param {string | number} [params.apiUserID] - Required for setting of cookies
@@ -33,7 +33,7 @@ declare function loginUser({ key, payload, database, additionalFields, response,
payload: {
email?: string;
username?: string;
password: string;
password?: string;
};
additionalFields?: string[];
response?: http.ServerResponse & {
@@ -45,7 +45,7 @@ declare function loginUser({ key, payload, database, additionalFields, response,
email_login_code?: string;
temp_code_field?: string;
token?: boolean;
user_id?: boolean;
user_id?: string | number;
skipPassword?: boolean;
useLocal?: boolean;
apiUserID?: string | number;
+22 -23
View File
@@ -28,7 +28,7 @@ const {
* @param {{
* email?: string,
* username?: string,
* password: string,
* password?: string,
* }} params.payload Login Email/Username and Password
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
* @param {http.ServerResponse & Object<string, any>} [params.response] - Http response object
@@ -38,7 +38,7 @@ const {
* @param {string} [params.email_login_code] - Email login code
* @param {string} [params.temp_code_field] - Database table field name for temporary code
* @param {boolean} [params.token] - Send access key as part of response body?
* @param {boolean} [params.user_id] - User ID
* @param {string | number} [params.user_id] - User ID
* @param {boolean} [params.skipPassword]
* @param {boolean} [params.useLocal]
* @param {string | number} [params.apiUserID] - Required for setting of cookies
@@ -121,12 +121,13 @@ async function loginUser({
*
* @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 } = process.env;
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } =
process.env;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
DSQL_DB_PASSWORD?.match(/./) &&
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
@@ -141,22 +142,20 @@ async function loginUser({
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
if (dbSchema) {
httpResponse = await apiLoginUser({
database: process.env.DSQL_DB_NAME || "",
email: payload.email,
username: payload.username,
password: payload.password,
skipPassword,
encryptionKey: finalEncryptionKey,
additionalFields,
email_login,
email_login_code,
email_login_field: emailLoginTempCodeFieldName,
token,
useLocal,
});
}
httpResponse = await apiLoginUser({
database: process.env.DSQL_DB_NAME || "",
email: payload.email,
username: payload.username,
password: payload.password,
skipPassword,
encryptionKey: finalEncryptionKey,
additionalFields,
email_login,
email_login_code,
email_login_field: emailLoginTempCodeFieldName,
token,
useLocal,
});
} else {
/**
* Make https request
@@ -245,7 +244,7 @@ async function loginUser({
const cookieNames = getAuthCookieNames({
database,
userId: apiUserID || process.env.DSQL_API_USER_ID,
userId: apiUserID || user_id || grabedHostNames.user_id,
});
if (httpResponse.csrf) {
+4 -4
View File
@@ -13,7 +13,7 @@ export = reauthUser;
*
* @param {object} params - Single Param object containing params
* @param {String} [params.key] - API Key
* @param {String} params.database - Target Database
* @param {String} [params.database]- Target Database slug
* @param {http.ServerResponse} [params.response] - Http response object
* @param {http.IncomingMessage} [params.request] - Http request object
* @param {("deep" | "normal")} [params.level] - Authentication level
@@ -21,14 +21,14 @@ export = reauthUser;
* @param {String} [params.encryptionSalt] - Encryption Salt
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
* @param {string} [params.encryptedUserString] - encrypted user string to use instead of getting from cookie header
* @param {boolean} [params.user_id] - User ID
* @param {string | number} [params.user_id] - User ID
* @param {boolean} [params.useLocal]
*
* @returns { Promise<import("../package-shared/types").APILoginFunctionReturn> }
*/
declare function reauthUser({ key, database, response, request, level, encryptionKey, encryptionSalt, additionalFields, encryptedUserString, user_id, useLocal, }: {
key?: string;
database: string;
database?: string;
response?: http.ServerResponse;
request?: http.IncomingMessage;
level?: ("deep" | "normal");
@@ -36,7 +36,7 @@ declare function reauthUser({ key, database, response, request, level, encryptio
encryptionSalt?: string;
additionalFields?: string[];
encryptedUserString?: string;
user_id?: boolean;
user_id?: string | number;
useLocal?: boolean;
}): Promise<import("../package-shared/types").APILoginFunctionReturn>;
import http = require("http");
+16 -16
View File
@@ -35,7 +35,7 @@ const getAuthCookieNames = require("../package-shared/functions/backend/cookies/
*
* @param {object} params - Single Param object containing params
* @param {String} [params.key] - API Key
* @param {String} params.database - Target Database
* @param {String} [params.database]- Target Database slug
* @param {http.ServerResponse} [params.response] - Http response object
* @param {http.IncomingMessage} [params.request] - Http request object
* @param {("deep" | "normal")} [params.level] - Authentication level
@@ -43,7 +43,7 @@ const getAuthCookieNames = require("../package-shared/functions/backend/cookies/
* @param {String} [params.encryptionSalt] - Encryption Salt
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
* @param {string} [params.encryptedUserString] - encrypted user string to use instead of getting from cookie header
* @param {boolean} [params.user_id] - User ID
* @param {string | number} [params.user_id] - User ID
* @param {boolean} [params.useLocal]
*
* @returns { Promise<import("../package-shared/types").APILoginFunctionReturn> }
@@ -101,12 +101,13 @@ async function reauthUser({
*
* @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 } = process.env;
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } =
process.env;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
DSQL_DB_PASSWORD?.match(/./) &&
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
@@ -121,14 +122,11 @@ async function reauthUser({
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
if (dbSchema) {
httpResponse = await apiReauthUser({
existingUser: existingUser.payload,
additionalFields,
database: DSQL_DB_NAME,
useLocal,
});
}
httpResponse = await apiReauthUser({
existingUser: existingUser.payload,
additionalFields,
useLocal,
});
} else {
/**
* Make https request
@@ -203,8 +201,10 @@ async function reauthUser({
encryptionSalt: finalEncryptionSalt,
});
const { userId } = httpResponse;
const cookieNames = getAuthCookieNames({ database, userId });
const cookieNames = getAuthCookieNames({
database,
userId: user_id || grabedHostNames.user_id,
});
httpResponse["cookieNames"] = cookieNames;
httpResponse["key"] = String(encryptedPayload);
+17 -18
View File
@@ -105,12 +105,13 @@ async function sendEmailCode({
*
* @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 } = process.env;
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } =
process.env;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
DSQL_DB_PASSWORD?.match(/./) &&
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
@@ -125,20 +126,18 @@ async function sendEmailCode({
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
if (dbSchema) {
httpResponse = await apiSendEmailCode({
database: DSQL_DB_NAME,
email,
email_login_field: emailLoginTempCodeFieldName,
html: emailHtml,
mail_domain,
mail_password,
mail_port,
mail_username,
sender,
useLocal,
});
}
httpResponse = await apiSendEmailCode({
database: DSQL_DB_NAME,
email,
email_login_field: emailLoginTempCodeFieldName,
html: emailHtml,
mail_domain,
mail_password,
mail_port,
mail_username,
sender,
useLocal,
});
} else {
/**
* Make https request
+15 -17
View File
@@ -141,9 +141,9 @@ async function githubAuth({
* @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_HOST,
DSQL_DB_USERNAME,
DSQL_DB_PASSWORD,
DSQL_DB_NAME,
DSQL_KEY,
DSQL_REF_DB_NAME,
@@ -151,9 +151,9 @@ async function githubAuth({
} = process.env;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
DSQL_DB_PASSWORD?.match(/./) &&
DSQL_DB_NAME?.match(/./)
) {
/** @type {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined | undefined} */
@@ -167,17 +167,15 @@ async function githubAuth({
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
if (dbSchema) {
httpResponse = await apiGithubLogin({
code,
email: email || undefined,
clientId,
clientSecret,
additionalFields,
res: response,
database: DSQL_DB_NAME,
});
}
httpResponse = await apiGithubLogin({
code,
email: email || undefined,
clientId,
clientSecret,
additionalFields,
res: response,
database: DSQL_DB_NAME,
});
} else {
/**
* Make https request
+5 -4
View File
@@ -124,12 +124,13 @@ async function googleAuth({
*
* @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 } = process.env;
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } =
process.env;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
DSQL_DB_PASSWORD?.match(/./) &&
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
+2 -2
View File
@@ -5,7 +5,7 @@ export = updateUser;
*
* @param {object} params - API Key
* @param {String} [params.key] - API Key
* @param {String} params.database - Target Database
* @param {String} [params.database] - Target Database
* @param {String | number} params.updatedUserId - Target Database
* @param {Object.<string, any>} params.payload - User Object: ID is required
* @param {boolean} [params.user_id] - User ID
@@ -15,7 +15,7 @@ export = updateUser;
*/
declare function updateUser({ key, payload, database, user_id, useLocal, updatedUserId, }: {
key?: string;
database: string;
database?: string;
updatedUserId: string | number;
payload: {
[x: string]: any;
+13 -14
View File
@@ -13,7 +13,7 @@ const apiUpdateUser = require("../package-shared/functions/api/users/api-update-
*
* @param {object} params - API Key
* @param {String} [params.key] - API Key
* @param {String} params.database - Target Database
* @param {String} [params.database] - Target Database
* @param {String | number} params.updatedUserId - Target Database
* @param {Object.<string, any>} params.payload - User Object: ID is required
* @param {boolean} [params.user_id] - User ID
@@ -34,15 +34,16 @@ async function updateUser({
*
* @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 } = process.env;
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } =
process.env;
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
DSQL_DB_PASSWORD?.match(/./) &&
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
@@ -57,15 +58,13 @@ async function updateUser({
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
if (dbSchema) {
return await apiUpdateUser({
payload: payload,
dbFullName: DSQL_DB_NAME,
useLocal,
updatedUserId,
dbSchema,
});
}
return await apiUpdateUser({
payload: payload,
dbFullName: DSQL_DB_NAME,
useLocal,
updatedUserId,
dbSchema,
});
}
/**