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 | Bin 128223 -> 133722 bytes package.json | 3 ++- src/Server.ts | 2 -- src/opcodes/Identify.ts | 4 +++- src/util/Config.ts | 45 ++++++++++++++++++++++++++-------------- 5 files changed, 35 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index c9428dd1565d12f027c35e0771ecdc43f586fee6..5243808f5aa9f30a0c75ea53a178358290d8c60e 100644 GIT binary patch delta 3465 zcmd6qTWs6b8OOoQj$?OOn!rwxHi=!u!P>AgZ5-E|C#5<`A z&=ti{Y%jx#o&bCrumF2l-?n<4`q;TI#U6@n9WWrkum;=1Hgp(*wJ%#AN=tEr1i`QY z@iILed^#kh|M&mS`F=cq_{+l0>!;rM=lNLZxuwP66B1M}^!!@rrPxMf1O4g0A}632 zz`nu;H!Nwes3d5r$!VJ;mFlJmz1!g;X4*m-bxf#E~*lcfy9H z8&i2(8|VghbVbsum6rmMp7 z3~s1)L@)|)qf{C2l`#ejFkA2n*=W)gxej%h(yML3cEX}08DYtEh2B&hv4PzcuG+d5 zwp+5)7s8Guc#>sH)4Q-BJ@hC&D|lM5q;T8P`(fL$6s>b7QFH@8y2-VausSnfGcW)T zEiHl*{U-tSyA|-JzW$hG=uOFz#$81N*y6?gr^M3Pf2wOR_IPm8v z3~vGKgH`axsX4|;N5>97=CV0yO<0Otjp{*IRB2Po=tuX`8h@nod?YrKn02CSr&yT1-YZOGBm;?+ps&2HI-lt|@4d zOwq03aS&J5!CN;k?f)rp?y=MNl-IvqfL0*F$3hT(<-W=X^6r}n6aL3J=nB&&Op4U0 z5vk&FIUylgl1-A>px7@Yl1T>Xq$*mW9Op9;*&bA)iDE%T$!sf<<;!ETmC3>IuxF%_ zxlT$@9M(pO_#PeG1h+>|?tfPL?mBq$^(VmEZ;7)rg990}AdCaEe1#P)GnFA)&DMB0 zOy_C6!nG=@BN|v9>5UAmU)Io}FxF^02KU{B+@V^Q*0c?M7~vxA$^gx3C9T;`jXf=0 zN;_sMolonr!PKJKZ+rrU4woNWF2#^4 zsS%vY$+?Dx^@?(LSBNL~Dhy3=YF%bG6LEyrDqd{pbxg-J;vAW)Z1!ntSQ}z=Pv3OR zv1B$;yVA2)6Vj?^2-aC=7hN{pTwz zN87Y2wqZ5W@4+KvD^ti;8%=8$i5WQG5zu_q5zW0Ek8@H)m-E#WX$z@*zF74P5-!&g zhQ)f4CvuHZQ&aJl7ptU2^zPak9Q^gn(%miZ$2-s^aBv=jAJ7N_aSxacQPY%!@6BM$`Fi2?T z`kp5-2$JI2jGf9v=|U0l(z7D}`G%z9EF zaTWmT<*RJeDlnrFK~UJJMaB#YOU3&|XTV`7xf|Wfwd=h)#*2Ea-oq3Ht4Uc!l4LZS zZC9lt-Y#*4tuboHq*lQu+g8#o<~>bJObPP%QI+mAM+}&S%M;-wd4&I%V-hob@erSB+kB3i()y^J3 kKf3nFkTh+cm+xm%@K%3#&U$5LEco>=mnWZp0{wB}3-p+He*gdg delta 387 zcmcaLh2#E9_6ffj%{B`#nleonE@af7ep-f6W4cl$qs-(9!YpAPZi)IX5!xnEsg|i0 zS^6a*`d+DKi6us%u9fMI9wrsV9+8Qa`oaE5Zh_v;hW`25?xu;Jk!6Y52HEA6RsM!X zIq8;$nFel7L7}c5rM_jE<$kFaj*|-&<)^pEGA^E8D93nek_qo*d3N^Ae^^`jHm8ei z(cHYlk)L(*3wJ^8%`749Dw`KqRx@t4sHykceD(Az{>{vH8BMlp2r-s$(a*)x6< mZ)G%fWGtNQs3JP~_30hkSD#>H); 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