diff --git a/src/app.module.ts b/src/app.module.ts index 5c47c896a3b452c15ffc23e1210b832720e088e2..a7e95e2a831407c03d73b949e7e7fab3a8f1a9cc 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,10 +1,19 @@ -import { Module } from '@nestjs/common'; +import { Module, MiddlewareConsumer, RequestMethod } from '@nestjs/common'; import { EmailModule } from './email/email.module'; import { ConfigModule } from './configuration/config.module'; +import { microserviceMiddleware } from './middlewares/microservice.middleware'; @Module({ imports: [ConfigModule, EmailModule], controllers: [], providers: [], }) -export class AppModule {} +export class AppModule { + configure(consumer: MiddlewareConsumer) { + // Applying the middleware that takes the Authorization header jwt payload and put it in the request headers + consumer + .apply(microserviceMiddleware).forRoutes( + { path: 'email/send', method: RequestMethod.POST }, + ); + } +} diff --git a/src/middlewares/microservice.middleware.ts b/src/middlewares/microservice.middleware.ts new file mode 100644 index 0000000000000000000000000000000000000000..91704bb6917f5b52d947e407606ad68f5232e8c5 --- /dev/null +++ b/src/middlewares/microservice.middleware.ts @@ -0,0 +1,17 @@ +import { Logger, ForbiddenException } from '@nestjs/common'; + +export function microserviceMiddleware(req, res, next) { + Logger.log('[-] Untokenise middleware called'); + if (req.headers['x-consumer-groups']) { + const arr = req.headers['x-consumer-groups'].split(','); + arr.map(e => e.trim()); + const group = arr.find(e => e === 'microservice'); + if (group === undefined) { + throw new ForbiddenException('You can\'t access this ressource.'); + } else { + next(); + } + } else { + throw new ForbiddenException('You can\'t access this ressource.'); + } +} \ No newline at end of file