Skip to content

Commit 2fcb476

Browse files
committed
Now in the manager, when logging in with the client's apikey, the listing only shows the instance corresponding to the provided apikey (only with MongoDB)
1 parent 395b81a commit 2fcb476

File tree

5 files changed

+59
-13
lines changed

5 files changed

+59
-13
lines changed

src/api/controllers/instance.controller.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { isURL } from 'class-validator';
33
import EventEmitter2 from 'eventemitter2';
44
import { v4 } from 'uuid';
55

6-
import { ConfigService, HttpServer, WaBusiness } from '../../config/env.config';
6+
import { Auth, ConfigService, HttpServer, WaBusiness } from '../../config/env.config';
77
import { Logger } from '../../config/logger.config';
8-
import { BadRequestException, InternalServerErrorException } from '../../exceptions';
8+
import { BadRequestException, InternalServerErrorException, UnauthorizedException } from '../../exceptions';
99
import { InstanceDto, SetPresenceDto } from '../dto/instance.dto';
1010
import { ChatwootService } from '../integrations/chatwoot/services/chatwoot.service';
1111
import { RabbitmqService } from '../integrations/rabbitmq/services/rabbitmq.service';
@@ -679,11 +679,27 @@ export class InstanceController {
679679
};
680680
}
681681

682-
public async fetchInstances({ instanceName, instanceId, number }: InstanceDto) {
683-
if (instanceName) {
684-
this.logger.verbose('requested fetchInstances from ' + instanceName + ' instance');
685-
this.logger.verbose('instanceName: ' + instanceName);
686-
return this.waMonitor.instanceInfo(instanceName);
682+
public async fetchInstances({ instanceName, instanceId, number }: InstanceDto, key: string) {
683+
const env = this.configService.get<Auth>('AUTHENTICATION').API_KEY;
684+
685+
let name = instanceName;
686+
let arrayReturn = false;
687+
688+
if (env.KEY !== key) {
689+
const instanceByKey = await this.repository.auth.findByKey(key);
690+
console.log('instanceByKey', instanceByKey);
691+
if (instanceByKey) {
692+
name = instanceByKey._id;
693+
arrayReturn = true;
694+
} else {
695+
throw new UnauthorizedException();
696+
}
697+
}
698+
699+
if (name) {
700+
this.logger.verbose('requested fetchInstances from ' + name + ' instance');
701+
this.logger.verbose('instanceName: ' + name);
702+
return this.waMonitor.instanceInfo(name, arrayReturn);
687703
} else if (instanceId || number) {
688704
return this.waMonitor.instanceInfoById(instanceId, number);
689705
}

src/api/guards/auth.guard.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,30 @@ async function apikey(req: Request, _: Response, next: NextFunction) {
5959
const env = configService.get<Auth>('AUTHENTICATION').API_KEY;
6060
const key = req.get('apikey');
6161

62+
if (!key) {
63+
throw new UnauthorizedException();
64+
}
65+
6266
if (env.KEY === key) {
6367
return next();
6468
}
6569

6670
if ((req.originalUrl.includes('/instance/create') || req.originalUrl.includes('/instance/fetchInstances')) && !key) {
6771
throw new ForbiddenException('Missing global api key', 'The global api key must be set');
6872
}
73+
const param = req.params as unknown as InstanceDto;
6974

7075
try {
71-
const param = req.params as unknown as InstanceDto;
72-
const instanceKey = await repository.auth.find(param.instanceName);
73-
if (instanceKey.apikey === key) {
74-
return next();
76+
if (param?.instanceName) {
77+
const instanceKey = await repository.auth.find(param.instanceName);
78+
if (instanceKey?.apikey === key) {
79+
return next();
80+
}
81+
} else {
82+
const instanceByKey = await repository.auth.findByKey(key);
83+
if (instanceByKey) {
84+
return next();
85+
}
7586
}
7687
} catch (error) {
7788
logger.error(error);

src/api/repository/auth.repository.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ export class AuthRepository extends Repository {
6868
}
6969
}
7070

71+
public async findByKey(key: string): Promise<AuthRaw> {
72+
try {
73+
this.logger.verbose('finding auth');
74+
if (this.dbSettings.ENABLED) {
75+
this.logger.verbose('finding auth in db');
76+
return await this.authModel.findOne({ apikey: key });
77+
}
78+
79+
return {};
80+
} catch (error) {
81+
return {};
82+
}
83+
}
84+
7185
public async list(): Promise<AuthRaw[]> {
7286
try {
7387
if (this.dbSettings.ENABLED) {

src/api/routes/instance.router.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ export class InstanceRouter extends RouterBroker {
103103
logger.verbose('request body: ');
104104
logger.verbose(req.body);
105105

106+
const key = req.get('apikey');
107+
106108
logger.verbose('request query: ');
107109
logger.verbose(req.query);
108110
const response = await this.dataValidate<InstanceDto>({
109111
request: req,
110112
schema: null,
111113
ClassRef: InstanceDto,
112-
execute: (instance) => instanceController.fetchInstances(instance),
114+
execute: (instance) => instanceController.fetchInstances(instance, key),
113115
});
114116

115117
return res.status(HttpStatus.OK).json(response);

src/api/services/monitor.service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class WAMonitoringService {
8383
}
8484
}
8585

86-
public async instanceInfo(instanceName?: string) {
86+
public async instanceInfo(instanceName?: string, arrayReturn = false) {
8787
this.logger.verbose('get instance info');
8888
if (instanceName && !this.waInstances[instanceName]) {
8989
throw new NotFoundException(`Instance "${instanceName}" not found`);
@@ -171,6 +171,9 @@ export class WAMonitoringService {
171171

172172
this.logger.verbose('return instance info: ' + instances.length);
173173

174+
if (arrayReturn) {
175+
return [instances.find((i) => i.instance.instanceName === instanceName) ?? instances];
176+
}
174177
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
175178
}
176179

0 commit comments

Comments
 (0)