Use crypto-random-string

This commit is contained in:
David Corbitt
2023-08-08 11:45:03 -07:00
parent ae7acbfdd4
commit ea91d692d3
3 changed files with 19 additions and 9 deletions

View File

@@ -50,6 +50,7 @@
"chroma-js": "^2.4.2", "chroma-js": "^2.4.2",
"concurrently": "^8.2.0", "concurrently": "^8.2.0",
"cors": "^2.8.5", "cors": "^2.8.5",
"crypto-random-string": "^5.0.0",
"dayjs": "^1.11.8", "dayjs": "^1.11.8",
"dedent": "^1.0.1", "dedent": "^1.0.1",
"dotenv": "^16.3.1", "dotenv": "^16.3.1",

15
app/pnpm-lock.yaml generated
View File

@@ -89,6 +89,9 @@ dependencies:
cors: cors:
specifier: ^2.8.5 specifier: ^2.8.5
version: 2.8.5 version: 2.8.5
crypto-random-string:
specifier: ^5.0.0
version: 5.0.0
dayjs: dayjs:
specifier: ^1.11.8 specifier: ^1.11.8
version: 1.11.8 version: 1.11.8
@@ -4393,6 +4396,13 @@ packages:
resolution: {integrity: sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow==} resolution: {integrity: sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow==}
dev: false dev: false
/crypto-random-string@5.0.0:
resolution: {integrity: sha512-KWjTXWwxFd6a94m5CdRGW/t82Tr8DoBc9dNnPCAbFI1EBweN6v1tv8y4Y1m7ndkp/nkIBRxUxAzpaBnR2k3bcQ==}
engines: {node: '>=14.16'}
dependencies:
type-fest: 2.19.0
dev: false
/css-background-parser@0.1.0: /css-background-parser@0.1.0:
resolution: {integrity: sha512-2EZLisiZQ+7m4wwur/qiYJRniHX4K5Tc9w93MT3AS0WS1u5kaZ4FKXlOTBhOjc+CgEgPiGY+fX1yWD8UwpEqUA==} resolution: {integrity: sha512-2EZLisiZQ+7m4wwur/qiYJRniHX4K5Tc9w93MT3AS0WS1u5kaZ4FKXlOTBhOjc+CgEgPiGY+fX1yWD8UwpEqUA==}
dev: false dev: false
@@ -8623,6 +8633,11 @@ packages:
engines: {node: '>=8'} engines: {node: '>=8'}
dev: false dev: false
/type-fest@2.19.0:
resolution: {integrity: sha512-RAH822pAdBgcNMAfWnCBU3CFZcfZ/i1eZjwFU/dsLKumyuuP3niueg2UAukXYF0E2AAoc82ZSSf9J0WQBinzHA==}
engines: {node: '>=12.20'}
dev: false
/type-fest@4.0.0: /type-fest@4.0.0:
resolution: {integrity: sha512-d/oYtUnPM9zar2fqqGLYPzgcY0qUlYK0evgNVti93xpzfjGkMgZHu9Lvgrkn0rqGXTgsFRxFamzjGoD9Uo+dgw==} resolution: {integrity: sha512-d/oYtUnPM9zar2fqqGLYPzgcY0qUlYK0evgNVti93xpzfjGkMgZHu9Lvgrkn0rqGXTgsFRxFamzjGoD9Uo+dgw==}
engines: {node: '>=16'} engines: {node: '>=16'}

View File

@@ -1,11 +1,5 @@
import cryptoRandomString from "crypto-random-string";
const KEY_LENGTH = 42; const KEY_LENGTH = 42;
export const generateApiKey = () => { export const generateApiKey = () => `opc_${cryptoRandomString({ length: KEY_LENGTH })}`;
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let randomChars = "";
for (let i = 0; i < KEY_LENGTH; i++) {
randomChars += chars.charAt(Math.floor(Math.random() * chars.length));
}
return `opc_${randomChars}`;
};