diff --git a/CHANGELOG.md b/CHANGELOG.md index b6819213..79d6be19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * Added configuration of events by webhook of instances * Now the api key can be exposed in fetch instances if the EXPOSE_IN_FETCH_INSTANCES variable is set to true * Added option to generate qrcode as soon as the instance is created +* The created instance token can now also be optionally defined manually in the creation endpoint ### Fixed diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index 3e6c7b04..554301e8 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -56,6 +56,7 @@ export const instanceNameSchema: JSONSchema7 = { }, }, qrcode: { type: 'boolean', enum: [true, false] }, + token: { type: 'string' }, }, ...isNotEmpty('instanceName'), }; diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index b2d5ed2c..985fc0df 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -23,7 +23,13 @@ export class InstanceController { private readonly logger = new Logger(InstanceController.name); - public async createInstance({ instanceName, webhook, events, qrcode }: InstanceDto) { + public async createInstance({ + instanceName, + webhook, + events, + qrcode, + token, + }: InstanceDto) { const mode = this.configService.get('AUTHENTICATION').INSTANCE.MODE; if (mode === 'container') { @@ -43,9 +49,12 @@ export class InstanceController { this.waMonitor.waInstances[instance.instanceName] = instance; this.waMonitor.delInstanceTime(instance.instanceName); - const hash = await this.authService.generateHash({ - instanceName: instance.instanceName, - }); + const hash = await this.authService.generateHash( + { + instanceName: instance.instanceName, + }, + token, + ); let getEvents: string[]; @@ -78,9 +87,12 @@ export class InstanceController { this.waMonitor.waInstances[instance.instanceName] = instance; this.waMonitor.delInstanceTime(instance.instanceName); - const hash = await this.authService.generateHash({ - instanceName: instance.instanceName, - }); + const hash = await this.authService.generateHash( + { + instanceName: instance.instanceName, + }, + token, + ); let getEvents: string[]; diff --git a/src/whatsapp/dto/instance.dto.ts b/src/whatsapp/dto/instance.dto.ts index 99af100b..2f461435 100644 --- a/src/whatsapp/dto/instance.dto.ts +++ b/src/whatsapp/dto/instance.dto.ts @@ -3,4 +3,5 @@ export class InstanceDto { webhook?: string; events?: string[]; qrcode?: boolean; + token?: string; } diff --git a/src/whatsapp/services/auth.service.ts b/src/whatsapp/services/auth.service.ts index 72f2cfe5..fc3cdcc1 100644 --- a/src/whatsapp/services/auth.service.ts +++ b/src/whatsapp/services/auth.service.ts @@ -56,14 +56,14 @@ export class AuthService { return { jwt: token }; } - private async apikey(instance: InstanceDto) { - const apikey = v4().toUpperCase(); + private async apikey(instance: InstanceDto, token?: string) { + const apikey = token ? token : v4().toUpperCase(); const auth = await this.repository.auth.create({ apikey }, instance.instanceName); if (auth['error']) { this.logger.error({ - localError: AuthService.name + '.jwt', + localError: AuthService.name + '.apikey', error: auth['error'], }); throw new BadRequestException('Authentication error', auth['error']?.toString()); @@ -72,9 +72,11 @@ export class AuthService { return { apikey }; } - public async generateHash(instance: InstanceDto) { + public async generateHash(instance: InstanceDto, token?: string) { const options = this.configService.get('AUTHENTICATION'); - return (await this[options.TYPE](instance)) as { jwt: string } | { apikey: string }; + return (await this[options.TYPE](instance, token)) as + | { jwt: string } + | { apikey: string }; } public async refreshToken({ oldToken }: OldToken) {