-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (69 loc) · 1.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
)
func verifyPayload(jsonBody string, headers http.Header) bool {
secret := os.Getenv("HOOKDECK_WEBHOOK_SECRET")
if secret == "" {
log.Println("No HOOKDECK_WEBHOOK_SECRET found in environment variables. Skipping verification.")
return true
}
hmacHeader := headers.Get("x-hookdeck-signature")
hmacHeader2 := headers.Get("x-hookdeck-signature-2")
hash := hmac.New(sha256.New, []byte(secret))
hash.Write([]byte(jsonBody))
expectedHash := base64.StdEncoding.EncodeToString(hash.Sum(nil))
if expectedHash == hmacHeader ||
(hmacHeader2 != "" && expectedHash == hmacHeader2) {
return true
}
return false
}
func main() {
r := gin.Default()
r.POST("/*path", func(c *gin.Context) {
bodyAsByteArray, err := io.ReadAll(c.Request.Body)
jsonBody := string(bodyAsByteArray)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "could not get request body data",
})
return
}
verified := verifyPayload(jsonBody, c.Request.Header)
if !verified {
c.JSON(http.StatusUnauthorized, gin.H{
"message": "invalid payload",
})
return
}
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, []byte(jsonBody), "", " "); err != nil {
log.Printf("Failed to format JSON: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{
"message": "failed to format JSON",
})
return
}
log.Printf("webhook_received: %v", prettyJSON.String())
c.JSON(http.StatusOK, gin.H{
"status": "ACCEPTED",
})
})
port := os.Getenv("PORT")
if port == "" {
port = "3032"
}
fmt.Printf("🪝 Server running at http://localhost:%s", port)
r.Run(fmt.Sprintf(":%s", port))
}