@@ -8,7 +8,6 @@ import * as Webflow from "../../../index";
88import urlJoin from "url-join" ;
99import * as serializers from "../../../../serialization/index" ;
1010import * as errors from "../../../../errors/index" ;
11- import crypto from "crypto" ;
1211
1312export declare namespace Webhooks {
1413 interface Options {
@@ -26,75 +25,13 @@ export declare namespace Webhooks {
2625 /** Additional headers to include in the request. */
2726 headers ?: Record < string , string > ;
2827 }
29-
30- interface RequestSignatureDetails {
31- /** The headers of the incoming webhook request as a record-like object */
32- headers : Record < string , string > ;
33- /** The body of the incoming webhook request as a string */
34- body : string ;
35- /** The secret key generated when creating the webhook or the OAuth client secret */
36- secret : string ;
37- }
3828}
3929
4030/**
4131 * Webhooks are the webhooks in your Webflow site.
4232 */
4333export class Webhooks {
4434 constructor ( protected readonly _options : Webhooks . Options ) { }
45-
46- /**
47- * Verify that the signature on the webhook message is from Webflow
48- * @link https://developers.webflow.com/data/docs/working-with-webhooks#validating-request-signatures
49- *
50- * @param {Webhooks.RequestSignatureDetails.headers } requestSignatureDetails - details of the incoming webhook request
51- * @example
52- * function incomingWebhookRouteHandler(req, res) {
53- * const headers = req.headers;
54- * const body = JSON.stringify(req.body);
55- * const secret = getWebhookSecret(WEBHOOK_ID);
56- * const isAuthenticated = await client.webhooks.verifySignature({ headers, body, secret });
57- *
58- * if (isAuthenticated) {
59- * // Process the webhook
60- * } else {
61- * // Alert the user that the webhook is not authenticated
62- * }
63- * res.sendStatus(200);
64- * }
65- *
66- */
67- public async verifySignature ( { headers, body, secret } : Webhooks . RequestSignatureDetails ) : Promise < boolean > {
68- // Creates a HMAC signature following directions from https://developers.webflow.com/data/docs/working-with-webhooks#steps-to-validate-the-request-signature
69- const createHmac = async ( signingSecret : string , message : string ) => {
70- const encoder = new TextEncoder ( ) ;
71-
72- // Encode the signingSecret key
73- // @ts -expect-error TS2339: Property 'subtle' does not exist on type 'typeof import("crypto")'.
74- const key = await crypto . subtle . importKey (
75- "raw" ,
76- encoder . encode ( signingSecret ) ,
77- { name : "HMAC" , hash : "SHA-256" } ,
78- false ,
79- [ "sign" ]
80- ) ;
81-
82- // Encode the message and compute HMAC signature
83- // @ts -expect-error TS2339: Property 'subtle' does not exist on type 'typeof import("crypto")'.
84- const signature = await crypto . subtle . sign ( "HMAC" , key , encoder . encode ( message ) ) ;
85-
86- // Convert signature to hex string
87- return Array . from ( new Uint8Array ( signature ) )
88- . map ( ( b ) => b . toString ( 16 ) . padStart ( 2 , "0" ) )
89- . join ( "" ) ;
90- } ;
91-
92- const message = `${ headers [ "x-webflow-timestamp" ] } :${ body } ` ;
93-
94- const generatedSignature = await createHmac ( secret , message ) ;
95- return headers [ "x-webflow-signature" ] === generatedSignature ;
96- }
97-
9835 /**
9936 * List all App-created Webhooks registered for a given site
10037 *
0 commit comments