Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import crypto from "crypto";
- class SignedRequest {
- constructor(requestSignature, requestTimestamp, requestBody, requestUrl) {
- this._requestSignature = requestSignature;
- this._requestTimestamp = requestTimestamp.toString();
- this._requestBody = requestBody;
- this._requestUrl = requestUrl;
- }
- verify(signingKey) {
- const payload = this._buildPayload();
- const expectedSignature = Buffer.from(this._requestSignature, "base64");
- const calculatedSignature = crypto
- .createHmac("sha256", Buffer.from(signingKey, "latin1"))
- .update(Buffer.from(payload, "latin1"))
- .digest();
- return expectedSignature.equals(calculatedSignature);
- }
- _buildPayload() {
- const checksumBody = crypto
- .createHash("sha256")
- .update(Buffer.from(this._requestBody, "latin1"))
- .digest();
- const strChecksumBody = checksumBody.toString("latin1");
- const parts = [this._requestTimestamp, this._requestUrl, strChecksumBody];
- return parts.join("\n");
- }
- }
- const sig = "lrv/39nhNz0qRfvAc/2cP8tqPpW1ZoRg8yqvM2WFDiA=";
- const timestamp = "1719823259";
- const body = {
- service: "channels",
- event: "whatsapp.outbound",
- payload: {
- id: "351fe763-99eb-4cc3-bd4c-1ce661fdc075",
- channelId: "aeb8f6af-d330-467b-9778-835144431687",
- sender: {
- connector: {
- id: "216852e9-5eaa-4c8c-b33c-572210004b9f",
- identifierValue: "104587572718860",
- },
- },
- receiver: {
- contacts: [
- {
- id: "bfda0e99-9454-47d7-afc3-447e15642bf3",
- identifierKey: "phonenumber",
- identifierValue: "+33757909733",
- annotations: { name: "Quentin Hausser" },
- countryCode: "FR",
- },
- ],
- },
- reference: "",
- status: "accepted",
- reason: "",
- lastStatusAt: "2024-07-01T08:40:58.363Z",
- createdAt: "2024-07-01T08:40:58.363Z",
- updatedAt: "2024-07-01T08:40:58.363Z",
- },
- };
- const url = "https://webhook.site/bb7e434b-2aeb-463c-82f5-6b05af10461e";
- const test = new SignedRequest(sig, timestamp, JSON.stringify(body), url);
- console.log(test.verify("thisiscryptos"));
Add Comment
Please, Sign In to add comment