feat: the created instance token can now also be optionally defined manually in the creation endpoint

This commit is contained in:
Davidson Gomes 2023-06-20 09:07:26 -03:00
parent 1b7015c0df
commit 30cd8a03eb
5 changed files with 29 additions and 12 deletions

View File

@ -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

View File

@ -56,6 +56,7 @@ export const instanceNameSchema: JSONSchema7 = {
},
},
qrcode: { type: 'boolean', enum: [true, false] },
token: { type: 'string' },
},
...isNotEmpty('instanceName'),
};

View File

@ -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<Auth>('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[];

View File

@ -3,4 +3,5 @@ export class InstanceDto {
webhook?: string;
events?: string[];
qrcode?: boolean;
token?: string;
}

View File

@ -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<Auth>('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) {