11import {
2- BadRequestException ,
3- HttpException ,
4- Injectable ,
5- InternalServerErrorException ,
6- NestMiddleware ,
7- UnauthorizedException ,
2+ BadRequestException ,
3+ HttpException ,
4+ Injectable ,
5+ InternalServerErrorException ,
6+ NestMiddleware ,
7+ UnauthorizedException ,
88} from '@nestjs/common' ;
99import { InjectRepository } from '@nestjs/typeorm' ;
1010import { Response } from 'express' ;
@@ -21,61 +21,73 @@ import { IRequestWithCognitoInfo } from './cognito-decoded.interface.js';
2121
2222@Injectable ( )
2323export class AuthMiddleware implements NestMiddleware {
24- public constructor (
25- @InjectRepository ( UserEntity ) readonly _userRepository : Repository < UserEntity > ,
26- @InjectRepository ( LogOutEntity )
27- private readonly logOutRepository : Repository < LogOutEntity > ,
28- ) { }
29- async use ( req : IRequestWithCognitoInfo , _res : Response , next : ( err ?: any , res ?: any ) => void ) : Promise < void > {
30- let token : string ;
31- try {
32- token = req . cookies [ Constants . JWT_COOKIE_KEY_NAME ] ;
33- } catch ( _e ) {
34- if ( process . env . NODE_ENV !== 'test' ) {
35- throw new UnauthorizedException ( 'JWT verification failed' ) ;
36- }
37- }
24+ public constructor (
25+ @InjectRepository ( UserEntity )
26+ private readonly userRepository : Repository < UserEntity > ,
27+ @InjectRepository ( LogOutEntity )
28+ private readonly logOutRepository : Repository < LogOutEntity > ,
29+ ) { }
30+ async use ( req : IRequestWithCognitoInfo , _res : Response , next : ( err ?: any , res ?: any ) => void ) : Promise < void > {
31+ let token : string ;
32+ try {
33+ token = req . cookies [ Constants . JWT_COOKIE_KEY_NAME ] ;
34+ } catch ( _e ) {
35+ if ( process . env . NODE_ENV !== 'test' ) {
36+ throw new UnauthorizedException ( 'JWT verification failed' ) ;
37+ }
38+ }
3839
39- if ( ! token ) {
40- throw new UnauthorizedException ( 'Token is missing' ) ;
41- }
40+ if ( ! token ) {
41+ throw new UnauthorizedException ( 'Token is missing' ) ;
42+ }
4243
43- const isLoggedOut = ! ! ( await this . logOutRepository . findOne ( { where : { jwtToken : token } } ) ) ;
44- if ( isLoggedOut ) {
45- throw new UnauthorizedException ( 'Token is invalid' ) ;
46- }
44+ const isLoggedOut = ! ! ( await this . logOutRepository . findOne ( { where : { jwtToken : token } } ) ) ;
45+ if ( isLoggedOut ) {
46+ throw new UnauthorizedException ( 'Token is invalid' ) ;
47+ }
4748
48- try {
49- const jwtSecret = process . env . JWT_SECRET ;
50- const data = jwt . verify ( token , jwtSecret ) as jwt . JwtPayload ;
51- const userId = data . id ;
52- if ( ! userId ) {
53- throw new UnauthorizedException ( 'JWT verification failed' ) ;
54- }
55- const addedScope : Array < JwtScopesEnum > = data . scope ;
56- if ( addedScope && addedScope . length > 0 ) {
57- if ( addedScope . includes ( JwtScopesEnum . TWO_FA_ENABLE ) ) {
58- throw new BadRequestException ( Messages . TWO_FA_REQUIRED ) ;
59- }
60- }
49+ try {
50+ const jwtSecret = process . env . JWT_SECRET ;
51+ const data = jwt . verify ( token , jwtSecret ) as jwt . JwtPayload ;
52+ const userId = data . id ;
6153
62- const payload = {
63- sub : userId ,
64- email : data . email ,
65- exp : data . exp ,
66- iat : data . iat ,
67- } ;
68- if ( ! payload || isObjectEmpty ( payload ) ) {
69- throw new UnauthorizedException ( 'JWT verification failed' ) ;
70- }
71- req . decoded = payload ;
72- next ( ) ;
73- } catch ( e ) {
74- Sentry . captureException ( e ) ;
75- if ( e instanceof HttpException || e instanceof UnauthorizedException ) {
76- throw e ;
77- }
78- throw new InternalServerErrorException ( Messages . AUTHORIZATION_REJECTED ) ;
79- }
80- }
54+ if ( ! userId ) {
55+ throw new UnauthorizedException ( 'JWT verification failed' ) ;
56+ }
57+
58+ const userExists = await this . userRepository . findOne ( { where : { id : userId } } ) ;
59+ if ( ! userExists ) {
60+ throw new UnauthorizedException ( 'JWT verification failed' ) ;
61+ }
62+
63+ if ( userExists . suspended ) {
64+ throw new UnauthorizedException ( Messages . ACCOUNT_SUSPENDED ) ;
65+ }
66+
67+ const addedScope : Array < JwtScopesEnum > = data . scope ;
68+ if ( addedScope && addedScope . length > 0 ) {
69+ if ( addedScope . includes ( JwtScopesEnum . TWO_FA_ENABLE ) ) {
70+ throw new BadRequestException ( Messages . TWO_FA_REQUIRED ) ;
71+ }
72+ }
73+
74+ const payload = {
75+ sub : userId ,
76+ email : data . email ,
77+ exp : data . exp ,
78+ iat : data . iat ,
79+ } ;
80+ if ( ! payload || isObjectEmpty ( payload ) ) {
81+ throw new UnauthorizedException ( 'JWT verification failed' ) ;
82+ }
83+ req . decoded = payload ;
84+ next ( ) ;
85+ } catch ( e ) {
86+ Sentry . captureException ( e ) ;
87+ if ( e instanceof HttpException || e instanceof UnauthorizedException ) {
88+ throw e ;
89+ }
90+ throw new InternalServerErrorException ( Messages . AUTHORIZATION_REJECTED ) ;
91+ }
92+ }
8193}
0 commit comments