From 7d795903ef9264d4febb89c62bf4abb269ab8851 Mon Sep 17 00:00:00 2001 From: Diego Magdaleno Date: Sat, 22 May 2021 22:30:36 -0500 Subject: [PATCH] Config: Implement new configuration options --- package-lock.json | 157 ++++++++++++++++++++++++++++++++++++++-- package.json | 3 +- src/Server.ts | 2 - src/opcodes/Identify.ts | 4 +- src/util/Config.ts | 45 ++++++++---- 5 files changed, 185 insertions(+), 26 deletions(-) diff --git a/package-lock.json b/package-lock.json index c9428dd1..5243808f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "@fosscord/server-util": "^1.2.6", + "@fosscord/server-util": "^1.2.8", + "ajv": "^8.5.0", "dotenv": "^8.2.0", "jsonwebtoken": "^8.5.1", "lambert-server": "^1.1.7", @@ -30,14 +31,17 @@ } }, "node_modules/@fosscord/server-util": { - "version": "1.2.6", - "resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.2.6.tgz", - "integrity": "sha512-HFa/DX+4Ze9e8j/tT/Je6at2UEygAH4xsHYay/SObFQKC1Oo+G5aIYvak0kwyzO12lg91i0FBRUEHuMviwNe8A==", + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.2.8.tgz", + "integrity": "sha512-akw6J6E+XxMn+diBRQbXn49QDkE3okgX5qEr0UjzRjaw/eE5+OieE/y9f9v8VhBQYYkNWFrdRt23QBRREQuqUg==", "dependencies": { "@types/jsonwebtoken": "^8.5.0", "@types/mongoose-autopopulate": "^0.10.1", "@types/mongoose-lean-virtuals": "^0.5.1", "@types/node": "^14.14.25", + "ajv": "^8.5.0", + "dot-prop": "^6.0.1", + "env-paths": "^2.2.1", "jsonwebtoken": "^8.5.1", "missing-native-js-functions": "^1.2.2", "mongodb": "^3.6.6", @@ -140,6 +144,21 @@ "node": ">= 0.6" } }, + "node_modules/ajv": { + "version": "8.5.0", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.5.0.tgz", + "integrity": "sha512-Y2l399Tt1AguU3BPRP9Fn4eN+Or+StUGWCUpbnFyXSo8NZ9S4uj+AG2pjs5apK+ZMOwYOz1+a+VKvKH7CudXgQ==", + "dependencies": { + "fast-deep-equal": "^3.1.1", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2", + "uri-js": "^4.2.2" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/epoberezkin" + } + }, "node_modules/anymatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.2.tgz", @@ -457,6 +476,20 @@ "node": ">=0.3.1" } }, + "node_modules/dot-prop": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/dot-prop/-/dot-prop-6.0.1.tgz", + "integrity": "sha512-tE7ztYzXHIeyvc7N+hR3oi7FIbf/NIjVP9hmAt3yMXzrQ072/fpjGLx2GxNxGxUl5V73MEqYzioOMoVhGMJ5cA==", + "dependencies": { + "is-obj": "^2.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/dotenv": { "version": "8.2.0", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", @@ -495,6 +528,14 @@ "node": ">= 0.8" } }, + "node_modules/env-paths": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/env-paths/-/env-paths-2.2.1.tgz", + "integrity": "sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A==", + "engines": { + "node": ">=6" + } + }, "node_modules/erlpack": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/erlpack/-/erlpack-0.1.3.tgz", @@ -581,6 +622,11 @@ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" }, + "node_modules/fast-deep-equal": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" + }, "node_modules/file-uri-to-path": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", @@ -877,6 +923,14 @@ "node": ">=0.12.0" } }, + "node_modules/is-obj": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-obj/-/is-obj-2.0.0.tgz", + "integrity": "sha512-drqDG3cbczxxEJRoOXcOjtdp1J/lyp1mNn0xaznRs8+muBhgQcrnbspox5X5fOw0HnMnbfDzvnEMEtqDEJEo8w==", + "engines": { + "node": ">=8" + } + }, "node_modules/is-utf8": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/is-utf8/-/is-utf8-0.2.1.tgz", @@ -888,6 +942,11 @@ "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" }, + "node_modules/json-schema-traverse": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", + "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==" + }, "node_modules/jsonwebtoken": { "version": "8.5.1", "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz", @@ -1511,6 +1570,14 @@ "node": ">= 0.10" } }, + "node_modules/punycode": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", + "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==", + "engines": { + "node": ">=6" + } + }, "node_modules/qs": { "version": "6.7.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", @@ -1638,6 +1705,14 @@ "semver": "^5.1.0" } }, + "node_modules/require-from-string": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", + "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/resolve": { "version": "1.20.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.20.0.tgz", @@ -2048,6 +2123,14 @@ "node": ">= 0.8" } }, + "node_modules/uri-js": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", + "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==", + "dependencies": { + "punycode": "^2.1.0" + } + }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", @@ -2134,14 +2217,17 @@ }, "dependencies": { "@fosscord/server-util": { - "version": "1.2.6", - "resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.2.6.tgz", - "integrity": "sha512-HFa/DX+4Ze9e8j/tT/Je6at2UEygAH4xsHYay/SObFQKC1Oo+G5aIYvak0kwyzO12lg91i0FBRUEHuMviwNe8A==", + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.2.8.tgz", + "integrity": "sha512-akw6J6E+XxMn+diBRQbXn49QDkE3okgX5qEr0UjzRjaw/eE5+OieE/y9f9v8VhBQYYkNWFrdRt23QBRREQuqUg==", "requires": { "@types/jsonwebtoken": "^8.5.0", "@types/mongoose-autopopulate": "^0.10.1", "@types/mongoose-lean-virtuals": "^0.5.1", "@types/node": "^14.14.25", + "ajv": "^8.5.0", + "dot-prop": "^6.0.1", + "env-paths": "^2.2.1", "jsonwebtoken": "^8.5.1", "missing-native-js-functions": "^1.2.2", "mongodb": "^3.6.6", @@ -2241,6 +2327,17 @@ "negotiator": "0.6.2" } }, + "ajv": { + "version": "8.5.0", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.5.0.tgz", + "integrity": "sha512-Y2l399Tt1AguU3BPRP9Fn4eN+Or+StUGWCUpbnFyXSo8NZ9S4uj+AG2pjs5apK+ZMOwYOz1+a+VKvKH7CudXgQ==", + "requires": { + "fast-deep-equal": "^3.1.1", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2", + "uri-js": "^4.2.2" + } + }, "anymatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.2.tgz", @@ -2500,6 +2597,14 @@ "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", "dev": true }, + "dot-prop": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/dot-prop/-/dot-prop-6.0.1.tgz", + "integrity": "sha512-tE7ztYzXHIeyvc7N+hR3oi7FIbf/NIjVP9hmAt3yMXzrQ072/fpjGLx2GxNxGxUl5V73MEqYzioOMoVhGMJ5cA==", + "requires": { + "is-obj": "^2.0.0" + } + }, "dotenv": { "version": "8.2.0", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", @@ -2532,6 +2637,11 @@ "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=" }, + "env-paths": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/env-paths/-/env-paths-2.2.1.tgz", + "integrity": "sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A==" + }, "erlpack": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/erlpack/-/erlpack-0.1.3.tgz", @@ -2611,6 +2721,11 @@ "integrity": "sha512-h6aK1da4tpqWSbyCa3FxB/V6Ehd4EEB15zyQq9qe75OZBp0krinNKuH4rAY+S/U/2I36vdLAUFSjQJ+TFmODng==", "requires": {} }, + "fast-deep-equal": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" + }, "file-uri-to-path": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", @@ -2834,6 +2949,11 @@ "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", "dev": true }, + "is-obj": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-obj/-/is-obj-2.0.0.tgz", + "integrity": "sha512-drqDG3cbczxxEJRoOXcOjtdp1J/lyp1mNn0xaznRs8+muBhgQcrnbspox5X5fOw0HnMnbfDzvnEMEtqDEJEo8w==" + }, "is-utf8": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/is-utf8/-/is-utf8-0.2.1.tgz", @@ -2845,6 +2965,11 @@ "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" }, + "json-schema-traverse": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", + "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==" + }, "jsonwebtoken": { "version": "8.5.1", "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz", @@ -3311,6 +3436,11 @@ "ipaddr.js": "1.9.1" } }, + "punycode": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", + "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" + }, "qs": { "version": "6.7.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", @@ -3416,6 +3546,11 @@ "semver": "^5.1.0" } }, + "require-from-string": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", + "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==" + }, "resolve": { "version": "1.20.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.20.0.tgz", @@ -3722,6 +3857,14 @@ "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" }, + "uri-js": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", + "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==", + "requires": { + "punycode": "^2.1.0" + } + }, "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", diff --git a/package.json b/package.json index e98a04d3..89e96cb4 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "author": "Fosscord", "license": "ISC", "dependencies": { - "@fosscord/server-util": "^1.2.6", + "@fosscord/server-util": "^1.2.8", + "ajv": "^8.5.0", "dotenv": "^8.2.0", "jsonwebtoken": "^8.5.1", "lambert-server": "^1.1.7", diff --git a/src/Server.ts b/src/Server.ts index 46ff56a3..46130b29 100644 --- a/src/Server.ts +++ b/src/Server.ts @@ -4,7 +4,6 @@ dotenv.config(); import { db } from "@fosscord/server-util"; import { Server as WebSocketServer } from "ws"; import { Connection } from "./events/Connection"; -import Config from "./util/Config"; // TODO: only listen/start the server if everything got initalized // https://www.npmjs.com/package/ws use "External HTTP/S server" and listen manually at the end of listen() @@ -38,7 +37,6 @@ export class Server { await (db as Promise); await this.setupSchema(); console.log("[DB] connected"); - await Config.init(); console.log(`[Gateway] online on 0.0.0.0:${port}`); } } diff --git a/src/opcodes/Identify.ts b/src/opcodes/Identify.ts index f31eebfe..be805eda 100644 --- a/src/opcodes/Identify.ts +++ b/src/opcodes/Identify.ts @@ -17,6 +17,7 @@ import { IdentifySchema } from "../schema/Identify"; import { Send } from "../util/Send"; import experiments from "./experiments.json"; import { check } from "./instanceOf"; +import { DefaultOptions, gatewayConfig } from "../util/Config"; // TODO: bot sharding // TODO: check priviliged intents @@ -29,7 +30,8 @@ export async function onIdentify(this: WebSocket, data: Payload) { const identify: IdentifySchema = data.d; try { - var decoded = await checkToken(identify.token); // will throw an error if invalid + const { jwtSecret } = (gatewayConfig.getAll() as DefaultOptions).security; + var decoded = await checkToken(identify.token, jwtSecret); // will throw an error if invalid } catch (error) { console.error("invalid token", error); return this.close(CLOSECODES.Authentication_failed); diff --git a/src/util/Config.ts b/src/util/Config.ts index eee20e1d..138e1c2f 100644 --- a/src/util/Config.ts +++ b/src/util/Config.ts @@ -1,21 +1,36 @@ import { Config } from "@fosscord/server-util"; - -export default { - init() { - return Config.init({ gateway: DefaultOptions }); - }, - get() { - return Config.getAll().gateway; - }, - set(val: any) { - return Config.setAll({ gateway: val }); - }, - getAll: Config.getAll, - setAll: Config.setAll, -}; +import { getConfigPathForFile } from "@fosscord/server-util/dist/util/Config"; +import Ajv, { JSONSchemaType } from "ajv"; export interface DefaultOptions { endpoint?: string; + security: { + jwtSecret: string; + } } -export const DefaultOptions: DefaultOptions = {}; +const schema: JSONSchemaType = { + type: "object", + properties: { + endpoint: { + type: "string", + nullable: true + }, + security: { + type: "object", + properties: { + jwtSecret: { + type: "string" + } + }, + required: ["jwtSecret"] + }, + }, + required: ["security"] +} + +const ajv = new Ajv(); +const validator = ajv.compile(schema); + +const configPath = getConfigPathForFile("fosscord", "gateway", ".json"); +export const gatewayConfig = new Config({path: configPath, schemaValidator: validator, schema: schema}) \ No newline at end of file