-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebhooks.go
175 lines (143 loc) · 5.03 KB
/
webhooks.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
package calendly
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"time"
)
// WebhookPayload holds a Calendly Webhook Payload object
type WebhookPayload struct {
CreatedAt time.Time `json:"created_at"`
CreatedBy string `json:"created_by"`
Event string `json:"event"`
Payload Payload `json:"payload"`
}
type Tracking struct {
UtmCampaign interface{} `json:"utm_campaign"`
UtmSource interface{} `json:"utm_source"`
UtmMedium interface{} `json:"utm_medium"`
UtmContent interface{} `json:"utm_content"`
UtmTerm interface{} `json:"utm_term"`
SalesforceUUID interface{} `json:"salesforce_uuid"`
}
type Payload struct {
CancelURL string `json:"cancel_url"`
CreatedAt time.Time `json:"created_at"`
Email string `json:"email"`
Event string `json:"event"`
Name string `json:"name"`
NewInvitee interface{} `json:"new_invitee"`
OldInvitee interface{} `json:"old_invitee"`
QuestionsAndAnswers []CustomQuestionsAndAnswers `json:"questions_and_answers"`
RescheduleURL string `json:"reschedule_url"`
Rescheduled bool `json:"rescheduled"`
Status string `json:"status"`
TextReminderNumber interface{} `json:"text_reminder_number"`
Timezone string `json:"timezone"`
Tracking Tracking `json:"tracking"`
UpdatedAt time.Time `json:"updated_at"`
URI string `json:"uri"`
Canceled bool `json:"canceled"`
Cancellation Cancellation `json:"cancellation"`
}
type CustomQuestionsAndAnswers struct {
Answer string `json:"answer"`
Position int `json:"position"`
Question string `json:"question"`
}
// WebhookSignature holds a Calendly webhook signature
type WebhookSignature struct {
Time int64
V1 string
}
// WebhookSubscription holds a Calendly Webhook Subscription object
type WebhookSubscription struct {
URI string `json:"uri"`
CallbackUrl string `json:"callback_url"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
RetryStartedAt time.Time `json:"retry_started_at"`
State string `json:"state"`
Events []string `json:"events"`
Scope string `json:"scope"`
Organization string `json:"organization"`
User string `json:"user"`
Creator string `json:"creator"`
}
// ListWebhookSubscriptionsInput is used as input for the ListWebhookSubscriptions function
type ListWebhookSubscriptionsInput struct {
Organization string
Scope string
Count int
PageToken string
Sort string
User string
}
// ListWebhookSubscriptions lists the webhook subscriptions for the provided user or organization
func (cw *CalendlyWrapper) ListWebhookSubscriptions(input *ListWebhookSubscriptionsInput) ([]WebhookSubscription, error) {
var ws []WebhookSubscription
var wsr map[string]json.RawMessage
url := fmt.Sprintf("%s%s", cw.baseApiUrl, "webhook_subscriptions")
if input.Organization == "" {
return ws, fmt.Errorf("you must provide an organization")
}
if strings.ToLower(input.Scope) == "user" && input.User == "" {
return ws, fmt.Errorf("with a user scope you must provide a user")
}
url += fmt.Sprintf("?organization=%s", input.Organization)
if input.Count != 0 {
url += fmt.Sprintf("&count=%d", input.Count)
}
if input.Scope != "" {
url += fmt.Sprintf("&scope=%s", input.Scope)
}
if input.PageToken != "" {
url += fmt.Sprintf("&page_token=%s", input.PageToken)
}
if input.User != "" {
url += fmt.Sprintf("&user=%s", input.User)
}
resp, err := cw.sendGetReq(url)
if err != nil {
return ws, err
}
err = json.Unmarshal(resp, &wsr)
if err != nil {
return ws, err
}
err = json.Unmarshal(wsr["collection"], &ws)
if err != nil {
return ws, err
}
return ws, nil
}
// GetWebhookSubscription gets the webhook subscription by id
func (cw *CalendlyWrapper) GetWebhookSubscription(id string) (WebhookSubscription, error) {
var ws WebhookSubscription
var wsr map[string]WebhookSubscription
url := fmt.Sprintf("%s%s%s", cw.baseApiUrl, "webhook_subscriptions/", id)
resp, err := cw.sendGetReq(url)
if err != nil {
return ws, err
}
err = json.Unmarshal(resp, &wsr)
if err != nil {
return ws, err
}
ws = wsr["resource"]
return ws, nil
}
// VerifyWebhookSignature verifies the given webhook signature
func (cw *CalendlyWrapper) VerifyWebhookSignature(body, secret []byte, sig *WebhookSignature) (bool, error) {
signedPayload := fmt.Sprintf("%d.%s", sig.Time, body)
h := hmac.New(sha256.New, secret)
h.Write([]byte(signedPayload))
sha := hex.EncodeToString(h.Sum(nil))
if sig.V1 == sha {
return true, nil
}
return false, nil
}