-
Notifications
You must be signed in to change notification settings - Fork 3
/
webhook.go
77 lines (63 loc) · 2.29 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
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"
)
var (
// WebhookDefaultIncludes specifies default includes for Webhook.
WebhookDefaultIncludes = []string{"campaign", "client"}
// WebhookFields is all fields in the Webhook Attributes struct
WebhookFields = getObjectFields(Webhook{}.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
}
// Webhook is fired based on events happening on a particular campaign.
type Webhook struct {
Type string `json:"type"`
ID string `json:"id"`
Attributes WebhookAttributes `json:"attributes"`
Relationships struct {
Campaign *CampaignRelationship `json:"campaign,omitempty"`
Memberships *MembershipsRelationship `json:"memberships,omitempty"`
} `json:"relationships"`
}
// WebhookAttributes is the attributes struct for Webhook
type WebhookAttributes struct {
LastAttemptedAt NullTime `json:"last_attempted_at"`
NumConsecutiveTimesFailed int `json:"num_consecutive_times_failed"`
Paused bool `json:"paused"`
Secret string `json:"secret"`
Triggers interface{} `json:"triggers"`
URI string `json:"uri"`
}
// WebhookResponse wraps Patreon's fetch user API response
type WebhookResponse struct {
Data Webhook `json:"data"`
Included Includes `json:"included"`
Links struct {
Self string `json:"self"`
} `json:"links"`
}