-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwebhook.go
43 lines (33 loc) · 1.01 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
package patreon
import (
"crypto/hmac"
"crypto/md5"
"encoding/hex"
)
const (
// EventCreatePledge specifies a create pledge event
EventCreatePledge = "pledges:create"
// EventUpdatePledge specifies an update pledge event
EventUpdatePledge = "pledges:update"
// EventDeletePledge specifies a delete pledge event
EventDeletePledge = "pledges:delete"
)
const (
// HeaderEventType specifies an event type HTTP header name
HeaderEventType = "X-Patreon-Event"
// HeaderEventSignature specifies message signature HTTP header name to verify message body
HeaderSignature = "X-Patreon-Signature"
)
type WebhookPledge struct {
Data Pledge `json:"data"`
}
// VerifySignature verifies the sender of the message
func VerifySignature(message []byte, secret string, signature string) (bool, error) {
hash := hmac.New(md5.New, []byte(secret))
if _, err := hash.Write(message); err != nil {
return false, err
}
sum := hash.Sum(nil)
expectedSignature := hex.EncodeToString(sum)
return expectedSignature == signature, nil
}