-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhooks.go
78 lines (59 loc) · 1.61 KB
/
hooks.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
package hooks
import (
"bytes"
"io"
"net/http"
"strconv"
"time"
"github.com/google/uuid"
hooks "github.com/standard-webhooks/standard-webhooks/libraries/go"
)
// Handler for hooks.
type Handler struct {
hook *hooks.Webhook
}
// NewHandler for hooks.
func NewHandler(hook *hooks.Webhook) *Handler {
return &Handler{hook: hook}
}
// ServeHTTP for hooks.
func (h *Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
payload, err := io.ReadAll(req.Body)
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
req.Body = io.NopCloser(bytes.NewReader(payload))
if err := h.hook.Verify(payload, req.Header); err != nil {
http.Error(resp, err.Error(), http.StatusBadRequest)
return
}
next(resp, req)
}
// NewRoundTripper for hooks.
func NewRoundTripper(hook *hooks.Webhook, rt http.RoundTripper) *RoundTripper {
return &RoundTripper{hook: hook, RoundTripper: rt}
}
// RoundTripper for hooks.
type RoundTripper struct {
hook *hooks.Webhook
http.RoundTripper
}
// RoundTrip for hooks.
func (r *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
payload, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
req.Body = io.NopCloser(bytes.NewReader(payload))
ts := time.Now()
id := uuid.New().String()
signature, err := r.hook.Sign(id, ts, payload)
if err != nil {
return nil, err
}
req.Header.Add(hooks.HeaderWebhookID, id)
req.Header.Add(hooks.HeaderWebhookSignature, signature)
req.Header.Add(hooks.HeaderWebhookTimestamp, strconv.FormatInt(ts.Unix(), 10))
return r.RoundTripper.RoundTrip(req)
}