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

Alternate Webhook Verification Implementation #117

Open
kingmesal opened this issue Apr 30, 2024 · 1 comment
Open

Alternate Webhook Verification Implementation #117

kingmesal opened this issue Apr 30, 2024 · 1 comment

Comments

@kingmesal
Copy link

I am running cloudconvert from within Cloudflare. I'm basically just pulling out all the types from it as the Axios client does work in the environment properly. Looking forward to the fetch implementation. All that aside, I wanted to share a code snippet for consideration of inclusion into the client.

The crypto library is not implemented in cloudflare and so I whipped together this implementation.

"@types/jsrsasign": "^10.5.13",

import { KJUR } from "jsrsasign";

export function verifyWebhookSignature(payloadString: string, signature: string, signingSecret: string): boolean {
  // Create the HMAC object with SHA256
  const hmacObj = new KJUR.crypto.Mac({ alg: "HmacSHA256", pass: { utf8: signingSecret } });

  // Update the HMAC with the payload
  hmacObj.updateString(payloadString);

  // Get the HMAC in hexadecimal format
  const signed = hmacObj.doFinal();

  // Compare the computed HMAC to the provided signature
  return signature === signed;
}
@josiasmontag
Copy link
Contributor

Thanks for sharing!

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

No branches or pull requests

2 participants