简体   繁体   English

Webex Teams Webhook 与 API 网关和 Lambda

[英]Webex Teams Webhook with API Gateway and Lambda

I am using a fairly standard pattern of a Webhook with the called endpoint provided by AWS API Gateway and a backend Lambda.我正在使用一个相当标准的 Webhook 模式,调用的端点由 AWS API 网关和后端 Lambda 提供。

Webex Teams webhooks allow you to provide a secret which is used to sign the outgoing payload with the resulting hash sent in the 'X-Spark-Signature' header. Webex Teams webhook 允许您提供用于签署传出有效负载的密钥,其中生成的 hash 在“X-Spark-Signature”header 中发送。

I create a webhook and receive the event payload in my Lambda but the hashes do not match.我创建了一个 webhook 并在我的 Lambda 中接收事件有效负载,但哈希值不匹配。 Below is my example code:下面是我的示例代码:

def validate(key, raw):
    hashed = hmac.new(key, raw, hashlib.sha1)
    print(hashed.hexdigest())
    return hashed.hexdigest()

key = bytes('somecazYs3Cret', 'UTF-8')
raw = bytes(event['body'], 'UTF-8')
signature = event['headers']['X-Spark-Signature']

if validate(key, raw) == signature:
    print('AUTHORIZED')
else:
    print('REJECTED')

In API Gateway I am using a Mapping Template as described here to pass the request headers through to my Lambda: https://aws.amazon.com/premiumsupport/knowledge-center/custom-headers-api-gateway-lambda/在 API 网关中,我使用此处所述的映射模板将请求标头传递到我的 Lambda: https://aws.amazon.com/premiumcustomsupport-gatewaynowledge-lambda.com/premiumcustomsupport-gate/

When the request payload arrives, all fields including the body are already loaded as a python type dict.当请求有效负载到达时,包括正文在内的所有字段都已作为 python 类型的字典加载。 so I am trying to serialise the body back to a string to check the hash.所以我试图将正文序列化回字符串以检查 hash。

Any help?有什么帮助吗?

This turned out to be the way API Gateway was passing the request payload through to Lambda.事实证明,这是 API 网关将请求有效负载传递到 Lambda 的方式。 Instead of the "Mapping Template" I had to enable the "Use Lambda Proxy integration" feature which passes the original body JSON through as a string.而不是“映射模板”,我必须启用“使用 Lambda 代理集成”功能,它将原始主体 JSON 作为字符串传递。

After enabling this and removing the json.dumps() parts of my code, the hashes validate ok.启用此功能并删除我的代码的json.dumps()部分后,哈希验证正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM