Skip to content
Snippets Groups Projects
Commit 4119dc04 authored by FORESTIER Fabien's avatar FORESTIER Fabien
Browse files

Add microservice middleware

parent 5d5cdc64
No related branches found
No related tags found
No related merge requests found
Pipeline #
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 },
);
}
}
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment