简体   繁体   English

HMAC - 通过 Postman 进行 SHA256 身份验证

[英]HMAC - SHA256 authentication via Postman

I'm trying to simulate webhook POST request to my Rails app (which works well in a real workflow) by Postman. I found lots of examples but none of them work - I keep getting a 401 code.我正在尝试通过 Postman 模拟对我的 Rails 应用程序的 webhook POST 请求(在实际工作流程中运行良好)。我找到了很多示例,但没有一个有效 - 我一直收到 401 代码。 What I did is defined headers and Pre-request Script like below:我所做的是定义标头和Pre-request Script ,如下所示:

邮递员标题

JS as Pre-request Script based on this docs JS 作为基于此文档Pre-request Script

postman.setEnvironmentVariable("hmac", CryptoJS.HmacSHA256(request.data, 'my_secret_string').toString(CryptoJS.digest));

And still I'm getting the 401 error.而且我仍然收到 401 错误。

The external API docs which I use to trigger webhook clearly state:我用来触发 webhook 的外部 API 文档 state:

Each webhook will be sent with the X-AQID-Signature header, which is created by hashing the request's payload with the HMAC method and SHA256 algorithm, using the shared secret as salt.每个 webhook 将与 X-AQID-Signature header 一起发送,它是通过使用 HMAC 方法和 SHA256 算法对请求的有效负载进行哈希处理,使用共享密钥作为盐来创建的。 This means that upon receiving a payload, you can verify its integrity by replicating the hashing method.这意味着在收到有效载荷后,您可以通过复制哈希方法来验证其完整性。

And like I said it works well in a real life workflow so I have an error in the postman implementation.就像我说的那样,它在现实生活中的工作流程中运行良好,所以我在 postman 实现中遇到了错误。 What did I missed?我错过了什么?

You don't need to set any environment variable, you just have to add a header from your script.您不需要设置任何环境变量,只需从脚本中添加一个 header 即可。 I did this in a very similar case:我在一个非常相似的案例中这样做了:

var signBytes = CryptoJS.HmacSHA256(pm.request.body.raw, 'YOUR_SECRET');
var signHex = CryptoJS.enc.Hex.stringify(signBytes);
pm.request.headers.add({
    key: "HEADER_NAME",
    value: signHex
});

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

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