-
Notifications
You must be signed in to change notification settings - Fork 27
/
handlers.go
423 lines (382 loc) · 11.4 KB
/
handlers.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package jitsi
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/rs/zerolog/hlog"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
)
const (
// error strings from slack api
errInvalidAuth = "invalid_auth"
errInactiveAccount = "account_inactive"
errMissingAuthToken = "not_authed"
errCannotDMBot = "cannot_dm_bot"
errAccessDenied = "access_denied"
)
var (
atMentionRE = regexp.MustCompile(`<@([^>|]+)`)
serverCmdRE = regexp.MustCompile(`^server`)
serverConfigRE = regexp.MustCompile(`^server\s+(<https?:\/\/\S+>)`)
helpCmdRE = regexp.MustCompile(`^help`)
)
// TokenReader provides an interface for reading access token data from
// a token store.
type TokenReader interface {
GetTokenForTeam(teamID string) (*TokenData, error)
}
// ServerConfigWriter provides an interface for writing server configuration
// data for a team's workspace.
type ServerConfigWriter interface {
Store(*ServerCfgData) error
Remove(string) error
}
func handleRequestValidation(w http.ResponseWriter, r *http.Request, SlackSigningSecret string) bool {
ts := r.Header.Get(RequestTimestampHeader)
sig := r.Header.Get(RequestSignatureHeader)
if ts == "" || sig == "" {
w.WriteHeader(http.StatusUnauthorized)
return false
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return false
}
defer r.Body.Close()
if !ValidRequest(SlackSigningSecret, string(body), ts, sig) {
w.WriteHeader(http.StatusUnauthorized)
return false
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
return true
}
func help(w http.ResponseWriter) {
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(helpMessage))
}
func install(w http.ResponseWriter, sharableURL string) {
installMsg := fmt.Sprintf(installMessage, sharableURL)
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(installMsg))
}
// EventHandler is used to handle event callbacks from Slack api.
type EventHandler struct {
SlackSigningSecret string
TokenWriter TokenWriter
}
// Handle handles event callbacks for the integration.
// adapated from https://github.com/slack-go/slack/blob/master/examples/eventsapi/events.go
func (e *EventHandler) Handle(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
hlog.FromRequest(r).Warn().Err(err).Msg("evhandle: malformed request")
w.WriteHeader(http.StatusBadRequest)
return
}
defer r.Body.Close()
sv, err := slack.NewSecretsVerifier(r.Header, e.SlackSigningSecret)
if err != nil {
hlog.FromRequest(r).Warn().Err(err).Msg("evhandle: signature failed")
w.WriteHeader(http.StatusBadRequest)
return
}
if _, err := sv.Write(body); err != nil {
hlog.FromRequest(r).Warn().Err(err).Msg("evhandle: write failed")
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := sv.Ensure(); err != nil {
hlog.FromRequest(r).Warn().Err(err).Msg("ensure failed: secrets may be not loading")
w.WriteHeader(http.StatusUnauthorized)
return
}
eventsAPIEvent, err := slackevents.ParseEvent(json.RawMessage(body), slackevents.OptionNoVerifyToken())
if err != nil {
hlog.FromRequest(r).Warn().Err(err).Msg("evhandle: parse failed")
w.WriteHeader(http.StatusInternalServerError)
return
}
if eventsAPIEvent.Type == slackevents.URLVerification {
var cr *slackevents.ChallengeResponse
err := json.Unmarshal([]byte(body), &cr)
if err != nil {
hlog.FromRequest(r).Warn().Err(err).Msg("evhandle: challenge resp failed")
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text")
w.Write([]byte(cr.Challenge))
}
if eventsAPIEvent.Type == slackevents.CallbackEvent {
innerEvent := eventsAPIEvent.InnerEvent
switch innerEvent.Data.(type) {
case *slackevents.AppUninstalledEvent:
{
err := e.TokenWriter.Remove(eventsAPIEvent.TeamID)
// do not error out or return 500 since this failing is non-critical
if err != nil {
hlog.FromRequest(r).Warn().
Err(err).
Msg(fmt.Sprintf("app_uninstalled failed for: %s", eventsAPIEvent.TeamID))
}
}
}
}
w.WriteHeader(http.StatusOK)
}
// SlashCommandHandlers provides http handlers for Slack slash commands
// that integrate with Jitsi Meet.
type SlashCommandHandlers struct {
MeetingGenerator *MeetingGenerator
SlackSigningSecret string
TokenReader TokenReader
TokenWriter TokenWriter
SharableURL string
ServerConfigWriter ServerConfigWriter
}
// Jitsi will create a conference and dispatch an invite message to both users.
// It is a slash command for Slack.
func (s *SlashCommandHandlers) Jitsi(w http.ResponseWriter, r *http.Request) {
if !handleRequestValidation(w, r, s.SlackSigningSecret) {
return
}
err := r.ParseForm()
if err != nil {
hlog.FromRequest(r).Error().
Err(err).
Msg("unable to parse form data")
w.WriteHeader(http.StatusInternalServerError)
return
}
text := r.PostFormValue("text")
if helpCmdRE.MatchString(text) {
help(w)
} else if serverCmdRE.MatchString(text) {
s.configureServer(w, r)
} else {
s.dispatchInvites(w, r)
}
}
func (s *SlashCommandHandlers) configureServer(w http.ResponseWriter, r *http.Request) {
teamID := r.PostFormValue("team_id")
text := r.PostFormValue("text")
// First check if the default is being requested.
configuration := strings.Split(text, " ")
if len(configuration) < 2 {
fmt.Fprintf(w, "Run '/jitsi server default' or '/jitsi server [url]' with the URL of your team's server")
return
}
if configuration[1] == "default" {
err := s.ServerConfigWriter.Remove(teamID)
if err != nil {
hlog.FromRequest(r).Error().
Err(err).
Msg("defaulting server")
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "Your team's conferences will now be hosted on https://meet.jit.si")
return
}
if !serverConfigRE.MatchString(text) {
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "A proper conference host must be provided.")
return
}
host := serverConfigRE.FindAllStringSubmatch(text, -1)[0][1]
host = strings.Trim(host, "<>")
err := s.ServerConfigWriter.Store(&ServerCfgData{
TeamID: teamID,
Server: host,
})
if err != nil {
hlog.FromRequest(r).Error().
Err(err).
Msg("configuring server")
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Your team's conferences will now be hosted on %s\nRun `/jitsi server default` if you'd like to continue using https://meet.jit.si", host)
}
func (s *SlashCommandHandlers) dispatchInvites(w http.ResponseWriter, r *http.Request) {
// Generate the meeting data.
teamID := r.PostFormValue("team_id")
teamName := r.PostFormValue("team_domain")
meeting, err := s.MeetingGenerator.New(teamID, teamName)
if err != nil {
hlog.FromRequest(r).Error().
Err(err).
Msg("generating meeting")
w.WriteHeader(http.StatusInternalServerError)
return
}
// If nobody was @-mentioned then just send a generic invite to the channel.
text := r.PostFormValue("text")
matches := atMentionRE.FindAllStringSubmatch(text, -1)
if matches == nil {
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
resp := fmt.Sprintf(roomTemplate, meeting.Host, meeting.Host, meeting.URL)
w.Write([]byte(resp))
return
}
// Grab a oauth token for the slack workspace.
token, err := s.TokenReader.GetTokenForTeam(teamID)
if err != nil {
switch err.Error() {
case errMissingAuthToken:
hlog.FromRequest(r).Info().
Err(err).
Msg("missing auth token")
install(w, s.SharableURL)
default:
hlog.FromRequest(r).Error().
Err(err).
Msg("retrieving token")
w.WriteHeader(http.StatusInternalServerError)
}
return
}
// Dispatch a personal invite to each user @-mentioned.
callerID := r.PostFormValue("user_id")
for _, match := range matches {
err = sendPersonalizedInvite(token.AccessToken, callerID, match[1], &meeting)
if err != nil {
switch err.Error() {
case errInactiveAccount, errMissingAuthToken:
hlog.FromRequest(r).Info().
Err(err).
Msg(fmt.Sprintf("inactive or missing auth token"))
install(w, s.SharableURL)
return
case errInvalidAuth:
// catches the case where a workspace has removed the app but
// someone tries to use the command anyways
hlog.FromRequest(r).Info().
Err(err).
Msg("invalid auth")
install(w, s.SharableURL)
return
case errCannotDMBot:
hlog.FromRequest(r).Warn().
Err(err).
Msg("bot cannot DM")
default:
hlog.FromRequest(r).Error().
Err(err).
Msg("unexpected sendPersonalizedInvite error")
}
}
}
// Create a personalized response for the meeting initiator.
resp, err := joinPersonalMeetingMsg(token.AccessToken, callerID, &meeting)
if err != nil {
switch err.Error() {
case errInvalidAuth, errInactiveAccount, errMissingAuthToken:
hlog.FromRequest(r).Info().
Err(err).
Msg("joinPersonalMeetingMsg invalid or missing token")
install(w, s.SharableURL)
return
default:
hlog.FromRequest(r).Error().
Err(err).
Msg("joinPersonalizedMeetingMsg error")
}
}
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(resp))
}
// TokenWriter provides an interface to write access token data to the
// token store.
type TokenWriter interface {
Store(data *TokenData) error
Remove(teamID string) error
}
// SlackOAuthHandlers is used for handling Slack OAuth validation.
type SlackOAuthHandlers struct {
ClientID string
ClientSecret string
AppID string
TokenWriter TokenWriter
}
// Auth validates OAuth access tokens.
func (o *SlackOAuthHandlers) Auth(w http.ResponseWriter, r *http.Request) {
params, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
hlog.FromRequest(r).Error().
Err(err).
Msg("parsing query params")
w.WriteHeader(http.StatusInternalServerError)
return
}
// An error is received when a user declines to install
// or an unexpected issue occurs. The app treats a
// declined install gracefully.
if params["error"] != nil {
switch params["error"][0] {
case errAccessDenied:
hlog.FromRequest(r).Info().
Err(errors.New(params["error"][0])).
Msg("user declined install")
w.WriteHeader(http.StatusOK)
return
default:
hlog.FromRequest(r).Error().
Err(errors.New(params["error"][0])).
Msg("failed install")
w.WriteHeader(http.StatusInternalServerError)
return
}
}
code := params["code"]
if len(code) != 1 {
hlog.FromRequest(r).Error().
Err(err).
Msg("code not provided")
w.WriteHeader(http.StatusInternalServerError)
return
}
resp, err := slack.GetOAuthV2Response(
http.DefaultClient,
o.ClientID,
o.ClientSecret,
code[0],
"")
if err != nil {
hlog.FromRequest(r).Error().
Err(err).
Msg("oauth req error")
w.WriteHeader(http.StatusInternalServerError)
return
}
err = o.TokenWriter.Store(&TokenData{
TeamID: resp.Team.ID,
AccessToken: resp.AccessToken,
})
if err != nil {
hlog.FromRequest(r).Error().
Err(err).
Msg("unable to store token")
w.WriteHeader(http.StatusInternalServerError)
return
}
redirect := fmt.Sprintf("https://slack.com/app_redirect?app=%s", o.AppID)
http.Redirect(w, r, redirect, http.StatusFound)
}