用羽毛获取原始请求正文

问题描述 投票:0回答:1

我尝试为羽毛实现WooCommerce Webhook功能。要验证请求,我需要像这样验证原始请求主体的签名

const isSignatureValid = (
      secret: string,
      body: any,
      signature?: string
    ): boolean => {
      const signatureComputed = crypto
        .createHmac("SHA256", secret)
        .update(new Buffer(JSON.stringify(body), "utf8"))
        .digest("base64");

      return signatureComputed === signature ? true : false;
    };

目前,我的签名从未验证过。我的猜测是,这是由于req.body不是请求的原始原始内容,而是添加了一些featherjs优点的已解析版本。

问题是:如何在标准Feathers应用程序(使用Feathers cli创建)中获取原始请求正文?

feathersjs
1个回答
0
投票

不确定这是否是最惯用的方法,但是我想到了以下内容:

在主app.ts文件的express.json()中,可以将未解析的原始主体添加到req对象。这很方便,因为对于某些Webhooks(woocommerce,stripe),您只需要原始主体即可验证签名,否则可以使用已解析的JSON。

export interface IRequestRawBody extends http.IncomingMessage {
  rawBody: Buffer;
}

app.use(
  express.json({
    verify: (req: IRequestRawBody, res, buf) => {
      const rawEndpoints: string[] = ["/wc-webhook"];

      if (req.url && rawEndpoints.includes(req.url)) {
        req.rawBody = buf;
      }
    }
  })
);
© www.soinside.com 2019 - 2024. All rights reserved.