generated from CS3219-AY2324S1/course-assessment-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from CS3219-AY2324S1/feat/add-notification
Feat/add notification
- Loading branch information
Showing
45 changed files
with
811 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum Difficulty { | ||
EASY = "Easy", | ||
MEDIUM = "Medium", | ||
HARD = "Hard", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum OAuthType { | ||
Google = "google", | ||
Github = "github", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export enum ProgrammingLanguage { | ||
PYTHON = "Python (3.8.1)", | ||
JAVASCRIPT = "JavaScript (Node.js 12.14.0)", | ||
TYPESCRIPT = "TypeScript (3.7.4)", | ||
CPP = "C++ (GCC 9.2.0)", | ||
JAVA = "Java (OpenJDK 13.0.1)", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum Role { | ||
Admin = "admin", | ||
Normal = "normal", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import axios, { AxiosError } from "axios"; | ||
import { Request, Response, NextFunction } from "express"; | ||
import { OAuthType } from "../enums/OAuthType.ts"; | ||
import { Role } from "../enums/Role.ts"; | ||
|
||
interface User { | ||
id: number; | ||
email: string; | ||
username: string; | ||
password: string; | ||
oauth: OAuthType[]; | ||
role: Role; | ||
iat: string; | ||
exp: string; | ||
} | ||
|
||
export interface AuthenticatedRequest extends Request { | ||
user?: User; | ||
} | ||
|
||
function getAxiosErrorMessage(error: unknown) { | ||
// console.log(error); | ||
if (error instanceof AxiosError) { | ||
if (error.code === "ECONNREFUSED") { | ||
return "Unable to connect with user service."; | ||
} | ||
return error.response?.data?.error; | ||
} | ||
return String(error); | ||
} | ||
|
||
const verifyAccessToken = async (accessToken?: string) => { | ||
const response = await axios.get( | ||
process.env.USER_SERVICE_URL + "/api/users/verifyJwt", | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
} | ||
); | ||
console.log("Verified User: ", response.data); | ||
return response.data; | ||
}; | ||
|
||
export const jwtGuard = async ( | ||
req: AuthenticatedRequest, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
console.log("Checking JWT"); | ||
const accessToken = req.headers.authorization?.split(" ")[1]; | ||
if (!accessToken) { | ||
res.status(401).json({ error: "No access token was provided." }); | ||
return; | ||
} | ||
|
||
try { | ||
req.user = await verifyAccessToken(accessToken); | ||
console.log(req.user); | ||
next(); | ||
} catch (error) { | ||
const errorMessage = getAxiosErrorMessage(error); | ||
console.log(errorMessage); | ||
res.status(401).json({ error: errorMessage }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import axios, { AxiosError } from "axios"; | ||
import { Request, Response, NextFunction } from "express"; | ||
import { OAuthType } from "../enums/OAuthType.ts"; | ||
import { Role } from "../enums/Role.ts"; | ||
import SessionModel, { SessionEntity } from "../model/Session.ts"; | ||
import { AuthenticatedRequest } from "./jwtGuard.ts"; | ||
|
||
export interface AuthenticatedRequestWithSession extends AuthenticatedRequest { | ||
session?: SessionEntity; | ||
} | ||
|
||
function getAxiosErrorMessage(error: unknown) { | ||
if (error instanceof AxiosError) { | ||
if (error.code === "ECONNREFUSED") { | ||
return "Unable to connect with user service."; | ||
} | ||
return error.response?.data?.error; | ||
} | ||
return String(error); | ||
} | ||
|
||
const verifyUserInSession = async (sessionId: string, userId: number) => { | ||
const session = await SessionModel.findById(sessionId); | ||
if (!session) { | ||
console.log("No session of this sessionId has been found"); | ||
return { isUserInSession: undefined, session: undefined }; | ||
} | ||
const isUserInSession: boolean = session.users.includes(userId); | ||
return { isUserInSession, session }; | ||
}; | ||
|
||
export const sessionGuard = async ( | ||
req: AuthenticatedRequestWithSession, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
const sessionId = req.params.sessionId; | ||
console.log("HELLO"); | ||
if (!sessionId) { | ||
res.status(401).json({ error: "No sesionId was provided." }); | ||
return; | ||
} | ||
|
||
try { | ||
if (!req.user || !req.user.id) { | ||
return res.status(401).json({ error: "User not found" }); | ||
} | ||
const { isUserInSession, session } = await verifyUserInSession( | ||
sessionId, | ||
req.user.id | ||
); | ||
if (!session) { | ||
console.log("No session of that sessionId was found."); | ||
return res | ||
.status(404) | ||
.json({ error: "No session of that sessionId was found." }); | ||
} | ||
if (!isUserInSession) { | ||
console.log("User is not a part of this session."); | ||
return res | ||
.status(401) | ||
.json({ error: "User is not a part of this session." }); | ||
} | ||
req.session = session; | ||
next(); | ||
} catch (error) { | ||
const errorMessage = getAxiosErrorMessage(error); | ||
res.status(401).json({ error: errorMessage }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { NextFunction, Response } from "express"; | ||
import { AuthenticatedRequest } from "./jwtGuard"; | ||
|
||
export const userGuard = async ( | ||
req: AuthenticatedRequest, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
const user = req.user; | ||
const uid = Number(req.params.user); | ||
|
||
if (user?.id != uid) { | ||
return res | ||
.status(401) | ||
.json({ | ||
err: "User is attempting to access the session history of another user", | ||
}); | ||
} else { | ||
next(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.