This commit is contained in:
Benjamin Toby
2024-12-06 12:55:03 +01:00
parent 34868ee0cf
commit 8a8257d17e
48 changed files with 360 additions and 221 deletions
+2 -2
View File
@@ -8,7 +8,7 @@ export = addUser;
* @param {string} param.key - FULL ACCESS API Key
* @param {string} param.database - Database Name
* @param {import("../package-shared/types").UserDataPayload} param.payload - User Data Payload
* @param {string} param.encryptionKey
* @param {string} [param.encryptionKey]
* @param {string} [param.encryptionSalt]
* @param {string | number} [param.user_id]
* @param {string | number} [param.apiUserId]
@@ -20,7 +20,7 @@ declare function addUser({ key, payload, database, encryptionKey, user_id, useLo
key: string;
database: string;
payload: import("../package-shared/types").UserDataPayload;
encryptionKey: string;
encryptionKey?: string;
encryptionSalt?: string;
user_id?: string | number;
apiUserId?: string | number;
+2 -1
View File
@@ -14,7 +14,7 @@ const apiCreateUser = require("../package-shared/functions/api/users/api-create-
* @param {string} param.key - FULL ACCESS API Key
* @param {string} param.database - Database Name
* @param {import("../package-shared/types").UserDataPayload} param.payload - User Data Payload
* @param {string} param.encryptionKey
* @param {string} [param.encryptionKey]
* @param {string} [param.encryptionSalt]
* @param {string | number} [param.user_id]
* @param {string | number} [param.apiUserId]
@@ -65,6 +65,7 @@ async function addUser({
encryptionKey,
payload,
userId: apiUserId,
useLocal,
});
}
}
+3 -1
View File
@@ -16,14 +16,16 @@ export = getToken;
* @param {string} params.encryptionKey - Encryption Key
* @param {string} params.encryptionSalt - Encryption Salt
* @param {string} params.database - Database Name
* @param {boolean} [params.useLocal]
*
* @returns {{ key: string | undefined, csrf: string | undefined }}
*/
declare function getToken({ request, encryptionKey, encryptionSalt, database }: {
declare function getToken({ request, encryptionKey, encryptionSalt, database, useLocal, }: {
request: http.IncomingMessage;
encryptionKey: string;
encryptionSalt: string;
database: string;
useLocal?: boolean;
}): {
key: string | undefined;
csrf: string | undefined;
+12 -4
View File
@@ -8,6 +8,7 @@
const http = require("http");
const decrypt = require("../package-shared/functions/dsql/decrypt");
const parseCookies = require("../utils/functions/parseCookies");
const getAuthCookieNames = require("../package-shared/functions/backend/cookies/get-auth-cookie-names");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -27,10 +28,17 @@ const parseCookies = require("../utils/functions/parseCookies");
* @param {string} params.encryptionKey - Encryption Key
* @param {string} params.encryptionSalt - Encryption Salt
* @param {string} params.database - Database Name
* @param {boolean} [params.useLocal]
*
* @returns {{ key: string | undefined, csrf: string | undefined }}
*/
function getToken({ request, encryptionKey, encryptionSalt, database }) {
function getToken({
request,
encryptionKey,
encryptionSalt,
database,
useLocal,
}) {
try {
/**
* Grab the payload
@@ -38,9 +46,9 @@ function getToken({ request, encryptionKey, encryptionSalt, database }) {
* @description Grab the payload
*/
const cookies = parseCookies({ request });
const dsqluid = cookies.dsqluid;
const authKeyName = `datasquirel_${dsqluid}_${database}_auth_key`;
const csrfName = `datasquirel_${dsqluid}_${database}_csrf`;
const keynames = getAuthCookieNames();
const authKeyName = keynames.keyCookieName;
const csrfName = keynames.csrfCookieName;
const key = cookies[authKeyName];
const csrf = cookies[csrfName];
+1
View File
@@ -98,6 +98,7 @@ async function getUser({ key, userId, database, fields, user_id, useLocal }) {
userId,
fields: [...new Set(updatedFields)],
dbFullName: DSQL_DB_NAME,
useLocal,
});
}
}
+6 -3
View File
@@ -12,6 +12,7 @@ const path = require("path");
const encrypt = require("../package-shared/functions/dsql/encrypt");
const grabHostNames = require("../package-shared/utils/grab-host-names");
const apiLoginUser = require("../package-shared/functions/api/users/api-login");
const getAuthCookieNames = require("../package-shared/functions/backend/cookies/get-auth-cookie-names");
/**
* Login A user
@@ -155,6 +156,7 @@ async function loginUser({
email_login_code,
email_login_field: emailLoginTempCodeFieldName,
token,
useLocal,
});
}
} else {
@@ -244,13 +246,14 @@ async function loginUser({
const { userId } = httpResponse;
const authKeyName = `datasquirel_${userId}_${database}_auth_key`;
const csrfName = `datasquirel_${userId}_${database}_csrf`;
const cookieNames = getAuthCookieNames();
const authKeyName = cookieNames.keyCookieName;
const csrfName = cookieNames.csrfCookieName;
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`,
]);
}
+32 -17
View File
@@ -2,6 +2,7 @@
const http = require("http");
const parseCookies = require("../utils/functions/parseCookies");
const getAuthCookieNames = require("../package-shared/functions/backend/cookies/get-auth-cookie-names");
/**
* Logout user
@@ -25,34 +26,48 @@ function logoutUser({ request, response, database }) {
const cookies = parseCookies({ request });
const cookiesKeys = Object.keys(cookies);
const dbUid = cookies.dsqluid;
const keyRegexp = new RegExp(`datasquirel_${dbUid}_${database}_auth_key`);
const csrfRegexp = new RegExp(`datasquirel_${dbUid}_${database}_csrf`);
const keyNames = getAuthCookieNames();
const authKeyName = cookiesKeys.filter((cookieKey) => cookieKey.match(keyRegexp))[0];
const csrfName = cookiesKeys.filter((cookieKey) => cookieKey.match(csrfRegexp))[0];
const keyRegexp = new RegExp(keyNames.keyCookieName);
const csrfRegexp = new RegExp(keyNames.csrfCookieName);
const authKeyName = cookiesKeys.filter((cookieKey) =>
cookieKey.match(keyRegexp)
)[0];
const csrfName = cookiesKeys.filter((cookieKey) =>
cookieKey.match(csrfRegexp)
)[0];
if (authKeyName && csrfName) {
response.setHeader("Set-Cookie", [`${authKeyName}=null;samesite=strict;path=/;HttpOnly=true;Secure=true`, `${csrfName}=null;samesite=strict;path=/;HttpOnly=true`, `dsqluid=null;samesite=strict;path=/;HttpOnly=true`]);
response.setHeader("Set-Cookie", [
`${authKeyName}=null;max-age=0`,
`${csrfName}=null;max-age=0`,
]);
} else {
const allKeys = cookiesKeys.filter((cookieKey) => cookieKey.match(/datasquirel_.*_auth_key/));
const allCsrfs = cookiesKeys.filter((cookieKey) => cookieKey.match(/datasquirel_.*_csrf/));
const allKeys = cookiesKeys.filter((cookieKey) =>
cookieKey.match(/datasquirel_.*_auth_key/)
);
const allCsrfs = cookiesKeys.filter((cookieKey) =>
cookieKey.match(/datasquirel_.*_csrf/)
);
response.setHeader("Set-Cookie", [...allKeys.map((key) => `${key}=null;samesite=strict;path=/;HttpOnly=true;Secure=true`), ...allCsrfs.map((csrf) => `${csrf}=null;samesite=strict;path=/;HttpOnly=true`), `dsqluid=null;samesite=strict;path=/;HttpOnly=true`]);
response.setHeader("Set-Cookie", [
...allKeys.map(
(key) =>
`${key}=null;samesite=strict;path=/;HttpOnly=true;Secure=true`
),
...allCsrfs.map(
(csrf) =>
`${csrf}=null;samesite=strict;path=/;HttpOnly=true`
),
`dsqluid=null;samesite=strict;path=/;HttpOnly=true`,
]);
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return {
success: true,
payload: "User Logged Out",
};
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
} catch (error) {
console.log(error);
+3 -1
View File
@@ -22,10 +22,11 @@ export = reauthUser;
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
* @param {string} [params.token] - access token to use instead of getting from cookie header
* @param {boolean} [params.user_id] - User ID
* @param {boolean} [params.useLocal]
*
* @returns { Promise<import("../package-shared/types").ReauthUserFunctionReturn> }
*/
declare function reauthUser({ key, database, response, request, level, encryptionKey, encryptionSalt, additionalFields, token, user_id, }: {
declare function reauthUser({ key, database, response, request, level, encryptionKey, encryptionSalt, additionalFields, token, user_id, useLocal, }: {
key: string;
database: string;
response: http.ServerResponse;
@@ -36,5 +37,6 @@ declare function reauthUser({ key, database, response, request, level, encryptio
additionalFields?: string[];
token?: string;
user_id?: boolean;
useLocal?: boolean;
}): Promise<import("../package-shared/types").ReauthUserFunctionReturn>;
import http = require("http");
+6 -10
View File
@@ -39,6 +39,7 @@ const apiReauthUser = require("../package-shared/functions/api/users/api-reauth-
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
* @param {string} [params.token] - access token to use instead of getting from cookie header
* @param {boolean} [params.user_id] - User ID
* @param {boolean} [params.useLocal]
*
* @returns { Promise<import("../package-shared/types").ReauthUserFunctionReturn> }
*/
@@ -53,6 +54,7 @@ async function reauthUser({
additionalFields,
token,
user_id,
useLocal,
}) {
/**
* Check Encryption Keys
@@ -89,21 +91,14 @@ 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,
DSQL_KEY,
DSQL_REF_DB_NAME,
DSQL_FULL_SYNC,
} = process.env;
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME } = process.env;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
@@ -121,6 +116,7 @@ async function reauthUser({
existingUser: existingUser.payload,
additionalFields,
database: DSQL_DB_NAME,
useLocal,
});
}
} else {
+3 -1
View File
@@ -24,10 +24,11 @@ export = sendEmailCode;
* @param {number} [params.mail_port]
* @param {string} [params.sender]
* @param {boolean} [params.user_id] - User ID
* @param {boolean} [params.useLocal]
*
* @returns { Promise<boolean>}
*/
declare function sendEmailCode({ key, email, database, encryptionKey, encryptionSalt, temp_code_field, mail_domain, mail_password, mail_username, mail_port, sender, user_id, }: {
declare function sendEmailCode({ key, email, database, encryptionKey, encryptionSalt, temp_code_field, mail_domain, mail_password, mail_username, mail_port, sender, user_id, useLocal, }: {
key: string;
database: string;
email: string;
@@ -41,5 +42,6 @@ declare function sendEmailCode({ key, email, database, encryptionKey, encryption
mail_port?: number;
sender?: string;
user_id?: boolean;
useLocal?: boolean;
}): Promise<boolean>;
import http = require("http");
+6 -10
View File
@@ -38,6 +38,7 @@ const apiSendEmailCode = require("../package-shared/functions/api/users/api-send
* @param {number} [params.mail_port]
* @param {string} [params.sender]
* @param {boolean} [params.user_id] - User ID
* @param {boolean} [params.useLocal]
*
* @returns { Promise<boolean>}
*/
@@ -54,6 +55,7 @@ async function sendEmailCode({
mail_port,
sender,
user_id,
useLocal,
}) {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
@@ -103,21 +105,14 @@ 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,
DSQL_KEY,
DSQL_REF_DB_NAME,
DSQL_FULL_SYNC,
} = process.env;
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME } = process.env;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
@@ -141,6 +136,7 @@ async function sendEmailCode({
mail_port,
mail_username,
sender,
useLocal,
});
}
} else {
+3 -1
View File
@@ -8,10 +8,11 @@ export = updateUser;
* @param {String} params.database - Target Database
* @param {{ id: number } & Object.<string, any>} params.payload - User Object: ID is required
* @param {boolean} [params.user_id] - User ID
* @param {boolean} [params.useLocal]
*
* @returns { Promise<import("../package-shared/types").UpdateUserFunctionReturn>}
*/
declare function updateUser({ key, payload, database, user_id }: {
declare function updateUser({ key, payload, database, user_id, useLocal }: {
key: string;
database: string;
payload: {
@@ -20,4 +21,5 @@ declare function updateUser({ key, payload, database, user_id }: {
[x: string]: any;
};
user_id?: boolean;
useLocal?: boolean;
}): Promise<import("../package-shared/types").UpdateUserFunctionReturn>;
+6 -11
View File
@@ -16,24 +16,17 @@ const apiUpdateUser = require("../package-shared/functions/api/users/api-update-
* @param {String} params.database - Target Database
* @param {{ id: number } & Object.<string, any>} params.payload - User Object: ID is required
* @param {boolean} [params.user_id] - User ID
* @param {boolean} [params.useLocal]
*
* @returns { Promise<import("../package-shared/types").UpdateUserFunctionReturn>}
*/
async function updateUser({ key, payload, database, user_id }) {
async function updateUser({ key, payload, database, user_id, useLocal }) {
/**
* 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 } = process.env;
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
@@ -42,7 +35,8 @@ async function updateUser({ key, payload, database, user_id }) {
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
@@ -59,6 +53,7 @@ async function updateUser({ key, payload, database, user_id }) {
return await apiUpdateUser({
payload: payload,
dbFullName: DSQL_DB_NAME,
useLocal,
});
}
}
-6
View File
@@ -1,10 +1,4 @@
export = userAuth;
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* Authenticate User from request
* ==============================================================================
+6 -27
View File
@@ -1,20 +1,9 @@
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const http = require("http");
const decrypt = require("../package-shared/functions/dsql/decrypt");
const parseCookies = require("../utils/functions/parseCookies");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
const getAuthCookieNames = require("../package-shared/functions/backend/cookies/get-auth-cookie-names");
/**
* Authenticate User from request
@@ -47,9 +36,11 @@ function userAuth({
* @description Grab the payload
*/
const cookies = parseCookies({ request });
const dsqluid = cookies.dsqluid;
const authKeyName = `datasquirel_${dsqluid}_${database}_auth_key`;
const csrfName = `datasquirel_${dsqluid}_${database}_csrf`;
const keyNames = getAuthCookieNames();
const authKeyName = keyNames.keyCookieName;
const csrfName = keyNames.csrfCookieName;
const key = token ? token : cookies[authKeyName];
const csrf = cookies[csrfName];
@@ -93,10 +84,6 @@ function userAuth({
};
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
/**
* Grab the payload
*
@@ -113,10 +100,6 @@ function userAuth({
};
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
/**
* Return User Object
*
@@ -140,8 +123,4 @@ function userAuth({
}
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
module.exports = userAuth;
+1 -7
View File
@@ -1,13 +1,7 @@
export = validateToken;
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* Validate Token
* ==============================================================================
* ======================================
* @description This Function takes in a encrypted token and returns a user object
*
* @param {Object} params - Arg
+1 -21
View File
@@ -1,23 +1,11 @@
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const http = require("http");
const decrypt = require("../package-shared/functions/dsql/decrypt");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* Validate Token
* ==============================================================================
* ======================================
* @description This Function takes in a encrypted token and returns a user object
*
* @param {Object} params - Arg
@@ -69,10 +57,6 @@ function validateToken({ token, encryptionKey, encryptionSalt }) {
return null;
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
/**
* Return User Object
*
@@ -89,8 +73,4 @@ function validateToken({ token, encryptionKey, encryptionSalt }) {
}
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
module.exports = validateToken;