@or-sdk/auth 0.24.8 → 0.24.9-beta.1028.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/cjs/Auth.js +0 -616
  2. package/dist/cjs/Auth.js.map +1 -1
  3. package/dist/cjs/AuthBase.js +97 -0
  4. package/dist/cjs/AuthBase.js.map +1 -0
  5. package/dist/cjs/AuthCookieStorage.js +217 -0
  6. package/dist/cjs/AuthCookieStorage.js.map +1 -0
  7. package/dist/cjs/AuthForBackend.js +28 -0
  8. package/dist/cjs/AuthForBackend.js.map +1 -0
  9. package/dist/cjs/AuthForClient.js +402 -0
  10. package/dist/cjs/AuthForClient.js.map +1 -0
  11. package/dist/cjs/index.js +10 -3
  12. package/dist/cjs/index.js.map +1 -1
  13. package/dist/esm/Auth.js +1 -449
  14. package/dist/esm/Auth.js.map +1 -1
  15. package/dist/esm/AuthBase.js +42 -0
  16. package/dist/esm/AuthBase.js.map +1 -0
  17. package/dist/esm/AuthCookieStorage.js +145 -0
  18. package/dist/esm/AuthCookieStorage.js.map +1 -0
  19. package/dist/esm/AuthForBackend.js +7 -0
  20. package/dist/esm/AuthForBackend.js.map +1 -0
  21. package/dist/esm/AuthForClient.js +257 -0
  22. package/dist/esm/AuthForClient.js.map +1 -0
  23. package/dist/esm/index.js +5 -1
  24. package/dist/esm/index.js.map +1 -1
  25. package/dist/types/Auth.d.ts +0 -56
  26. package/dist/types/Auth.d.ts.map +1 -1
  27. package/dist/types/AuthBase.d.ts +14 -0
  28. package/dist/types/AuthBase.d.ts.map +1 -0
  29. package/dist/types/AuthCookieStorage.d.ts +40 -0
  30. package/dist/types/AuthCookieStorage.d.ts.map +1 -0
  31. package/dist/types/AuthForBackend.d.ts +6 -0
  32. package/dist/types/AuthForBackend.d.ts.map +1 -0
  33. package/dist/types/AuthForClient.d.ts +30 -0
  34. package/dist/types/AuthForClient.d.ts.map +1 -0
  35. package/dist/types/index.d.ts +5 -1
  36. package/dist/types/index.d.ts.map +1 -1
  37. package/dist/types/types.d.ts +20 -2
  38. package/dist/types/types.d.ts.map +1 -1
  39. package/package.json +2 -3
  40. package/src/Auth.ts +763 -763
  41. package/src/AuthBase.ts +44 -0
  42. package/src/AuthCookieStorage.ts +307 -0
  43. package/src/AuthForBackend.ts +8 -0
  44. package/src/AuthForClient.ts +426 -0
  45. package/src/index.ts +6 -1
  46. package/src/types.ts +22 -4
package/src/Auth.ts CHANGED
@@ -1,763 +1,763 @@
1
- import axios from 'axios';
2
- import { Settings } from '@or-sdk/settings';
3
- import { SdkApi } from '@or-sdk/sdk-api';
4
- import {
5
- AuthConfig,
6
- AuthResponse,
7
- Domain,
8
- HeadersObj,
9
- MultiUserLoginUserArgs,
10
- MultiUserResponse,
11
- MultiUserUpdateTwoFactorArgs,
12
- MultiUserUploadIconArgs,
13
- MultiUserUploadIconResponse,
14
- SaveUserArgs,
15
- SignInArgs,
16
- User,
17
- } from './types';
18
- import { generateFingerPrint, NoRightsError } from './utils';
19
- import { isNode } from 'browser-or-node';
20
- import Cookie from 'universal-cookie';
21
- import _ from 'lodash';
22
-
23
- const defaultCookiePollingInterval = 500; // 0.5 second
24
- const defaultExpireInShort = 3600000; // 1 hour
25
- const defaultExpireInLong = 30758400000;
26
-
27
- export class Auth {
28
- private readonly discoveryUrl?: string;
29
- private readonly sdkUrl?: string;
30
- private readonly cookieDomain?: string;
31
- private readonly cookie: Cookie;
32
- private readonly userCookieName: string;
33
- private readonly allowGuestLogin: boolean;
34
- private readonly allowIframe?: boolean;
35
- private readonly deprecatedCookieName: string;
36
- private readonly deprecatedUserExpireCookieName: string;
37
- private readonly deprecatedDomain: Domain;
38
- private readonly multiUserCookieName?: string;
39
- private readonly userExpireCookieName: string;
40
- private readonly cookiePollingInterval: number;
41
- private readonly expireInShort: number;
42
- private readonly expireInLong: number;
43
- private loggedIn = false;
44
- private monitorCookieTimeout?: ReturnType<typeof setTimeout>;
45
-
46
- constructor(params: AuthConfig) {
47
- const {
48
- discoveryUrl,
49
- sdkUrl,
50
- cookieDomain,
51
- allowGuestLogin,
52
- userCookieName,
53
- userExpireCookieName,
54
- cookiePrefix,
55
- multiUserCookieName,
56
- cookiePollingInterval,
57
- expireInShort,
58
- expireInLong,
59
- allowIframe,
60
- } = params;
61
-
62
- this.discoveryUrl = discoveryUrl;
63
- this.sdkUrl = sdkUrl;
64
-
65
- this.cookie = new Cookie();
66
-
67
- this.cookieDomain = cookieDomain;
68
- this.allowGuestLogin = Boolean(allowGuestLogin);
69
- this.allowIframe = allowIframe;
70
-
71
- // should be used for feature migration only. Should be deleted after all feature migrations happened!
72
- this.deprecatedCookieName = userCookieName || 'user';
73
- this.deprecatedUserExpireCookieName = userExpireCookieName || 'user_expire';
74
- this.deprecatedDomain = { domain: '.onereach.ai' };
75
-
76
- this.userCookieName = cookiePrefix ? cookiePrefix : `${userCookieName}_new`;
77
- this.multiUserCookieName = multiUserCookieName;
78
- this.userExpireCookieName = `${this.userCookieName}_expire`;
79
-
80
- this.cookiePollingInterval = cookiePollingInterval || defaultCookiePollingInterval;
81
- this.expireInShort = expireInShort ? +expireInShort : defaultExpireInShort;
82
- this.expireInLong = expireInLong ? +expireInLong : defaultExpireInLong;
83
- }
84
-
85
- public get domain(): Domain {
86
- return this.cookieDomain ? { domain: this.cookieDomain } : {};
87
- }
88
-
89
- private get _getUser(): User | null {
90
- return this.cookie.get(this.userCookieName) || null;
91
- }
92
-
93
- /**
94
- * Get user expire
95
- * ```typescript
96
- * const result = instance.getUserExpire();
97
- * ```
98
- */
99
- public getUserExpire(cookieName?: string): number {
100
- const expireCookie = this.cookie.get(cookieName || this.userExpireCookieName);
101
- return expireCookie ? parseInt(expireCookie, 10) : this.expireInShort;
102
- }
103
-
104
- private _getNextExpiration(expireIn: number): Date {
105
- return new Date(Date.now() + expireIn);
106
- }
107
-
108
- /**
109
- * Stop cookie monitor
110
- * ```typescript
111
- * instance.stopCookieMonitor();
112
- * ```
113
- */
114
- public stopCookieMonitor(): void {
115
- if (window) {
116
- clearTimeout(Number(this.monitorCookieTimeout));
117
- }
118
- }
119
-
120
- /**
121
- * Start cookie monitor
122
- * ```typescript
123
- * instance.monitorCookie(logoutCallback, loginCallback);
124
- * ```
125
- */
126
- public monitorCookie(logoutCallback?: () => void, loginCallback?: (user: User) => void): void {
127
- // just in case monitorCookie called multiple times in a row
128
- // we only need one running constantly
129
- this.stopCookieMonitor();
130
- this.monitorCookieTimeout = setTimeout(() => {
131
- const user = this._getUser;
132
- if (user) {
133
- if (!this.loggedIn) {
134
- this.loggedIn = true;
135
- }
136
- if (_.isFunction(loginCallback)) { loginCallback(user); }
137
- } else if (this.loggedIn) {
138
- this.loggedIn = false;
139
- if (_.isFunction(logoutCallback)) { logoutCallback(); }
140
- }
141
-
142
- this.monitorCookie(logoutCallback, loginCallback);
143
- }, this.cookiePollingInterval);
144
- }
145
-
146
- private _saveCookies(user: User, expireIn: number): void {
147
- const nextExpiration = this._getNextExpiration(expireIn);
148
- this.cookie.set(this.userCookieName, user, {
149
- path: '/',
150
- expires: nextExpiration,
151
- secure: true,
152
- sameSite: this.allowIframe ? 'none' : 'lax',
153
- ...this.domain,
154
- });
155
- this.cookie.set(this.userExpireCookieName, expireIn, {
156
- path: '/',
157
- expires: nextExpiration,
158
- secure: true,
159
- sameSite: this.allowIframe ? 'none' : 'lax',
160
- ...this.domain,
161
- });
162
- // Should be deleted after migration
163
- this.cookie.set(this.deprecatedCookieName, user, {
164
- path: '/',
165
- expires: nextExpiration,
166
- ...this.domain,
167
- });
168
- this.cookie.set(this.deprecatedUserExpireCookieName, expireIn, {
169
- path: '/',
170
- expires: nextExpiration,
171
- ...this.domain,
172
- });
173
- }
174
-
175
- /**
176
- * Save multi-user
177
- * ```typescript
178
- * instance.saveMultiUser(user, expiration);
179
- * ```
180
- */
181
- public saveMultiUser(user: User, expiration: number): void {
182
- this.cookie.set(this.multiUserCookieName!, user, {
183
- path: '/',
184
- expires: this._getNextExpiration(expiration),
185
- secure: true,
186
- ...this.domain,
187
- });
188
- }
189
-
190
- /**
191
- * Update cookie expiration
192
- * ```typescript
193
- * instance.updateCookieExpiration();
194
- * ```
195
- */
196
- public updateCookieExpiration(): void {
197
- const user = this._getUser;
198
- const expire = this.getUserExpire();
199
- if (user) {
200
- this._saveCookies(user, expire);
201
- }
202
- const multiUser = this.cookie.get(this.multiUserCookieName!);
203
- if (multiUser) {
204
- this.saveMultiUser(multiUser, expire);
205
- }
206
- }
207
-
208
- private _validateGuest(role: string, allowGuestLoginOverride = false): void {
209
- if (!(this.allowGuestLogin || allowGuestLoginOverride) && _.toLower(role) === 'guest') {
210
- throw new NoRightsError('Authorization for guests is not allowed');
211
- }
212
- }
213
-
214
- /**
215
- * Save user
216
- * ```typescript
217
- * await instance.saveUser({
218
- * long: true,
219
- * user: user
220
- * });
221
- * ```
222
- */
223
- public async saveUser({ long, user }: SaveUserArgs): Promise<void> {
224
- const key = long ? 'cookieExpirationLong' : 'cookieExpirationShort';
225
- const settingsApi = new Settings({
226
- token: user.token!,
227
- discoveryUrl: this.discoveryUrl,
228
- sdkUrl: this.sdkUrl,
229
- });
230
-
231
- let cookieExpirationDuration;
232
- try {
233
- cookieExpirationDuration = await settingsApi.getMergedSettings({ key });
234
- } catch (error) {
235
- // eslint-disable-next-line no-console
236
- console.log('Unable to fetch settingCookiesExpire from user Settings: ', error);
237
- }
238
-
239
- if (!cookieExpirationDuration) {
240
- cookieExpirationDuration = long ? this.expireInLong : this.expireInShort;
241
- }
242
-
243
- this._saveCookies(user, cookieExpirationDuration);
244
- }
245
-
246
- /**
247
- * Sign in
248
- * ```typescript
249
- * const user = await instance.signIn({
250
- * credentials: {
251
- * username: 'username',
252
- * password: 'password'
253
- * },
254
- * long: true,
255
- * captchaToken: 'captchaToken'
256
- * });
257
- * ```
258
- */
259
- public async signIn({
260
- credentials,
261
- long,
262
- allowGuestLogin = false,
263
- captchaToken,
264
- isCaptchaV2Checkbox,
265
- verificationCode,
266
- userToken,
267
- rememberTwoFactor,
268
- }: SignInArgs): Promise<User> {
269
- const fingerPrint = await generateFingerPrint();
270
-
271
- const options = {
272
- ...credentials,
273
- captchaToken,
274
- isCaptchaV2Checkbox,
275
- verificationCode,
276
- fingerPrint,
277
- rememberTwoFactor,
278
- ... userToken ? { userToken } : {},
279
- };
280
-
281
- const sdkApi = new SdkApi({
282
- token: '',
283
- discoveryUrl: this.discoveryUrl,
284
- sdkUrl: this.sdkUrl,
285
- });
286
-
287
- const user = await sdkApi.makeRequest<AuthResponse>({
288
- method: 'POST',
289
- route: '/auth/token',
290
- data: options,
291
- });
292
-
293
- this._validateGuest(user.role!, allowGuestLogin);
294
-
295
- if (!user.twoFactorCheck && !user.captchaCheck) {
296
- if (user.tokenType === 'multi-user') {
297
- this.saveMultiUser(user, long ? this.expireInLong : this.expireInShort);
298
- } else {
299
- await this.saveUser({
300
- long,
301
- user,
302
- });
303
- }
304
- }
305
-
306
- return user;
307
- }
308
-
309
- private _checkDeprecatedCookies(): void {
310
- const user = this.cookie.get(this.deprecatedCookieName);
311
- if (user) {
312
- const deprecatedUserExpireValue = this.getUserExpire(this.deprecatedUserExpireCookieName);
313
- const nextExpiration = this._getNextExpiration(deprecatedUserExpireValue);
314
- this.cookie.set(this.userCookieName, user, {
315
- path: '/',
316
- expires: nextExpiration,
317
- secure: true,
318
- ...this.domain,
319
- });
320
- this.cookie.set(this.userExpireCookieName, deprecatedUserExpireValue, {
321
- path: '/',
322
- expires: nextExpiration,
323
- secure: true,
324
- ...this.domain,
325
- });
326
- }
327
- }
328
-
329
- private async _validateToken(token: string, allowGuestLogin = false): Promise<User> {
330
- const sdkApi = new SdkApi({
331
- token,
332
- discoveryUrl: this.discoveryUrl,
333
- sdkUrl: this.sdkUrl,
334
- });
335
-
336
- const user = await sdkApi.makeRequest<User>({
337
- method: 'GET',
338
- route: '/auth/token',
339
- });
340
-
341
- this._validateGuest(user.role, allowGuestLogin);
342
-
343
- return user;
344
- }
345
-
346
- /**
347
- * Sign in with token
348
- * ```typescript
349
- * const user = await instance.signInWithToken({
350
- * token: 'token'
351
- * });
352
- * ```
353
- */
354
- public async signInWithToken(
355
- token: string | { token: string; allowGuestLogin?: boolean; shouldNotSaveCookies?: boolean; },
356
- allowGuestLogin = false,
357
- shouldNotSaveCookies = false
358
- ): Promise<User> {
359
- let userInfo = {};
360
- if (_.isObject(token)) {
361
- allowGuestLogin = _.get(token, 'allowGuestLogin', false);
362
- shouldNotSaveCookies = _.get(token, 'shouldNotSaveCookies', false);
363
- userInfo = _.get(token, 'userInfo', {});
364
- token = token.token;
365
- }
366
-
367
- if (!token) {
368
- return Promise.reject(new Error('No token given'));
369
- }
370
-
371
- this._checkDeprecatedCookies();
372
-
373
- const user = await this._validateToken(token, allowGuestLogin);
374
-
375
- _.assign(user, { token });
376
-
377
- const allow = _.get(user, 'allow', false);
378
- if (!allow) {
379
- throw Error('Invalid Token');
380
- }
381
-
382
- if (!shouldNotSaveCookies) {
383
- this._saveCookies({
384
- ...userInfo,
385
- ...user,
386
- }, this.getUserExpire());
387
- }
388
-
389
- return user;
390
- }
391
-
392
- public get multiUserHeaders(): HeadersObj {
393
- const { token = '' } = this.cookie.get(this.multiUserCookieName!) || {};
394
- return { headers: { Authorization: token } };
395
- }
396
-
397
- /**
398
- * Remove cookies
399
- * ```typescript
400
- * instance.removeCookies();
401
- * ```
402
- */
403
- public removeCookies(): void {
404
- this.cookie.remove(this.userExpireCookieName, {
405
- path: '/',
406
- ...this.domain,
407
- });
408
- this.cookie.remove(this.userCookieName, {
409
- path: '/',
410
- ...this.domain,
411
- });
412
- this.cookie.remove(this.multiUserCookieName!, {
413
- path: '/',
414
- ...this.domain,
415
- });
416
- // should be removed after feature migration only.
417
- this.cookie.remove(this.deprecatedCookieName, {
418
- path: '/',
419
- ...this.domain,
420
- });
421
- this.cookie.remove(this.deprecatedUserExpireCookieName, {
422
- path: '/',
423
- ...this.domain,
424
- });
425
- this.cookie.remove(this.deprecatedCookieName, {
426
- path: '/',
427
- ...this.domain,
428
- });
429
- this.cookie.remove(this.deprecatedUserExpireCookieName, {
430
- path: '/',
431
- ...this.domain,
432
- });
433
- }
434
-
435
- /**
436
- * Sign out
437
- * ```typescript
438
- * await instance.signOut('token');
439
- * ```
440
- */
441
- public async signOut(token: string): Promise<void> {
442
- // TODO add {secure : true}
443
- try {
444
- const promises = [];
445
- if (token) {
446
- const sdkApi = new SdkApi({
447
- token,
448
- discoveryUrl: this.discoveryUrl,
449
- sdkUrl: this.sdkUrl,
450
- });
451
-
452
- promises.push(sdkApi.makeRequest<void>({
453
- method: 'DELETE',
454
- route: '/auth/fingerprint-token',
455
- }));
456
- }
457
- if (this.multiUserHeaders.headers.Authorization) {
458
-
459
- const sdkApi = new SdkApi({
460
- token: this.multiUserHeaders.headers.Authorization,
461
- discoveryUrl: this.discoveryUrl,
462
- sdkUrl: this.sdkUrl,
463
- });
464
-
465
- promises.push(sdkApi.makeRequest<void>({
466
- method: 'DELETE',
467
- route: '/multi-user/fingerprint-token',
468
- }));
469
- }
470
- await Promise.all(promises);
471
- } catch (error) {
472
- // eslint-disable-next-line no-console
473
- console.log('Logout failed: ', error);
474
- }
475
- this.removeCookies();
476
- }
477
-
478
- /**
479
- * Validate user
480
- * ```typescript
481
- * const user = await instance.validateUser();
482
- * ```
483
- */
484
- public async validateUser(allowGuestLogin = false): Promise<User> {
485
- this._checkDeprecatedCookies();
486
- const userParams = this._getUser;
487
-
488
- if (!userParams) {
489
- return Promise.reject(new Error('No cached user params are available'));
490
- }
491
-
492
- const user = await this._validateToken(userParams.token!, allowGuestLogin);
493
-
494
- if (!user.identityProvider && userParams.identityProvider) {
495
- user.identityProvider = userParams.identityProvider;
496
- }
497
- _.assign(user, { token: userParams.token });
498
-
499
- const allow = _.get(user, 'allow', false);
500
- if (!allow) {
501
- throw Error('Invalid Token');
502
- }
503
-
504
- return user;
505
- }
506
-
507
- /**
508
- * Check existence of a user cookie
509
- * ```typescript
510
- * const result = instance.hasUserParams();
511
- * ```
512
- */
513
- public hasUserParams(): boolean {
514
- return Boolean(this._getUser);
515
- }
516
-
517
- /**
518
- * Get users list of a multi-user
519
- * ```typescript
520
- * const result = await instance.multiUserGetUsersList();
521
- * ```
522
- */
523
- public async multiUserGetUsersList(): Promise<any> {
524
- if (!this.multiUserHeaders.headers.Authorization) return false;
525
-
526
- const sdkApi = new SdkApi({
527
- token: this.multiUserHeaders.headers.Authorization,
528
- discoveryUrl: this.discoveryUrl,
529
- sdkUrl: this.sdkUrl,
530
- });
531
-
532
- return sdkApi.makeRequest<any>({
533
- method: 'GET',
534
- route: '/multi-user/list-users',
535
- });
536
- }
537
-
538
- /**
539
- * Log in user of a multi-user
540
- * ```typescript
541
- * const result = await instance.multiUserLoginUser({
542
- * accountId: 'account-id',
543
- * id: 'id',
544
- * long: true
545
- * });
546
- * ```
547
- */
548
- public async multiUserLoginUser({ accountId, id, long }: MultiUserLoginUserArgs): Promise<User> {
549
- const sdkApi = new SdkApi({
550
- token: this.multiUserHeaders.headers.Authorization,
551
- discoveryUrl: this.discoveryUrl,
552
- sdkUrl: this.sdkUrl,
553
- });
554
-
555
- const user = await sdkApi.makeRequest<User>({
556
- method: 'POST',
557
- route: '/multi-user/user-token',
558
- data: {
559
- accountId,
560
- id,
561
- },
562
- });
563
-
564
- await this.saveUser({
565
- long,
566
- user,
567
- });
568
-
569
- return user;
570
- }
571
-
572
- /**
573
- * Get username for lock screen
574
- * ```typescript
575
- * const username = await instance.getUserNameForLockScreen(user);
576
- * ```
577
- */
578
- public async getUserNameForLockScreen(user: User): Promise<string> {
579
- const multiUserCookie = this.cookie.get(this.multiUserCookieName!) || {};
580
- if (multiUserCookie.username) return multiUserCookie.username;
581
-
582
- const { multiUserId, username, token } = user;
583
- if (multiUserId) {
584
- const sdkApi = new SdkApi({
585
- token: token!,
586
- discoveryUrl: this.discoveryUrl,
587
- sdkUrl: this.sdkUrl,
588
- });
589
-
590
- const { username } = await sdkApi.makeRequest<MultiUserResponse>({
591
- method: 'GET',
592
- route: '/user/multi-user',
593
- });
594
-
595
- return username;
596
-
597
- }
598
- return username;
599
- }
600
-
601
- /**
602
- * Get profile for multi-user
603
- * ```typescript
604
- * const result = await instance.multiUserGetProfile();
605
- * ```
606
- */
607
- public async multiUserGetProfile(): Promise<any> {
608
- const sdkApi = new SdkApi({
609
- token: this.multiUserHeaders.headers.Authorization,
610
- discoveryUrl: this.discoveryUrl,
611
- sdkUrl: this.sdkUrl,
612
- });
613
-
614
- return sdkApi.makeRequest<any>({
615
- method: 'GET',
616
- route: '/multi-user/profile',
617
- });
618
- }
619
-
620
- /**
621
- * Set profile for multi-user
622
- * ```typescript
623
- * const knowledgeModels = await instance.multiUserSetProfile(profile);
624
- * ```
625
- */
626
- public async multiUserSetProfile(profile: unknown): Promise<any> {
627
- const sdkApi = new SdkApi({
628
- token: this.multiUserHeaders.headers.Authorization,
629
- discoveryUrl: this.discoveryUrl,
630
- sdkUrl: this.sdkUrl,
631
- });
632
-
633
- return sdkApi.makeRequest<any>({
634
- method: 'POST',
635
- route: '/multi-user/profile',
636
- data: profile,
637
- });
638
- }
639
-
640
- /**
641
- * Validate multi-user token
642
- * ```typescript
643
- * const result = await instance.validateMultiUserToken();
644
- * ```
645
- */
646
- public async validateMultiUserToken(): Promise<any> {
647
- const sdkApi = new SdkApi({
648
- token: this.multiUserHeaders.headers.Authorization,
649
- discoveryUrl: this.discoveryUrl,
650
- sdkUrl: this.sdkUrl,
651
- });
652
-
653
- return sdkApi.makeRequest<any>({
654
- method: 'GET',
655
- route: '/multi-user/token',
656
- });
657
- }
658
-
659
- /**
660
- * Upload icon for multi-user
661
- * ```typescript
662
- * const downloadUrl = await instance.multiUserUploadIcon({
663
- * name: 'name',
664
- * contentType: 'content-type',
665
- * file: file
666
- * });
667
- * ```
668
- */
669
- public async multiUserUploadIcon({ name, contentType, cacheControl = 'no-cache', file }: MultiUserUploadIconArgs): Promise<string> {
670
- const sdkApi = new SdkApi({
671
- token: this.multiUserHeaders.headers.Authorization,
672
- discoveryUrl: this.discoveryUrl,
673
- sdkUrl: this.sdkUrl,
674
- });
675
-
676
- const data = await sdkApi.makeRequest<MultiUserUploadIconResponse>({
677
- method: 'POST',
678
- route: '/multi-user/sign-upload-url',
679
- data: {
680
- name,
681
- contentType,
682
- cacheControl,
683
- },
684
- });
685
-
686
- const FormDataLib = isNode ? require('form-data') : FormData;
687
- const formData = new FormDataLib();
688
-
689
- _.forEach(data.uploadParams.fields, (value, key) => {
690
- formData.append(key, value);
691
- });
692
-
693
- formData.append('cache-control', cacheControl);
694
- formData.append('content-type', contentType);
695
- formData.append('File', file, name);
696
-
697
- if (isNode) {
698
- await new Promise<void>((resolve, reject) => {
699
- formData.submit(data.uploadParams.url, (error: any) => {
700
- if (error) return reject(error);
701
- resolve();
702
- });
703
- });
704
- } else {
705
- await axios.post(data.uploadParams.url, formData);
706
- }
707
-
708
- return data.downloadUrl;
709
- }
710
-
711
- /**
712
- * Confirm email change
713
- * ```typescript
714
- * const result = await instance.confirmEmailChange({ token: 'token' });
715
- * ```
716
- */
717
- public async confirmEmailChange({ token }: { token: string; }): Promise<any> {
718
- const sdkApi = new SdkApi({
719
- token: '',
720
- discoveryUrl: this.discoveryUrl,
721
- sdkUrl: this.sdkUrl,
722
- });
723
-
724
- return sdkApi.makeRequest<any>({
725
- method: 'POST',
726
- route: '/multi-user/change-email-finish',
727
- data: {
728
- token,
729
- },
730
- });
731
- }
732
-
733
- /**
734
- * Update 2fa for multi-user
735
- * ```typescript
736
- * const result = await instance.multiUserUpdateTwoFactor({
737
- * secret: 'secret',
738
- * enabled: true,
739
- * codes: codes,
740
- * verificationCode: 'verification-code'
741
- * });
742
- * ```
743
- */
744
- public async multiUserUpdateTwoFactor({ secret, enabled, codes, verificationCode }: MultiUserUpdateTwoFactorArgs): Promise<any> {
745
- const sdkApi = new SdkApi({
746
- token: this.multiUserHeaders.headers.Authorization,
747
- discoveryUrl: this.discoveryUrl,
748
- sdkUrl: this.sdkUrl,
749
- });
750
-
751
- return sdkApi.makeRequest<any>({
752
- method: 'POST',
753
- route: '/multi-user/twofactor',
754
- data: {
755
- secret,
756
- enabled,
757
- codes,
758
- verificationCode,
759
- },
760
- });
761
- }
762
-
763
- }
1
+ // import axios from 'axios';
2
+ // import { Settings } from '@or-sdk/settings';
3
+ // import { SdkApi } from '@or-sdk/sdk-api';
4
+ // import {
5
+ // AuthConfig,
6
+ // AuthResponse,
7
+ // Domain,
8
+ // HeadersObj,
9
+ // MultiUserLoginUserArgs,
10
+ // MultiUserResponse,
11
+ // MultiUserUpdateTwoFactorArgs,
12
+ // MultiUserUploadIconArgs,
13
+ // MultiUserUploadIconResponse,
14
+ // SaveUserArgs,
15
+ // SignInArgs,
16
+ // User,
17
+ // } from './types';
18
+ // import { generateFingerPrint, NoRightsError } from './utils';
19
+ // import { isNode } from 'browser-or-node';
20
+ // import Cookie from 'universal-cookie';
21
+ // import _ from 'lodash';
22
+ //
23
+ // const defaultCookiePollingInterval = 500; // 0.5 second
24
+ // const defaultExpireInShort = 3600000; // 1 hour
25
+ // const defaultExpireInLong = 30758400000;
26
+ //
27
+ // export class Auth {
28
+ // private readonly discoveryUrl?: string;
29
+ // private readonly sdkUrl?: string;
30
+ // private readonly cookieDomain?: string;
31
+ // private readonly cookie: Cookie;
32
+ // private readonly userCookieName: string;
33
+ // private readonly allowGuestLogin: boolean;
34
+ // private readonly allowIframe?: boolean;
35
+ // private readonly deprecatedCookieName: string;
36
+ // private readonly deprecatedUserExpireCookieName: string;
37
+ // private readonly deprecatedDomain: Domain;
38
+ // private readonly multiUserCookieName?: string;
39
+ // private readonly userExpireCookieName: string;
40
+ // private readonly cookiePollingInterval: number;
41
+ // private readonly expireInShort: number;
42
+ // private readonly expireInLong: number;
43
+ // private loggedIn = false;
44
+ // private monitorCookieTimeout?: ReturnType<typeof setTimeout>;
45
+ //
46
+ // constructor(params: AuthConfig) {
47
+ // const {
48
+ // discoveryUrl,
49
+ // sdkUrl,
50
+ // cookieDomain,
51
+ // allowGuestLogin,
52
+ // userCookieName,
53
+ // userExpireCookieName,
54
+ // cookiePrefix,
55
+ // multiUserCookieName,
56
+ // cookiePollingInterval,
57
+ // expireInShort,
58
+ // expireInLong,
59
+ // allowIframe,
60
+ // } = params;
61
+ //
62
+ // this.discoveryUrl = discoveryUrl;
63
+ // this.sdkUrl = sdkUrl;
64
+ //
65
+ // this.cookie = new Cookie();
66
+ //
67
+ // this.cookieDomain = cookieDomain;
68
+ // this.allowGuestLogin = Boolean(allowGuestLogin);
69
+ // this.allowIframe = allowIframe;
70
+ //
71
+ // // should be used for feature migration only. Should be deleted after all feature migrations happened!
72
+ // this.deprecatedCookieName = userCookieName || 'user';
73
+ // this.deprecatedUserExpireCookieName = userExpireCookieName || 'user_expire';
74
+ // this.deprecatedDomain = { domain: '.onereach.ai' };
75
+ //
76
+ // this.userCookieName = cookiePrefix ? cookiePrefix : `${userCookieName}_new`;
77
+ // this.multiUserCookieName = multiUserCookieName;
78
+ // this.userExpireCookieName = `${this.userCookieName}_expire`;
79
+ //
80
+ // this.cookiePollingInterval = cookiePollingInterval || defaultCookiePollingInterval;
81
+ // this.expireInShort = expireInShort ? +expireInShort : defaultExpireInShort;
82
+ // this.expireInLong = expireInLong ? +expireInLong : defaultExpireInLong;
83
+ // }
84
+ //
85
+ // public get domain(): Domain {
86
+ // return this.cookieDomain ? { domain: this.cookieDomain } : {};
87
+ // }
88
+ //
89
+ // private get _getUser(): User | null {
90
+ // return this.cookie.get(this.userCookieName) || null;
91
+ // }
92
+ //
93
+ // /**
94
+ // * Get user expire
95
+ // * ```typescript
96
+ // * const result = instance.getUserExpire();
97
+ // * ```
98
+ // */
99
+ // public getUserExpire(cookieName?: string): number {
100
+ // const expireCookie = this.cookie.get(cookieName || this.userExpireCookieName);
101
+ // return expireCookie ? parseInt(expireCookie, 10) : this.expireInShort;
102
+ // }
103
+ //
104
+ // private _getNextExpiration(expireIn: number): Date {
105
+ // return new Date(Date.now() + expireIn);
106
+ // }
107
+ //
108
+ // /**
109
+ // * Stop cookie monitor
110
+ // * ```typescript
111
+ // * instance.stopCookieMonitor();
112
+ // * ```
113
+ // */
114
+ // public stopCookieMonitor(): void {
115
+ // if (window) {
116
+ // clearTimeout(Number(this.monitorCookieTimeout));
117
+ // }
118
+ // }
119
+ //
120
+ // /**
121
+ // * Start cookie monitor
122
+ // * ```typescript
123
+ // * instance.monitorCookie(logoutCallback, loginCallback);
124
+ // * ```
125
+ // */
126
+ // public monitorCookie(logoutCallback?: () => void, loginCallback?: (user: User) => void): void {
127
+ // // just in case monitorCookie called multiple times in a row
128
+ // // we only need one running constantly
129
+ // this.stopCookieMonitor();
130
+ // this.monitorCookieTimeout = setTimeout(() => {
131
+ // const user = this._getUser;
132
+ // if (user) {
133
+ // if (!this.loggedIn) {
134
+ // this.loggedIn = true;
135
+ // }
136
+ // if (_.isFunction(loginCallback)) { loginCallback(user); }
137
+ // } else if (this.loggedIn) {
138
+ // this.loggedIn = false;
139
+ // if (_.isFunction(logoutCallback)) { logoutCallback(); }
140
+ // }
141
+ //
142
+ // this.monitorCookie(logoutCallback, loginCallback);
143
+ // }, this.cookiePollingInterval);
144
+ // }
145
+ //
146
+ // private _saveCookies(user: User, expireIn: number): void {
147
+ // const nextExpiration = this._getNextExpiration(expireIn);
148
+ // this.cookie.set(this.userCookieName, user, {
149
+ // path: '/',
150
+ // expires: nextExpiration,
151
+ // secure: true,
152
+ // sameSite: this.allowIframe ? 'none' : 'lax',
153
+ // ...this.domain,
154
+ // });
155
+ // this.cookie.set(this.userExpireCookieName, expireIn, {
156
+ // path: '/',
157
+ // expires: nextExpiration,
158
+ // secure: true,
159
+ // sameSite: this.allowIframe ? 'none' : 'lax',
160
+ // ...this.domain,
161
+ // });
162
+ // // Should be deleted after migration
163
+ // this.cookie.set(this.deprecatedCookieName, user, {
164
+ // path: '/',
165
+ // expires: nextExpiration,
166
+ // ...this.domain,
167
+ // });
168
+ // this.cookie.set(this.deprecatedUserExpireCookieName, expireIn, {
169
+ // path: '/',
170
+ // expires: nextExpiration,
171
+ // ...this.domain,
172
+ // });
173
+ // }
174
+ //
175
+ // /**
176
+ // * Save multi-user
177
+ // * ```typescript
178
+ // * instance.saveMultiUser(user, expiration);
179
+ // * ```
180
+ // */
181
+ // public saveMultiUser(user: User, expiration: number): void {
182
+ // this.cookie.set(this.multiUserCookieName!, user, {
183
+ // path: '/',
184
+ // expires: this._getNextExpiration(expiration),
185
+ // secure: true,
186
+ // ...this.domain,
187
+ // });
188
+ // }
189
+ //
190
+ // /**
191
+ // * Update cookie expiration
192
+ // * ```typescript
193
+ // * instance.updateCookieExpiration();
194
+ // * ```
195
+ // */
196
+ // public updateCookieExpiration(): void {
197
+ // const user = this._getUser;
198
+ // const expire = this.getUserExpire();
199
+ // if (user) {
200
+ // this._saveCookies(user, expire);
201
+ // }
202
+ // const multiUser = this.cookie.get(this.multiUserCookieName!);
203
+ // if (multiUser) {
204
+ // this.saveMultiUser(multiUser, expire);
205
+ // }
206
+ // }
207
+ //
208
+ // private _validateGuest(role: string, allowGuestLoginOverride = false): void {
209
+ // if (!(this.allowGuestLogin || allowGuestLoginOverride) && _.toLower(role) === 'guest') {
210
+ // throw new NoRightsError('Authorization for guests is not allowed');
211
+ // }
212
+ // }
213
+ //
214
+ // /**
215
+ // * Save user
216
+ // * ```typescript
217
+ // * await instance.saveUser({
218
+ // * long: true,
219
+ // * user: user
220
+ // * });
221
+ // * ```
222
+ // */
223
+ // public async saveUser({ long, user }: SaveUserArgs): Promise<void> {
224
+ // const key = long ? 'cookieExpirationLong' : 'cookieExpirationShort';
225
+ // const settingsApi = new Settings({
226
+ // token: user.token!,
227
+ // discoveryUrl: this.discoveryUrl,
228
+ // sdkUrl: this.sdkUrl,
229
+ // });
230
+ //
231
+ // let cookieExpirationDuration;
232
+ // try {
233
+ // cookieExpirationDuration = await settingsApi.getMergedSettings({ key });
234
+ // } catch (error) {
235
+ // // eslint-disable-next-line no-console
236
+ // console.log('Unable to fetch settingCookiesExpire from user Settings: ', error);
237
+ // }
238
+ //
239
+ // if (!cookieExpirationDuration) {
240
+ // cookieExpirationDuration = long ? this.expireInLong : this.expireInShort;
241
+ // }
242
+ //
243
+ // this._saveCookies(user, cookieExpirationDuration);
244
+ // }
245
+ //
246
+ // /**
247
+ // * Sign in
248
+ // * ```typescript
249
+ // * const user = await instance.signIn({
250
+ // * credentials: {
251
+ // * username: 'username',
252
+ // * password: 'password'
253
+ // * },
254
+ // * long: true,
255
+ // * captchaToken: 'captchaToken'
256
+ // * });
257
+ // * ```
258
+ // */
259
+ // public async signIn({
260
+ // credentials,
261
+ // long,
262
+ // allowGuestLogin = false,
263
+ // captchaToken,
264
+ // isCaptchaV2Checkbox,
265
+ // verificationCode,
266
+ // userToken,
267
+ // rememberTwoFactor,
268
+ // }: SignInArgs): Promise<User> {
269
+ // const fingerPrint = await generateFingerPrint();
270
+ //
271
+ // const options = {
272
+ // ...credentials,
273
+ // captchaToken,
274
+ // isCaptchaV2Checkbox,
275
+ // verificationCode,
276
+ // fingerPrint,
277
+ // rememberTwoFactor,
278
+ // ... userToken ? { userToken } : {},
279
+ // };
280
+ //
281
+ // const sdkApi = new SdkApi({
282
+ // token: '',
283
+ // discoveryUrl: this.discoveryUrl,
284
+ // sdkUrl: this.sdkUrl,
285
+ // });
286
+ //
287
+ // const user = await sdkApi.makeRequest<AuthResponse>({
288
+ // method: 'POST',
289
+ // route: '/auth/token',
290
+ // data: options,
291
+ // });
292
+ //
293
+ // this._validateGuest(user.role!, allowGuestLogin);
294
+ //
295
+ // if (!user.twoFactorCheck && !user.captchaCheck) {
296
+ // if (user.tokenType === 'multi-user') {
297
+ // this.saveMultiUser(user, long ? this.expireInLong : this.expireInShort);
298
+ // } else {
299
+ // await this.saveUser({
300
+ // long,
301
+ // user,
302
+ // });
303
+ // }
304
+ // }
305
+ //
306
+ // return user;
307
+ // }
308
+ //
309
+ // private _checkDeprecatedCookies(): void {
310
+ // const user = this.cookie.get(this.deprecatedCookieName);
311
+ // if (user) {
312
+ // const deprecatedUserExpireValue = this.getUserExpire(this.deprecatedUserExpireCookieName);
313
+ // const nextExpiration = this._getNextExpiration(deprecatedUserExpireValue);
314
+ // this.cookie.set(this.userCookieName, user, {
315
+ // path: '/',
316
+ // expires: nextExpiration,
317
+ // secure: true,
318
+ // ...this.domain,
319
+ // });
320
+ // this.cookie.set(this.userExpireCookieName, deprecatedUserExpireValue, {
321
+ // path: '/',
322
+ // expires: nextExpiration,
323
+ // secure: true,
324
+ // ...this.domain,
325
+ // });
326
+ // }
327
+ // }
328
+ //
329
+ // private async _validateToken(token: string, allowGuestLogin = false): Promise<User> {
330
+ // const sdkApi = new SdkApi({
331
+ // token,
332
+ // discoveryUrl: this.discoveryUrl,
333
+ // sdkUrl: this.sdkUrl,
334
+ // });
335
+ //
336
+ // const user = await sdkApi.makeRequest<User>({
337
+ // method: 'GET',
338
+ // route: '/auth/token',
339
+ // });
340
+ //
341
+ // this._validateGuest(user.role, allowGuestLogin);
342
+ //
343
+ // return user;
344
+ // }
345
+ //
346
+ // /**
347
+ // * Sign in with token
348
+ // * ```typescript
349
+ // * const user = await instance.signInWithToken({
350
+ // * token: 'token'
351
+ // * });
352
+ // * ```
353
+ // */
354
+ // public async signInWithToken(
355
+ // token: string | { token: string; allowGuestLogin?: boolean; shouldNotSaveCookies?: boolean; },
356
+ // allowGuestLogin = false,
357
+ // shouldNotSaveCookies = false
358
+ // ): Promise<User> {
359
+ // let userInfo = {};
360
+ // if (_.isObject(token)) {
361
+ // allowGuestLogin = _.get(token, 'allowGuestLogin', false);
362
+ // shouldNotSaveCookies = _.get(token, 'shouldNotSaveCookies', false);
363
+ // userInfo = _.get(token, 'userInfo', {});
364
+ // token = token.token;
365
+ // }
366
+ //
367
+ // if (!token) {
368
+ // return Promise.reject(new Error('No token given'));
369
+ // }
370
+ //
371
+ // this._checkDeprecatedCookies();
372
+ //
373
+ // const user = await this._validateToken(token, allowGuestLogin);
374
+ //
375
+ // _.assign(user, { token });
376
+ //
377
+ // const allow = _.get(user, 'allow', false);
378
+ // if (!allow) {
379
+ // throw Error('Invalid Token');
380
+ // }
381
+ //
382
+ // if (!shouldNotSaveCookies) {
383
+ // this._saveCookies({
384
+ // ...userInfo,
385
+ // ...user,
386
+ // }, this.getUserExpire());
387
+ // }
388
+ //
389
+ // return user;
390
+ // }
391
+ //
392
+ // public get multiUserHeaders(): HeadersObj {
393
+ // const { token = '' } = this.cookie.get(this.multiUserCookieName!) || {};
394
+ // return { headers: { Authorization: token } };
395
+ // }
396
+ //
397
+ // /**
398
+ // * Remove cookies
399
+ // * ```typescript
400
+ // * instance.removeCookies();
401
+ // * ```
402
+ // */
403
+ // public removeCookies(): void {
404
+ // this.cookie.remove(this.userExpireCookieName, {
405
+ // path: '/',
406
+ // ...this.domain,
407
+ // });
408
+ // this.cookie.remove(this.userCookieName, {
409
+ // path: '/',
410
+ // ...this.domain,
411
+ // });
412
+ // this.cookie.remove(this.multiUserCookieName!, {
413
+ // path: '/',
414
+ // ...this.domain,
415
+ // });
416
+ // // should be removed after feature migration only.
417
+ // this.cookie.remove(this.deprecatedCookieName, {
418
+ // path: '/',
419
+ // ...this.domain,
420
+ // });
421
+ // this.cookie.remove(this.deprecatedUserExpireCookieName, {
422
+ // path: '/',
423
+ // ...this.domain,
424
+ // });
425
+ // this.cookie.remove(this.deprecatedCookieName, {
426
+ // path: '/',
427
+ // ...this.domain,
428
+ // });
429
+ // this.cookie.remove(this.deprecatedUserExpireCookieName, {
430
+ // path: '/',
431
+ // ...this.domain,
432
+ // });
433
+ // }
434
+ //
435
+ // /**
436
+ // * Sign out
437
+ // * ```typescript
438
+ // * await instance.signOut('token');
439
+ // * ```
440
+ // */
441
+ // public async signOut(token: string): Promise<void> {
442
+ // // TODO add {secure : true}
443
+ // try {
444
+ // const promises = [];
445
+ // if (token) {
446
+ // const sdkApi = new SdkApi({
447
+ // token,
448
+ // discoveryUrl: this.discoveryUrl,
449
+ // sdkUrl: this.sdkUrl,
450
+ // });
451
+ //
452
+ // promises.push(sdkApi.makeRequest<void>({
453
+ // method: 'DELETE',
454
+ // route: '/auth/fingerprint-token',
455
+ // }));
456
+ // }
457
+ // if (this.multiUserHeaders.headers.Authorization) {
458
+ //
459
+ // const sdkApi = new SdkApi({
460
+ // token: this.multiUserHeaders.headers.Authorization,
461
+ // discoveryUrl: this.discoveryUrl,
462
+ // sdkUrl: this.sdkUrl,
463
+ // });
464
+ //
465
+ // promises.push(sdkApi.makeRequest<void>({
466
+ // method: 'DELETE',
467
+ // route: '/multi-user/fingerprint-token',
468
+ // }));
469
+ // }
470
+ // await Promise.all(promises);
471
+ // } catch (error) {
472
+ // // eslint-disable-next-line no-console
473
+ // console.log('Logout failed: ', error);
474
+ // }
475
+ // this.removeCookies();
476
+ // }
477
+ //
478
+ // /**
479
+ // * Validate user
480
+ // * ```typescript
481
+ // * const user = await instance.validateUser();
482
+ // * ```
483
+ // */
484
+ // public async validateUser(allowGuestLogin = false): Promise<User> {
485
+ // this._checkDeprecatedCookies();
486
+ // const userParams = this._getUser;
487
+ //
488
+ // if (!userParams) {
489
+ // return Promise.reject(new Error('No cached user params are available'));
490
+ // }
491
+ //
492
+ // const user = await this._validateToken(userParams.token!, allowGuestLogin);
493
+ //
494
+ // if (!user.identityProvider && userParams.identityProvider) {
495
+ // user.identityProvider = userParams.identityProvider;
496
+ // }
497
+ // _.assign(user, { token: userParams.token });
498
+ //
499
+ // const allow = _.get(user, 'allow', false);
500
+ // if (!allow) {
501
+ // throw Error('Invalid Token');
502
+ // }
503
+ //
504
+ // return user;
505
+ // }
506
+ //
507
+ // /**
508
+ // * Check existence of a user cookie
509
+ // * ```typescript
510
+ // * const result = instance.hasUserParams();
511
+ // * ```
512
+ // */
513
+ // public hasUserParams(): boolean {
514
+ // return Boolean(this._getUser);
515
+ // }
516
+ //
517
+ // /**
518
+ // * Get users list of a multi-user
519
+ // * ```typescript
520
+ // * const result = await instance.multiUserGetUsersList();
521
+ // * ```
522
+ // */
523
+ // public async multiUserGetUsersList(): Promise<any> {
524
+ // if (!this.multiUserHeaders.headers.Authorization) return false;
525
+ //
526
+ // const sdkApi = new SdkApi({
527
+ // token: this.multiUserHeaders.headers.Authorization,
528
+ // discoveryUrl: this.discoveryUrl,
529
+ // sdkUrl: this.sdkUrl,
530
+ // });
531
+ //
532
+ // return sdkApi.makeRequest<any>({
533
+ // method: 'GET',
534
+ // route: '/multi-user/list-users',
535
+ // });
536
+ // }
537
+ //
538
+ // /**
539
+ // * Log in user of a multi-user
540
+ // * ```typescript
541
+ // * const result = await instance.multiUserLoginUser({
542
+ // * accountId: 'account-id',
543
+ // * id: 'id',
544
+ // * long: true
545
+ // * });
546
+ // * ```
547
+ // */
548
+ // public async multiUserLoginUser({ accountId, id, long }: MultiUserLoginUserArgs): Promise<User> {
549
+ // const sdkApi = new SdkApi({
550
+ // token: this.multiUserHeaders.headers.Authorization,
551
+ // discoveryUrl: this.discoveryUrl,
552
+ // sdkUrl: this.sdkUrl,
553
+ // });
554
+ //
555
+ // const user = await sdkApi.makeRequest<User>({
556
+ // method: 'POST',
557
+ // route: '/multi-user/user-token',
558
+ // data: {
559
+ // accountId,
560
+ // id,
561
+ // },
562
+ // });
563
+ //
564
+ // await this.saveUser({
565
+ // long,
566
+ // user,
567
+ // });
568
+ //
569
+ // return user;
570
+ // }
571
+ //
572
+ // /**
573
+ // * Get username for lock screen
574
+ // * ```typescript
575
+ // * const username = await instance.getUserNameForLockScreen(user);
576
+ // * ```
577
+ // */
578
+ // public async getUserNameForLockScreen(user: User): Promise<string> {
579
+ // const multiUserCookie = this.cookie.get(this.multiUserCookieName!) || {};
580
+ // if (multiUserCookie.username) return multiUserCookie.username;
581
+ //
582
+ // const { multiUserId, username, token } = user;
583
+ // if (multiUserId) {
584
+ // const sdkApi = new SdkApi({
585
+ // token: token!,
586
+ // discoveryUrl: this.discoveryUrl,
587
+ // sdkUrl: this.sdkUrl,
588
+ // });
589
+ //
590
+ // const { username } = await sdkApi.makeRequest<MultiUserResponse>({
591
+ // method: 'GET',
592
+ // route: '/user/multi-user',
593
+ // });
594
+ //
595
+ // return username;
596
+ //
597
+ // }
598
+ // return username;
599
+ // }
600
+ //
601
+ // /**
602
+ // * Get profile for multi-user
603
+ // * ```typescript
604
+ // * const result = await instance.multiUserGetProfile();
605
+ // * ```
606
+ // */
607
+ // public async multiUserGetProfile(): Promise<any> {
608
+ // const sdkApi = new SdkApi({
609
+ // token: this.multiUserHeaders.headers.Authorization,
610
+ // discoveryUrl: this.discoveryUrl,
611
+ // sdkUrl: this.sdkUrl,
612
+ // });
613
+ //
614
+ // return sdkApi.makeRequest<any>({
615
+ // method: 'GET',
616
+ // route: '/multi-user/profile',
617
+ // });
618
+ // }
619
+ //
620
+ // /**
621
+ // * Set profile for multi-user
622
+ // * ```typescript
623
+ // * const knowledgeModels = await instance.multiUserSetProfile(profile);
624
+ // * ```
625
+ // */
626
+ // public async multiUserSetProfile(profile: unknown): Promise<any> {
627
+ // const sdkApi = new SdkApi({
628
+ // token: this.multiUserHeaders.headers.Authorization,
629
+ // discoveryUrl: this.discoveryUrl,
630
+ // sdkUrl: this.sdkUrl,
631
+ // });
632
+ //
633
+ // return sdkApi.makeRequest<any>({
634
+ // method: 'POST',
635
+ // route: '/multi-user/profile',
636
+ // data: profile,
637
+ // });
638
+ // }
639
+ //
640
+ // /**
641
+ // * Validate multi-user token
642
+ // * ```typescript
643
+ // * const result = await instance.validateMultiUserToken();
644
+ // * ```
645
+ // */
646
+ // public async validateMultiUserToken(): Promise<any> {
647
+ // const sdkApi = new SdkApi({
648
+ // token: this.multiUserHeaders.headers.Authorization,
649
+ // discoveryUrl: this.discoveryUrl,
650
+ // sdkUrl: this.sdkUrl,
651
+ // });
652
+ //
653
+ // return sdkApi.makeRequest<any>({
654
+ // method: 'GET',
655
+ // route: '/multi-user/token',
656
+ // });
657
+ // }
658
+ //
659
+ // /**
660
+ // * Upload icon for multi-user
661
+ // * ```typescript
662
+ // * const downloadUrl = await instance.multiUserUploadIcon({
663
+ // * name: 'name',
664
+ // * contentType: 'content-type',
665
+ // * file: file
666
+ // * });
667
+ // * ```
668
+ // */
669
+ // public async multiUserUploadIcon({ name, contentType, cacheControl = 'no-cache', file }: MultiUserUploadIconArgs): Promise<string> {
670
+ // const sdkApi = new SdkApi({
671
+ // token: this.multiUserHeaders.headers.Authorization,
672
+ // discoveryUrl: this.discoveryUrl,
673
+ // sdkUrl: this.sdkUrl,
674
+ // });
675
+ //
676
+ // const data = await sdkApi.makeRequest<MultiUserUploadIconResponse>({
677
+ // method: 'POST',
678
+ // route: '/multi-user/sign-upload-url',
679
+ // data: {
680
+ // name,
681
+ // contentType,
682
+ // cacheControl,
683
+ // },
684
+ // });
685
+ //
686
+ // const FormDataLib = isNode ? require('form-data') : FormData;
687
+ // const formData = new FormDataLib();
688
+ //
689
+ // _.forEach(data.uploadParams.fields, (value, key) => {
690
+ // formData.append(key, value);
691
+ // });
692
+ //
693
+ // formData.append('cache-control', cacheControl);
694
+ // formData.append('content-type', contentType);
695
+ // formData.append('File', file, name);
696
+ //
697
+ // if (isNode) {
698
+ // await new Promise<void>((resolve, reject) => {
699
+ // formData.submit(data.uploadParams.url, (error: any) => {
700
+ // if (error) return reject(error);
701
+ // resolve();
702
+ // });
703
+ // });
704
+ // } else {
705
+ // await axios.post(data.uploadParams.url, formData);
706
+ // }
707
+ //
708
+ // return data.downloadUrl;
709
+ // }
710
+ //
711
+ // /**
712
+ // * Confirm email change
713
+ // * ```typescript
714
+ // * const result = await instance.confirmEmailChange({ token: 'token' });
715
+ // * ```
716
+ // */
717
+ // public async confirmEmailChange({ token }: { token: string; }): Promise<any> {
718
+ // const sdkApi = new SdkApi({
719
+ // token: '',
720
+ // discoveryUrl: this.discoveryUrl,
721
+ // sdkUrl: this.sdkUrl,
722
+ // });
723
+ //
724
+ // return sdkApi.makeRequest<any>({
725
+ // method: 'POST',
726
+ // route: '/multi-user/change-email-finish',
727
+ // data: {
728
+ // token,
729
+ // },
730
+ // });
731
+ // }
732
+ //
733
+ // /**
734
+ // * Update 2fa for multi-user
735
+ // * ```typescript
736
+ // * const result = await instance.multiUserUpdateTwoFactor({
737
+ // * secret: 'secret',
738
+ // * enabled: true,
739
+ // * codes: codes,
740
+ // * verificationCode: 'verification-code'
741
+ // * });
742
+ // * ```
743
+ // */
744
+ // public async multiUserUpdateTwoFactor({ secret, enabled, codes, verificationCode }: MultiUserUpdateTwoFactorArgs): Promise<any> {
745
+ // const sdkApi = new SdkApi({
746
+ // token: this.multiUserHeaders.headers.Authorization,
747
+ // discoveryUrl: this.discoveryUrl,
748
+ // sdkUrl: this.sdkUrl,
749
+ // });
750
+ //
751
+ // return sdkApi.makeRequest<any>({
752
+ // method: 'POST',
753
+ // route: '/multi-user/twofactor',
754
+ // data: {
755
+ // secret,
756
+ // enabled,
757
+ // codes,
758
+ // verificationCode,
759
+ // },
760
+ // });
761
+ // }
762
+ //
763
+ // }