-
-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Add pairing auth method for NOWEB, add QR endpoint
- Loading branch information
allburov
committed
Aug 13, 2023
1 parent
6da479c
commit 5a62890
Showing
16 changed files
with
253 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.yarn | ||
example.ts | ||
.*sessions | ||
tokens | ||
files | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.