Newer
Older
FORESTIER Fabien
committed
import { Logger, ForbiddenException, NestMiddleware, Injectable, UnauthorizedException } from '@nestjs/common';
import { ConfigService } from '../configuration/config.service';
FORESTIER Fabien
committed
@Injectable()
export class EmailWriterMiddleware implements NestMiddleware {
constructor(private configService: ConfigService) {
}
resolve() {
return (req, res, next) => {
Logger.log('[-] Untokenise middleware called');
const groupHeaderName = this.configService.config.groupHeader;
if (req.headers[groupHeaderName]) {
let arr = req.headers[groupHeaderName].split(',');
arr = arr.map(e => e.trim());
const group = arr.find(e => e === this.configService.config.emailWriterGroupName);
if (group === undefined) {
throw new ForbiddenException('You can\'t access this ressource.');
} else {
next();
}
} else {
throw new UnauthorizedException('You can\'t access this ressource.');
}
};
}
}