Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEVPL-2717] add hmac verification method to JS SDK #214

Merged
merged 10 commits into from
Feb 21, 2025

Conversation

KeithRyanWong
Copy link
Contributor

@KeithRyanWong KeithRyanWong commented Feb 6, 2025

Description

Developer documentation will be updated to push users to use the SDK rather than build their own solution to verify signatures. The reason is because we may change signature generation in the future, and using the SDK will allow users to simply update the WebflowAPI package. This means that the function signature should remain the same between versions. However, because Request objects vary between server implementations, I don't think it's feasible to allow users to pass in the request-like object to the function and expect things to work. Since HMAC generation will always include a secret key, header information, and the request body– I think we can safely require users to pass in these parameters in the expected format without breaking them in the future.

  1. If we need access to the request body fields, we can always transform it into a JSON object.
  2. While headers may be subject to change, passing in the whole headers collection as a record allows us the flexibility to access whatever we need.

Here's a link to the related PR for updating the docs:
https://github.com/webflow/openapi-internal/pull/342

Usage

To use the new function, users will break the http request down into its relevant parts and pass them into the function, which will return a promise that resolves to a Boolean representing whether the signature is valid.

Example

 function incomingWebhookRouteHandler(req, res) {
     const headers = req.headers;
     const body = JSON.stringify(req.body);
     const secret = getWebhookSecret(WEBHOOK_ID);
     const isAuthenticated = await client.webhooks.verifySignature({ headers, body, secret });
     
     if (isAuthenticated) {
         // Process the webhook
     } else {
         // Alert the user that the webhook is not authenticated
     }
     res.sendStatus(200);
}
     

Test Plan

OAuth App generated webhooks

OAuth App generated webhooks will not have a secret key and will continue to use the associated Client Secret to verify HMAC signatures as detailed in the Working with Webhooks documentation
To test:

  1. Generate a webhook through the Auth'd application
  2. Trigger the webhook, setup your handler to pass in the Client Secret Key into the secret field of the RequestSignatureDetails object

New client only Secret Key

  1. In the Webflow dashboard, navigate to a site's Apps & Integrations tab and add a new webhook
  2. Add the generated Secret Key to your local application and setup your handler to pass it into the secret field of the RequestSignatureDetails object
  3. Trigger the webhook

/dist
.idea
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Webstorm workspace setup

@KeithRyanWong KeithRyanWong marked this pull request as ready for review February 18, 2025 02:59
@KeithRyanWong KeithRyanWong requested a review from zplata February 18, 2025 03:00
@KeithRyanWong KeithRyanWong changed the title [DEVPL-2717] add hmac verification method [DEVPL-2717] add hmac verification method to JS SDK Feb 18, 2025
@KeithRyanWong KeithRyanWong merged commit 4a5ff59 into master Feb 21, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants