forked from laustindasauce/patreon-go-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriggers.go
48 lines (37 loc) · 1.44 KB
/
triggers.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
package patreon
import (
"crypto/hmac"
"crypto/md5"
"encoding/hex"
)
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"
)
var (
// WebhookTriggersDefaultIncludes specifies default includes for Webhook.
WebhookTriggersDefaultIncludes = []string{"campaign", "user"}
// TestWebhookTriggersDefaultIncludes specifies default includes for Webhook.
TestWebhookTriggersDefaultIncludes = []string{"campaign", "user", "reward"}
// WebhookTriggersFields is all fields in the Webhook Attributes struct
// WebhookTriggers is pretty much Member Attributes.
WebhookTriggersFields = getObjectFields(LiveWebhookMember{}.Attributes)
// TestWebhookTriggersFields is all fields in the Test Webhook struct
TestWebhookTriggersFields = getObjectFields(TestWebhookMember{}.Attributes)
)
// 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
}
type WebhookTriggersResponse struct {
Data LiveWebhookMember `json:"data"`
Included Includes `json:"included"`
}