@@ -8,7 +8,6 @@ import * as Webflow from "../../../index";
8
8
import urlJoin from "url-join" ;
9
9
import * as serializers from "../../../../serialization/index" ;
10
10
import * as errors from "../../../../errors/index" ;
11
- import crypto from "crypto" ;
12
11
13
12
export declare namespace Webhooks {
14
13
interface Options {
@@ -26,75 +25,13 @@ export declare namespace Webhooks {
26
25
/** Additional headers to include in the request. */
27
26
headers ?: Record < string , string > ;
28
27
}
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
- }
38
28
}
39
29
40
30
/**
41
31
* Webhooks are the webhooks in your Webflow site.
42
32
*/
43
33
export class Webhooks {
44
34
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
-
98
35
/**
99
36
* List all App-created Webhooks registered for a given site
100
37
*
0 commit comments