diff --git a/.env.example b/.env.example index 43d177c93..39045bd53 100644 --- a/.env.example +++ b/.env.example @@ -9,6 +9,7 @@ FRONTEND_URL = "http://localhost:3000" # URL for Staart UI JWT_SECRET = "staart" JWT_ISSUER = "staart" SERVICE_2FA = "staart" +HASH_IDS = "staart" # MySQL/MariaDB connection DB_HOST = "localhost" diff --git a/package.json b/package.json index 0fcc4f5c6..4720852a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "staart-manager", - "version": "1.1.9", + "version": "1.1.10", "main": "index.js", "repository": "git@github.com:AnandChowdhary/staart.git", "author": "Anand Chowdhary ", @@ -114,6 +114,7 @@ "fs-extra": "^8.1.0", "geolite2": "^1.2.1", "handy-redis": "^1.6.2", + "hashids": "^2.0.0", "helmet": "^3.20.1", "http-aws-es": "^6.0.0", "http-status-codes": "^1.3.2", @@ -146,5 +147,5 @@ "setup" ], "snyk": true, - "staart-version": "1.1.9" + "staart-version": "1.1.10" } \ No newline at end of file diff --git a/src/config.ts b/src/config.ts index 2e3db07f1..bd05d5971 100644 --- a/src/config.ts +++ b/src/config.ts @@ -67,6 +67,7 @@ export const ALLOW_DISPOSABLE_EMAILS = !!process.env.DISPOSABLE_EMAIL; export const JWT_SECRET = process.env.JWT_SECRET || "staart"; export const JWT_ISSUER = process.env.JWT_ISSUER || "staart"; export const SERVICE_2FA = process.env.SERVICE_2FA || "staart"; +export const HASH_IDS = process.env.HASH_IDS || "staart"; export const TOKEN_EXPIRY_EMAIL_VERIFICATION = process.env.TOKEN_EXPIRY_EMAIL_VERIFICATION || "7d"; diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index fe2b2c45d..13f5796ae 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -9,7 +9,15 @@ import cryptoRandomString from "crypto-random-string"; import { Tokens } from "../interfaces/enum"; import { ApiKeyResponse } from "./jwt"; import { isMatch } from "matcher"; +import Hashids from "hashids"; import { getUserIdFromUsername } from "../crud/user"; +import { HASH_IDS } from "../config"; + +const hashIds = new Hashids( + HASH_IDS, + 10, + "abcdefghijklmnopqrstuvwxyz1234567890" +); /** * Capitalize each first letter in a string @@ -79,6 +87,21 @@ export const userUsernameToId = async (id: string, tokenUserId: number) => { } }; +export const generateHashId = (id: number) => hashIds.encode(id); + +export const hashIdToId = (id: string | number) => { + if (typeof id === "number") return id; + if (id.startsWith("h")) { + const numberId = parseInt(hashIds.decode(id).join("")); + if (isNaN(numberId)) { + return parseInt(id); + } else { + return numberId; + } + } + return parseInt(id); +}; + export const localsToTokenOrKey = (res: Response) => { if (res.locals.token.sub == Tokens.API_KEY) { return res.locals.token as ApiKeyResponse; diff --git a/yarn.lock b/yarn.lock index 5a0cf5f60..8e8c29696 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3499,6 +3499,11 @@ has@^1.0.1, has@^1.0.3: dependencies: function-bind "^1.1.1" +hashids@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/hashids/-/hashids-2.0.0.tgz#8a3ad77598233e399594b73ad5fdb3c2c880528e" + integrity sha512-URg7dQlk6wQS9WqhQwKBC0cT/G0JMaEcwlHTvtRNB45JcIrr6mxY7dXiCUJwESf+tXnyaFdz7C8ETDYrQsyY1A== + helmet-crossdomain@0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/helmet-crossdomain/-/helmet-crossdomain-0.4.0.tgz#5f1fe5a836d0325f1da0a78eaa5fd8429078894e"