-
Notifications
You must be signed in to change notification settings - Fork 67
/
webhook.go
213 lines (180 loc) · 5.94 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
package main
import (
"crypto/hmac"
"crypto/sha256"
"crypto/subtle"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"strings"
"time"
"github.com/mattermost/mattermost/server/public/model"
"github.com/pkg/errors"
"github.com/mattermost/mattermost-plugin-zoom/server/zoom"
)
func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) {
if !p.verifyMattermostWebhookSecret(r) {
p.API.LogWarn("Could not verify Mattermost webhook secret")
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
if !strings.Contains(r.Header.Get("Content-Type"), "application/json") {
res := fmt.Sprintf("Expected Content-Type 'application/json' for webhook request, received '%s'.", r.Header.Get("Content-Type"))
p.API.LogWarn(res)
http.Error(w, res, http.StatusBadRequest)
return
}
b, err := io.ReadAll(r.Body)
if err != nil {
p.API.LogWarn("Cannot read body from Webhook")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var webhook zoom.Webhook
if err = json.Unmarshal(b, &webhook); err != nil {
p.API.LogError("Error unmarshaling webhook", "err", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if webhook.Event != zoom.EventTypeValidateWebhook {
err = p.verifyZoomWebhookSignature(r, b)
if err != nil {
p.API.LogWarn("Could not verify webhook signature: " + err.Error())
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
}
switch webhook.Event {
case zoom.EventTypeMeetingEnded:
p.handleMeetingEnded(w, r, b)
case zoom.EventTypeValidateWebhook:
p.handleValidateZoomWebhook(w, r, b)
default:
w.WriteHeader(http.StatusOK)
}
}
func (p *Plugin) handleMeetingEnded(w http.ResponseWriter, r *http.Request, body []byte) {
var webhook zoom.MeetingWebhook
if err := json.Unmarshal(body, &webhook); err != nil {
p.API.LogError("Error unmarshaling meeting webhook", "err", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
meetingPostID := webhook.Payload.Object.ID
postID, appErr := p.fetchMeetingPostID(meetingPostID)
if appErr != nil {
http.Error(w, appErr.Error(), appErr.StatusCode)
return
}
post, appErr := p.API.GetPost(postID)
if appErr != nil {
p.API.LogWarn("Could not get meeting post by id", "err", appErr)
http.Error(w, appErr.Error(), appErr.StatusCode)
return
}
start := time.Unix(0, post.CreateAt*int64(time.Millisecond))
length := int(math.Ceil(float64((model.GetMillis()-post.CreateAt)/1000) / 60))
startText := start.Format("Mon Jan 2 15:04:05 -0700 MST 2006")
topic, ok := post.Props["meeting_topic"].(string)
if !ok {
topic = defaultMeetingTopic
}
meetingID, ok := post.Props["meeting_id"].(float64)
if !ok {
meetingID = 0
}
slackAttachment := model.SlackAttachment{
Fallback: fmt.Sprintf("Meeting %s has ended: started at %s, length: %d minute(s).", post.Props["meeting_id"], startText, length),
Title: topic,
Text: fmt.Sprintf(
"Meeting ID: %d\n\n##### Meeting Summary\n\nDate: %s\n\nMeeting Length: %d minute(s)",
int(meetingID),
startText,
length,
),
}
post.Message = "The meeting has ended."
post.Props["meeting_status"] = zoom.WebhookStatusEnded
post.Props["attachments"] = []*model.SlackAttachment{&slackAttachment}
_, appErr = p.API.UpdatePost(post)
if appErr != nil {
p.API.LogWarn("Could not update the post", "err", appErr)
http.Error(w, appErr.Error(), appErr.StatusCode)
return
}
if appErr = p.deleteMeetingPostID(meetingPostID); appErr != nil {
p.API.LogWarn("failed to delete db entry", "error", appErr.Error())
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(post); err != nil {
p.API.LogWarn("failed to write response", "error", err.Error())
}
}
func (p *Plugin) verifyMattermostWebhookSecret(r *http.Request) bool {
config := p.getConfiguration()
return subtle.ConstantTimeCompare([]byte(r.URL.Query().Get("secret")), []byte(config.WebhookSecret)) == 1
}
func (p *Plugin) verifyZoomWebhookSignature(r *http.Request, body []byte) error {
config := p.getConfiguration()
if config.ZoomWebhookSecret == "" {
return nil
}
var webhook zoom.Webhook
err := json.Unmarshal(body, &webhook)
if err != nil {
return errors.Wrap(err, "error unmarshaling webhook payload")
}
ts := r.Header.Get("x-zm-request-timestamp")
msg := fmt.Sprintf("v0:%s:%s", ts, string(body))
hash, err := createWebhookSignatureHash(config.ZoomWebhookSecret, msg)
if err != nil {
return err
}
computedSignature := fmt.Sprintf("v0=%s", hash)
providedSignature := r.Header.Get("x-zm-signature")
if computedSignature != providedSignature {
return errors.New("provided signature does not match")
}
return nil
}
func (p *Plugin) handleValidateZoomWebhook(w http.ResponseWriter, r *http.Request, body []byte) {
config := p.getConfiguration()
if config.ZoomWebhookSecret == "" {
p.API.LogWarn("Failed to validate Zoom webhook: Zoom webhook secret not set")
w.WriteHeader(http.StatusBadRequest)
return
}
var webhook zoom.ValidationWebhook
err := json.Unmarshal(body, &webhook)
if err != nil {
p.API.LogWarn("Failed to unmarshal Zoom webhook: " + err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
hash, err := createWebhookSignatureHash(config.ZoomWebhookSecret, webhook.Payload.PlainToken)
if err != nil {
p.API.LogWarn("Failed to create webhook signature hash: " + err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
out := zoom.ValidationWebhookResponse{
PlainToken: webhook.Payload.PlainToken,
EncryptedToken: hash,
}
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(out); err != nil {
p.API.LogWarn("failed to write response", "error", err.Error())
}
}
func createWebhookSignatureHash(secret, data string) (string, error) {
h := hmac.New(sha256.New, []byte(secret))
_, err := h.Write([]byte(data))
if err != nil {
return "", errors.Wrap(err, "failed to create webhook signature hash")
}
return hex.EncodeToString(h.Sum(nil)), nil
}