节点js中的Sendgrid webhook签名验证失败

编程入门 行业动态 更新时间:2024-10-04 13:25:15

<a href=https://www.elefans.com/category/jswz/34/1771452.html style=节点js中的Sendgrid webhook签名验证失败"/>

节点js中的Sendgrid webhook签名验证失败

我正在尝试在 node.js 中验证 Sendgrid 的 webhooks 的签名,但我遇到了一致的验证失败。我尝试过使用“测试集成”功能、“测试电子邮件”进行单次发送以及实际发送,但所有这些选项最终都以验证失败告终。

我正在使用 node.js 的官方帮助程序,可在此处获取:.js 请注意,这是在 Cloud Functions 上实现的。

import * as functions from "firebase-functions";
import { EventWebhook } from "@sendgrid/eventwebhook";

export const handleSendGridWebhookEvents = functions
  .region(...fb.REGIONS)
  .https.onRequest(async (req, resp) => {

    const signature =
      (req.get("x-twilio-email-event-webhook-signature") as string) ??
      "";
    const timestamp =
      (req.get("x-twilio-email-event-webhook-timestamp") as string) ??
      "";

    if (!signature || !timestamp) {
      resp.json({
        error: "Webhook handler failed",
      });
      throw new Error("Sendgrid headers missing");
    }

    const verify = new EventWebhook();
    const payload = req.body;
    const publicKey = process.env.SENDGRID_WEBHOOK_PK

    try {
      const ecdsaPublicKey = verify.convertPublicKeyToECDSA(publicKey);
      const isVerified = verify.verifySignature(
        ecdsaPublicKey,
        payload,
        signature,
        timestamp
      );
      console.log({ isVerified }); // Always returns false
      resp.status(200);
    } catch (e) {
      throw e;
    }
    resp.json({ received: true });
  });

我也尝试过直接使用公钥而不进行初始转换为 ECDSA,但也没有运气。

是否需要任何额外的步骤来验证签名?或者上面的代码有什么问题......?

谢谢

回答如下:

更多推荐

节点js中的Sendgrid webhook签名验证失败

本文发布于:2024-05-30 22:22:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770986.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   js   webhook   Sendgrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!