forked from frain-dev/convoy-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
234 lines (191 loc) · 4.57 KB
/
webhook.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package convoy_go
import (
"crypto/hmac"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"hash"
"io"
"net/http"
"strconv"
"strings"
"time"
)
var (
ErrInvalidSignatureHeader = errors.New("webhook has no signature header")
ErrInvalidHeader = errors.New("webhook has invalid header")
ErrInvalidEncoding = errors.New("invalid encoding")
ErrInvalidSignature = errors.New("webhook has no valid signature")
ErrInvalidHashAlgorithm = errors.New("invalid hash algorithm")
ErrTimestampExpired = errors.New("timestamp has expired")
)
var (
DefaultTolerance = 300 * time.Second
DefaultEncoding EncodingType = HexEncoding
DefaultHash = "SHA256"
DefaultSigHeader = "X-Convoy-Signature"
)
type signedHeader struct {
timestamp time.Time
signatures [][]byte
isAdvanced bool
}
type Webhook struct {
opts *WebhookOpts
}
type WebhookOpts struct {
SigHeader string
Secret string
Encoding EncodingType
Hash string
Tolerance time.Duration
}
type EncodingType string
const (
Base64Encoding EncodingType = "base64"
HexEncoding EncodingType = "hex"
)
func NewWebhook(opts *WebhookOpts) *Webhook {
if isStringEmpty(opts.Hash) {
opts.Hash = DefaultHash
}
if isStringEmpty(string(opts.Encoding)) {
opts.Encoding = DefaultEncoding
}
if opts.Tolerance == 0 {
opts.Tolerance = DefaultTolerance
}
if isStringEmpty(opts.SigHeader) {
opts.SigHeader = DefaultSigHeader
}
return &Webhook{opts}
}
func (w *Webhook) VerifyRequest(r *http.Request) error {
body, err := io.ReadAll(r.Body)
if err != nil {
return err
}
header := r.Header.Get(w.opts.SigHeader)
if isStringEmpty(header) {
return ErrInvalidHeader
}
return w.verify(body, header)
}
func (w *Webhook) VerifyPayload(b []byte, header string) error {
return w.verify(b, header)
}
func (w *Webhook) verify(body []byte, header string) error {
sh, err := w.parseSignatureHeader(header)
if err != nil {
return err
}
expectedSignature, err := w.generateSignature(sh, body)
if err != nil {
return err
}
for _, sig := range sh.signatures {
// Check all signatures for a match
if hmac.Equal(expectedSignature, sig) {
return nil
}
}
return ErrInvalidSignature
}
func (w *Webhook) parseSignatureHeader(header string) (*signedHeader, error) {
var err error
sh := &signedHeader{}
if isStringEmpty(header) {
return sh, ErrInvalidSignatureHeader
}
pairs := strings.Split(header, ",")
if len(pairs) > 1 {
sh, err = w.decodeAdvanced(sh, pairs)
if err != nil {
return sh, err
}
} else {
sh, err = w.decodeSimple(sh, header)
if err != nil {
return sh, err
}
}
if len(sh.signatures) == 0 {
return sh, ErrInvalidSignature
}
return sh, nil
}
func (w *Webhook) generateSignature(sh *signedHeader, body []byte) ([]byte, error) {
fn, err := w.getHashFunction(w.opts.Hash)
if err != nil {
return nil, err
}
h := hmac.New(fn, []byte(w.opts.Secret))
if sh.isAdvanced {
h.Write([]byte(fmt.Sprintf("%d", sh.timestamp.Unix())))
h.Write([]byte(","))
}
h.Write(body)
return h.Sum(nil), nil
}
func (w *Webhook) getHashFunction(algorithm string) (func() hash.Hash, error) {
switch algorithm {
case "SHA256":
return sha256.New, nil
case "SHA512":
return sha512.New, nil
}
return nil, ErrInvalidHashAlgorithm
}
func (w *Webhook) decodeAdvanced(sh *signedHeader, pairs []string) (*signedHeader, error) {
for _, pair := range pairs {
parts := strings.SplitN(pair, "=", 2)
if len(parts) != 2 {
return sh, ErrInvalidHeader
}
item := parts[0]
if item == "t" {
timestamp, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return sh, ErrInvalidHeader
}
sh.timestamp = time.Unix(timestamp, 0)
continue
}
if strings.Contains(item, "v") {
sig, err := w.decodeString(parts[1])
if err != nil {
continue
}
sh.signatures = append(sh.signatures, sig)
}
}
expiredTimestamp := time.Since(sh.timestamp) > w.opts.Tolerance
if expiredTimestamp {
return nil, ErrTimestampExpired
}
sh.isAdvanced = true
return sh, nil
}
func (w *Webhook) decodeSimple(sh *signedHeader, value string) (*signedHeader, error) {
sig, err := w.decodeString(value)
if err != nil {
return sh, err
}
sh.signatures = append(sh.signatures, sig)
return sh, nil
}
func (w *Webhook) decodeString(value string) ([]byte, error) {
switch w.opts.Encoding {
case HexEncoding:
sig, err := hex.DecodeString(value)
return sig, err
case Base64Encoding:
sig, err := base64.StdEncoding.DecodeString(value)
return sig, err
default:
return nil, ErrInvalidEncoding
}
}