微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Twilio validateRequest on Cloud Functions在生产中失败

如何解决Twilio validateRequest on Cloud Functions在生产中失败

我有一个使用firebase-functions部署的NodeJS函数。我正在尝试使用签名标头验证从Twilio API发送的请求(请参见此处:https://www.twilio.com/docs/usage/webhooks/webhooks-security

当我为Twilio设置要连接的代理时,它在本地工作,但是一旦将其部署在产品上,它就不再工作;它会提前返回并按预期方式向我发送403。

exports.reply = functions.https.onRequest((req,res) => {
  let isValid = true
  const twilioSignature = req.headers['x-twilio-signature']
  const url = `https://${region}-${projectId}.cloudfunctions.net/reply`

  // Only validate that requests came from Twilio when the function has been
  // deployed to production.
  if (process.env.NODE_ENV === 'production') {
    isValid = twilio.validateRequest(
      config.TWILIO_AUTH_TOKEN,twilioSignature,url,req.params
    )

    console.log(`Token: ${config.TWILIO_AUTH_TOKEN}`)
    console.log(`Region: ${region}`)
    console.log(`projectId: ${projectId}`)
    console.log(`Signature: ${twilioSignature}`)
    console.log(`url: ${url}`)
    console.log(`Params: ${req.params}`)
    console.log(isValid)
  }

  // Halt early if the request was not sent from Twilio
  if (!isValid) {
    res
      .type('text/plain')
      .status(403)
      .send('Twilio Request Validation Failed.')
      .end()
    return
  }

  // Prepare a response to the SMS message
  const response = new MessagingResponse()

  // Add text to the response
  response.message('Hello from Cloud Functions!')

  // Send the response
  res.status(200).type('text/xml').end(response.toString())
})

控制台日志看起来像它返回了正确的值,但是twilio.validateRequest()函数仍然以某种方式返回false。为什么?

我得出的结论:

  1. Env变量-使用NODE_ENV检查后不能进入.status(403)fn
  2. Cloud Functions用户-启用allUsers意味着它可以公开访问

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。