diff --git a/src/middlewares/Authentication.ts b/src/middlewares/Authentication.ts
index 0ecc1bc0..78533489 100644
--- a/src/middlewares/Authentication.ts
+++ b/src/middlewares/Authentication.ts
@@ -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;
diff --git a/src/middlewares/GlobalRateLimit.ts b/src/middlewares/GlobalRateLimit.ts
index 38098981..ce2bfa52 100644
--- a/src/middlewares/GlobalRateLimit.ts
+++ b/src/middlewares/GlobalRateLimit.ts
@@ -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(":", "_");
}
diff --git a/src/routes/auth/login.ts b/src/routes/auth/login.ts
index 1938b794..275f78c6 100644
--- a/src/routes/auth/login.ts
+++ b/src/routes/auth/login.ts
@@ -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,
},
diff --git a/src/routes/auth/register.ts b/src/routes/auth/register.ts
index ca6351fa..b52b5cf3 100644
--- a/src/routes/auth/register.ts
+++ b/src/routes/auth/register.ts
@@ -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) {
diff --git a/src/routes/channels/#channel_id/messages/bulk-delete.ts b/src/routes/channels/#channel_id/messages/bulk-delete.ts
index 8a11475e..615a0d7d 100644
--- a/src/routes/channels/#channel_id/messages/bulk-delete.ts
+++ b/src/routes/channels/#channel_id/messages/bulk-delete.ts
@@ -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");
diff --git a/src/routes/channels/#channel_id/pins.ts b/src/routes/channels/#channel_id/pins.ts
index ccb909b8..6d938b79 100644
--- a/src/routes/channels/#channel_id/pins.ts
+++ b/src/routes/channels/#channel_id/pins.ts
@@ -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();
diff --git a/src/routes/gateway.ts b/src/routes/gateway.ts
index 04ab1248..8d0eb06f 100644
--- a/src/routes/gateway.ts
+++ b/src/routes/gateway.ts
@@ -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" });
});
diff --git a/src/routes/guilds/index.ts b/src/routes/guilds/index.ts
index 8860bcdf..9e787e5b 100644
--- a/src/routes/guilds/index.ts
+++ b/src/routes/guilds/index.ts
@@ -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) {
diff --git a/src/routes/guilds/templates/index.ts b/src/routes/guilds/templates/index.ts
index a7af8295..21a3a6aa 100644
--- a/src/routes/guilds/templates/index.ts
+++ b/src/routes/guilds/templates/index.ts
@@ -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) {
diff --git a/src/util/Config.ts b/src/util/Config.ts
index 1e29508b..6054166f 100644
--- a/src/util/Config.ts
+++ b/src/util/Config.ts
@@ -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});
\ No newline at end of file
+export const apiConfig = new Config({path: configPath, schemaValidator: validator, schema: schema});
\ No newline at end of file
diff --git a/src/util/Member.ts b/src/util/Member.ts
index d03a8f12..b15eef69 100644
--- a/src/util/Member.ts
+++ b/src/util/Member.ts
@@ -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);
}
diff --git a/src/util/passwordStrength.ts b/src/util/passwordStrength.ts
index 7196f797..bc8ce2c4 100644
--- a/src/util/passwordStrength.ts
+++ b/src/util/passwordStrength.ts
@@ -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
|