From aa771335ae9310afc24f13575b6eb129b4e9a3a1 Mon Sep 17 00:00:00 2001 From: Richard Bao Date: Thu, 13 Apr 2023 19:50:26 +1000 Subject: [PATCH] updated it with new changes from other people --- backend/src/auth.ts | 47 +++++++++++++++++++++++++++- backend/src/index.ts | 10 ++++++ backend/src/interfaces/interfaces.ts | 8 +++++ backend/src/middleware.ts | 6 ++++ 4 files changed, 70 insertions(+), 1 deletion(-) diff --git a/backend/src/auth.ts b/backend/src/auth.ts index 9f1a0c04c..5b69fc7e5 100644 --- a/backend/src/auth.ts +++ b/backend/src/auth.ts @@ -8,7 +8,8 @@ import Helpers, { IResponseWithStatus } from './helpers'; import JWT from './jwt'; import { Logger, LogModule } from './logging'; import Secrets from './secrets'; -import { AuthRequest } from './interfaces/interfaces'; +import { AuthRequest, VerifyTokenRequest } from './interfaces/interfaces'; +import Middleware from './middleware'; import ev from './environment'; const LM = new LogModule('AUTH'); @@ -244,4 +245,48 @@ export default class Auth { } return true; } + + // check if token is valid + public static AuthenticateToken( + this: void, + req: VerifyTokenRequest, + res: Response, + next: NextFunction, + ) { + // const encodedJwt = req.body.jwt; + const { accountType } = req.body; + let response: IResponseWithStatus; + + // currently this line does not work as jwt decryption algorithm doesn't work + // const jwt: IToken = JWT.get(encodedJwt); + + // hard coded example + const jwt: IToken = { + id: 'test', + type: AccountType.Student, + lastRequestTimestamp: Date.now(), + ipAddress: '::1', + }; + + // checks it token is valid or not + try { + Middleware.verifyToken(req, jwt, accountType); + + response = { + status: StatusCodes.OK, + msg: jwt, + }; + } catch (error) { + // Error thrown meaning that token is invalid. + response = { status: StatusCodes.UNAUTHORIZED, msg: 'Token is invalid' }; + } + + if (response.msg === undefined) { + res.sendStatus(response.status); + } + else { + res.status(response.status).send(response.msg); + } + next(); + } } diff --git a/backend/src/index.ts b/backend/src/index.ts index de289f50b..32be6ca3a 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -43,6 +43,7 @@ import { UpdateCompanyDetailsRequest, VerifyCompanyAccountRequest, SearchJobRequest, + VerifyTokenRequest, } from './interfaces/interfaces'; const LM = new LogModule('INDEX'); @@ -470,6 +471,15 @@ app.get( Middleware.genericLoggingMiddleware, ); +app.get( + '/check-token-valid', + cors(corsOptions), + (req: VerifyTokenRequest, res, next) => { + Auth.AuthenticateToken(req, res, next); + }, + Middleware.genericLoggingMiddleware, +); + if (ev.data().NODE_ENV === 'development') { app.post('/email', (req, res) => { (async () => { diff --git a/backend/src/interfaces/interfaces.ts b/backend/src/interfaces/interfaces.ts index 02f7c9551..e77419c9f 100644 --- a/backend/src/interfaces/interfaces.ts +++ b/backend/src/interfaces/interfaces.ts @@ -1,5 +1,6 @@ import { Request } from 'express'; import { ParamsDictionary } from 'express-serve-static-core'; +import { AccountType } from '../auth'; import { JobMode, @@ -86,6 +87,11 @@ interface Year { year: string; } +interface VerifyBody { + jwt: string; + accountType: AccountType; +} + interface JobIDParams extends ParamsDictionary, JobID {} interface CompanyIdParams extends ParamsDictionary, CompanyID {} interface CompanyAccountIdParams extends ParamsDictionary, CompanyAccountID {} @@ -101,6 +107,8 @@ export interface PasswordResetRequest extends Request, CompanyAccountID {} // * Auth Functions export type AuthRequest = Request, never, AuthBody>; +export type VerifyTokenRequest = Request, never, VerifyBody>; + // * Admin Functions type AdminRequestBase = AdminID & JbToken; diff --git a/backend/src/middleware.ts b/backend/src/middleware.ts index abb2348a0..dd0ebc039 100644 --- a/backend/src/middleware.ts +++ b/backend/src/middleware.ts @@ -11,6 +11,7 @@ import { AuthoriseCompanyRequest, AuthoriseAdminRequest, PasswordResetRequest, + VerifyTokenRequest, } from './interfaces/interfaces'; import ev from './environment'; @@ -205,4 +206,9 @@ export default class Middleware { next(); } } + + public static verifyToken(req: VerifyTokenRequest, jwt: IToken, expectedType: AccountType) { + Middleware.verifyAccountType(jwt.type, expectedType); + Middleware.verifyTokenProperties(req, jwt); + } }