feat: prisma

This commit is contained in:
Davidson Gomes
2024-06-06 01:08:24 -03:00
parent 8eced6c575
commit 272bed1351
74 changed files with 1140 additions and 4148 deletions

View File

@@ -1,60 +1,13 @@
import { isJWT } from 'class-validator';
import { NextFunction, Request, Response } from 'express';
import jwt from 'jsonwebtoken';
import { name } from '../../../package.json';
import { Auth, configService, Database } from '../../config/env.config';
import { Logger } from '../../config/logger.config';
import { ForbiddenException, UnauthorizedException } from '../../exceptions';
import { InstanceDto } from '../dto/instance.dto';
import { repository } from '../server.module';
import { JwtPayload } from '../services/auth.service';
import { mongodbRepository } from '../server.module';
const logger = new Logger('GUARD');
async function jwtGuard(req: Request, res: Response, next: NextFunction) {
const key = req.get('apikey');
if (key && configService.get<Auth>('AUTHENTICATION').API_KEY.KEY !== key) {
throw new UnauthorizedException();
}
if (configService.get<Auth>('AUTHENTICATION').API_KEY.KEY === key) {
return next();
}
if ((req.originalUrl.includes('/instance/create') || req.originalUrl.includes('/instance/fetchInstances')) && !key) {
throw new ForbiddenException('Missing global api key', 'The global api key must be set');
}
const jwtOpts = configService.get<Auth>('AUTHENTICATION').JWT;
try {
const [bearer, token] = req.get('authorization').split(' ');
if (bearer.toLowerCase() !== 'bearer') {
throw new UnauthorizedException();
}
if (!isJWT(token)) {
throw new UnauthorizedException();
}
const param = req.params as unknown as InstanceDto;
const decode = jwt.verify(token, jwtOpts.SECRET, {
ignoreExpiration: jwtOpts.EXPIRIN_IN === 0,
}) as JwtPayload;
if (param.instanceName !== decode.instanceName || name !== decode.apiName) {
throw new UnauthorizedException();
}
return next();
} catch (error) {
logger.error(error);
throw new UnauthorizedException();
}
}
async function apikey(req: Request, _: Response, next: NextFunction) {
const env = configService.get<Auth>('AUTHENTICATION').API_KEY;
const key = req.get('apikey');
@@ -75,13 +28,13 @@ async function apikey(req: Request, _: Response, next: NextFunction) {
try {
if (param?.instanceName) {
const instanceKey = await repository.auth.find(param.instanceName);
const instanceKey = await mongodbRepository.auth.find(param.instanceName);
if (instanceKey?.apikey === key) {
return next();
}
} else {
if (req.originalUrl.includes('/instance/fetchInstances') && db.ENABLED) {
const instanceByKey = await repository.auth.findByKey(key);
const instanceByKey = await mongodbRepository.auth.findByKey(key);
if (instanceByKey) {
return next();
}
@@ -94,4 +47,4 @@ async function apikey(req: Request, _: Response, next: NextFunction) {
throw new UnauthorizedException();
}
export const authGuard = { jwt: jwtGuard, apikey };
export const authGuard = { apikey };

View File

@@ -10,7 +10,7 @@ import {
InternalServerErrorException,
NotFoundException,
} from '../../exceptions';
import { dbserver } from '../../libs/db.connect';
import { mongodbServer } from '../../libs/mongodb.connect';
import { InstanceDto } from '../dto/instance.dto';
import { cache, waMonitor } from '../server.module';
@@ -28,7 +28,7 @@ async function getInstance(instanceName: string) {
}
if (db.ENABLED) {
const collection = dbserver
const collection = mongodbServer
.getClient()
.db(db.CONNECTION.DB_PREFIX_NAME + '-instances')
.collection(instanceName);