I wrote the below code by Following: https://developer.atlassian.com/cloud/trello/guides/rest-api/webhooks/ Node.js Reference in Golang Fiber But Hash is not the same as Given Hash, Anyone kindly tell me what I missed? Thanks in advance.
func TrelloWebhook(c *fiber.Ctx) error {
var (
TRELLO_SECRET = "****ea6308995"
webhookURL = "https://*****ssum.ngrok-free.app/trello-webhook"
)
base64Digest := func(secret string) string {
key := []byte(TRELLO_SECRET)
mac := hmac.New(sha1.New, key)
mac.Write([]byte(secret))
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}
hashedHeader := c.Get("x-trello-webhook")
requestBody := string(c.BodyRaw())
doubleHashedHeader := base64Digest(hashedHeader)
doubleHashedBody := base64Digest(base64Digest(requestBody + webhookURL))
fmt.Println("doubleHashedHeader : ", doubleHashedHeader)
fmt.Println("doubleHashedBody : ", doubleHashedBody)
if hmac.Equal([]byte(doubleHashedHeader), []byte(doubleHashedBody)) {
fmt.Println("SUCCESS")
}
// Your existing return statement
return c.Status(200).JSON(&fiber.Map{
"status": true,
"information": "customers-data_request!",
})
}
I also tryed below ways too it's also not working
func TrelloWebhook(c *fiber.Ctx) error {
secret := "****ea6308995"
callbackURL := "https://*****ssum.ngrok-free.app/trello-webhook"
base64Digest := func(s string) string {
h := hmac.New(sha1.New, []byte(secret))
h.Write([]byte(s))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
var request map[string]interface{}
if err := json.Unmarshal(c.Body(), &request); err != nil {
fmt.Println("Error decoding JSON:", err)
return c.SendStatus(fiber.StatusBadRequest)
}
contentBuffer := bytes.NewBuffer(c.Body())
contentBuffer.WriteString(callbackURL)
content := contentBuffer.String()
doubleHash := base64Digest(content)
headerHash := c.Get("x-trello-webhook")
if doubleHash == headerHash {
fmt.Println(" Working ? ")
}
fmt.Println("doubleHash : ", doubleHash)
fmt.Println("headerHash : ", headerHash)
// Your existing return statement
return c.Status(200).JSON(&fiber.Map{
"status": true,
"information": "customers-data_request!",
})
}
This is another way i changed but not working
func TrelloWebhook(c *fiber.Ctx) error {
secret := "****ea6308995"
callbackURL := "https://*****ssum.ngrok-free.app/trello-webhook"
base64Digest := func(s string) string {
h := hmac.New(sha1.New, []byte(secret))
h.Write([]byte(s))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
var request map[string]interface{}
if err := json.Unmarshal(c.Body(), &request); err != nil {
fmt.Println("Error decoding JSON:", err)
return c.SendStatus(fiber.StatusBadRequest)
}
requestBody, err := json.Marshal(request)
if err != nil {
fmt.Println("Error encoding JSON:", err)
return c.SendStatus(fiber.StatusInternalServerError)
}
content := string(requestBody) + callbackURL
doubleHash := base64Digest(content)
headerHash := c.Get("x-trello-webhook")
fmt.Println("doubleHash : ", doubleHash)
fmt.Println("headerHash : ", headerHash)
// Your existing return statement
return c.Status(200).JSON(&fiber.Map{
"status": true,
"information": "customers-data_request!",
})
}