-
Notifications
You must be signed in to change notification settings - Fork 100
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
KeithRyanWong
merged 10 commits into
master
from
devpl-2717-add-HMAC-verification-method
Feb 21, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
170128a
ignore webstorm workspace
KeithRyanWong a79b18a
add verification method
KeithRyanWong 2eece07
import crypto
KeithRyanWong 99005db
add comments
KeithRyanWong 10717e1
move code to wrapper
KeithRyanWong f624728
undo space
KeithRyanWong 1a373bf
remove space
KeithRyanWong 5a2d838
add crypto polyfill
KeithRyanWong b2c78b2
redo space
KeithRyanWong 86ed9de
specify no browser polyfill
KeithRyanWong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
.DS_Store | ||
/dist | ||
/dist | ||
.idea | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Webhooks } from "../api/resources/webhooks/client/Client"; | ||
import crypto from "crypto"; | ||
|
||
// Extends the namespace declared in the Fern generated client | ||
declare module "../api/resources/webhooks/client/Client" { | ||
export namespace Webhooks { | ||
interface RequestSignatureDetails { | ||
/** The headers of the incoming webhook request as a record-like object */ | ||
headers: Record<string, string>; | ||
/** The body of the incoming webhook request as a string */ | ||
body: string; | ||
/** The secret key generated when creating the webhook or the OAuth client secret */ | ||
secret: string; | ||
} | ||
} | ||
} | ||
|
||
export class Client extends Webhooks { | ||
constructor(protected readonly _options: Webhooks.Options) { | ||
super(_options); | ||
} | ||
|
||
/** | ||
* Verify that the signature on the webhook message is from Webflow | ||
* @link https://developers.webflow.com/data/docs/working-with-webhooks#validating-request-signatures | ||
* | ||
* @param {Webhooks.RequestSignatureDetails.headers} requestSignatureDetails - details of the incoming webhook request | ||
* @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); | ||
* } | ||
* | ||
*/ | ||
public async verifySignature({ headers, body, secret }: Webhooks.RequestSignatureDetails): Promise<boolean> { | ||
// Creates a HMAC signature following directions from https://developers.webflow.com/data/docs/working-with-webhooks#steps-to-validate-the-request-signature | ||
const createHmac = async (signingSecret: string, message: string) => { | ||
const encoder = new TextEncoder(); | ||
|
||
// Encode the signingSecret key | ||
// @ts-expect-error TS2339: Property 'subtle' does not exist on type 'typeof import("crypto")'. | ||
const key = await crypto.subtle.importKey( | ||
"raw", | ||
encoder.encode(signingSecret), | ||
{ name: "HMAC", hash: "SHA-256" }, | ||
false, | ||
["sign"] | ||
); | ||
|
||
// Encode the message and compute HMAC signature | ||
// @ts-expect-error TS2339: Property 'subtle' does not exist on type 'typeof import("crypto")'. | ||
const signature = await crypto.subtle.sign("HMAC", key, encoder.encode(message)); | ||
|
||
// Convert signature to hex string | ||
return Array.from(new Uint8Array(signature)) | ||
.map((b) => b.toString(16).padStart(2, "0")) | ||
.join(""); | ||
}; | ||
|
||
const message = `${headers["x-webflow-timestamp"]}:${body}`; | ||
|
||
const generatedSignature = await createHmac(secret, message); | ||
return headers["x-webflow-signature"] === generatedSignature; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Webstorm workspace setup