forked from blindside-io/octohooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_handler.go
73 lines (59 loc) · 1.8 KB
/
http_handler.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
package octohooks
import (
"io/ioutil"
"net/http"
"github.com/sirupsen/logrus"
)
// Handler implements http.Handler for handling incoming github webhooks
type Handler struct {
SecretResolver SecretResolver
Events chan Event
}
var _ http.Handler = &Handler{}
// NewHandler returns a http.Handler compliant struct that exposes a channel
// of events that you can create your own consumer against.
func NewHandler(resolver SecretResolver) *Handler {
return &Handler{
SecretResolver: resolver,
Events: make(chan Event),
}
}
// ServeHTTP implements http.Handler
// This is where we handle all incoming github webhooks and check signing
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
if r.Method != "POST" {
logrus.Info("invalid method for github webhook")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
logrus.WithField("content-type", ct).Info("invalid content-type for github webhook")
http.Error(w, "content type not allowed", http.StatusUnsupportedMediaType)
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
secret, err := h.SecretResolver.Resolve(r)
if err != nil {
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
err = secret.Validate(r.Header.Get("X-Hub-Signature"), body)
if err != nil {
switch err.(type) {
case signatureInvalid:
http.Error(w, err.Error(), http.StatusForbidden)
default:
http.Error(w, "internal server error", http.StatusInternalServerError)
}
return
}
e := NewEventFromRequestAndBody(r, body)
h.Events <- e
w.WriteHeader(http.StatusAccepted)
w.Write([]byte("ok"))
}