Skip to content
This repository has been archived by the owner on Jul 25, 2021. It is now read-only.

Feat: Added /discord/:id endpoint to allow connection to UniCS Discord Bot. #145

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/controllers/DiscordController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { NextFunction, Request } from 'express';
import { inject, injectable } from 'tsyringe';
import { AuthenticatedResponse } from '../routes/middleware/getUser';
import DiscordService from '../services/DiscordService';
import { HttpCode } from '../util/errors';
import { APIError, HttpCode } from '../util/errors';

enum GetUserError {
UserNotFound = 'User not found',
}

@injectable()
export class DiscordController {
Expand All @@ -12,6 +16,17 @@ export class DiscordController {
this.discordService = discordService;
}

public async getDiscordUser(req: Request & { params: { id: string } }, res: AuthenticatedResponse, next: NextFunction): Promise<void> {
try {
if (!req.params.id) throw new APIError(HttpCode.NotFound, GetUserError.UserNotFound);
const link = await this.discordService.fetch(req.params.id);
if (!link) throw new APIError(HttpCode.NotFound, GetUserError.UserNotFound);
res.json(link);
} catch (error) {
next(error);
}
}

public async getOAuth2AuthorizeURL(req: Request, res: AuthenticatedResponse, next: NextFunction): Promise<void> {
try {
const url = await this.discordService.generateAuthorizeURL(res.locals.user.id);
Expand Down
4 changes: 3 additions & 1 deletion src/routes/UserRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Router } from 'express';
import { UserController } from '../controllers/UserController';
import { inject, injectable } from 'tsyringe';
import { getUser, isVerified, uploadImg } from './middleware';
import { getUser, isAdmin, isVerified, uploadImg } from './middleware';
import { TokenType } from '../util/auth';
import { DiscordController } from '../controllers/DiscordController';
import { getRatelimiter, RateLimiter } from '../util/ratelimits';
Expand Down Expand Up @@ -33,6 +33,8 @@ export class UserRoutes {

router.get('/users/:id', getUser(TokenType.Auth), isVerified, this.userController.getUser.bind(this.userController));

router.get('/users/discord/:id', getUser(TokenType.Auth), isAdmin, this.discordController.getDiscordUser.bind(this.discordController));

router.get('/users/@me/notes', getUser(TokenType.Auth), isVerified, this.userController.getNotes.bind(this.userController));

router.put('/users/@me/notes/:id', getUser(TokenType.Auth), isVerified, this.userController.createNote.bind(this.userController));
Expand Down
14 changes: 14 additions & 0 deletions src/services/DiscordService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ export default class DiscordService {
});
}

public async fetch(discordID: string) {
return getConnection().transaction(async entityManager => {
const link = await entityManager.findOne(DiscordLink, {
where: [
{ discordID }
],
relations: [
'user'
]
});
return link;
});
}

public generateOAuth2State(userID: string) {
const hmac = createHmac('sha256', getConfig().discord.oauth2Secret)
.update(userID)
Expand Down