Config: Final config additons, now everything should work as desired

This commit is contained in:
Diego Magdaleno 2021-05-22 18:51:46 -05:00
parent e4a4759e97
commit ff7985ad7d
14 changed files with 41 additions and 174 deletions

32
package-lock.json generated
View File

@ -9,7 +9,7 @@
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@fosscord/server-util": "^1.2.6", "@fosscord/server-util": "^1.2.8",
"@types/jest": "^26.0.22", "@types/jest": "^26.0.22",
"@types/json-schema": "^7.0.7", "@types/json-schema": "^7.0.7",
"ajv": "^8.4.0", "ajv": "^8.4.0",
@ -498,14 +498,17 @@
} }
}, },
"node_modules/@fosscord/server-util": { "node_modules/@fosscord/server-util": {
"version": "1.2.6", "version": "1.2.8",
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.2.6.tgz", "resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.2.8.tgz",
"integrity": "sha512-HFa/DX+4Ze9e8j/tT/Je6at2UEygAH4xsHYay/SObFQKC1Oo+G5aIYvak0kwyzO12lg91i0FBRUEHuMviwNe8A==", "integrity": "sha512-akw6J6E+XxMn+diBRQbXn49QDkE3okgX5qEr0UjzRjaw/eE5+OieE/y9f9v8VhBQYYkNWFrdRt23QBRREQuqUg==",
"dependencies": { "dependencies": {
"@types/jsonwebtoken": "^8.5.0", "@types/jsonwebtoken": "^8.5.0",
"@types/mongoose-autopopulate": "^0.10.1", "@types/mongoose-autopopulate": "^0.10.1",
"@types/mongoose-lean-virtuals": "^0.5.1", "@types/mongoose-lean-virtuals": "^0.5.1",
"@types/node": "^14.14.25", "@types/node": "^14.14.25",
"ajv": "^8.5.0",
"dot-prop": "^6.0.1",
"env-paths": "^2.2.1",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"missing-native-js-functions": "^1.2.2", "missing-native-js-functions": "^1.2.2",
"mongodb": "^3.6.6", "mongodb": "^3.6.6",
@ -1856,9 +1859,9 @@
} }
}, },
"node_modules/ajv": { "node_modules/ajv": {
"version": "8.4.0", "version": "8.5.0",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-8.4.0.tgz", "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.5.0.tgz",
"integrity": "sha512-7QD2l6+KBSLwf+7MuYocbWvRPdOu63/trReTLu2KFwkgctnub1auoF+Y1WYcm09CTM7quuscrzqmASaLHC/K4Q==", "integrity": "sha512-Y2l399Tt1AguU3BPRP9Fn4eN+Or+StUGWCUpbnFyXSo8NZ9S4uj+AG2pjs5apK+ZMOwYOz1+a+VKvKH7CudXgQ==",
"dependencies": { "dependencies": {
"fast-deep-equal": "^3.1.1", "fast-deep-equal": "^3.1.1",
"json-schema-traverse": "^1.0.0", "json-schema-traverse": "^1.0.0",
@ -12661,14 +12664,17 @@
} }
}, },
"@fosscord/server-util": { "@fosscord/server-util": {
"version": "1.2.6", "version": "1.2.8",
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.2.6.tgz", "resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.2.8.tgz",
"integrity": "sha512-HFa/DX+4Ze9e8j/tT/Je6at2UEygAH4xsHYay/SObFQKC1Oo+G5aIYvak0kwyzO12lg91i0FBRUEHuMviwNe8A==", "integrity": "sha512-akw6J6E+XxMn+diBRQbXn49QDkE3okgX5qEr0UjzRjaw/eE5+OieE/y9f9v8VhBQYYkNWFrdRt23QBRREQuqUg==",
"requires": { "requires": {
"@types/jsonwebtoken": "^8.5.0", "@types/jsonwebtoken": "^8.5.0",
"@types/mongoose-autopopulate": "^0.10.1", "@types/mongoose-autopopulate": "^0.10.1",
"@types/mongoose-lean-virtuals": "^0.5.1", "@types/mongoose-lean-virtuals": "^0.5.1",
"@types/node": "^14.14.25", "@types/node": "^14.14.25",
"ajv": "^8.5.0",
"dot-prop": "^6.0.1",
"env-paths": "^2.2.1",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"missing-native-js-functions": "^1.2.2", "missing-native-js-functions": "^1.2.2",
"mongodb": "^3.6.6", "mongodb": "^3.6.6",
@ -13810,9 +13816,9 @@
} }
}, },
"ajv": { "ajv": {
"version": "8.4.0", "version": "8.5.0",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-8.4.0.tgz", "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.5.0.tgz",
"integrity": "sha512-7QD2l6+KBSLwf+7MuYocbWvRPdOu63/trReTLu2KFwkgctnub1auoF+Y1WYcm09CTM7quuscrzqmASaLHC/K4Q==", "integrity": "sha512-Y2l399Tt1AguU3BPRP9Fn4eN+Or+StUGWCUpbnFyXSo8NZ9S4uj+AG2pjs5apK+ZMOwYOz1+a+VKvKH7CudXgQ==",
"requires": { "requires": {
"fast-deep-equal": "^3.1.1", "fast-deep-equal": "^3.1.1",
"json-schema-traverse": "^1.0.0", "json-schema-traverse": "^1.0.0",

View File

@ -29,7 +29,7 @@
}, },
"homepage": "https://github.com/fosscord/fosscord-api#readme", "homepage": "https://github.com/fosscord/fosscord-api#readme",
"dependencies": { "dependencies": {
"@fosscord/server-util": "^1.2.6", "@fosscord/server-util": "^1.2.8",
"@types/jest": "^26.0.22", "@types/jest": "^26.0.22",
"@types/json-schema": "^7.0.7", "@types/json-schema": "^7.0.7",
"ajv": "^8.4.0", "ajv": "^8.4.0",

View File

@ -1,6 +1,7 @@
import { NextFunction, Request, Response } from "express"; import { NextFunction, Request, Response } from "express";
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
import { checkToken } from "@fosscord/server-util"; import { checkToken } from "@fosscord/server-util";
import * as Config from "../util/Config"
export const NO_AUTHORIZATION_ROUTES = [ export const NO_AUTHORIZATION_ROUTES = [
"/api/v8/auth/login", "/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 // TODO: check if user is banned/token expired
try { 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.token = decoded;
req.user_id = decoded.id; req.user_id = decoded.id;

View File

@ -44,7 +44,7 @@ export async function GlobalRateLimit(req: Request, res: Response, next: NextFun
} }
export function getIpAdress(req: Request): string { 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; const ip = forwadedFor ? <string>req.headers[forwadedFor] : req.ip;
return ip.replaceAll(".", "_").replaceAll(":", "_"); return ip.replaceAll(".", "_").replaceAll(":", "_");
} }

View File

@ -27,7 +27,7 @@ router.post(
// TODO: Rewrite this to have the proper config syntax on the new method // 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 (config.login.requireCaptcha && config.security.captcha.enabled) {
if (!captcha_key) { if (!captcha_key) {
@ -71,7 +71,7 @@ export async function generateToken(id: string) {
return new Promise((res, rej) => { return new Promise((res, rej) => {
jwt.sign( jwt.sign(
{ id: id, iat }, { id: id, iat },
Config.apiConfig.getAll().security.jwtSecret, (Config.apiConfig.getAll() as Config.DefaultOptions).security.jwtSecret,
{ {
algorithm, algorithm,
}, },

View File

@ -52,7 +52,7 @@ router.post(
let discriminator = ""; let discriminator = "";
// get register Config // get register Config
const { register, security } = Config.apiConfig.getAll(); const { register, security } = Config.apiConfig.getAll() as Config.DefaultOptions;
// check if registration is allowed // check if registration is allowed
if (!register.allowNewRegistration) { if (!register.allowNewRegistration) {

View File

@ -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 }); const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel });
permission.hasThrow("MANAGE_MESSAGES"); 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[] }; const { messages } = req.body as { messages: string[] };
if (messages.length < 2) throw new HTTPError("You must at least specify 2 messages to bulk delete"); if (messages.length < 2) throw new HTTPError("You must at least specify 2 messages to bulk delete");

View File

@ -18,7 +18,7 @@ router.put("/:message_id", async (req: Request, res: Response) => {
if (channel.guild_id) permission.hasThrow("MANAGE_MESSAGES"); if (channel.guild_id) permission.hasThrow("MANAGE_MESSAGES");
const pinned_count = await MessageModel.count({ channel_id, pinned: true }).exec(); 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); if (pinned_count >= maxPins) throw new HTTPError("Max pin count reached: " + maxPins);
await MessageModel.updateOne({ id: message_id }, { pinned: true }).exec(); await MessageModel.updateOne({ id: message_id }, { pinned: true }).exec();

View File

@ -4,7 +4,7 @@ import * as Config from "../util/Config"
const router = Router(); const router = Router();
router.get("/", (req, res) => { router.get("/", (req, res) => {
const { gateway } = Config.apiConfig.getAll(); const { gateway } = Config.apiConfig.getAll() as Config.DefaultOptions;
res.send({ url: gateway || "ws://localhost:3002" }); res.send({ url: gateway || "ws://localhost:3002" });
}); });

View File

@ -15,7 +15,7 @@ const router: Router = Router();
router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) => { router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) => {
const body = req.body as GuildCreateSchema; 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 }); const user = await getPublicUser(req.user_id, { guilds: true });
if (user.guilds.length >= maxGuilds) { if (user.guilds.length >= maxGuilds) {

View File

@ -21,7 +21,7 @@ router.post("/:code", check(GuildTemplateCreateSchema), async (req: Request, res
const { code } = req.params; const { code } = req.params;
const body = req.body as GuildTemplateCreateSchema; 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 }); const user = await getPublicUser(req.user_id, { guilds: true });
if (user.guilds.length >= maxGuilds) { if (user.guilds.length >= maxGuilds) {

View File

@ -1,11 +1,6 @@
import Ajv, { JSONSchemaType, ValidateFunction } from "ajv" import Ajv, { JSONSchemaType } from "ajv"
import ajvFormats from 'ajv-formats'; import { getConfigPathForFile } from "@fosscord/server-util/dist/util/Config";
import dotProp from "dot-prop"; import {Config} from "@fosscord/server-util"
import envPaths from "env-paths";
import path from "node:path";
import fs from 'fs'
import assert from "assert";
import atomically from "atomically"
export interface RateLimitOptions { export interface RateLimitOptions {
count: number; 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 ajv = new Ajv();
const validator = ajv.compile(schema); const validator = ajv.compile(schema);
const configPath = getConfigPath() const configPath = getConfigPathForFile("fosscord", "api", ".json");
console.log(configPath)
export const apiConfig = new Config({path: configPath, schemaValidator: validator}); export const apiConfig = new Config({path: configPath, schemaValidator: validator, schema: schema});

View File

@ -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 }) { export async function addMember(user_id: string, guild_id: string, cache?: { guild?: GuildDocument }) {
const user = await getPublicUser(user_id, { guilds: true }); 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) { if (user.guilds.length >= maxGuilds) {
throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403); throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403);
} }

View File

@ -23,7 +23,7 @@ export function check(password: string): number {
minUpperCase, minUpperCase,
minSymbols, minSymbols,
blockInsecureCommonPasswords, blockInsecureCommonPasswords,
} = Config.apiConfig.getAll().register.password; } = (Config.apiConfig.getAll() as Config.DefaultOptions).register.password;
var strength = 0; var strength = 0;
// checks for total password len // checks for total password len