mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-13 14:12:41 +01:00
Config: Refactor config method, so we have a new get all option, fix issues in configurations
This commit is contained in:
parent
8872f806f7
commit
05057b922a
@ -44,8 +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 rateLimitProperties = Config.apiConfig.get('security', {jwtSecret: crypto.randomBytes(256).toString("base64"), forwadedFor: null, captcha: {enabled:false, service: null, sitekey: null, secret: null}}) as Config.DefaultOptions;
|
const { forwadedFor } = Config.apiConfig.getAll().security;
|
||||||
const { forwadedFor } = rateLimitProperties.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(":", "_");
|
||||||
}
|
}
|
||||||
|
@ -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.store as unknown as Config.DefaultOptions;
|
const config = Config.apiConfig.getAll();
|
||||||
|
|
||||||
if (config.login.requireCaptcha && config.security.captcha.enabled) {
|
if (config.login.requireCaptcha && config.security.captcha.enabled) {
|
||||||
if (!captcha_key) {
|
if (!captcha_key) {
|
||||||
@ -69,10 +69,9 @@ export async function generateToken(id: string) {
|
|||||||
const algorithm = "HS256";
|
const algorithm = "HS256";
|
||||||
|
|
||||||
return new Promise((res, rej) => {
|
return new Promise((res, rej) => {
|
||||||
const securityPropertiesSecret = Config.apiConfig.get('security.jwtSecret') as Config.DefaultOptions;
|
|
||||||
jwt.sign(
|
jwt.sign(
|
||||||
{ id: id, iat },
|
{ id: id, iat },
|
||||||
securityPropertiesSecret.security.jwtSecret,
|
Config.apiConfig.getAll().security.jwtSecret,
|
||||||
{
|
{
|
||||||
algorithm,
|
algorithm,
|
||||||
},
|
},
|
||||||
|
@ -52,8 +52,7 @@ router.post(
|
|||||||
let discriminator = "";
|
let discriminator = "";
|
||||||
|
|
||||||
// get register Config
|
// get register Config
|
||||||
const securityProperties = Config.apiConfig.store as unknown as Config.DefaultOptions;
|
const { register, security } = Config.apiConfig.getAll();
|
||||||
const { register, security } = securityProperties;
|
|
||||||
|
|
||||||
// check if registration is allowed
|
// check if registration is allowed
|
||||||
if (!register.allowNewRegistration) {
|
if (!register.allowNewRegistration) {
|
||||||
|
@ -20,8 +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 limitsProperties = Config.apiConfig.get('limits.message') as Config.DefaultOptions;
|
const { maxBulkDelete } = Config.apiConfig.getAll().limits.message;
|
||||||
const { maxBulkDelete } = limitsProperties.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");
|
||||||
|
@ -18,8 +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 limitsProperties = Config.apiConfig.get('limits.channel') as Config.DefaultOptions;
|
const { maxPins } = Config.apiConfig.getAll().limits.channel;
|
||||||
const { maxPins } = limitsProperties.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();
|
||||||
|
@ -4,8 +4,7 @@ import * as Config from "../util/Config"
|
|||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.get("/", (req, res) => {
|
router.get("/", (req, res) => {
|
||||||
const generalConfig = Config.apiConfig.get('gateway', 'ws://localhost:3002') as Config.DefaultOptions;
|
const { gateway } = Config.apiConfig.getAll();
|
||||||
const { gateway } = generalConfig;
|
|
||||||
res.send({ url: gateway || "ws://localhost:3002" });
|
res.send({ url: gateway || "ws://localhost:3002" });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -14,8 +14,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 limitsProperties = Config.apiConfig.get('limits.user') as Config.DefaultOptions;
|
const { maxGuilds } = Config.apiConfig.getAll().limits.user;
|
||||||
const { maxGuilds } = limitsProperties.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) {
|
||||||
|
@ -21,8 +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 limitsProperties = Config.apiConfig.get('limits.user') as Config.DefaultOptions;
|
const { maxGuilds } = Config.apiConfig.getAll().limits.user;
|
||||||
const { maxGuilds } = limitsProperties.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) {
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import Ajv, { JSONSchemaType } from "ajv"
|
import Ajv, { JSONSchemaType, ValidateFunction } from "ajv"
|
||||||
import { ValidateFunction } from 'ajv'
|
|
||||||
import ajvFormats from 'ajv-formats';
|
import ajvFormats from 'ajv-formats';
|
||||||
import dotProp from "dot-prop";
|
import dotProp from "dot-prop";
|
||||||
import envPaths from "env-paths";
|
import envPaths from "env-paths";
|
||||||
@ -101,6 +100,7 @@ export interface DefaultOptions {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const schema: JSONSchemaType<DefaultOptions> & {
|
const schema: JSONSchemaType<DefaultOptions> & {
|
||||||
definitions: {
|
definitions: {
|
||||||
rateLimitOptions: JSONSchemaType<RateLimitOptions>
|
rateLimitOptions: JSONSchemaType<RateLimitOptions>
|
||||||
@ -398,6 +398,10 @@ class Config<T extends Record<string, any> = Record<string, unknown>> implements
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _has<Key extends keyof T>(key: Key | string): boolean {
|
||||||
|
return dotProp.has(this.store, key as string);
|
||||||
|
}
|
||||||
|
|
||||||
private _validate(data: T | unknown): void {
|
private _validate(data: T | unknown): void {
|
||||||
if (!this.#validator) {
|
if (!this.#validator) {
|
||||||
return;
|
return;
|
||||||
@ -432,7 +436,7 @@ class Config<T extends Record<string, any> = Record<string, unknown>> implements
|
|||||||
private _ensureDirectory(): void {
|
private _ensureDirectory(): void {
|
||||||
fs.mkdirSync(path.dirname(this.path), { recursive: true })
|
fs.mkdirSync(path.dirname(this.path), { recursive: true })
|
||||||
}
|
}
|
||||||
|
|
||||||
set store(value: T) {
|
set store(value: T) {
|
||||||
this._validate(value);
|
this._validate(value);
|
||||||
|
|
||||||
@ -449,9 +453,17 @@ class Config<T extends Record<string, any> = Record<string, unknown>> implements
|
|||||||
return this._get(key, defaultValue);
|
return this._get(key, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getAll(): DefaultOptions {
|
||||||
|
return this.store as unknown as DefaultOptions
|
||||||
|
}
|
||||||
|
|
||||||
private _get<Key extends keyof T>(key: Key): T[Key] | undefined;
|
private _get<Key extends keyof T>(key: Key): T[Key] | undefined;
|
||||||
private _get<Key extends keyof T, Default = unknown>(key: Key, defaultValue: Default): T[Key] | Default;
|
private _get<Key extends keyof T, Default = unknown>(key: Key, defaultValue: Default): T[Key] | Default;
|
||||||
private _get<Key extends keyof T, Default = unknown>(key: Key | string, defaultValue?: Default): Default | undefined {
|
private _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]);
|
return dotProp.get<T[Key] | undefined>(this.store, key as string, defaultValue as T[Key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,8 +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 limitsUserProperties = Config.apiConfig.get('limits.user', {maxGuilds: 100, masxUsername: 32, maxFriends: 1000}) as Config.DefaultOptions;
|
const { maxGuilds } = Config.apiConfig.getAll().limits.user;
|
||||||
const { maxGuilds } = limitsUserProperties.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);
|
||||||
}
|
}
|
||||||
|
@ -17,14 +17,13 @@ const blocklist: string[] = []; // TODO: update ones passwordblocklist is stored
|
|||||||
* Returns: 0 > pw > 1
|
* Returns: 0 > pw > 1
|
||||||
*/
|
*/
|
||||||
export function check(password: string): number {
|
export function check(password: string): number {
|
||||||
const passwordProperties = Config.apiConfig.get('register.password', { minLength: 8, minNumbers: 2, minUpperCase: 2, minSymbols: 0, blockInsecureCommonPasswords: false }) as Config.DefaultOptions;
|
|
||||||
const {
|
const {
|
||||||
minLength,
|
minLength,
|
||||||
minNumbers,
|
minNumbers,
|
||||||
minUpperCase,
|
minUpperCase,
|
||||||
minSymbols,
|
minSymbols,
|
||||||
blockInsecureCommonPasswords,
|
blockInsecureCommonPasswords,
|
||||||
} = passwordProperties.register.password;
|
} = Config.apiConfig.getAll().register.password;
|
||||||
var strength = 0;
|
var strength = 0;
|
||||||
|
|
||||||
// checks for total password len
|
// checks for total password len
|
||||||
|
Loading…
Reference in New Issue
Block a user