-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
161 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { TTokenPayload } from './src/shared/modules/auth/types/index.js'; | ||
|
||
declare module 'express-serve-static-core' { | ||
export interface Request { | ||
tokenPayload: TTokenPayload; | ||
} | ||
} |
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,40 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { jwtVerify } from 'jose'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
|
||
import { createSecretKey } from 'node:crypto'; | ||
|
||
import { IMiddleware } from '../types/index.js'; | ||
import { HttpError } from '../index.js'; | ||
import { isTokenPayload } from '../../shared/helpers/index.js'; | ||
|
||
export class ParseTokenMiddleware implements IMiddleware { | ||
constructor(private readonly jwtSecret: string) {} | ||
|
||
public async execute(req: Request, _res: Response, next: NextFunction): Promise<void> { | ||
const authorizationHeader = req.headers?.authorization?.split(' '); | ||
|
||
if (!authorizationHeader) { | ||
return next(); | ||
} | ||
|
||
const [, token] = authorizationHeader; | ||
|
||
try { | ||
const { payload } = await jwtVerify(token, createSecretKey(this.jwtSecret, 'utf-8')); | ||
|
||
if (isTokenPayload(payload)) { | ||
req.tokenPayload = { ...payload }; | ||
return next(); | ||
} else { | ||
throw new Error('Bad token'); | ||
} | ||
} catch { | ||
return next(new HttpError( | ||
StatusCodes.UNAUTHORIZED, | ||
'Invalid token', | ||
'ParseTokenMiddleware', | ||
)); | ||
} | ||
} | ||
} |
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,19 @@ | ||
import { StatusCodes } from 'http-status-codes'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
|
||
import { IMiddleware } from '../types/index.js'; | ||
import { HttpError } from '../index.js'; | ||
|
||
export class PrivateRouteMiddleware implements IMiddleware { | ||
public async execute({ tokenPayload }: Request, _res: Response, next: NextFunction): Promise<void> { | ||
if (!tokenPayload) { | ||
throw new HttpError( | ||
StatusCodes.UNAUTHORIZED, | ||
'Unauthorized', | ||
'PrivateRouteMiddleware', | ||
); | ||
} | ||
|
||
return 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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
|
||
export interface IMiddleware { | ||
execute(req: Request, res: Response, next: NextFunction): void; | ||
execute(req: Request, res: Response, next: NextFunction): Promise<void> | void; | ||
} |
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,8 @@ | ||
import { TTokenPayload } from '../modules/auth/types/index.js'; | ||
|
||
export const isTokenPayload = (payload: unknown): payload is TTokenPayload => (( | ||
(typeof payload === 'object' && payload !== null) && | ||
('email' in payload && typeof payload.email === 'string') && | ||
('name' in payload && typeof payload.name === 'string') && | ||
('id' in payload && typeof payload.id === 'string') | ||
)); |
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
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,9 @@ | ||
import { Expose } from 'class-transformer'; | ||
|
||
export class LoggedUserRDO { | ||
@Expose() | ||
public token!: string; | ||
|
||
@Expose() | ||
public email!: string; | ||
} |
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.