diff --git a/package-shared/functions/backend/api-cred.js b/package-shared/functions/backend/api-cred.js index 0187272..4286240 100644 --- a/package-shared/functions/backend/api-cred.js +++ b/package-shared/functions/backend/api-cred.js @@ -5,6 +5,8 @@ const decrypt = require("./decrypt"); /** @type {import("../../types").CheckApiCredentialsFn} */ const grabApiCred = ({ key, database, table }) => { + if (!key) return null; + try { const allowedKeysPath = process.env.DSQL_API_KEYS_PATH; diff --git a/package-shared/functions/backend/suSocketAuth.js b/package-shared/functions/backend/suSocketAuth.js index 1ceaf8c..eb29d51 100644 --- a/package-shared/functions/backend/suSocketAuth.js +++ b/package-shared/functions/backend/suSocketAuth.js @@ -2,7 +2,7 @@ const { IncomingMessage } = require("http"); const decrypt = require("./decrypt"); -const parseCookies = require("../../../utils/functions/parseCookies"); +const parseCookies = require("../../utils/backend/parseCookies"); /** * @async diff --git a/package-shared/types/index.d.ts b/package-shared/types/index.d.ts index 3793567..383e425 100644 --- a/package-shared/types/index.d.ts +++ b/package-shared/types/index.d.ts @@ -1092,7 +1092,7 @@ export type CheckApiCredentialsFn = ( param: CheckApiCredentialsFnParam ) => ApiKeyObject | null | undefined; export type CheckApiCredentialsFnParam = { - key: string; + key?: string; database?: string; table?: string; }; diff --git a/package-shared/utils/backend/global-db/NO_DB_HANDLER.js b/package-shared/utils/backend/global-db/NO_DB_HANDLER.js index 1357401..5fc8812 100644 --- a/package-shared/utils/backend/global-db/NO_DB_HANDLER.js +++ b/package-shared/utils/backend/global-db/NO_DB_HANDLER.js @@ -3,15 +3,6 @@ const fs = require("fs"); const path = require("path"); -// const mysql = require("mysql"); - -// const NO_DB = mysql.createConnection({ -// host: process.env.DSQL_DB_HOST, -// user: process.env.DSQL_DB_USERNAME, -// password: process.env.DSQL_DB_PASSWORD, -// charset: "utf8mb4", -// }); - const mysql = require("serverless-mysql"); const SSL_DIR = diff --git a/package-shared/utils/backend/parseCookies.js b/package-shared/utils/backend/parseCookies.js new file mode 100644 index 0000000..7556ae0 --- /dev/null +++ b/package-shared/utils/backend/parseCookies.js @@ -0,0 +1,60 @@ +// @ts-check + +/** + * ============================================================================== + * Imports + * ============================================================================== + */ +const http = require("http"); + +/** + * Parse request cookies + * ============================================================================== + * + * @description This function takes in a request object and returns the cookies as a JS object + * + * @async + * + * @param {object} params - main params object + * @param {http.IncomingMessage} params.request - HTTPS request object + * + * @returns {any | null} + */ +module.exports = function ({ request }) { + /** + * Check inputs + * + * @description Check inputs + */ + + /** @type {string | undefined} */ + const cookieString = request.headers.cookie; + + if (!cookieString || typeof cookieString !== "string") { + return null; + } + + /** @type {string[]} */ + const cookieSplitArray = cookieString.split(";"); + + /** @type {*} */ + let cookieObject = {}; + + cookieSplitArray.forEach((keyValueString) => { + const [key, value] = keyValueString.split("="); + if (key && typeof key == "string") { + cookieObject[key.replace(/^ +| +$/, "")] = + value && typeof value == "string" + ? value.replace(/^ +| +$/, "") + : null; + } + }); + + /** + * Make https request + * + * @description make a request to datasquirel.com + */ + + return cookieObject; +}; diff --git a/package.json b/package.json index 45decc3..6d7eb6c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "2.4.0", + "version": "2.4.1", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": {