This repository has been archived by the owner on Oct 26, 2018. It is now read-only.
forked from jitsi/jitsi-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
369 lines (330 loc) · 9.57 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
package jitsi
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/nlopes/slack"
"github.com/rs/zerolog/hlog"
)
const (
roomTemplate = `{"response_type":"in_channel","attachments":[{"fallback":"Meeting started %s","title":"Meeting started %s","color":"#3AA3E3","attachment_type":"default","actions":[{"name":"join","text":"Join","type":"button","url":"%s","style":"primary"}]}]}`
userTemplate = `{"response_type":"ephemeral","attachments":[{"fallback":"Invitations have been sent for your meeting.","title":"Invitations have been sent for your meeting.","color":"#3AA3E3","attachment_type":"default","actions":[{"name":"join","text":"Join","type":"button","url":"%s","style":"primary"}]}]}`
helpMessage = `{"response_type":"ephemeral","text":"How to use /jitsi...","attachments":[{"text":"To share a conference link with the channel, use '/jitsi'. Now everyone can join.\nTo share a conference link with users, use 'jitsi @bob @alice'. Now you can meet with Bob and Alice."}]}`
installMessage = `{"response_type":"ephemeral","text":"Please install the jitsi meet app to integrate with your slack workspace.","attachments":[{"text":"%s"}]}`
// error strings from slack api
errInvalidAuth = "invalid_auth"
errInactiveAccount = "account_inactive"
errMissingAuthToken = "not_authed"
)
var atMentionRE = regexp.MustCompile(`<@([^>|]+)`)
// ConferenceTokenGenerator provides an interface for creating video conference
// authenticated access via JWT.
type ConferenceTokenGenerator interface {
CreateJWT(tenantID, tenantName, roomClaim, userID, userName, avatarURL string) (string, error)
}
// TokenReader provides an interface for reading access token data from
// a token store.
type TokenReader interface {
GetFirstBotTokenForTeam(teamID string) (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))
}
// SlashCommandHandlers provides http handlers for Slack slash commands
// that integrate with Jitsi Meet.
type SlashCommandHandlers struct {
ConferenceHost string
TokenGenerator ConferenceTokenGenerator
SlackSigningSecret string
TokenReader TokenReader
SharableURL string
}
func (s *SlashCommandHandlers) inviteUser(client *slack.Client, hostID, userID, teamID, teamName, room string) error {
userInfo, err := client.GetUserInfo(userID)
if err != nil {
return err
}
userToken, err := s.TokenGenerator.CreateJWT(
strings.ToLower(teamID),
strings.ToLower(teamName),
room,
userInfo.ID,
userInfo.Name,
userInfo.Profile.Image192,
)
if err != nil {
return err
}
channel, _, _, err := client.OpenConversation(
&slack.OpenConversationParameters{
Users: []string{userID},
},
)
if err != nil {
return err
}
confURL := fmt.Sprintf(
"%s/%s/%s?jwt=%s",
s.ConferenceHost,
strings.ToLower(teamName),
room,
userToken,
)
params := slack.PostMessageParameters{}
msg := fmt.Sprintf("<@%s> would like you to join a meeting.", hostID)
attachment := slack.Attachment{
Fallback: msg,
Title: msg,
Color: "#3AA3E3",
Actions: []slack.AttachmentAction{
slack.AttachmentAction{
Name: "join",
Text: "Join",
Type: "button",
Style: "primary",
URL: confURL,
},
},
}
params.Attachments = []slack.Attachment{attachment}
_, _, err = client.PostMessage(
channel.ID,
"",
params,
)
if err != nil {
return err
}
return nil
}
// 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
}
callerID := r.PostFormValue("user_id")
teamID := r.PostFormValue("team_id")
teamName := r.PostFormValue("team_domain")
text := r.PostFormValue("text")
if strings.ToLower(text) == "help" {
help(w)
return
}
// Grab an access token after validating request and body
// so we can fail early if we don't have one.
token, err := s.TokenReader.GetFirstBotTokenForTeam(teamID)
if err != nil {
switch err.Error() {
case errMissingAuthToken:
install(w, s.SharableURL)
default:
hlog.FromRequest(r).Error().
Err(err).
Msg("retrieving token")
w.WriteHeader(http.StatusInternalServerError)
}
return
}
room := RandomName()
matches := atMentionRE.FindAllStringSubmatch(text, -1)
if matches == nil {
meetingURL := fmt.Sprintf(
"%s/%s/%s",
s.ConferenceHost,
strings.ToLower(teamName),
room,
)
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
resp := fmt.Sprintf(roomTemplate, meetingURL, meetingURL, meetingURL)
w.Write([]byte(resp))
return
}
slackClient := slack.New(token)
for _, match := range matches {
err = s.inviteUser(slackClient, callerID, match[1], teamID, teamName, room)
if err != nil {
switch err.Error() {
case errInvalidAuth, errInactiveAccount, errMissingAuthToken:
install(w, s.SharableURL)
return
default:
hlog.FromRequest(r).Error().
Err(err).
Msg("inviting user")
}
}
}
callerInfo, err := slackClient.GetUserInfo(callerID)
if err != nil {
switch err.Error() {
case errInvalidAuth, errInactiveAccount, errMissingAuthToken:
install(w, s.SharableURL)
default:
hlog.FromRequest(r).Error().
Err(err).
Msg("retrieving user info from slack")
w.WriteHeader(http.StatusInternalServerError)
}
return
}
callerToken, err := s.TokenGenerator.CreateJWT(
strings.ToLower(teamID),
strings.ToLower(teamName),
room,
callerID,
callerInfo.Name,
callerInfo.Profile.Image192,
)
callerConfURL := fmt.Sprintf(
"%s/%s/%s?jwt=%s",
s.ConferenceHost,
strings.ToLower(teamName),
room,
callerToken,
)
// TODO: determine what's an error that gets exposed to the user.
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
resp := fmt.Sprintf(userTemplate, callerConfURL)
w.Write([]byte(resp))
}
// TokenWriter provides an interface to write access token data to the
// token store.
type TokenWriter interface {
Store(data *TokenData) error
}
// SlackOAuthHandlers is used for handling Slack OAuth validation.
type SlackOAuthHandlers struct {
AccessURLTemplate string
ClientID string
ClientSecret string
AppID string
TokenWriter TokenWriter
}
type botToken struct {
BotUserID string `json:"bot_user_id"`
BotAccessToken string `json:"bot_access_token"`
}
type accessResponse struct {
OK bool `json:"ok"`
AccessToken string `json:"access_token"`
Scope string `json:"scope"`
UserID string `json:"user_id"`
TeamName string `json:"team_name"`
TeamID string `json:"team_id"`
Bot botToken `json:"bot"`
}
// 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
}
if params["error"] != nil {
hlog.FromRequest(r).Error().
Err(err).
Msg("error response user declined 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
}
// TODO: inject an http client with http logging.
resp, err := http.Get(fmt.Sprintf(
o.AccessURLTemplate,
o.ClientID,
o.ClientSecret,
code[0],
))
if err != nil {
hlog.FromRequest(r).Error().
Err(err).
Msg("oauth req error")
w.WriteHeader(http.StatusInternalServerError)
return
}
var access accessResponse
if err := json.NewDecoder(resp.Body).Decode(&access); err != nil {
hlog.FromRequest(r).Error().
Err(err).
Msg("unable to decode slack access response")
w.WriteHeader(http.StatusInternalServerError)
return
}
if !access.OK {
hlog.FromRequest(r).Error().
Err(err).
Msg("access not ok")
w.WriteHeader(http.StatusInternalServerError)
return
}
err = o.TokenWriter.Store(&TokenData{
TeamID: access.TeamID,
UserID: access.UserID,
BotToken: access.Bot.BotAccessToken,
BotUserID: access.Bot.BotUserID,
AccessToken: access.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)
}