我目前正在使用 Forge Webhooks API 来处理项目中可能发生的不同事件。除了有效负载签名检查之外,一切正常。
我想检查有效负载的原因是因为回调将在我的 API 上结束,并且我想拒绝所有不是来自 Forge 的 webhook 服务的请求。
我遵循的步骤:
computedSignature
与从 Forge 收到的签名不同。我的 C# 代码如下所示:
private const string SHA_HASH = "sha1hash";
var secretKeyBytes = Encoding.UTF8.GetBytes(ForgeAuthConfiguration.AntiForgeryToken);
using var hmac = new HMACSHA1(secretKeyBytes);
var computedHash = hmac.ComputeHash(request.Body.ReadAsBytes());
var computedSignature = $"{SHA_HASH}={computedHash.Aggregate("", (s, e) => s + $"{e:x2}", s => s)}";
举个例子,Forge 的请求具有以下签名标头:
sha1hash=303c4e7d2a94ccfa559560dc2421cee8496d2d83
我的 C# 代码计算此签名:
sha1hash=3bb8d41c3c1cb6c9652745f5996b4e7f832ca8d5
在步骤 1 中将相同的 AntiForgeryToken 发送到 Forge
好吧,我以为我的 C# 代码已损坏,然后我尝试了 这个在线 HMAC 生成器,对于给定的输入,结果是:
3bb8d41c3c1cb6c9652745f5996b4e7f832ca8d5
(与 C# 相同)
好吧,也许在线生成器坏了,我在node js中尝试了他们自己的代码,结果是这样的:
我有 3 种使用相同密钥加密相同主体的方法,并且每次都得到相同的结果。但这些结果与 Forge 提供的签名不同,导致检查失败并拒绝有效请求......
有人知道这个签名是怎么回事吗?
为什么我按照他们的教程得到的结果不一样?
您如何验证您的请求?
下面的代码在我这边工作。如果有帮助的话可以尝试一下吗?
[HttpPost]
[Route("api/forge/callback/webhookbysig")]
public async Task<IActionResult> WebhookCallbackBySig()
{
try
{
var encoding = Encoding.UTF8;
byte[] rawBody = null;
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
rawBody = encoding.GetBytes(reader.ReadToEnd());
}
var requestSignature = Request.Headers["x-adsk-signature"];
string myPrivateToken = Credentials.GetAppSetting("FORGE_WEBHOOK_PRIVATE_TOKEN");
var tokenBytes = encoding.GetBytes(myPrivateToken);
var hmacSha1 = new HMACSHA1(tokenBytes);
byte[] hashmessage = hmacSha1.ComputeHash(rawBody);
var calculatedSignature = "sha1hash=" + BitConverter.ToString(hashmessage).ToLower().Replace("-", "");
if (requestSignature.Equals(calculatedSignature))
{
System.Diagnostics.Debug.Write("Same!");
}
else
{
System.Diagnostics.Debug.Write("diff!");
}
}
catch(Exception ex)
{
}
// ALWAYS return ok (200)
return Ok();
}
如果这没有帮助,请分享您的 webhook ID(最好通过 [email protected] 发送电子邮件)。我们会请工程师团队检查。
检查签名的方法是否正确,我相信问题在于设置秘密令牌,您是否通过端点正确设置了应用程序的秘密令牌https://aps.autodesk.com/en/docs/webhooks/v1 /reference/http/tokens/tokens-POST/ ?确保使用秘密令牌来验证所有即将到来的回调。