Skip to content

Commit

Permalink
[core] Add pairing auth method for NOWEB, add QR endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
allburov committed Aug 13, 2023
1 parent 6da479c commit 5a62890
Show file tree
Hide file tree
Showing 16 changed files with 253 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.yarn
example.ts
.*sessions
tokens
files
Expand Down
34 changes: 33 additions & 1 deletion docs/site/content/en/docs/how-to/sessions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ images: [ ]
weight: 125
---

## Endpoints
## Sessions

### Start

Expand Down Expand Up @@ -129,6 +129,38 @@ In order to log out the session - call `POST /api/sessions/logout`
```


## Authentication
### Get QR
The simplest way to authenticate a new session - get QR code and scan it on your device.
```bash
GET /api/{session}/auth/qr
```
You'll get QR image that you can scan and get authenticated

### Get pairing code
{{< alert icon="👉" text="Available in **NOWEB** engine only." />}}

You can [link a session with phone number](https://faq.whatsapp.com/1324084875126592) - make a request to the endpoint.
```bash
POST /api/{session}/auth/request-code
```

Body example:
```json
{
"phoneNumber": "12132132130"
}
```

You'll get code in the response that you can use on your WhatsApp app to connect the session:
```json
{
"code": "ABCD-ABCD"
}
```




## Advanced sessions ![](/images/versions/plus.png)

Expand Down
5 changes: 5 additions & 0 deletions docs/site/content/en/docs/overview/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ menu:
weight: 199
toc: true
---
## 2023.9
September 2023
- Add dedicated [Get QR](https://waha.devlike.pro/docs/how-to/sessions/#get-qr) endpoint!
- Support [pairing method (NOWEB)](http://localhost:1313/docs/how-to/sessions/#get-pairing-code) - you can connect with a code instead of QR.

## 2023.8
August 2023
- Added [stories (aka status) endpoints](https://waha.devlike.pro/docs/how-to/send-messages/#send-status-aka-stories) to **NOWEB** engine!
Expand Down
2 changes: 1 addition & 1 deletion docs/site/static/swagger/openapi.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"express-basic-auth": "^1.2.1",
"file-type": "16.5.4",
"https-proxy-agent": "^7.0.0",
"libphonenumber-js": "^1.10.36",
"link-preview-js": "^3.0.4",
"lodash": "^4.17.21",
"mime-types": "^2.1.27",
Expand Down
67 changes: 67 additions & 0 deletions src/api/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Body, Controller, Get, Post, Res } from '@nestjs/common';
import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';

import { SessionManager } from '../core/abc/manager.abc';
import { WhatsappSession } from '../core/abc/session.abc';
import { OTPRequest, RequestCodeRequest } from '../structures/auth.dto';
import { SessionApiParam, SessionParam } from './helpers';
import { WAHASessionStatus } from '../structures/enums.dto';
import { UnprocessableEntityException } from '@nestjs/common/exceptions/unprocessable-entity.exception';
import { Readable } from 'stream';
import { Response } from 'express';

@ApiSecurity('api_key')
@Controller('api/:session/auth')
@ApiTags('auth')
class AuthController {
constructor(private manager: SessionManager) {}

@Get('qr')
@SessionApiParam
@ApiOperation({
summary: 'Get QR code for pairing WhatsApp Web.',
})
async getQR(@Res() res: Response, @SessionParam session: WhatsappSession) {
if (session.status != WAHASessionStatus.SCAN_QR_CODE) {
const err = `Can get QR code only in SCAN_QR_CODE status. The current status is '${session.status}'`;
throw new UnprocessableEntityException(err);
}

const buffer = await session.getQR();
const stream = new Readable();
stream.push(buffer);
stream.push(null);

res.set({
'Content-Type': 'image/png',
'Content-Length': buffer.length,
});
stream.pipe(res);
}

@Post('request-code')
@SessionApiParam
@ApiOperation({
summary: 'Request authentication code. NOWEB and NOWEB_MOBILE engines only',
})
requestCode(
@SessionParam session: WhatsappSession,
@Body() request: RequestCodeRequest,
) {
return session.requestCode(request.phoneNumber, request.method);
}

@Post('authorize-code')
@SessionApiParam
@ApiOperation({
summary: 'Send OTP authentication code. NOWEB_MOBILE engine only',
})
authorizeCode(
@SessionParam session: WhatsappSession,
@Body() request: OTPRequest,
) {
return session.authorizeCode(request.code);
}
}

export { AuthController };
21 changes: 21 additions & 0 deletions src/core/abc/session.abc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { WAHAChatPresences } from '../../structures/presence.dto';
import { ProxyConfig, SessionConfig } from '../../structures/sessions.dto';
import { NotImplementedByEngineError } from '../exceptions';
import { LocalSessionStorage, MediaStorage } from './storage.abc';
import { OTPRequest, RequestCodeRequest } from '../../structures/auth.dto';
import {
ImageStatus,
TextStatus,
Expand Down Expand Up @@ -147,8 +148,28 @@ export abstract class WhatsappSession {
/**
* START - Methods for API
*/

/**
* Auth methods
*/

public getQR(): Promise<Buffer> {
throw new NotImplementedByEngineError();
}

public requestCode(phoneNumber: string, method: string) {
throw new NotImplementedByEngineError();
}

public authorizeCode(code: string) {
throw new NotImplementedByEngineError();
}

abstract getScreenshot(): Promise<Buffer | string>;

/**
* Other methods
*/
abstract checkNumberStatus(request: CheckNumberStatusQuery);

abstract sendText(request: MessageTextRequest);
Expand Down
2 changes: 2 additions & 0 deletions src/core/app.module.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { VersionController } from '../api/version.controller';
import { WhatsappConfigService } from '../config.service';
import { SessionManager } from './abc/manager.abc';
import { SessionManagerCore } from './manager.core';
import { AuthController } from '../api/auth.controller';
import { ChatsController } from '../api/chats.controller';
import { StatusController } from '../api/status.controller';

Expand All @@ -36,6 +37,7 @@ export const IMPORTS = [
PassportModule,
];
export const CONTROLLERS = [
AuthController,
SessionsController,
ChattingController,
ChatsController,
Expand Down
54 changes: 50 additions & 4 deletions src/core/session.noweb.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import makeWASocket, {
PresenceData,
useMultiFileAuthState,
} from '@adiwajshing/baileys';
import { WABrowserDescription } from '@adiwajshing/baileys';
import { UnprocessableEntityException } from '@nestjs/common';
import * as fs from 'fs/promises';
import { Agent } from 'https';
import * as lodash from 'lodash';
import { PairingCodeResponse } from 'src/structures/auth.dto';
import { Message } from 'whatsapp-web.js';

import { flipObject } from '../helpers';
import { flipObject, splitAt } from '../helpers';
import {
Button,
ChatRequest,
CheckNumberStatusQuery,
GetMessageQuery,
MessageContactVcardRequest,
MessageFileRequest,
MessageImageRequest,
Expand Down Expand Up @@ -131,9 +130,12 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
}

connectStore() {
this.log.debug(`Connecting store...`);
if (!this.store) {
this.log.debug(`Making a new auth store...`);
this.store = makeInMemoryStore({});
}
this.log.debug(`Binding store to socket...`);
this.store.bind(this.sock.ev);
}

Expand All @@ -146,6 +148,12 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
async buildClient() {
this.sock = await this.makeSocket();
this.connectStore();
this.listenConnectionEvents();
this.events.emit(WAHAInternalEvent.engine_start);
}

protected listenConnectionEvents() {
this.log.debug(`Start listening ${BaileysEvents.CONNECTION_UPDATE}...`);
this.sock.ev.on(BaileysEvents.CONNECTION_UPDATE, async (update) => {
const { connection, lastDisconnect, qr } = update;
if (connection === 'connecting') {
Expand Down Expand Up @@ -179,7 +187,6 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
});
}
});
this.events.emit(WAHAInternalEvent.engine_start);
}

stop() {
Expand All @@ -192,6 +199,42 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
/**
* START - Methods for API
*/

/**
* Auth methods
*/
public async getQR(): Promise<Buffer> {
return Promise.resolve(this.qr.get());
}

public async requestCode(
phoneNumber: string,
method: string,
): Promise<PairingCodeResponse> {
if (method) {
const err = `NOWEB engine doesn't support any 'method', remove it and try again`;
throw new UnprocessableEntityException(err);
}

if (this.status == WAHASessionStatus.STARTING) {
this.log.debug('Waiting for connection update...');
await this.sock.waitForConnectionUpdate((update) => !!update.qr);
}

if (this.status != WAHASessionStatus.SCAN_QR_CODE) {
const err = `Can request code only in SCAN_QR_CODE status. The current status is ${this.status}`;
throw new UnprocessableEntityException(err);
}

this.log.debug('Requesting pairing code...');
const code: string = await this.sock.requestPairingCode(phoneNumber);
// show it as ABCD-ABCD
const parts = splitAt(code, 4);
const codeRepr = parts.join('-');
this.log.debug(`Your code: ${codeRepr}`);
return { code: codeRepr };
}

async getScreenshot(): Promise<Buffer | string> {
if (this.status === WAHASessionStatus.STARTING) {
throw new UnprocessableEntityException(
Expand All @@ -208,6 +251,9 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
}
}

/**
* Other methods
*/
async checkNumberStatus(
request: CheckNumberStatusQuery,
): Promise<WANumberExistResult> {
Expand Down
42 changes: 34 additions & 8 deletions src/core/session.webjs.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ import {
AvailableInPlusVersion,
NotImplementedByEngineError,
} from './exceptions';
import { QR } from './QR';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const QRCode = require('qrcode');

// eslint-disable-next-line @typescript-eslint/no-var-requires
const qrcode = require('qrcode-terminal');
Expand All @@ -54,6 +58,12 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
engine = WAHAEngine.WEBJS;

whatsapp: Client;
protected qr: QR;

public constructor(config) {
super(config);
this.qr = new QR();
}

protected buildClient() {
const clientOptions: ClientOptions = {
Expand Down Expand Up @@ -86,34 +96,50 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {

async start() {
this.whatsapp = this.buildClient();

this.whatsapp.initialize().catch((error) => {
this.status = WAHASessionStatus.FAILED;
this.log.error(error);
return;
});
this.listenConnectionEvents();
this.events.emit(WAHAInternalEvent.engine_start);
return this;
}

stop() {
return this.whatsapp.destroy();
}

// Connect events
protected listenConnectionEvents() {
this.whatsapp.on(Events.QR_RECEIVED, (qr) => {
this.log.debug('QR received');
// Convert to image and save
QRCode.toDataURL(qr).then((url) => {
this.qr.save(url);
});
// Print in terminal
qrcode.generate(qr, { small: true });
this.status = WAHASessionStatus.SCAN_QR_CODE;
});

this.whatsapp.on(Events.AUTHENTICATED, () => {
this.status = WAHASessionStatus.WORKING;
this.qr.save('');
this.log.log(`Session '${this.name}' has been authenticated!`);
});
this.events.emit(WAHAInternalEvent.engine_start);
return this;
}

stop() {
return this.whatsapp.destroy();
}

/**
* START - Methods for API
*/

/**
* Auth methods
*/
public async getQR(): Promise<Buffer> {
return Promise.resolve(this.qr.get());
}

async getScreenshot(): Promise<Buffer | string> {
if (this.status === WAHASessionStatus.FAILED) {
throw new UnprocessableEntityException(
Expand Down
1 change: 1 addition & 0 deletions src/core/swagger.module.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class SwaggerModuleCore {
.setExternalDoc('Documentation', 'https://waha.devlike.pro/')
.setVersion(VERSION.version)
.addTag('sessions', 'Control WhatsApp sessions')
.addTag('auth', 'Authentication')
.addTag('screenshot', 'Get screenshot of WhatsApp and show QR code')
.addTag('chatting', 'Chatting methods')
.addTag(
Expand Down
Loading

0 comments on commit 5a62890

Please sign in to comment.