/
pipeline.go
83 lines (69 loc) · 1.62 KB
/
pipeline.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
package api39
import (
"encoding/json"
"github.com/kataras/iris/v12"
)
func RecordBody(ctx iris.Context) {
ctx.RecordRequestBody(true)
ctx.Next()
}
func NewWithConfig(cfgfile string) (iris.Handler, error) {
cfg, err := ReadConfig(cfgfile)
if err != nil {
return nil, err
}
return WithConfig(cfg), nil
}
func ParseBody(ctx iris.Context) {
if ctx.Method() != "GET" {
body, err := ctx.GetBody()
if err != nil {
ctx.StopWithError(iris.StatusBadRequest, err)
return
}
var parsed iris.Map
if err := json.Unmarshal(body, &parsed); err != nil {
ctx.StopWithError(iris.StatusBadRequest, err)
return
} else {
ctx.Values().Set("JSONBody", parsed)
}
}
ctx.Next()
}
func WithConfig(cfg *Config) iris.Handler {
return func(ctx iris.Context) {
ctx.Values().Set("config", cfg)
ctx.Next()
}
}
func VerifyGithubSignature(ctx iris.Context) {
cfg, ok := ctx.Values().Get("config").(*Config)
if !ok {
ctx.StopWithJSON(iris.StatusInternalServerError, iris.Map{
"error": "no configuration loaded",
})
return
}
var headers struct {
HubSignature string `header:"X-Hub-Signature-256,required"`
}
if err := ctx.ReadHeaders(&headers); err != nil {
ctx.StopWithError(iris.StatusBadRequest, err)
return
}
signature := headers.HubSignature[7:len(headers.HubSignature)]
body, err := ctx.GetBody()
if err != nil {
ctx.StopWithJSON(iris.StatusBadRequest, iris.Map{
"error": "cannot read request body",
})
return
}
if !IsValidMAC(body, []byte(signature), []byte(cfg.Apikey)) {
ctx.StopWithJSON(iris.StatusUnauthorized, iris.Map{
"error": "invalid request signature",
})
}
ctx.Next()
}