Config: Final config additons, now everything should work as desired
This commit is contained in:
		
							parent
							
								
									e4a4759e97
								
							
						
					
					
						commit
						ff7985ad7d
					
				
							
								
								
									
										32
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										32
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @ -9,7 +9,7 @@ | ||||
| 			"version": "1.0.0", | ||||
| 			"license": "ISC", | ||||
| 			"dependencies": { | ||||
| 				"@fosscord/server-util": "^1.2.6", | ||||
| 				"@fosscord/server-util": "^1.2.8", | ||||
| 				"@types/jest": "^26.0.22", | ||||
| 				"@types/json-schema": "^7.0.7", | ||||
| 				"ajv": "^8.4.0", | ||||
| @ -498,14 +498,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", | ||||
| @ -1856,9 +1859,9 @@ | ||||
| 			} | ||||
| 		}, | ||||
| 		"node_modules/ajv": { | ||||
| 			"version": "8.4.0", | ||||
| 			"resolved": "https://registry.npmjs.org/ajv/-/ajv-8.4.0.tgz", | ||||
| 			"integrity": "sha512-7QD2l6+KBSLwf+7MuYocbWvRPdOu63/trReTLu2KFwkgctnub1auoF+Y1WYcm09CTM7quuscrzqmASaLHC/K4Q==", | ||||
| 			"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", | ||||
| @ -12661,14 +12664,17 @@ | ||||
| 			} | ||||
| 		}, | ||||
| 		"@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", | ||||
| @ -13810,9 +13816,9 @@ | ||||
| 			} | ||||
| 		}, | ||||
| 		"ajv": { | ||||
| 			"version": "8.4.0", | ||||
| 			"resolved": "https://registry.npmjs.org/ajv/-/ajv-8.4.0.tgz", | ||||
| 			"integrity": "sha512-7QD2l6+KBSLwf+7MuYocbWvRPdOu63/trReTLu2KFwkgctnub1auoF+Y1WYcm09CTM7quuscrzqmASaLHC/K4Q==", | ||||
| 			"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", | ||||
|  | ||||
| @ -29,7 +29,7 @@ | ||||
| 	}, | ||||
| 	"homepage": "https://github.com/fosscord/fosscord-api#readme", | ||||
| 	"dependencies": { | ||||
| 		"@fosscord/server-util": "^1.2.6", | ||||
| 		"@fosscord/server-util": "^1.2.8", | ||||
| 		"@types/jest": "^26.0.22", | ||||
| 		"@types/json-schema": "^7.0.7", | ||||
| 		"ajv": "^8.4.0", | ||||
|  | ||||
| @ -1,6 +1,7 @@ | ||||
| import { NextFunction, Request, Response } from "express"; | ||||
| import { HTTPError } from "lambert-server"; | ||||
| import { checkToken } from "@fosscord/server-util"; | ||||
| import * as Config from "../util/Config" | ||||
| 
 | ||||
| export const NO_AUTHORIZATION_ROUTES = [ | ||||
| 	"/api/v8/auth/login", | ||||
| @ -27,7 +28,10 @@ export async function Authentication(req: Request, res: Response, next: NextFunc | ||||
| 	// TODO: check if user is banned/token expired
 | ||||
| 
 | ||||
| 	try { | ||||
| 		const decoded: any = await checkToken(req.headers.authorization); | ||||
| 
 | ||||
| 		const { jwtSecret } = (Config.apiConfig.getAll() as Config.DefaultOptions).security; | ||||
| 
 | ||||
| 		const decoded: any = await checkToken(req.headers.authorization, jwtSecret); | ||||
| 
 | ||||
| 		req.token = decoded; | ||||
| 		req.user_id = decoded.id; | ||||
|  | ||||
| @ -44,7 +44,7 @@ export async function GlobalRateLimit(req: Request, res: Response, next: NextFun | ||||
| } | ||||
| 
 | ||||
| export function getIpAdress(req: Request): string { | ||||
| 	const { forwadedFor } = Config.apiConfig.getAll().security; | ||||
| 	const { forwadedFor } = (Config.apiConfig.getAll() as Config.DefaultOptions).security; | ||||
| 	const ip = forwadedFor ? <string>req.headers[forwadedFor] : req.ip; | ||||
| 	return ip.replaceAll(".", "_").replaceAll(":", "_"); | ||||
| } | ||||
|  | ||||
| @ -27,7 +27,7 @@ router.post( | ||||
| 
 | ||||
| 		// TODO: Rewrite this to have the proper config syntax on the new method 
 | ||||
|   | ||||
| 		const config = Config.apiConfig.getAll(); | ||||
| 		const config = Config.apiConfig.getAll() as Config.DefaultOptions; | ||||
| 
 | ||||
| 		if (config.login.requireCaptcha && config.security.captcha.enabled) { | ||||
| 			if (!captcha_key) { | ||||
| @ -71,7 +71,7 @@ export async function generateToken(id: string) { | ||||
| 	return new Promise((res, rej) => { | ||||
| 		jwt.sign( | ||||
| 			{ id: id, iat }, | ||||
| 			Config.apiConfig.getAll().security.jwtSecret, | ||||
| 			(Config.apiConfig.getAll() as Config.DefaultOptions).security.jwtSecret, | ||||
| 			{ | ||||
| 				algorithm, | ||||
| 			}, | ||||
|  | ||||
| @ -52,7 +52,7 @@ router.post( | ||||
| 		let discriminator = ""; | ||||
| 
 | ||||
| 		// get register Config
 | ||||
| 		const { register, security } = Config.apiConfig.getAll(); | ||||
| 		const { register, security } = Config.apiConfig.getAll() as Config.DefaultOptions; | ||||
| 
 | ||||
| 		// check if registration is allowed
 | ||||
| 		if (!register.allowNewRegistration) { | ||||
|  | ||||
| @ -20,7 +20,7 @@ router.post("/", check({ messages: [String] }), async (req, res) => { | ||||
| 	const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel }); | ||||
| 	permission.hasThrow("MANAGE_MESSAGES"); | ||||
| 
 | ||||
| 	const { maxBulkDelete } =  Config.apiConfig.getAll().limits.message; | ||||
| 	const { maxBulkDelete } =  (Config.apiConfig.getAll() as Config.DefaultOptions).limits.message; | ||||
| 
 | ||||
| 	const { messages } = req.body as { messages: string[] }; | ||||
| 	if (messages.length < 2) throw new HTTPError("You must at least specify 2 messages to bulk delete"); | ||||
|  | ||||
| @ -18,7 +18,7 @@ router.put("/:message_id", async (req: Request, res: Response) => { | ||||
| 	if (channel.guild_id) permission.hasThrow("MANAGE_MESSAGES"); | ||||
| 
 | ||||
| 	const pinned_count = await MessageModel.count({ channel_id, pinned: true }).exec(); | ||||
| 	const { maxPins } = Config.apiConfig.getAll().limits.channel; | ||||
| 	const { maxPins } = (Config.apiConfig.getAll() as Config.DefaultOptions).limits.channel; | ||||
| 	if (pinned_count >= maxPins) throw new HTTPError("Max pin count reached: " + maxPins); | ||||
| 
 | ||||
| 	await MessageModel.updateOne({ id: message_id }, { pinned: true }).exec(); | ||||
|  | ||||
| @ -4,7 +4,7 @@ import * as Config from "../util/Config" | ||||
| const router = Router(); | ||||
| 
 | ||||
| router.get("/", (req, res) => { | ||||
| 	const { gateway } = Config.apiConfig.getAll(); | ||||
| 	const { gateway } = Config.apiConfig.getAll() as Config.DefaultOptions; | ||||
| 	res.send({ url: gateway || "ws://localhost:3002" }); | ||||
| }); | ||||
| 
 | ||||
|  | ||||
| @ -15,7 +15,7 @@ const router: Router = Router(); | ||||
| router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) => { | ||||
| 	const body = req.body as GuildCreateSchema; | ||||
| 
 | ||||
| 	const { maxGuilds } = Config.apiConfig.getAll().limits.user; | ||||
| 	const { maxGuilds } = (Config.apiConfig.getAll() as Config.DefaultOptions).limits.user; | ||||
| 	const user = await getPublicUser(req.user_id, { guilds: true }); | ||||
| 
 | ||||
| 	if (user.guilds.length >= maxGuilds) { | ||||
|  | ||||
| @ -21,7 +21,7 @@ router.post("/:code", check(GuildTemplateCreateSchema), async (req: Request, res | ||||
| 	const { code } = req.params; | ||||
| 	const body = req.body as GuildTemplateCreateSchema; | ||||
| 
 | ||||
| 	const { maxGuilds } =  Config.apiConfig.getAll().limits.user; | ||||
| 	const { maxGuilds } =  (Config.apiConfig.getAll() as Config.DefaultOptions).limits.user; | ||||
| 	const user = await getPublicUser(req.user_id, { guilds: true }); | ||||
| 
 | ||||
| 	if (user.guilds.length >= maxGuilds) { | ||||
|  | ||||
| @ -1,11 +1,6 @@ | ||||
| import Ajv, { JSONSchemaType, ValidateFunction } from "ajv" | ||||
| import ajvFormats from 'ajv-formats'; | ||||
| import dotProp from "dot-prop"; | ||||
| import envPaths from "env-paths"; | ||||
| import path from "node:path"; | ||||
| import fs from 'fs' | ||||
| import assert from "assert"; | ||||
| import atomically from "atomically" | ||||
| import Ajv, { JSONSchemaType } from "ajv" | ||||
| import { getConfigPathForFile } from "@fosscord/server-util/dist/util/Config"; | ||||
| import {Config} from "@fosscord/server-util" | ||||
| 
 | ||||
| export interface RateLimitOptions { | ||||
| 	count: number; | ||||
| @ -362,147 +357,9 @@ const schema: JSONSchemaType<DefaultOptions> & { | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| const createPlainObject = <T = unknown>(): T => { | ||||
| 	return Object.create(null); | ||||
| }; | ||||
| type Serialize<T> = (value: T) => string; | ||||
| type Deserialize<T> = (text: string) => T; | ||||
| 
 | ||||
| function getConfigPath(): string { | ||||
| 	const configEnvPath = envPaths('fosscord', {suffix: ""}).config; | ||||
| 	const configPath = path.resolve(configEnvPath, 'api.json'); | ||||
| 	return configPath | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| class Store<T extends Record<string, any> = Record<string, unknown>> implements Iterable<[keyof T, T[keyof T]]>{ | ||||
| 	readonly path: string; | ||||
| 	readonly validator: ValidateFunction; | ||||
| 	constructor(path: string, validator: ValidateFunction) { | ||||
| 		this.validator = validator; | ||||
| 		if (fs.existsSync(path)) { | ||||
| 			this.path = path | ||||
| 		} else { | ||||
| 			this._ensureDirectory() | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	private _ensureDirectory(): void { | ||||
| 		fs.mkdirSync(path.dirname(this.path), {recursive: true}) | ||||
| 	} | ||||
| 
 | ||||
| 	protected _validate(data: T | unknown): void { | ||||
| 		const valid = this.validator(data); | ||||
| 		if (valid || !this.validator.errors) { | ||||
| 			return; | ||||
| 		} | ||||
| 
 | ||||
| 		const errors = this.validator.errors.map(({ instancePath, message = '' }) => `\`${instancePath.slice(1)}\` ${message}`); | ||||
| 		throw new Error('The config schema was violated!: ' + errors.join('; ')); | ||||
| 	} | ||||
| 
 | ||||
| 	private _write(value: T): void { | ||||
| 		let data: string | Buffer = this._serialize(value); | ||||
| 
 | ||||
| 		try { | ||||
| 			atomically.writeFileSync(this.path, data); | ||||
| 		} catch (error) { | ||||
| 			throw error; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	private readonly _serialize: Serialize<T> = value => JSON.stringify(value, undefined, '\t'); | ||||
| 	private readonly _deserialize: Deserialize<T> = value => JSON.parse(value); | ||||
| 
 | ||||
| 	public get store(): T { | ||||
| 		try { | ||||
| 			const data = fs.readFileSync(this.path).toString(); | ||||
| 			const deserializedData = this._deserialize(data); | ||||
| 			this._validate(deserializedData); | ||||
| 			return Object.assign(Object.create(null), deserializedData) | ||||
| 		} catch (error) { | ||||
| 			if (error.code == 'ENOENT') { | ||||
| 				this._ensureDirectory(); | ||||
| 				return Object.create(null); | ||||
| 			} | ||||
| 
 | ||||
| 			throw error; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	public set store(value: T) { | ||||
| 		this._validate(value); | ||||
| 
 | ||||
| 		this._write(value); | ||||
| 	} | ||||
| 
 | ||||
| 	*[Symbol.iterator](): IterableIterator<[keyof T, T[keyof T]]>{ | ||||
| 		for (const [key, value] of Object.entries(this.store)) { | ||||
| 			yield [key, value] | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| interface Options { | ||||
| 	path: string; | ||||
| 	schemaValidator: ValidateFunction; | ||||
| } | ||||
| 
 | ||||
| class Config<T extends Record<string, any> = Record<string, unknown>> extends Store<T> implements Iterable<[keyof T, T[keyof T]]> { | ||||
| 	readonly path: string; | ||||
| 
 | ||||
| 	constructor(options: Readonly<Partial<Options>> = {}) { | ||||
| 		super(options.path!, options.schemaValidator!); | ||||
| 
 | ||||
| 
 | ||||
| 		const fileStore = this.store; | ||||
| 		const store = Object.assign(createPlainObject<T>(), fileStore); | ||||
| 		this._validate(store); | ||||
| 
 | ||||
| 		try { | ||||
| 			assert.deepStrictEqual(fileStore, store); | ||||
| 		} catch { | ||||
| 			this.store = store; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	public get<Key extends keyof T>(key: Key): T[Key]; | ||||
| 	public get<Key extends keyof T>(key: Key, defaultValue: Required<T>[Key]): Required<T>[Key]; | ||||
| 	public get<Key extends string, Value = unknown>(key: Exclude<Key, keyof T>, defaultValue?: Value): Value; | ||||
| 	public get(key: string, defaultValue?: unknown): unknown { | ||||
| 		return this._get(key, defaultValue); | ||||
| 	} | ||||
| 
 | ||||
| 	private _has<Key extends keyof T>(key: Key | string): boolean { | ||||
| 		return dotProp.has(this.store, key as string); | ||||
| 	} | ||||
| 
 | ||||
| 	public getAll(): DefaultOptions { | ||||
| 		return this.store as unknown as DefaultOptions | ||||
| 	} | ||||
| 
 | ||||
| 	_get<Key extends keyof T>(key: Key): T[Key] | undefined; | ||||
| 	_get<Key extends keyof T, Default = unknown>(key: Key, defaultValue: Default): T[Key] | Default; | ||||
| 	_get<Key extends keyof T, Default = unknown>(key: Key | string, defaultValue?: Default): Default | undefined { | ||||
| 		if (!this._has(key)) { | ||||
| 			throw new Error("Tried to acess a non existant property in the config"); | ||||
| 		} | ||||
| 
 | ||||
| 		return dotProp.get<T[Key] | undefined>(this.store, key as string, defaultValue as T[Key]); | ||||
| 	} | ||||
| 
 | ||||
| 	* [Symbol.iterator](): IterableIterator<[keyof T, T[keyof T]]> { | ||||
| 		for (const [key, value] of Object.entries(this.store)) { | ||||
| 			yield [key, value]; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| const ajv = new Ajv(); | ||||
| const validator = ajv.compile(schema); | ||||
| 
 | ||||
| const configPath = getConfigPath() | ||||
| console.log(configPath) | ||||
| const configPath = getConfigPathForFile("fosscord", "api", ".json"); | ||||
| 
 | ||||
| export const apiConfig = new Config({path: configPath, schemaValidator: validator}); | ||||
| export const apiConfig = new Config({path: configPath, schemaValidator: validator, schema: schema}); | ||||
| @ -39,7 +39,7 @@ export async function isMember(user_id: string, guild_id: string) { | ||||
| export async function addMember(user_id: string, guild_id: string, cache?: { guild?: GuildDocument }) { | ||||
| 	const user = await getPublicUser(user_id, { guilds: true }); | ||||
| 
 | ||||
| 	const { maxGuilds } = Config.apiConfig.getAll().limits.user; | ||||
| 	const { maxGuilds } = (Config.apiConfig.getAll() as Config.DefaultOptions).limits.user; | ||||
| 	if (user.guilds.length >= maxGuilds) { | ||||
| 		throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403); | ||||
| 	} | ||||
|  | ||||
| @ -23,7 +23,7 @@ export function check(password: string): number { | ||||
| 		minUpperCase, | ||||
| 		minSymbols, | ||||
| 		blockInsecureCommonPasswords, | ||||
| 	} = Config.apiConfig.getAll().register.password; | ||||
| 	} = (Config.apiConfig.getAll() as Config.DefaultOptions).register.password; | ||||
| 	var strength = 0; | ||||
| 
 | ||||
| 	// checks for total password len
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Diego Magdaleno
						Diego Magdaleno