From 90ac8fd00e2152d3b1fbefb7916614290de2dc58 Mon Sep 17 00:00:00 2001 From: Dom Webber Date: Sat, 16 Dec 2023 08:11:14 +0000 Subject: [PATCH] feat: introduce CloudAPI.createVerifyToken() --- src/CloudAPI/index.ts | 58 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/CloudAPI/index.ts b/src/CloudAPI/index.ts index aa383ca..7c9b96c 100644 --- a/src/CloudAPI/index.ts +++ b/src/CloudAPI/index.ts @@ -9,6 +9,33 @@ import AbstractAPI, { AbstractAPIParams } from "../API/AbstractAPI"; import CloudAPIMessage from "./CloudAPIMessage"; import CloudAPIWebhook from "./CloudAPIWebhook"; +import { randomBytes } from "crypto"; + +export interface WhatsAppAPICreateVerifyTokenParams { + /** + * The length of the verify token. + * + * @since 5.1.0 + * @default 16 + */ + length?: number; + + /** + * The encoding of the verify token. + * + * @since 5.1.0 + * @default hex + */ + encoding?: BufferEncoding; + + /** + * Random Bytes Generation. + * + * @since 5.1.0 + * @default crypto.randomBytes + */ + random?: (length: number) => Buffer; +} export interface WhatsAppAPIParams extends AbstractAPIParams {} @@ -22,6 +49,20 @@ export interface WhatsAppAPIParams extends AbstractAPIParams {} * const sdk = new CloudWhatsAppAPI("123456") */ export default class CloudAPI extends AbstractAPI { + /** + * Default Verify Token Length. + * + * @since 5.6.0 + */ + public static DEFAULT_VERIFY_TOKEN_LENGTH = 16; + + /** + * Default Verify Token Encoding. + * + * @since 5.6.0 + */ + public static DEFAULT_VERIFY_TOKEN_ENCODING: BufferEncoding = "hex"; + /** * Message API. * @@ -36,6 +77,7 @@ export default class CloudAPI extends AbstractAPI { /** * Webhook API. + * Receive and handle messages from WhatsApp via WebHook. * * @since 4.0.0 */ @@ -46,4 +88,20 @@ export default class CloudAPI extends AbstractAPI { this.message = new CloudAPIMessage(params); this.webhook = new CloudAPIWebhook(params); } + + /** + * Create a new Verify Token. + * This is a random string that is used to verify that the request is coming + * from WhatsApp. This method **only** creates the value, it's usage is up to + * the implementer. + * + * @since 5.6.0 + */ + public static createVerifyToken({ + length = this.DEFAULT_VERIFY_TOKEN_LENGTH, + encoding = this.DEFAULT_VERIFY_TOKEN_ENCODING, + random = randomBytes, + }: WhatsAppAPICreateVerifyTokenParams): string { + return random(length).toString(encoding); + } }