-
Notifications
You must be signed in to change notification settings - Fork 22
/
client.js
382 lines (314 loc) · 14.1 KB
/
client.js
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
import {Client4} from 'mattermost-redux/client';
import {ClientError} from 'mattermost-redux/client/client4';
import AuthenticationContext from 'adal-angular';
import {isDesktopApp} from '../utils/user_utils';
import {Periods} from '../constants';
import {id as pluginID} from '../manifest';
// workaround for the "Token renewal operation failed due to timeout" issue
// https://github.com/AzureAD/azure-activedirectory-library-for-js/issues/391#issuecomment-384784134
// eslint-disable-next-line no-underscore-dangle
AuthenticationContext.prototype._addAdalFrame = function _addAdalFrame(iframeId) {
if (typeof iframeId === 'undefined') {
return;
}
this.info('Add adal frame to document:' + iframeId);
let adalFrame = document.getElementById(iframeId);
const self = this;
const handleFrameCallback = () => {
if (adalFrame) {
self.handleWindowCallback(adalFrame.contentWindow.location.hash);
}
};
if (!adalFrame) {
if (document.createElement && document.documentElement &&
(window.opera || window.navigator.userAgent.indexOf('MSIE 5.0') === -1)) {
const ifr = document.createElement('iframe');
ifr.setAttribute('id', iframeId);
ifr.setAttribute('aria-hidden', 'true');
// added sandbox attribute to prevent site from reloading, you only need the token
ifr.setAttribute('sandbox', 'allow-same-origin');
ifr.addEventListener('load', handleFrameCallback, false);
ifr.style.visibility = 'hidden';
ifr.style.position = 'absolute';
ifr.style.width = '0px';
ifr.style.height = '0px';
ifr.style.border = 'none';
adalFrame = document.getElementsByTagName('body')[0].appendChild(ifr);
} else if (document.body && document.body.insertAdjacentHTML) {
document.body.insertAdjacentHTML(
'beforeEnd',
`<iframe name="${iframeId}" id="${iframeId}" style="display:none"></iframe>`,
);
}
if (window.frames && window.frames[iframeId]) {
adalFrame = window.frames[iframeId];
}
}
// eslint-disable-next-line consistent-return
return adalFrame;
};
// workaround for issues with the Desktop App
// eslint-disable-next-line no-underscore-dangle
AuthenticationContext.prototype._loginPopup = function _loginPopup(urlNavigate, resource, callback) {
const targetUrl = this.config.popupRedirectUrl + encodeURIComponent(urlNavigate + '&response_mode=form_post');
let popupWindow;
if (this.config.isDesktopApp) {
popupWindow = window.open(targetUrl, 'login', 'width=' + this.CONSTANTS.POPUP_WIDTH + ', height=' + this.CONSTANTS.POPUP_HEIGHT);
} else {
// eslint-disable-next-line no-underscore-dangle
popupWindow = this._openPopup(targetUrl, 'login', this.CONSTANTS.POPUP_WIDTH, this.CONSTANTS.POPUP_HEIGHT);
}
const loginCallback = callback || this.callback;
if (!this.config.isDesktopApp && popupWindow == null) {
const error = 'Error opening popup';
const errorDesc = 'Popup Window is null. This can happen if you are using IE';
// eslint-disable-next-line no-underscore-dangle
this._handlePopupError(loginCallback, resource, error, errorDesc, errorDesc);
return;
}
const pollTimer = setInterval(() => {
if (!this.config.isDesktopApp) {
// eslint-disable-next-line no-undefined
if (!popupWindow || popupWindow.closed || popupWindow.closed === undefined) {
const error = 'Popup Window closed';
const errorDesc = 'Popup Window closed by UI action/ Popup Window handle destroyed due to cross zone navigation in IE/Edge';
if (this.isAngular) {
// eslint-disable-next-line no-underscore-dangle
this._broadcast('adal:popUpClosed', errorDesc + this.CONSTANTS.RESOURCE_DELIMETER + error);
}
// eslint-disable-next-line no-underscore-dangle
this._handlePopupError(loginCallback, resource, error, errorDesc, errorDesc);
clearInterval(pollTimer);
return;
}
}
const {token, state} = this.config.getAuthenticationResult();
if (token) {
// eslint-disable-next-line no-underscore-dangle
const decodedToken = this._extractIdToken(token);
window.localStorage.setItem(this.CONSTANTS.STORAGE.IDTOKEN, token);
window.localStorage.setItem(this.CONSTANTS.STORAGE.STATE_LOGIN, decodedToken.upn);
this.getCachedUser();
this.handleWindowCallback('#access_token=' + token + '&state=' + state);
// eslint-disable-next-line no-underscore-dangle
this._loginInProgress = false;
// eslint-disable-next-line no-underscore-dangle
this._acquireTokenInProgress = false;
// eslint-disable-next-line no-underscore-dangle
this._openedWindows = [];
clearInterval(pollTimer);
}
});
if (this.config.isDesktopApp) {
setTimeout(() => clearInterval(pollTimer), Periods.FIVE_MINUTES_IN_MILISECONDS);
}
};
export default class Client {
setServerRoute(url) {
this.url = `${url}/plugins/${pluginID}/api/v1`;
this.autodiscoverServiceUrl = 'https://webdir.online.lync.com/autodiscover/autodiscoverservice.svc/root';
this.registerMeetingFromOnlineVersionUrl = `${this.url}/register_meeting_from_online_version`;
this.clientIdUrl = `${this.url}/client_id`;
this.createMeetingInServerVersionUrl = `${this.url}/create_meeting_in_server_version`;
this.productTypeUrl = `${this.url}/product_type`;
this.authUrl = `${this.url}/auth`;
this.redirectUrl = `${this.url}/auth_redirect`;
}
createMeeting = async (channelId, currentUserId, getAuthenticationResult, personal = true, topic = '') => {
let isServerVersion;
try {
isServerVersion = await this.isServerVersion();
} catch (error) {
throw new Error('Cannot connect with the server. Please try again later.');
}
try {
if (isServerVersion) {
await this.doCreateMeetingInServerVersion(channelId, personal);
} else {
await this.doCreateMeetingInOnlineVersion(channelId, currentUserId, getAuthenticationResult, personal, topic);
}
} catch (error) {
if (isServerVersion) {
throw new Error('An error occurred when creating the meeting. Please try again later.');
} else {
throw new Error('An error occurred when creating the meeting. Make sure your browser doesn\'t block pop-ups on this website, then try again.');
}
}
};
getClientId = async () => {
const response = await this.doGet(this.clientIdUrl);
return response.client_id;
};
doCreateMeetingInServerVersion = async (channelId, personal) => {
const body = {
channel_id: channelId,
personal,
};
const response = await this.doPost(this.createMeetingInServerVersionUrl, body);
return response;
};
doCreateMeetingInOnlineVersion = async (channelId, currentUserId, getAuthenticationResult, personal, topic) => {
const clientId = await this.getClientId();
this.authContext = new AuthenticationContext({
clientId,
popUp: true,
cacheLocation: 'localStorage',
callback: this.onUserSignedIn.bind(this),
navigateToLoginRequestUrl: false,
isDesktopApp: isDesktopApp(),
redirectUri: window.location.origin + this.redirectUrl,
popupRedirectUrl: this.authUrl + '?mattermost_user_id=' + currentUserId + '+&navigateTo=',
getAuthenticationResult,
});
await this.assureUserIsSignedIn();
const applicationsResourceHref = await this.getApplicationsHref(this.autodiscoverServiceUrl);
const applicationsResourceName = applicationsResourceHref.substring(0, applicationsResourceHref.indexOf('/ucwa'));
const accessTokenToApplicationResource = await this.getAccessTokenForResource(applicationsResourceName);
const myOnlineMeetingsHref = await this.getMyOnlineMeetingsHref(applicationsResourceHref, accessTokenToApplicationResource);
const url = applicationsResourceName + myOnlineMeetingsHref;
const {meetingId, meetingUrl} = await this.sendMeetingData(url, accessTokenToApplicationResource);
this.doPost(this.registerMeetingFromOnlineVersionUrl, {
channel_id: channelId,
personal,
topic,
meeting_id: meetingId,
metting_url: meetingUrl,
});
};
getApplicationsHref = async (autodiscoverServiceUrl) => {
const autodiscoverResponse = await this.doGet(autodiscoverServiceUrl, {}, 'omit');
// eslint-disable-next-line no-underscore-dangle
const userResourceHref = autodiscoverResponse._links.user.href;
const userResourceName = userResourceHref.substring(0, userResourceHref.indexOf('/Autodiscover'));
const accessTokenToUserResource = await this.getAccessTokenForResource(userResourceName);
const userResourceResponse = await this.doGet(userResourceHref, {Authorization: 'Bearer ' + accessTokenToUserResource}, 'omit');
// eslint-disable-next-line no-underscore-dangle
const links = userResourceResponse._links;
if (links.applications) {
return links.applications.href;
} else if (links.redirect) {
const applicationHref = await this.getApplicationsHref(links.redirect.href);
return applicationHref;
}
throw new Error('Unexpected response');
};
getMyOnlineMeetingsHref = async (oauthApplicationHref, accessToken) => {
const authorizationValue = 'Bearer ' + accessToken;
const endpointId = this.generateUuid4();
const data = {
UserAgent: 'mm-skype4b-plugin',
EndpointId: endpointId,
Culture: 'en-US',
};
const response = await this.doPost(oauthApplicationHref, data, {Authorization: authorizationValue}, 'omit');
if (response.endpointId !== endpointId) {
throw new Error('Endpoints don\'t match!');
}
// eslint-disable-next-line no-underscore-dangle
return response._embedded.onlineMeetings._links.myOnlineMeetings.href;
};
sendMeetingData = async (url, appAccessToken) => {
const data = {
subject: 'Meeting created by the Mattermost Skype for Business plugin',
automaticLeaderAssignment: 'SameEnterprise',
};
const response = await this.doPost(url, data, {Authorization: 'Bearer ' + appAccessToken});
return {
meetingId: response.onlineMeetingId,
meetingUrl: response.joinUrl,
};
};
doGet = async (url, headers = {}, credentials) => {
headers.Accept = 'application/json';
let options = {
method: 'get',
headers,
};
if (url.includes('plugins/' + pluginID)) {
options = Client4.getOptions(options);
}
if (credentials) {
options.credentials = credentials;
}
const response = await fetch(url, options);
if (response.ok) {
return response.json();
}
const text = await response.text();
throw new ClientError(Client4.url, {
message: text || '',
status_code: response.status,
url,
});
}
doPost = async (url, body, headers = {}) => {
headers.Accept = 'application/json';
headers['Content-Type'] = 'application/json';
let options = {
method: 'post',
body: JSON.stringify(body),
headers,
};
if (url.includes('plugins/' + pluginID)) {
options = Client4.getOptions(options);
}
const response = await fetch(url, options);
if (response.ok) {
return response.json();
}
const text = await response.text();
throw new ClientError(Client4.url, {
message: text || '',
status_code: response.status,
url,
});
};
generateUuid4 = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
// eslint-disable-next-line
let r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
getAccessTokenForResource = async (resourceName) => {
const authContext = this.authContext;
const self = this;
return new Promise((resolve, reject) => {
self.resolveIfSignedIn = resolve;
authContext.acquireToken(resourceName, (errorDesc, token, error) => {
if (error) {
authContext.acquireTokenPopup(resourceName, null, null, (errorDesc2, token2, error2) => {
if (error2) {
return reject(new Error(errorDesc2));
}
return resolve(token2);
});
}
return resolve(token);
});
});
};
assureUserIsSignedIn = async () => {
return new Promise((resolve, reject) => {
const user = this.authContext.getCachedUser();
if (user) {
resolve();
} else {
this.resolveIfSignedIn = resolve;
this.rejectIfNotSigned = reject;
this.authContext.login();
}
});
};
onUserSignedIn = (errorDesc, token, error) => {
if (error && this.rejectIfNotSigned) {
this.rejectIfNotSigned(new Error(errorDesc));
} else if (this.resolveIfSignedIn) {
this.resolveIfSignedIn(token);
}
};
isServerVersion = async () => {
const response = await this.doGet(this.productTypeUrl);
return response.product_type === 'server';
};
}