无法在环回4中集成xero webhook?

时间:2019-12-09 18:45:14

标签: typescript webhooks xero-api loopback4

我正在Loopback 4应用程序中设置零Web钩子。我已经使用this链接使用节点js配置了它

现在在环回中,我面临验证问题。我生成的令牌和我从标头获得的令牌是不同的。

代码。

import { Request, RestBindings, get, ResponseObject, post, Response, requestBody } from '@loopback/rest';
import { inject } from '@loopback/context';
const crypto = require('crypto')
const xero_webhook_key = 'XXXXXXXXXXXXXX'
const bodyParser = require('body-parser')
var options = {
  type: 'application/json'
};

/**
 * A simple controller to bounce back http requests
 */
export class PingController {
  constructor(@inject(RestBindings.Http.REQUEST) private req: Request,
    @inject(RestBindings.Http.RESPONSE) private res: Response) { }

  @post('/webhook', {
    responses: {
      '200': {},
    },
  })
  getWebhook(@requestBody({
    description: 'Ping Response',
    content: {
      'application/json': {
        schema: {
          type: 'object',
        },
      },
    },
  }) body: any): object {
    var req = this.req
    var res = this.res

    console.log("Body: " + JSON.stringify(body))
    console.log("Xero Signature: " + req.headers['x-xero-signature'])

    // Create our HMAC hash of the body, using our webhooks key
    let hmac = crypto.createHmac("sha256", xero_webhook_key).update(JSON.stringify(body)).digest("base64");
    console.log("Resp Signature: " + hmac)

    if (req.headers['x-xero-signature'] == hmac) {
      return res.status(200).send()
    } else {
      return res.status(401).send()
    }


  }

}

我不确定是否应该使用JSON.stringfy(req.body)或req.body.toString(),但两者的哈希值不同。

此外,我不知道如何在回送4中使用body-parser。 任何帮助将不胜感激。

0 个答案:

没有答案