2017-03-14 4 views
2

github에서 들어오는 Webhook을 처리 중이며 x-hub-signature를 확인하려고합니다. 나는 hmac을 사용하여 "비밀"을 해시 한 다음 두 해시를 비교합니다. 문제는 그들이 절대로 일치하지 않는다는 것입니다. 이것은 내 설정입니다.HMAC 서명이 github의 x-hub-signature와 일치하지 않습니다.

router.route("/auth") 

.post((req, res) => { 

    var hmac = crypto.createHmac("sha1", process.env.WEBHOOK_SECRET); 
    var calculatedSignature = "sha1=" + hmac.update(JSON.stringify(req.body)).digest("hex"); 
    console.log(req.headers["x-hub-signature"] === calculatedSignature); // Returns false 
    console.log(req.headers["x-hub-signature"]) // => sha1=blablabla 
    console.log(calculatedSignature) // => sha1=foofoofoo 

    res.end(); 
}); 

모든 것을 시도했지만 제대로 작동하지 않습니다. hmac.update()JSON.stringify(req.body) 이외의 매개 변수가 있어야하는지 궁금합니다. 아무도 왜 그들이 일치하지 않을지 알고 있습니까?

답변

0

그래서 문제는 webhook의 설정에있었습니다. content-format은 application/x-www-form-urlencoded로 설정되었습니다. 이는 어떤 이유로 x-hub-signature를 다르게 해시합니다. 방금 application/json으로 변경 한 다음 작동했습니다!

0

웹 훅 Content-Type이 application/x-www-url-encoded으로 설정된 경우 HMAC를 확인하는 데 사용해야하는 문자열은 "payload=" + query_encoded_payload입니다. 예를 들면 golang

payloadForm := r.PostFormValue("payload") 
escaped := url.QueryEscape(payloadForm) # ex. http://www.url-encode-decode.com/ 
checkMe := "payload=" + escaped