Skip to content

Commit 2847a95

Browse files
committed
feat: expose api key in fetch instances
1 parent fc30bb9 commit 2847a95

7 files changed

Lines changed: 26 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Route to update group description
1212
* Route to accept invite code
1313
* Added configuration of events by webhook of instances
14+
* Now the api key can be exposed in fetch instances if the EXPOSE_IN_FETCH_INSTANCES variable is set to true
1415

1516
### Fixed
1617

Docker/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ AUTHENTICATION_TYPE='jwt' # or 'apikey'
7070
## Define a global apikey to access all instances.
7171
### OBS: This key must be inserted in the request header to create an instance.
7272
AUTHENTICATION_API_KEY='t8OOEeISKzpmc3jjcMqBWYSaJsafdefer'
73+
AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=true
7374
## Set the secret key to encrypt and decrypt your token and its expiration time
7475
AUTHENTICATION_JWT_EXPIRIN_IN=3600 # seconds - 3600s ===1h | zero (0) - never expires
7576
AUTHENTICATION_JWT_SECRET='L0YWtjb2w554WFqPG'

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ ENV QRCODE_LIMIT=$QRCODE_LIMIT
7373
ENV AUTHENTICATION_TYPE=$AUTHENTICATION_TYPE
7474

7575
ENV AUTHENTICATION_API_KEY=$AUTHENTICATION_API_KEY
76+
ENV AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=$AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES
7677

7778
ENV AUTHENTICATION_JWT_EXPIRIN_IN=$AUTHENTICATION_JWT_EXPIRIN_IN
7879
ENV AUTHENTICATION_JWT_SECRET="L=0YWt]b2w[WF>#>:&E`"

docker-compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ services:
7575
# Define a global apikey to access all instances
7676
# OBS: This key must be inserted in the request header to create an instance.
7777
- AUTHENTICATION_API_KEY=B6D711FCDE4D4FD5936544120E713976
78+
# Expose the api key on return from fetch instances
79+
- AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=true
7880
# Set the secret key to encrypt and decrypt your token and its expiration time.
7981
- AUTHENTICATION_JWT_EXPIRIN_IN=0 # seconds - 3600s === 1h | zero (0) - never expires
8082
# Set the instance name and webhook url to create an instance in init the application

src/config/env.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export type Instance = {
8282
};
8383
export type Auth = {
8484
API_KEY: ApiKey;
85+
EXPOSE_IN_FETCH_INSTANCES: boolean;
8586
JWT: Jwt;
8687
TYPE: 'jwt' | 'apikey';
8788
INSTANCE: Instance;
@@ -234,6 +235,8 @@ export class ConfigService {
234235
API_KEY: {
235236
KEY: process.env.AUTHENTICATION_API_KEY,
236237
},
238+
EXPOSE_IN_FETCH_INSTANCES:
239+
process.env?.AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES === 'true',
237240
JWT: {
238241
EXPIRIN_IN: Number.isInteger(process.env?.AUTHENTICATION_JWT_EXPIRIN_IN)
239242
? Number.parseInt(process.env.AUTHENTICATION_JWT_EXPIRIN_IN)

src/dev-env.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ AUTHENTICATION:
118118
API_KEY:
119119
# OBS: This key must be inserted in the request header to create an instance.
120120
KEY: B6D711FC-DE4D-4FD5-9365-44120E713976
121+
# Expose the api key on return from fetch instances
122+
EXPOSE_IN_FETCH_INSTANCES: true
121123
# Set the secret key to encrypt and decrypt your token and its expiration time.
122124
JWT:
123125
EXPIRIN_IN: 0 # seconds - 3600s === 1h | zero (0) - never expires

src/whatsapp/services/monitor.service.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { INSTANCE_DIR } from '../../config/path.config';
44
import EventEmitter2 from 'eventemitter2';
55
import { join } from 'path';
66
import { Logger } from '../../config/logger.config';
7-
import { ConfigService, Database, DelInstance, Redis } from '../../config/env.config';
7+
import {
8+
Auth,
9+
ConfigService,
10+
Database,
11+
DelInstance,
12+
Redis,
13+
} from '../../config/env.config';
814
import { RepositoryBroker } from '../repository/repository.manager';
915
import { NotFoundException } from '../../exceptions';
1016
import { Db } from 'mongodb';
@@ -62,13 +68,22 @@ export class WAMonitoringService {
6268
for await (const [key, value] of Object.entries(this.waInstances)) {
6369
if (value) {
6470
if (value.connectionStatus.state === 'open') {
71+
let apikey: string;
72+
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
73+
const tokenStore = await this.repository.auth.find(key);
74+
apikey = tokenStore.apikey || '';
75+
} else {
76+
apikey = '';
77+
}
78+
6579
instances.push({
6680
instance: {
6781
instanceName: key,
6882
owner: value.wuid,
6983
profileName: (await value.getProfileName()) || 'not loaded',
7084
profilePictureUrl: value.profilePictureUrl,
7185
status: (await value.getProfileStatus()) || '',
86+
apikey,
7287
},
7388
});
7489
} else {

0 commit comments

Comments
 (0)