diff --git a/src/api/middlewares/Authentication.ts b/src/api/middlewares/Authentication.ts index 078a566..865d1aa 100644 --- a/src/api/middlewares/Authentication.ts +++ b/src/api/middlewares/Authentication.ts @@ -16,7 +16,7 @@ along with this program. If not, see . */ -import { checkToken, Rights } from "@spacebar/util"; +import { checkToken, Rights, Session, User, UserTokenData } from "@spacebar/util"; import { NextFunction, Request, Response } from "express"; import { HTTPError } from "lambert-server"; @@ -68,7 +68,10 @@ interface Request { user_id: string; user_bot: boolean; + tokenData: UserTokenData; token: { id: string; iat: number; ver?: number; did?: string }; + user: User; + session?: Session; rights: Rights; fingerprint?: string; } @@ -116,14 +119,16 @@ if (!req.headers.authorization) return next(new HTTPError("Missing Authorization Header", 401)); try { - const { decoded, user, session, tokenVersion } = await checkToken(req.headers.authorization, { + const { decoded, user, session, tokenVersion } = (req.tokenData = await checkToken(req.headers.authorization, { ipAddress: req.ip, fingerprint: req.fingerprint, - }); + })); req.token = decoded; req.user_id = decoded.id; req.user_bot = user.bot; + req.user = user; + req.session = session; req.rights = new Rights(Number(user.rights)); return next(); } catch (error) { diff --git "a/src/api/routes/applications/\043application_id/bot/index.ts" "b/src/api/routes/applications/\043application_id/bot/index.ts" index 83b4fc6..835225e 100644 --- "a/src/api/routes/applications/\043application_id/bot/index.ts" +++ "b/src/api/routes/applications/\043application_id/bot/index.ts" @@ -67,7 +67,7 @@ }), async (req: Request, res: Response) => { const bot = await User.findOneOrFail({ where: { id: req.params.application_id } }); - const owner = await User.findOneOrFail({ where: { id: req.user_id } }); + const owner = req.user; if (owner.id != req.user_id) throw DiscordApiErrors.ACTION_NOT_AUTHORIZED_ON_APPLICATION; diff --git a/src/api/routes/applications/index.ts b/src/api/routes/applications/index.ts index ff89f03..e719e44 100644 --- a/src/api/routes/applications/index.ts +++ b/src/api/routes/applications/index.ts @@ -53,20 +53,18 @@ }), async (req: Request, res: Response) => { const body = req.body as ApplicationCreateSchema; - const user = await User.findOneOrFail({ where: { id: req.user_id } }); const app = Application.create({ name: trimSpecial(body.name), description: "", bot_public: true, - owner: user, + owner: req.user, verify_key: "IMPLEMENTME", flags: 0, }); // april 14, 2023: discord made bot users be automatically added to all new apps - const { autoCreateBotUsers } = Config.get().general; - if (autoCreateBotUsers) { + if (Config.get().general.autoCreateBotUsers) { await createAppBotUser(app, req); } else await app.save(); diff --git a/src/api/routes/auth/logout.ts b/src/api/routes/auth/logout.ts index b460e31..b4df13a 100644 --- a/src/api/routes/auth/logout.ts +++ b/src/api/routes/auth/logout.ts @@ -39,9 +39,7 @@ if (Object.keys(req.body).length != 0) console.log(`[LOGOUT]: Extra fields sent in logout!`, req.body); } - if (req.token.did) { - await Session.delete({ user_id: req.user_id, session_id: req.token.did }); - } + if (req.session) await Session.remove(req.session); res.status(204).send(); }, diff --git "a/src/api/routes/channels/\043channel_id/attachments.ts" "b/src/api/routes/channels/\043channel_id/attachments.ts" index 30fd2c6..0d5daa4 100644 --- "a/src/api/routes/channels/\043channel_id/attachments.ts" +++ "b/src/api/routes/channels/\043channel_id/attachments.ts" @@ -42,7 +42,7 @@ const payload = req.body as UploadAttachmentRequestSchema; const { channel_id } = req.params; - const user = await User.findOneOrFail({ where: { id: req.user_id } }); + const user = req.user; const channel = await Channel.findOneOrFail({ where: { id: channel_id } }); if (!(await channel.getUserPermissions({ user_id: req.user_id })).has(Permissions.FLAGS.ATTACH_FILES)) { @@ -102,7 +102,7 @@ router.delete("/:cloud_attachment_url", async (req: Request, res: Response) => { const { channel_id, cloud_attachment_url } = req.params; - const user = await User.findOneOrFail({ where: { id: req.user_id } }); + const user = req.user; const channel = await Channel.findOneOrFail({ where: { id: channel_id } }); const att = await CloudAttachment.findOneOrFail({ where: { uploadFilename: decodeURI(cloud_attachment_url) } }); if (att.userId !== user.id) { diff --git "a/src/api/routes/guilds/\043guild_id/emojis.ts" "b/src/api/routes/guilds/\043guild_id/emojis.ts" index 5b07380..f256ce3 100644 --- "a/src/api/routes/guilds/\043guild_id/emojis.ts" +++ "b/src/api/routes/guilds/\043guild_id/emojis.ts" @@ -108,7 +108,7 @@ if (emoji_count >= maxEmojis) throw DiscordApiErrors.MAXIMUM_NUMBER_OF_EMOJIS_REACHED.withParams(maxEmojis); if (body.require_colons == null) body.require_colons = true; - const user = await User.findOneOrFail({ where: { id: req.user_id } }); + const user = req.user; await handleFile(`/emojis/${id}`, body.image); const mimeType = body.image.split(":")[1].split(";")[0]; diff --git a/src/api/routes/interactions/index.ts b/src/api/routes/interactions/index.ts index eb516ce..e997964 100644 --- a/src/api/routes/interactions/index.ts +++ b/src/api/routes/interactions/index.ts @@ -41,7 +41,7 @@ }, } as InteractionCreateEvent); - const user = await User.findOneOrFail({ where: { id: req.user_id } }); + const user = req.user; const interactionData: Partial = { id: interactionId, diff --git a/src/api/routes/invites/index.ts b/src/api/routes/invites/index.ts index 26b0ad9..b4fa8a5 100644 --- a/src/api/routes/invites/index.ts +++ b/src/api/routes/invites/index.ts @@ -71,15 +71,13 @@ if (req.user_bot) throw DiscordApiErrors.BOT_PROHIBITED_ENDPOINT; const { invite_code } = req.params; + const { public_flags } = req.user; const { guild_id } = await Invite.findOneOrFail({ where: { code: invite_code }, }); const { features } = await Guild.findOneOrFail({ where: { id: guild_id }, }); - const { public_flags } = await User.findOneOrFail({ - where: { id: req.user_id }, - }); const ban = await Ban.findOne({ where: [ { guild_id: guild_id, user_id: req.user_id }, diff --git a/src/api/routes/teams.ts b/src/api/routes/teams.ts index 27ccabf..090807d 100644 --- a/src/api/routes/teams.ts +++ b/src/api/routes/teams.ts @@ -63,7 +63,7 @@ }), async (req: Request, res: Response) => { const user = await User.findOneOrFail({ - where: [{ id: req.user_id }], + where: { id: req.user_id }, select: ["mfa_enabled"], }); if (!user.mfa_enabled) throw new HTTPError("You must enable MFA to create a team"); diff --git a/src/api/routes/users/@me/mentions.ts b/src/api/routes/users/@me/mentions.ts index 2d631e9..c2985ca 100644 --- a/src/api/routes/users/@me/mentions.ts +++ b/src/api/routes/users/@me/mentions.ts @@ -43,9 +43,7 @@ const before = req.query.before !== undefined ? String(req.query.before as string) : undefined; const guild_id = req.query.guild_id !== undefined ? req.query.guild_id : undefined; - const user = await User.findOneOrFail({ - where: { id: req.user_id }, - }); + const user = req.user; const memberships = await Member.find({ where: { id: req.user_id, ...(guild_id === undefined ? {} : { guild_id: String(guild_id) }) }, diff --git a/src/api/routes/users/@me/mfa/codes-verification.ts b/src/api/routes/users/@me/mfa/codes-verification.ts index 4cc4c07..9421303 100644 --- a/src/api/routes/users/@me/mfa/codes-verification.ts +++ b/src/api/routes/users/@me/mfa/codes-verification.ts @@ -46,7 +46,7 @@ // TODO: We don't have email/etc etc, so can't send a verification code. // Once that's done, this route can verify `key` - // const user = await User.findOneOrFail({ where: { id: req.user_id } }); + // const user = req.user; if ((await User.count({ where: { id: req.user_id } })) === 0) throw DiscordApiErrors.UNKNOWN_USER; let codes: BackupCode[]; diff --git a/src/api/routes/users/@me/notes.ts b/src/api/routes/users/@me/notes.ts index 8cd3330..f25aca7 100644 --- a/src/api/routes/users/@me/notes.ts +++ b/src/api/routes/users/@me/notes.ts @@ -65,7 +65,7 @@ }), async (req: Request, res: Response) => { const { user_id } = req.params; - const owner = await User.findOneOrFail({ where: { id: req.user_id } }); + const owner = req.user; const target = await User.findOneOrFail({ where: { id: user_id } }); //if noted user does not exist throw const { note } = req.body;