summary refs log tree commit diff
path: root/util/src
diff options
context:
space:
mode:
Diffstat (limited to 'util/src')
-rw-r--r--util/src/entities/Config.ts2
-rw-r--r--util/src/entities/User.ts4
-rw-r--r--util/src/util/Config.ts2
-rw-r--r--util/src/util/Database.ts3
-rw-r--r--util/src/util/Email.ts20
-rw-r--r--util/src/util/Event.ts2
-rw-r--r--util/src/util/Token.ts (renamed from util/src/util/checkToken.ts)20
-rw-r--r--util/src/util/index.ts3
8 files changed, 49 insertions, 7 deletions
diff --git a/util/src/entities/Config.ts b/util/src/entities/Config.ts

index f969b6bb..a460b437 100644 --- a/util/src/entities/Config.ts +++ b/util/src/entities/Config.ts
@@ -77,6 +77,7 @@ export interface ConfigValue { maxWebhooks: number; }; rate: { + disabled: boolean; ip: Omit<RateLimitOptions, "bot_count">; global: RateLimitOptions; error: RateLimitOptions; @@ -188,6 +189,7 @@ export const DefaultConfigOptions: ConfigValue = { maxWebhooks: 10, }, rate: { + disabled: true, ip: { count: 500, window: 5, diff --git a/util/src/entities/User.ts b/util/src/entities/User.ts
index 4c86b2d8..b5c2c308 100644 --- a/util/src/entities/User.ts +++ b/util/src/entities/User.ts
@@ -161,15 +161,13 @@ export class User extends BaseClass { } static async getPublicUser(user_id: string, opts?: FindOneOptions<User>) { - const user = await User.findOne( + return await User.findOneOrFail( { id: user_id }, { ...opts, select: [...PublicUserProjection, ...(opts?.select || [])], } ); - if (!user) throw new HTTPError("User not found", 404); - return user; } } diff --git a/util/src/util/Config.ts b/util/src/util/Config.ts
index 1ec71ad0..c87d598e 100644 --- a/util/src/util/Config.ts +++ b/util/src/util/Config.ts
@@ -14,7 +14,7 @@ export const Config = { get: function get() { return config.value as ConfigValue; }, - set: function set(val: any) { + set: function set(val: Partial<ConfigValue>) { if (!config) return; config.value = val.merge(config?.value || {}); return config.save(); diff --git a/util/src/util/Database.ts b/util/src/util/Database.ts
index c22d8abd..0c3d7cef 100644 --- a/util/src/util/Database.ts +++ b/util/src/util/Database.ts
@@ -1,3 +1,4 @@ +import path from "path"; import "reflect-metadata"; import { Connection, createConnection, ValueTransformer } from "typeorm"; import * as Models from "../entities"; @@ -15,7 +16,7 @@ export function initDatabase() { // @ts-ignore promise = createConnection({ type: "sqlite", - database: "database.db", + database: path.join(process.cwd(), "database.db"), // type: "postgres", // url: "postgres://fosscord:wb94SmuURM2Syv&@localhost/fosscord", // diff --git a/util/src/util/Email.ts b/util/src/util/Email.ts new file mode 100644
index 00000000..c304f584 --- /dev/null +++ b/util/src/util/Email.ts
@@ -0,0 +1,20 @@ +export const EMAIL_REGEX = + /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + +export function adjustEmail(email: string): string | undefined { + if (!email) return email; + // body parser already checked if it is a valid email + const parts = <RegExpMatchArray>email.match(EMAIL_REGEX); + // @ts-ignore + if (!parts || parts.length < 5) return undefined; + const domain = parts[5]; + const user = parts[1]; + + // TODO: check accounts with uncommon email domains + if (domain === "gmail.com" || domain === "googlemail.com") { + // replace .dots and +alternatives -> Gmail Dot Trick https://support.google.com/mail/answer/7436150 and https://generator.email/blog/gmail-generator + return user.replace(/[.]|(\+.*)/g, "") + "@gmail.com"; + } + + return email; +} diff --git a/util/src/util/Event.ts b/util/src/util/Event.ts
index 765e5fc7..bf9547b1 100644 --- a/util/src/util/Event.ts +++ b/util/src/util/Event.ts
@@ -2,7 +2,7 @@ import { Channel } from "amqplib"; import { RabbitMQ } from "./RabbitMQ"; import EventEmitter from "events"; import { EVENT, Event } from "../interfaces"; -const events = new EventEmitter(); +export const events = new EventEmitter(); export async function emitEvent(payload: Omit<Event, "created_at">) { const id = (payload.channel_id || payload.user_id || payload.guild_id) as string; diff --git a/util/src/util/checkToken.ts b/util/src/util/Token.ts
index 8415e8c0..111d59a2 100644 --- a/util/src/util/checkToken.ts +++ b/util/src/util/Token.ts
@@ -1,4 +1,5 @@ import jwt, { VerifyOptions } from "jsonwebtoken"; +import { Config } from "./Config"; import { User } from "../entities"; export const JWTOptions: VerifyOptions = { algorithms: ["HS256"] }; @@ -21,3 +22,22 @@ export function checkToken(token: string, jwtSecret: string): Promise<any> { }); }); } + +export async function generateToken(id: string) { + const iat = Math.floor(Date.now() / 1000); + const algorithm = "HS256"; + + return new Promise((res, rej) => { + jwt.sign( + { id: id, iat }, + Config.get().security.jwtSecret, + { + algorithm, + }, + (err, token) => { + if (err) return rej(err); + return res(token); + } + ); + }); +} diff --git a/util/src/util/index.ts b/util/src/util/index.ts
index 3160380f..d73bf4ca 100644 --- a/util/src/util/index.ts +++ b/util/src/util/index.ts
@@ -1,11 +1,12 @@ export * from "./ApiError"; export * from "./BitField"; -export * from "./checkToken"; +export * from "./Token"; export * from "./cdn"; export * from "./Config"; export * from "./Constants"; export * from "./Database"; export * from "./Event"; +export * from "./Email"; export * from "./Intents"; export * from "./MessageFlags"; export * from "./Permissions";