This commit is contained in:
Benjamin Toby 2024-12-13 14:08:41 +01:00
parent 357365a1ba
commit d98deb06c5
10 changed files with 38 additions and 17 deletions

View File

@ -17,7 +17,7 @@ const http = require("http");
* *
* @returns {Object<string, string>} * @returns {Object<string, string>}
*/ */
module.exports = function ({ request, cookieString }) { module.exports = function parseCookies({ request, cookieString }) {
try { try {
/** @type {string | undefined} */ /** @type {string | undefined} */
const cookieStr = request const cookieStr = request

View File

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

View File

@ -12,7 +12,8 @@ export = getToken;
* string and csrf token string * string and csrf token string
* *
* @param {Object} params - Arg * @param {Object} params - Arg
* @param {http.IncomingMessage} params.request - Http request object * @param {http.IncomingMessage} [params.request] - Http request object
* @param {string} [params.cookieString]
* @param {string} params.encryptionKey - Encryption Key * @param {string} params.encryptionKey - Encryption Key
* @param {string} params.encryptionSalt - Encryption Salt * @param {string} params.encryptionSalt - Encryption Salt
* @param {string} params.database - Database Name * @param {string} params.database - Database Name
@ -20,8 +21,9 @@ export = getToken;
* *
* @returns {{ key: string | undefined, csrf: string | undefined }} * @returns {{ key: string | undefined, csrf: string | undefined }}
*/ */
declare function getToken({ request, encryptionKey, encryptionSalt, database, useLocal, }: { declare function getToken({ request, encryptionKey, encryptionSalt, database, useLocal, cookieString, }: {
request: http.IncomingMessage; request?: http.IncomingMessage;
cookieString?: string;
encryptionKey: string; encryptionKey: string;
encryptionSalt: string; encryptionSalt: string;
database: string; database: string;

View File

@ -24,7 +24,8 @@ const parseCookies = require("../package-shared/utils/backend/parseCookies");
* string and csrf token string * string and csrf token string
* *
* @param {Object} params - Arg * @param {Object} params - Arg
* @param {http.IncomingMessage} params.request - Http request object * @param {http.IncomingMessage} [params.request] - Http request object
* @param {string} [params.cookieString]
* @param {string} params.encryptionKey - Encryption Key * @param {string} params.encryptionKey - Encryption Key
* @param {string} params.encryptionSalt - Encryption Salt * @param {string} params.encryptionSalt - Encryption Salt
* @param {string} params.database - Database Name * @param {string} params.database - Database Name
@ -38,6 +39,7 @@ function getToken({
encryptionSalt, encryptionSalt,
database, database,
useLocal, useLocal,
cookieString,
}) { }) {
try { try {
/** /**
@ -45,7 +47,7 @@ function getToken({
* *
* @description Grab the payload * @description Grab the payload
*/ */
const cookies = parseCookies({ request }); const cookies = parseCookies({ request, cookieString });
const keynames = getAuthCookieNames(); const keynames = getAuthCookieNames();
const authKeyName = keynames.keyCookieName; const authKeyName = keynames.keyCookieName;
const csrfName = keynames.csrfCookieName; const csrfName = keynames.csrfCookieName;

View File

@ -6,12 +6,13 @@ export = logoutUser;
* @param {string} [params.encryptedUserString] - Encrypted User String * @param {string} [params.encryptedUserString] - Encrypted User String
* @param {http.IncomingMessage & Object<string, any>} [params.request] - Request Object * @param {http.IncomingMessage & Object<string, any>} [params.request] - Request Object
* @param {http.ServerResponse & Object<string, any>} [params.response] - Http response object * @param {http.ServerResponse & Object<string, any>} [params.response] - Http response object
* @param {string} [params.cookieString]
* @param {string} [params.database] - Target database name(slug): optional * @param {string} [params.database] - Target database name(slug): optional
* @param {string | number} [params.dsqlUserId] * @param {string | number} [params.dsqlUserId]
* *
* @returns {{success: boolean, msg: string, cookieNames?: any}} * @returns {{success: boolean, msg: string, cookieNames?: any}}
*/ */
declare function logoutUser({ response, database, dsqlUserId, encryptedUserString, request, }: { declare function logoutUser({ response, database, dsqlUserId, encryptedUserString, request, cookieString, }: {
encryptedUserString?: string; encryptedUserString?: string;
request?: http.IncomingMessage & { request?: http.IncomingMessage & {
[x: string]: any; [x: string]: any;
@ -19,6 +20,7 @@ declare function logoutUser({ response, database, dsqlUserId, encryptedUserStrin
response?: http.ServerResponse & { response?: http.ServerResponse & {
[x: string]: any; [x: string]: any;
}; };
cookieString?: string;
database?: string; database?: string;
dsqlUserId?: string | number; dsqlUserId?: string | number;
}): { }): {

View File

@ -16,6 +16,7 @@ const parseCookies = require("../package-shared/utils/backend/parseCookies");
* @param {string} [params.encryptedUserString] - Encrypted User String * @param {string} [params.encryptedUserString] - Encrypted User String
* @param {http.IncomingMessage & Object<string, any>} [params.request] - Request Object * @param {http.IncomingMessage & Object<string, any>} [params.request] - Request Object
* @param {http.ServerResponse & Object<string, any>} [params.response] - Http response object * @param {http.ServerResponse & Object<string, any>} [params.response] - Http response object
* @param {string} [params.cookieString]
* @param {string} [params.database] - Target database name(slug): optional * @param {string} [params.database] - Target database name(slug): optional
* @param {string | number} [params.dsqlUserId] * @param {string | number} [params.dsqlUserId]
* *
@ -27,6 +28,7 @@ function logoutUser({
dsqlUserId, dsqlUserId,
encryptedUserString, encryptedUserString,
request, request,
cookieString,
}) { }) {
/** /**
* Check Encryption Keys * Check Encryption Keys
@ -46,7 +48,10 @@ function logoutUser({
const decryptedUserJSON = (() => { const decryptedUserJSON = (() => {
try { try {
if (request) { if (request) {
const cookiesObject = parseCookies({ request }); const cookiesObject = parseCookies({
request,
cookieString,
});
return decrypt({ return decrypt({
encryptedString: cookiesObject[authKeyName], encryptedString: cookiesObject[authKeyName],
}); });

View File

@ -8,6 +8,7 @@ export = userAuth;
* @param {Object} params - Arg * @param {Object} params - Arg
* @param {http.IncomingMessage & Object<string, any>} [params.request] - Http request object * @param {http.IncomingMessage & Object<string, any>} [params.request] - Http request object
* @param {http.IncomingMessage & Object<string, any>} [params.req] - Http request object * @param {http.IncomingMessage & Object<string, any>} [params.req] - Http request object
* @param {string} [params.cookieString]
* @param {string} [params.encryptedUserString] - Encrypted user string to use instead of getting from cookie header * @param {string} [params.encryptedUserString] - Encrypted user string to use instead of getting from cookie header
* @param {string} [params.encryptionKey] - Encryption Key: alt env: DSQL_ENCRYPTION_PASSWORD * @param {string} [params.encryptionKey] - Encryption Key: alt env: DSQL_ENCRYPTION_PASSWORD
* @param {string} [params.encryptionSalt] - Encryption Salt: alt env: DSQL_ENCRYPTION_SALT * @param {string} [params.encryptionSalt] - Encryption Salt: alt env: DSQL_ENCRYPTION_SALT
@ -18,13 +19,14 @@ export = userAuth;
* *
* @returns { import("../package-shared/types").AuthenticatedUser } * @returns { import("../package-shared/types").AuthenticatedUser }
*/ */
declare function userAuth({ request, req, encryptionKey, encryptionSalt, level, database, dsqlUserId, encryptedUserString, expiry, }: { declare function userAuth({ request, req, encryptionKey, encryptionSalt, level, database, dsqlUserId, encryptedUserString, expiry, cookieString, }: {
request?: http.IncomingMessage & { request?: http.IncomingMessage & {
[x: string]: any; [x: string]: any;
}; };
req?: http.IncomingMessage & { req?: http.IncomingMessage & {
[x: string]: any; [x: string]: any;
}; };
cookieString?: string;
encryptedUserString?: string; encryptedUserString?: string;
encryptionKey?: string; encryptionKey?: string;
encryptionSalt?: string; encryptionSalt?: string;

View File

@ -24,6 +24,7 @@ const yearInMilliseconds = dayInMilliseconds * 365;
* @param {Object} params - Arg * @param {Object} params - Arg
* @param {http.IncomingMessage & Object<string, any>} [params.request] - Http request object * @param {http.IncomingMessage & Object<string, any>} [params.request] - Http request object
* @param {http.IncomingMessage & Object<string, any>} [params.req] - Http request object * @param {http.IncomingMessage & Object<string, any>} [params.req] - Http request object
* @param {string} [params.cookieString]
* @param {string} [params.encryptedUserString] - Encrypted user string to use instead of getting from cookie header * @param {string} [params.encryptedUserString] - Encrypted user string to use instead of getting from cookie header
* @param {string} [params.encryptionKey] - Encryption Key: alt env: DSQL_ENCRYPTION_PASSWORD * @param {string} [params.encryptionKey] - Encryption Key: alt env: DSQL_ENCRYPTION_PASSWORD
* @param {string} [params.encryptionSalt] - Encryption Salt: alt env: DSQL_ENCRYPTION_SALT * @param {string} [params.encryptionSalt] - Encryption Salt: alt env: DSQL_ENCRYPTION_SALT
@ -44,6 +45,7 @@ function userAuth({
dsqlUserId, dsqlUserId,
encryptedUserString, encryptedUserString,
expiry = weekInMilliseconds, expiry = weekInMilliseconds,
cookieString,
}) { }) {
try { try {
const finalEncryptionKey = const finalEncryptionKey =
@ -51,7 +53,10 @@ function userAuth({
const finalEncryptionSalt = const finalEncryptionSalt =
encryptionSalt || process.env.DSQL_ENCRYPTION_SALT; encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
const cookies = parseCookies({ request: request || req }); const cookies = parseCookies({
request: request || req,
cookieString,
});
const keyNames = getAuthCookieNames({ const keyNames = getAuthCookieNames({
userId: dsqlUserId || process.env.DSQL_API_USER_ID, userId: dsqlUserId || process.env.DSQL_API_USER_ID,

View File

@ -5,15 +5,17 @@ export = validateTempEmailCode;
* @async * @async
* *
* @param {object} params - Single Param object containing params * @param {object} params - Single Param object containing params
* @param {http.IncomingMessage & Object<string, any>} params.request * @param {http.IncomingMessage & Object<string, any>} [params.request]
* @param {string} [params.cookieString]
* @param {string} [params.email] * @param {string} [params.email]
* *
* @returns { Promise<import("../package-shared/types").SendOneTimeCodeEmailResponse | null>} * @returns { Promise<import("../package-shared/types").SendOneTimeCodeEmailResponse | null>}
*/ */
declare function validateTempEmailCode({ request, email }: { declare function validateTempEmailCode({ request, email, cookieString }: {
request: http.IncomingMessage & { request?: http.IncomingMessage & {
[x: string]: any; [x: string]: any;
}; };
cookieString?: string;
email?: string; email?: string;
}): Promise<import("../package-shared/types").SendOneTimeCodeEmailResponse | null>; }): Promise<import("../package-shared/types").SendOneTimeCodeEmailResponse | null>;
import http = require("http"); import http = require("http");

View File

@ -12,17 +12,18 @@ const EJSON = require("../package-shared/utils/ejson");
* @async * @async
* *
* @param {object} params - Single Param object containing params * @param {object} params - Single Param object containing params
* @param {http.IncomingMessage & Object<string, any>} params.request * @param {http.IncomingMessage & Object<string, any>} [params.request]
* @param {string} [params.cookieString]
* @param {string} [params.email] * @param {string} [params.email]
* *
* @returns { Promise<import("../package-shared/types").SendOneTimeCodeEmailResponse | null>} * @returns { Promise<import("../package-shared/types").SendOneTimeCodeEmailResponse | null>}
*/ */
async function validateTempEmailCode({ request, email }) { async function validateTempEmailCode({ request, email, cookieString }) {
try { try {
const keyNames = getAuthCookieNames(); const keyNames = getAuthCookieNames();
const oneTimeCodeCookieName = keyNames.oneTimeCodeName; const oneTimeCodeCookieName = keyNames.oneTimeCodeName;
const cookies = parseCookies({ request }); const cookies = parseCookies({ request, cookieString });
const encryptedOneTimeCode = cookies[oneTimeCodeCookieName]; const encryptedOneTimeCode = cookies[oneTimeCodeCookieName];
const encryptedPayload = decrypt({ const encryptedPayload = decrypt({