-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgofoodmiddleware.go
41 lines (36 loc) · 960 Bytes
/
gofoodmiddleware.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
package main
import (
"io"
"net/http"
"github.com/gin-gonic/gin"
)
func GofoodSignatureMiddleware(secretKey string) gin.HandlerFunc {
return func(c *gin.Context) {
bodyBytes, err := io.ReadAll(c.Request.Body)
if err != nil {
log := map[string]interface{}{
"error": err.Error(),
"level": "error",
"message": "Error reading body",
}
logJson(log)
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H(log))
return
}
defer c.Request.Body.Close()
err = verifyGofoodSignature(bodyBytes, c.Request.Header.Get("X-Go-Signature"), secretKey)
if err != nil {
logJson(map[string]interface{}{
"error": err.Error(),
"level": "warning",
"message": "Error validating signature",
})
// Cannot verify signature in sandbox
// w.WriteHeader(http.StatusUnauthorized)
// w.Header().Set("Content-Type", "application/json")
// fmt.Fprintf(w, `{"message": "error"}`)
// return
}
c.Next()
}
}