@@ -19,6 +19,7 @@ import jwt from 'jsonwebtoken';
1919import Sentry from '@sentry/minimal' ;
2020import { Encryptor } from '../helpers/encryption/encryptor.js' ;
2121import { EncryptionAlgorithmEnum } from '../enums/encryption-algorithm.enum.js' ;
22+ import { IRequestWithCognitoInfo } from './cognito-decoded.interface.js' ;
2223
2324@Injectable ( )
2425export class AuthWithApiMiddleware implements NestMiddleware {
@@ -27,7 +28,7 @@ export class AuthWithApiMiddleware implements NestMiddleware {
2728 private readonly userRepository : Repository < UserEntity > ,
2829 ) { }
2930
30- async use ( req : Request , res : Response , next : ( err ?: any , res ?: any ) => void ) : Promise < void > {
31+ async use ( req : IRequestWithCognitoInfo , _res : Response , next : ( err ?: any , res ?: any ) => void ) : Promise < void > {
3132 try {
3233 await this . authenticateRequest ( req ) ;
3334 next ( ) ;
@@ -37,7 +38,7 @@ export class AuthWithApiMiddleware implements NestMiddleware {
3738 }
3839 }
3940
40- private async authenticateRequest ( req : Request ) : Promise < void > {
41+ private async authenticateRequest ( req : IRequestWithCognitoInfo ) : Promise < void > {
4142 const tokenFromCookie = this . getTokenFromCookie ( req ) ;
4243 if ( tokenFromCookie ) {
4344 await this . authenticateWithToken ( tokenFromCookie , req ) ;
@@ -57,15 +58,15 @@ export class AuthWithApiMiddleware implements NestMiddleware {
5758 throw new InternalServerErrorException ( Messages . AUTHORIZATION_REJECTED ) ;
5859 }
5960
60- private async authenticateWithToken ( tokenFromCookie : string , req : Request ) : Promise < void > {
61+ private async authenticateWithToken ( tokenFromCookie : string , req : IRequestWithCognitoInfo ) : Promise < void > {
6162 try {
6263 const jwtSecret = process . env . JWT_SECRET ;
63- const data = jwt . verify ( tokenFromCookie , jwtSecret ) ;
64- const userId = data [ 'id' ] ;
64+ const data = jwt . verify ( tokenFromCookie , jwtSecret ) as jwt . JwtPayload ;
65+ const userId = data . id ;
6566 if ( ! userId ) {
6667 throw new UnauthorizedException ( 'JWT verification failed' ) ;
6768 }
68- const addedScope : Array < JwtScopesEnum > = data [ ' scope' ] ;
69+ const addedScope : Array < JwtScopesEnum > = data . scope ;
6970 if ( addedScope && addedScope . length > 0 ) {
7071 if ( addedScope . includes ( JwtScopesEnum . TWO_FA_ENABLE ) ) {
7172 throw new BadRequestException ( Messages . TWO_FA_REQUIRED ) ;
@@ -74,21 +75,21 @@ export class AuthWithApiMiddleware implements NestMiddleware {
7475
7576 const payload = {
7677 sub : userId ,
77- email : data [ ' email' ] ,
78- exp : data [ ' exp' ] ,
79- iat : data [ ' iat' ] ,
78+ email : data . email ,
79+ exp : data . exp ,
80+ iat : data . iat ,
8081 } ;
8182 if ( ! payload || isObjectEmpty ( payload ) ) {
8283 throw new UnauthorizedException ( 'JWT verification failed' ) ;
8384 }
84- req [ ' decoded' ] = payload ;
85+ req . decoded = payload ;
8586 } catch ( error ) {
8687 Sentry . captureException ( error ) ;
8788 throw error ;
8889 }
8990 }
9091
91- private async authenticateWithApiKey ( req : Request ) : Promise < void > {
92+ private async authenticateWithApiKey ( req : IRequestWithCognitoInfo ) : Promise < void > {
9293 let apiKey = req . headers ?. [ 'x-api-key' ] ;
9394 if ( Array . isArray ( apiKey ) ) {
9495 apiKey = apiKey [ 0 ] ;
@@ -106,7 +107,7 @@ export class AuthWithApiMiddleware implements NestMiddleware {
106107 if ( ! foundUserByApiKey ) {
107108 throw new NotFoundException ( Messages . NO_AUTH_KEYS_FOUND ) ;
108109 }
109- req [ ' decoded' ] = {
110+ req . decoded = {
110111 sub : foundUserByApiKey . id ,
111112 email : foundUserByApiKey . email ,
112113 } ;
0 commit comments