-
Notifications
You must be signed in to change notification settings - Fork 68
/
plugin.go
273 lines (222 loc) · 7.1 KB
/
plugin.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
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License for license information.
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"sync"
"github.com/mattermost/mattermost-plugin-api/experimental/telemetry"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"github.com/mattermost/mattermost-plugin-zoom/server/zoom"
)
const (
botUserName = "zoom"
botDisplayName = "Zoom"
botDescription = "Created by the Zoom plugin."
trueString = "true"
falseString = "false"
zoomProviderName = "Zoom"
)
type Plugin struct {
plugin.MattermostPlugin
jwtClient zoom.Client
// botUserID of the created bot account.
botUserID string
// configurationLock synchronizes access to the configuration.
configurationLock sync.RWMutex
// configuration is the active plugin configuration. Consult getConfiguration and
// setConfiguration for usage.
configuration *configuration
siteURL string
telemetryClient telemetry.Client
tracker telemetry.Tracker
}
// Client defines a common interface for the API and OAuth Zoom clients
type Client interface {
GetMeeting(meetingID int) (*zoom.Meeting, error)
GetUser(user *model.User, firstConnect bool) (*zoom.User, *zoom.AuthError)
}
// OnActivate checks if the configurations is valid and ensures the bot account exists
func (p *Plugin) OnActivate() error {
config := p.getConfiguration()
if err := config.IsValid(p.isCloudLicense()); err != nil {
return err
}
if err := p.registerSiteURL(); err != nil {
return errors.Wrap(err, "could not register site URL")
}
command, err := p.getCommand()
if err != nil {
return errors.Wrap(err, "failed to get command")
}
err = p.API.RegisterCommand(command)
if err != nil {
return errors.Wrap(err, "failed to register command")
}
botUserID, err := p.Helpers.EnsureBot(&model.Bot{
Username: botUserName,
DisplayName: botDisplayName,
Description: botDescription,
})
if err != nil {
return errors.Wrap(err, "failed to ensure bot account")
}
p.botUserID = botUserID
bundlePath, err := p.API.GetBundlePath()
if err != nil {
return errors.Wrap(err, "couldn't get bundle path")
}
profileImage, err := ioutil.ReadFile(filepath.Join(bundlePath, "assets", "profile.png"))
if err != nil {
return errors.Wrap(err, "couldn't read profile image")
}
if appErr := p.API.SetProfileImage(botUserID, profileImage); appErr != nil {
return errors.Wrap(appErr, "couldn't set profile image")
}
p.jwtClient = zoom.NewJWTClient(p.getZoomAPIURL(), config.APIKey, config.APISecret)
p.telemetryClient, err = telemetry.NewRudderClient()
if err != nil {
p.API.LogWarn("telemetry client not started", "error", err.Error())
}
return nil
}
func (p *Plugin) OnDeactivate() error {
if p.telemetryClient != nil {
err := p.telemetryClient.Close()
if err != nil {
p.API.LogWarn("OnDeactivate: failed to close telemetryClient", "error", err.Error())
}
}
return nil
}
// registerSiteURL fetches the site URL and sets it in the plugin object.
func (p *Plugin) registerSiteURL() error {
siteURL := p.API.GetConfig().ServiceSettings.SiteURL
if siteURL == nil || *siteURL == "" {
return errors.New("could not fetch siteURL")
}
p.siteURL = *siteURL
return nil
}
// getActiveClient returns an OAuth Zoom client if available, otherwise it returns the API client.
func (p *Plugin) getActiveClient(user *model.User) (Client, string, error) {
config := p.getConfiguration()
// JWT
if !p.OAuthEnabled() {
return p.jwtClient, "", nil
}
// OAuth Account Level
if config.AccountLevelApp {
message := "Zoom App not connected. Contact your System administrator."
token, err := p.getSuperuserToken()
if user.IsSystemAdmin() {
message = fmt.Sprintf(zoom.OAuthPrompt, p.siteURL)
}
if err != nil {
return nil, message, errors.Wrap(err, "could not get token")
}
if token == nil {
return nil, message, errors.New("zoom app not connected")
}
return zoom.NewOAuthClient(token, p.getOAuthConfig(), p.siteURL, p.getZoomAPIURL(), true, p), "", nil
}
// Oauth User Level
message := fmt.Sprintf(zoom.OAuthPrompt, p.siteURL)
info, err := p.fetchOAuthUserInfo(zoomUserByMMID, user.Id)
if err != nil {
return nil, message, errors.Wrap(err, "could not fetch Zoom OAuth info")
}
conf := p.getOAuthConfig()
return zoom.NewOAuthClient(info.OAuthToken, conf, p.siteURL, p.getZoomAPIURL(), false, p), "", nil
}
// getOAuthConfig returns the Zoom OAuth2 flow configuration.
func (p *Plugin) getOAuthConfig() *oauth2.Config {
config := p.getConfiguration()
zoomURL := p.getZoomURL()
return &oauth2.Config{
ClientID: config.OAuthClientID,
ClientSecret: config.OAuthClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf("%v/oauth/authorize", zoomURL),
TokenURL: fmt.Sprintf("%v/oauth/token", zoomURL),
},
RedirectURL: fmt.Sprintf("%s/plugins/zoom/oauth2/complete", p.siteURL),
}
}
// authenticateAndFetchZoomUser uses the active Zoom client to authenticate and return the Zoom user
func (p *Plugin) authenticateAndFetchZoomUser(user *model.User) (*zoom.User, *zoom.AuthError) {
zoomClient, message, err := p.getActiveClient(user)
if err != nil {
return nil, &zoom.AuthError{
Message: message,
Err: err,
}
}
firstConnect := false
return zoomClient.GetUser(user, firstConnect)
}
func (p *Plugin) sendDirectMessage(userID string, message string) error {
channel, err := p.API.GetDirectChannel(userID, p.botUserID)
if err != nil {
msg := fmt.Sprintf("could not get or create DM channel for bot with ID: %s", p.botUserID)
p.API.LogInfo(msg, "user_id", userID)
return errors.Wrap(err, msg)
}
post := &model.Post{
Message: message,
ChannelId: channel.Id,
UserId: p.botUserID,
}
_, err = p.API.CreatePost(post)
return err
}
func (p *Plugin) GetZoomSuperUserToken() (*oauth2.Token, error) {
token, err := p.getSuperuserToken()
if err != nil {
return nil, errors.Wrap(err, "could not get token")
}
if token == nil {
return nil, errors.New("zoom app not connected")
}
return token, nil
}
func (p *Plugin) SetZoomSuperUserToken(token *oauth2.Token) error {
err := p.setSuperUserToken(token)
if err != nil {
return errors.Wrap(err, "could not set token")
}
return nil
}
func (p *Plugin) GetZoomOAuthUserInfo(userID string) (*zoom.OAuthUserInfo, error) {
info, err := p.fetchOAuthUserInfo(zoomUserByMMID, userID)
if err != nil {
return nil, errors.Wrap(err, "could not get token")
}
if info == nil {
return nil, errors.New("zoom app not connected")
}
return info, nil
}
func (p *Plugin) UpdateZoomOAuthUserInfo(userID string, info *zoom.OAuthUserInfo) error {
if err := p.storeOAuthUserInfo(info); err != nil {
msg := "unable to update user token"
p.API.LogWarn(msg, "error", err.Error())
return errors.Wrap(err, msg)
}
return nil
}
func (p *Plugin) isCloudLicense() bool {
license := p.API.GetLicense()
return license != nil && license.Features != nil && license.Features.Cloud != nil && *license.Features.Cloud
}
func (p *Plugin) OAuthEnabled() bool {
config := p.getConfiguration()
if config.EnableOAuth {
return true
}
return p.isCloudLicense()
}