@thoughtspot/visual-embed-sdk 1.10.0-alpha.4 → 1.11.0-alpha.1

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 (78) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +1 -1
  3. package/dist/src/auth.d.ts +18 -5
  4. package/dist/src/embed/base.d.ts +21 -5
  5. package/dist/src/embed/pinboard.d.ts +6 -0
  6. package/dist/src/embed/ts-embed.d.ts +1 -1
  7. package/dist/src/index.d.ts +3 -2
  8. package/dist/src/types.d.ts +21 -0
  9. package/dist/src/utils/authService.d.ts +1 -0
  10. package/dist/src/utils/plugin.d.ts +0 -0
  11. package/dist/src/utils/processData.d.ts +1 -1
  12. package/dist/src/v1/api.d.ts +19 -0
  13. package/dist/tsembed.es.js +536 -36
  14. package/dist/tsembed.js +534 -35
  15. package/lib/package.json +2 -1
  16. package/lib/src/auth.d.ts +18 -5
  17. package/lib/src/auth.js +48 -9
  18. package/lib/src/auth.js.map +1 -1
  19. package/lib/src/auth.spec.js +69 -11
  20. package/lib/src/auth.spec.js.map +1 -1
  21. package/lib/src/embed/base.d.ts +21 -5
  22. package/lib/src/embed/base.js +64 -10
  23. package/lib/src/embed/base.js.map +1 -1
  24. package/lib/src/embed/base.spec.js +49 -3
  25. package/lib/src/embed/base.spec.js.map +1 -1
  26. package/lib/src/embed/embed.spec.js +1 -1
  27. package/lib/src/embed/embed.spec.js.map +1 -1
  28. package/lib/src/embed/events.spec.js +30 -1
  29. package/lib/src/embed/events.spec.js.map +1 -1
  30. package/lib/src/embed/pinboard.d.ts +6 -0
  31. package/lib/src/embed/pinboard.js +4 -1
  32. package/lib/src/embed/pinboard.js.map +1 -1
  33. package/lib/src/embed/ts-embed.d.ts +1 -1
  34. package/lib/src/embed/ts-embed.js +18 -14
  35. package/lib/src/embed/ts-embed.js.map +1 -1
  36. package/lib/src/embed/ts-embed.spec.js +16 -6
  37. package/lib/src/embed/ts-embed.spec.js.map +1 -1
  38. package/lib/src/index.d.ts +3 -2
  39. package/lib/src/index.js +3 -2
  40. package/lib/src/index.js.map +1 -1
  41. package/lib/src/test/test-utils.js +1 -1
  42. package/lib/src/test/test-utils.js.map +1 -1
  43. package/lib/src/types.d.ts +21 -0
  44. package/lib/src/types.js +16 -0
  45. package/lib/src/types.js.map +1 -1
  46. package/lib/src/utils/authService.d.ts +1 -0
  47. package/lib/src/utils/authService.js +21 -3
  48. package/lib/src/utils/authService.js.map +1 -1
  49. package/lib/src/utils/authService.spec.js +21 -5
  50. package/lib/src/utils/authService.spec.js.map +1 -1
  51. package/lib/src/utils/plugin.d.ts +0 -0
  52. package/lib/src/utils/plugin.js +1 -0
  53. package/lib/src/utils/plugin.js.map +1 -0
  54. package/lib/src/utils/processData.d.ts +1 -1
  55. package/lib/src/utils/processData.js +37 -3
  56. package/lib/src/utils/processData.js.map +1 -1
  57. package/lib/src/utils/processData.spec.js +106 -4
  58. package/lib/src/utils/processData.spec.js.map +1 -1
  59. package/lib/src/visual-embed-sdk.d.ts +107 -8
  60. package/package.json +2 -1
  61. package/src/auth.spec.ts +90 -11
  62. package/src/auth.ts +63 -13
  63. package/src/embed/base.spec.ts +56 -4
  64. package/src/embed/base.ts +83 -16
  65. package/src/embed/embed.spec.ts +1 -1
  66. package/src/embed/events.spec.ts +32 -0
  67. package/src/embed/ts-embed.spec.ts +19 -9
  68. package/src/embed/ts-embed.ts +24 -15
  69. package/src/index.ts +5 -1
  70. package/src/test/test-utils.ts +1 -1
  71. package/src/types.ts +22 -0
  72. package/src/utils/authService.spec.ts +31 -5
  73. package/src/utils/authService.ts +27 -3
  74. package/src/utils/processData.spec.ts +139 -4
  75. package/src/utils/processData.ts +54 -4
  76. package/lib/src/utils/fetchAnswers.d.ts +0 -3
  77. package/lib/src/utils/fetchAnswers.js +0 -49
  78. package/lib/src/utils/fetchAnswers.js.map +0 -1
@@ -1,4 +1,6 @@
1
1
  // Generated by dts-bundle v0.7.3
2
+ // Dependencies for this module:
3
+ // ../../eventemitter3
2
4
 
3
5
  declare module '@thoughtspot/visual-embed-sdk' {
4
6
  /**
@@ -11,11 +13,12 @@ declare module '@thoughtspot/visual-embed-sdk' {
11
13
  * @author Ayon Ghosh <ayon.ghosh@thoughtspot.com>
12
14
  */
13
15
  import { AppEmbed, Page, AppViewConfig } from '@thoughtspot/visual-embed-sdk/embed/app';
14
- import { init, prefetch } from '@thoughtspot/visual-embed-sdk/embed/base';
16
+ import { init, prefetch, logout } from '@thoughtspot/visual-embed-sdk/embed/base';
15
17
  import { PinboardEmbed, LiveboardViewConfig, LiveboardEmbed } from '@thoughtspot/visual-embed-sdk/embed/liveboard';
16
18
  import { SearchEmbed, SearchViewConfig } from '@thoughtspot/visual-embed-sdk/embed/search';
19
+ import { AuthFailureType, AuthStatus } from '@thoughtspot/visual-embed-sdk/auth';
17
20
  import { AuthType, RuntimeFilter, RuntimeFilterOp, EmbedEvent, HostEvent, DataSourceVisualMode, Action, EmbedConfig } from '@thoughtspot/visual-embed-sdk/types';
18
- export { init, prefetch, SearchEmbed, PinboardEmbed, LiveboardEmbed, AppEmbed, Page, AuthType, RuntimeFilter, RuntimeFilterOp, EmbedEvent, HostEvent, DataSourceVisualMode, Action, EmbedConfig, SearchViewConfig, LiveboardViewConfig, AppViewConfig, };
21
+ export { init, logout, prefetch, SearchEmbed, PinboardEmbed, LiveboardEmbed, AppEmbed, AuthFailureType, AuthStatus, Page, AuthType, RuntimeFilter, RuntimeFilterOp, EmbedEvent, HostEvent, DataSourceVisualMode, Action, EmbedConfig, SearchViewConfig, LiveboardViewConfig, AppViewConfig, };
19
22
  }
20
23
 
21
24
  declare module '@thoughtspot/visual-embed-sdk/embed/app' {
@@ -134,14 +137,27 @@ declare module '@thoughtspot/visual-embed-sdk/embed/app' {
134
137
  }
135
138
 
136
139
  declare module '@thoughtspot/visual-embed-sdk/embed/base' {
140
+ /**
141
+ * Copyright (c) 2022
142
+ *
143
+ * Base classes
144
+ *
145
+ * @summary Base classes
146
+ * @author Ayon Ghosh <ayon.ghosh@thoughtspot.com>
147
+ */
148
+ import EventEmitter from 'eventemitter3';
137
149
  import { EmbedConfig } from '@thoughtspot/visual-embed-sdk/types';
138
- export let authPromise: Promise<void>;
150
+ import { AuthFailureType } from '@thoughtspot/visual-embed-sdk/auth';
151
+ export let authPromise: Promise<boolean>;
152
+ export const getEmbedConfig: () => EmbedConfig;
153
+ export const getAuthPromise: () => Promise<boolean>;
154
+ export function notifyAuthSuccess(): void;
155
+ export function notifyAuthFailure(failureType: AuthFailureType): void;
156
+ export function notifyLogout(): void;
139
157
  /**
140
158
  * Perform authentication on the ThoughtSpot app as applicable.
141
159
  */
142
- export const handleAuth: () => Promise<void>;
143
- export const getEmbedConfig: () => EmbedConfig;
144
- export const getAuthPromise: () => Promise<void>;
160
+ export const handleAuth: () => Promise<boolean>;
145
161
  /**
146
162
  * Prefetches static resources from the specified URL. Web browsers can then cache the prefetched resources and serve them from the user's local disk to provide faster access to your app.
147
163
  * @param url The URL provided for prefetch
@@ -155,12 +171,15 @@ declare module '@thoughtspot/visual-embed-sdk/embed/base' {
155
171
  *
156
172
  * @returns authPromise Promise which resolves when authentication is complete.
157
173
  */
158
- export const init: (embedConfig: EmbedConfig) => Promise<void>;
174
+ export const init: (embedConfig: EmbedConfig) => EventEmitter;
175
+ export function disableAutoLogin(): void;
176
+ export const logout: (doNotDisableAutoLogin?: boolean) => Promise<boolean>;
159
177
  /**
160
178
  * Renders functions in a queue, resolves to next function only after the callback next is called
161
179
  * @param fn The function being registered
162
180
  */
163
181
  export const renderInQueue: (fn: (next?: (val?: any) => void) => void) => void;
182
+ export function reset(): void;
164
183
  }
165
184
 
166
185
  declare module '@thoughtspot/visual-embed-sdk/embed/liveboard' {
@@ -353,6 +372,65 @@ declare module '@thoughtspot/visual-embed-sdk/embed/search' {
353
372
  export {};
354
373
  }
355
374
 
375
+ declare module '@thoughtspot/visual-embed-sdk/auth' {
376
+ import { EmbedConfig } from '@thoughtspot/visual-embed-sdk/types';
377
+ export let loggedInStatus: boolean;
378
+ export let samlAuthWindow: Window;
379
+ export let samlCompletionPromise: Promise<void>;
380
+ export let sessionInfo: any;
381
+ export const SSO_REDIRECTION_MARKER_GUID = "5e16222e-ef02-43e9-9fbd-24226bf3ce5b";
382
+ export const EndPoints: {
383
+ AUTH_VERIFICATION: string;
384
+ SAML_LOGIN_TEMPLATE: (targetUrl: string) => string;
385
+ OIDC_LOGIN_TEMPLATE: (targetUrl: string) => string;
386
+ TOKEN_LOGIN: string;
387
+ BASIC_LOGIN: string;
388
+ LOGOUT: string;
389
+ };
390
+ export enum AuthFailureType {
391
+ SDK = "SDK",
392
+ NO_COOKIE_ACCESS = "NO_COOKIE_ACCESS",
393
+ EXPIRY = "EXPIRY",
394
+ OTHER = "OTHER"
395
+ }
396
+ export enum AuthStatus {
397
+ FAILURE = "FAILURE",
398
+ SUCCESS = "SUCCESS",
399
+ LOGOUT = "LOGOUT"
400
+ }
401
+ /**
402
+ * Return sessionInfo if available else make a loggedIn check to fetch the sessionInfo
403
+ */
404
+ export function getSessionInfo(): any;
405
+ export function initSession(sessionDetails: any): void;
406
+ /**
407
+ * Perform token based authentication
408
+ * @param embedConfig The embed configuration
409
+ */
410
+ export const doTokenAuth: (embedConfig: EmbedConfig) => Promise<boolean>;
411
+ /**
412
+ * Perform basic authentication to the ThoughtSpot cluster using the cluster
413
+ * credentials.
414
+ *
415
+ * Warning: This feature is primarily intended for developer testing. It is
416
+ * strongly advised not to use this authentication method in production.
417
+ * @param embedConfig The embed configuration
418
+ */
419
+ export const doBasicAuth: (embedConfig: EmbedConfig) => Promise<boolean>;
420
+ export const doSamlAuth: (embedConfig: EmbedConfig) => Promise<boolean>;
421
+ export const doOIDCAuth: (embedConfig: EmbedConfig) => Promise<boolean>;
422
+ export const logout: (embedConfig: EmbedConfig) => Promise<boolean>;
423
+ /**
424
+ * Perform authentication on the ThoughtSpot cluster
425
+ * @param embedConfig The embed configuration
426
+ */
427
+ export const authenticate: (embedConfig: EmbedConfig) => Promise<boolean>;
428
+ /**
429
+ * Check if we are authenticated to the ThoughtSpot cluster
430
+ */
431
+ export const isAuthenticated: () => boolean;
432
+ }
433
+
356
434
  declare module '@thoughtspot/visual-embed-sdk/types' {
357
435
  /**
358
436
  * The authentication mechanism for allowing access to the
@@ -467,6 +545,11 @@ declare module '@thoughtspot/visual-embed-sdk/types' {
467
545
  * @default false
468
546
  */
469
547
  disableLoginRedirect?: boolean;
548
+ /**
549
+ * This message is displayed on the embed view when the login fails.
550
+ * @version 1.10.1 | ThoughtSpot: *
551
+ */
552
+ loginFailedMessage?: string;
470
553
  /**
471
554
  * Calls the prefetch method internally when set to true
472
555
  * @default false
@@ -680,6 +763,16 @@ declare module '@thoughtspot/visual-embed-sdk/types' {
680
763
  * The ThoughtSpot auth session has expired.
681
764
  */
682
765
  AuthExpire = "ThoughtspotAuthExpired",
766
+ /**
767
+ * ThoughtSpot failed to validate the auth session.
768
+ * @hidden
769
+ */
770
+ AuthFailure = "ThoughtspotAuthFailure",
771
+ /**
772
+ * ThoughtSpot failed to validate the auth session.
773
+ * @hidden
774
+ */
775
+ AuthLogout = "ThoughtspotAuthLogout",
683
776
  /**
684
777
  * The height of the embedded Liveboard or visualization has been computed.
685
778
  * @return data - The height of the embedded Liveboard or visualization
@@ -731,6 +824,12 @@ declare module '@thoughtspot/visual-embed-sdk/types' {
731
824
  * @version 1.9.1 or later
732
825
  */
733
826
  LiveboardRendered = "PinboardRendered",
827
+ /**
828
+ * This can be used to register an event listener which
829
+ * is triggered on all events.
830
+ * @version SDK: 1.10.0 | ThoughtSpot: any
831
+ */
832
+ ALL = "*",
734
833
  /**
735
834
  * Emitted when answer is saved in the app
736
835
  * @version SDK: 1.11.0 | ThoughtSpot: 8.3.0.cl
@@ -1323,7 +1422,7 @@ declare module '@thoughtspot/visual-embed-sdk/embed/ts-embed' {
1323
1422
  * @param iframeSrc
1324
1423
  */
1325
1424
  protected renderV1Embed(iframeSrc: string): void;
1326
- on(messageType: EmbedEvent, callback: MessageCallback): typeof TsEmbed.prototype;
1425
+ on(messageType: EmbedEvent, callback: MessageCallback, options?: MessageOptions): typeof TsEmbed.prototype;
1327
1426
  }
1328
1427
  }
1329
1428
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thoughtspot/visual-embed-sdk",
3
- "version": "1.10.0-alpha.4",
3
+ "version": "1.11.0-alpha.1",
4
4
  "description": "ThoughtSpot Embed SDK",
5
5
  "module": "lib/src/index.js",
6
6
  "main": "dist/tsembed.js",
@@ -44,6 +44,7 @@
44
44
  "dependencies": {
45
45
  "algoliasearch": "^4.10.5",
46
46
  "classnames": "^2.3.1",
47
+ "eventemitter3": "^4.0.7",
47
48
  "mixpanel-browser": "^2.41.0"
48
49
  },
49
50
  "devDependencies": {
package/src/auth.spec.ts CHANGED
@@ -9,12 +9,12 @@ const password = '12345678';
9
9
  const samalLoginUrl = `${thoughtSpotHost}/callosum/v1/saml/login?targetURLPath=%235e16222e-ef02-43e9-9fbd-24226bf3ce5b`;
10
10
 
11
11
  const embedConfig: any = {
12
- doTokenAuthSuccess: {
12
+ doTokenAuthSuccess: (token: string) => ({
13
13
  thoughtSpotHost,
14
14
  username,
15
15
  authEndpoint: 'auth',
16
- getAuthToken: jest.fn(() => Promise.resolve('authToken')),
17
- },
16
+ getAuthToken: jest.fn(() => Promise.resolve(token)),
17
+ }),
18
18
  doTokenAuthFailureWithoutAuthEndPoint: {
19
19
  thoughtSpotHost,
20
20
  username,
@@ -35,9 +35,15 @@ const embedConfig: any = {
35
35
  doSamlAuth: {
36
36
  thoughtSpotHost,
37
37
  },
38
+ doOidcAuth: {
39
+ thoughtSpotHost,
40
+ },
38
41
  SSOAuth: {
39
42
  authType: AuthType.SSO,
40
43
  },
44
+ OIDCAuth: {
45
+ authType: AuthType.OIDC,
46
+ },
41
47
  authServerFailure: {
42
48
  thoughtSpotHost,
43
49
  username,
@@ -107,12 +113,14 @@ describe('Unit test for auth', () => {
107
113
  status: 200,
108
114
  }),
109
115
  );
110
- await authInstance.doTokenAuth(embedConfig.doTokenAuthSuccess);
116
+ await authInstance.doTokenAuth(
117
+ embedConfig.doTokenAuthSuccess('authToken'),
118
+ );
111
119
  expect(authService.fetchSessionInfoService).toBeCalled();
112
120
  expect(authInstance.loggedInStatus).toBe(true);
113
121
  });
114
122
 
115
- test('doTokenAuth: when user is not loggedIn & getAuthToken have response, isLoggedIn should called', async () => {
123
+ test('doTokenAuth: when user is not loggedIn & getAuthToken have response', async () => {
116
124
  jest.spyOn(authService, 'fetchSessionInfoService').mockImplementation(
117
125
  () => false,
118
126
  );
@@ -120,13 +128,19 @@ describe('Unit test for auth', () => {
120
128
  authService,
121
129
  'fetchAuthTokenService',
122
130
  ).mockImplementation(() => ({ text: () => Promise.resolve('abc') }));
123
- jest.spyOn(authService, 'fetchAuthService');
124
- await authInstance.doTokenAuth(embedConfig.doTokenAuthSuccess);
131
+ jest.spyOn(authService, 'fetchAuthService').mockImplementation(() =>
132
+ Promise.resolve({
133
+ status: 200,
134
+ }),
135
+ );
136
+ await authInstance.doTokenAuth(
137
+ embedConfig.doTokenAuthSuccess('authToken2'),
138
+ );
125
139
  expect(authService.fetchSessionInfoService).toBeCalled();
126
140
  expect(authService.fetchAuthService).toBeCalledWith(
127
141
  thoughtSpotHost,
128
142
  username,
129
- 'authToken',
143
+ 'authToken2',
130
144
  );
131
145
  });
132
146
 
@@ -140,7 +154,12 @@ describe('Unit test for auth', () => {
140
154
  ).mockImplementation(() =>
141
155
  Promise.resolve({ text: () => Promise.resolve('abc') }),
142
156
  );
143
- jest.spyOn(authService, 'fetchAuthService');
157
+ jest.spyOn(authService, 'fetchAuthService').mockImplementation(() =>
158
+ Promise.resolve({
159
+ status: 200,
160
+ ok: true,
161
+ }),
162
+ );
144
163
  await authInstance.doTokenAuth(
145
164
  embedConfig.doTokenAuthFailureWithoutGetAuthToken,
146
165
  );
@@ -155,6 +174,38 @@ describe('Unit test for auth', () => {
155
174
  });
156
175
  });
157
176
 
177
+ test('doTokenAuth: Should raise error when duplicate token is used', async () => {
178
+ jest.spyOn(authService, 'fetchSessionInfoService').mockResolvedValue({
179
+ status: 401,
180
+ });
181
+ jest.spyOn(window, 'alert').mockClear();
182
+ jest.spyOn(window, 'alert').mockReturnValue(undefined);
183
+ jest.spyOn(authService, 'fetchAuthService').mockReset();
184
+ jest.spyOn(authService, 'fetchAuthService').mockImplementation(() =>
185
+ Promise.resolve({
186
+ status: 200,
187
+ ok: true,
188
+ }),
189
+ );
190
+ await authInstance.doTokenAuth(
191
+ embedConfig.doTokenAuthSuccess('authToken3'),
192
+ );
193
+
194
+ try {
195
+ await authInstance.doTokenAuth(
196
+ embedConfig.doTokenAuthSuccess('authToken3'),
197
+ );
198
+ expect(false).toBe(true);
199
+ } catch (e) {
200
+ expect(e.message).toContain('Duplicate token');
201
+ }
202
+ await executeAfterWait(() => {
203
+ expect(authInstance.loggedInStatus).toBe(false);
204
+ expect(window.alert).toBeCalled();
205
+ expect(authService.fetchAuthService).toHaveBeenCalledTimes(1);
206
+ });
207
+ });
208
+
158
209
  describe('doBasicAuth', () => {
159
210
  beforeEach(() => {
160
211
  global.fetch = window.fetch;
@@ -181,7 +232,7 @@ describe('Unit test for auth', () => {
181
232
  jest.spyOn(
182
233
  authService,
183
234
  'fetchBasicAuthService',
184
- ).mockImplementation(() => ({ status: 200 }));
235
+ ).mockImplementation(() => ({ status: 200, ok: true }));
185
236
 
186
237
  await authInstance.doBasicAuth(embedConfig.doBasicAuth);
187
238
  expect(authService.fetchSessionInfoService).toBeCalled();
@@ -253,6 +304,7 @@ describe('Unit test for auth', () => {
253
304
  },
254
305
  });
255
306
  spyOn(authInstance, 'samlCompletionPromise');
307
+ global.window.open = jest.fn();
256
308
  jest.spyOn(
257
309
  authService,
258
310
  'fetchSessionInfoService',
@@ -263,8 +315,28 @@ describe('Unit test for auth', () => {
263
315
  ...embedConfig.doSamlAuth,
264
316
  noRedirect: true,
265
317
  }),
266
- ).toBe(undefined);
318
+ ).toBe(true);
319
+ expect(authService.fetchSessionInfoService).toBeCalled();
320
+ });
321
+ });
322
+
323
+ describe('doOIDCAuth', () => {
324
+ afterEach(() => {
325
+ delete global.window;
326
+ global.window = Object.create(originalWindow);
327
+ global.window.open = jest.fn();
328
+ global.fetch = window.fetch;
329
+ });
330
+
331
+ it('when user is not loggedIn & isAtSSORedirectUrl is true', async () => {
332
+ jest.spyOn(
333
+ authService,
334
+ 'fetchSessionInfoService',
335
+ ).mockImplementation(() => Promise.reject());
336
+ await authInstance.doOIDCAuth(embedConfig.doOidcAuth);
267
337
  expect(authService.fetchSessionInfoService).toBeCalled();
338
+ expect(window.location.hash).toBe('');
339
+ expect(authInstance.loggedInStatus).toBe(false);
268
340
  });
269
341
  });
270
342
 
@@ -275,6 +347,13 @@ describe('Unit test for auth', () => {
275
347
  expect(authInstance.doSamlAuth).toBeCalled();
276
348
  });
277
349
 
350
+ it('authenticate: when authType is OIDC', async () => {
351
+ jest.spyOn(authInstance, 'doOIDCAuth');
352
+ await authInstance.authenticate(embedConfig.OIDCAuth);
353
+ expect(window.location.hash).toBe('');
354
+ expect(authInstance.doOIDCAuth).toBeCalled();
355
+ });
356
+
278
357
  it('authenticate: when authType is AuthServer', async () => {
279
358
  spyOn(authInstance, 'doTokenAuth');
280
359
  await authInstance.authenticate(embedConfig.authServerFailure);
package/src/auth.ts CHANGED
@@ -7,6 +7,7 @@ import {
7
7
  fetchAuthTokenService,
8
8
  fetchAuthService,
9
9
  fetchBasicAuthService,
10
+ fetchLogoutService,
10
11
  } from './utils/authService';
11
12
 
12
13
  // eslint-disable-next-line import/no-mutable-exports
@@ -29,8 +30,22 @@ export const EndPoints = {
29
30
  `/callosum/v1/oidc/login?targetURLPath=${targetUrl}`,
30
31
  TOKEN_LOGIN: '/callosum/v1/session/login/token',
31
32
  BASIC_LOGIN: '/callosum/v1/session/login',
33
+ LOGOUT: '/callosum/v1/session/logout',
32
34
  };
33
35
 
36
+ export enum AuthFailureType {
37
+ SDK = 'SDK',
38
+ NO_COOKIE_ACCESS = 'NO_COOKIE_ACCESS',
39
+ EXPIRY = 'EXPIRY',
40
+ OTHER = 'OTHER',
41
+ }
42
+
43
+ export enum AuthStatus {
44
+ FAILURE = 'FAILURE',
45
+ SUCCESS = 'SUCCESS',
46
+ LOGOUT = 'LOGOUT',
47
+ }
48
+
34
49
  /**
35
50
  * Check if we are logged into the ThoughtSpot cluster
36
51
  * @param thoughtSpotHost The ThoughtSpot cluster hostname or IP
@@ -58,6 +73,19 @@ export function initSession(sessionDetails: any) {
58
73
  initMixpanel(sessionInfo);
59
74
  }
60
75
 
76
+ const DUPLICATE_TOKEN_ERR =
77
+ 'Duplicate token, please issue a new token every time getAuthToken callback is called.' +
78
+ 'See https://developers.thoughtspot.com/docs/?pageid=embed-auth#trusted-auth-embed for more details.';
79
+ let prevAuthToken: string = null;
80
+ function alertForDuplicateToken(authtoken: string) {
81
+ if (prevAuthToken === authtoken) {
82
+ // eslint-disable-next-line no-alert
83
+ alert(DUPLICATE_TOKEN_ERR);
84
+ throw new Error(DUPLICATE_TOKEN_ERR);
85
+ }
86
+ prevAuthToken = authtoken;
87
+ }
88
+
61
89
  /**
62
90
  * Check if we are stuck at the SSO redirect URL
63
91
  */
@@ -83,7 +111,9 @@ function removeSSORedirectUrlMarker(): void {
83
111
  * Perform token based authentication
84
112
  * @param embedConfig The embed configuration
85
113
  */
86
- export const doTokenAuth = async (embedConfig: EmbedConfig): Promise<void> => {
114
+ export const doTokenAuth = async (
115
+ embedConfig: EmbedConfig,
116
+ ): Promise<boolean> => {
87
117
  const {
88
118
  thoughtSpotHost,
89
119
  username,
@@ -95,20 +125,25 @@ export const doTokenAuth = async (embedConfig: EmbedConfig): Promise<void> => {
95
125
  'Either auth endpoint or getAuthToken function must be provided',
96
126
  );
97
127
  }
98
- const loggedIn = await isLoggedIn(thoughtSpotHost);
99
- if (!loggedIn) {
128
+ loggedInStatus = await isLoggedIn(thoughtSpotHost);
129
+ if (!loggedInStatus) {
100
130
  let authToken = null;
101
131
  if (getAuthToken) {
102
132
  authToken = await getAuthToken();
133
+ alertForDuplicateToken(authToken);
103
134
  } else {
104
135
  const response = await fetchAuthTokenService(authEndpoint);
105
136
  authToken = await response.text();
106
137
  }
107
- await fetchAuthService(thoughtSpotHost, username, authToken);
108
- loggedInStatus = false;
138
+ const resp = await fetchAuthService(
139
+ thoughtSpotHost,
140
+ username,
141
+ authToken,
142
+ );
143
+ // token login issues a 302 when successful
144
+ loggedInStatus = resp.ok || resp.type === 'opaqueredirect';
109
145
  }
110
-
111
- loggedInStatus = true;
146
+ return loggedInStatus;
112
147
  };
113
148
 
114
149
  /**
@@ -119,7 +154,9 @@ export const doTokenAuth = async (embedConfig: EmbedConfig): Promise<void> => {
119
154
  * strongly advised not to use this authentication method in production.
120
155
  * @param embedConfig The embed configuration
121
156
  */
122
- export const doBasicAuth = async (embedConfig: EmbedConfig): Promise<void> => {
157
+ export const doBasicAuth = async (
158
+ embedConfig: EmbedConfig,
159
+ ): Promise<boolean> => {
123
160
  const { thoughtSpotHost, username, password } = embedConfig;
124
161
  const loggedIn = await isLoggedIn(thoughtSpotHost);
125
162
  if (!loggedIn) {
@@ -128,10 +165,11 @@ export const doBasicAuth = async (embedConfig: EmbedConfig): Promise<void> => {
128
165
  username,
129
166
  password,
130
167
  );
131
- loggedInStatus = response.status === 200;
168
+ loggedInStatus = response.ok;
169
+ } else {
170
+ loggedInStatus = true;
132
171
  }
133
-
134
- loggedInStatus = true;
172
+ return loggedInStatus;
135
173
  };
136
174
 
137
175
  async function samlPopupFlow(ssoURL: string) {
@@ -198,6 +236,7 @@ const doSSOAuth = async (
198
236
  const ssoURL = `${thoughtSpotHost}${ssoEndPoint}`;
199
237
  if (embedConfig.noRedirect) {
200
238
  await samlPopupFlow(ssoURL);
239
+ loggedInStatus = true;
201
240
  return;
202
241
  }
203
242
 
@@ -218,6 +257,7 @@ export const doSamlAuth = async (embedConfig: EmbedConfig) => {
218
257
  )}`;
219
258
 
220
259
  await doSSOAuth(embedConfig, ssoEndPoint);
260
+ return loggedInStatus;
221
261
  };
222
262
 
223
263
  export const doOIDCAuth = async (embedConfig: EmbedConfig) => {
@@ -234,13 +274,23 @@ export const doOIDCAuth = async (embedConfig: EmbedConfig) => {
234
274
  )}`;
235
275
 
236
276
  await doSSOAuth(embedConfig, ssoEndPoint);
277
+ return loggedInStatus;
278
+ };
279
+
280
+ export const logout = async (embedConfig: EmbedConfig): Promise<boolean> => {
281
+ const { thoughtSpotHost } = embedConfig;
282
+ const response = await fetchLogoutService(thoughtSpotHost);
283
+ loggedInStatus = false;
284
+ return loggedInStatus;
237
285
  };
238
286
 
239
287
  /**
240
288
  * Perform authentication on the ThoughtSpot cluster
241
289
  * @param embedConfig The embed configuration
242
290
  */
243
- export const authenticate = async (embedConfig: EmbedConfig): Promise<void> => {
291
+ export const authenticate = async (
292
+ embedConfig: EmbedConfig,
293
+ ): Promise<boolean> => {
244
294
  const { authType } = embedConfig;
245
295
  switch (authType) {
246
296
  case AuthType.SSO:
@@ -252,7 +302,7 @@ export const authenticate = async (embedConfig: EmbedConfig): Promise<void> => {
252
302
  case AuthType.Basic:
253
303
  return doBasicAuth(embedConfig);
254
304
  default:
255
- return Promise.resolve();
305
+ return Promise.resolve(true);
256
306
  }
257
307
  };
258
308
 
@@ -1,4 +1,7 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ import * as auth from '../auth';
1
3
  import * as index from '../index';
4
+ import * as base from './base';
2
5
  import {
3
6
  executeAfterWait,
4
7
  getAllIframeEl,
@@ -9,10 +12,11 @@ import {
9
12
  } from '../test/test-utils';
10
13
 
11
14
  const thoughtSpotHost = 'tshost';
15
+ let authEE: EventEmitter;
12
16
 
13
17
  describe('Base TS Embed', () => {
14
18
  beforeAll(() => {
15
- index.init({
19
+ authEE = index.init({
16
20
  thoughtSpotHost,
17
21
  authType: index.AuthType.None,
18
22
  });
@@ -38,10 +42,12 @@ describe('Base TS Embed', () => {
38
42
  },
39
43
  '*',
40
44
  );
41
-
42
- jest.spyOn(window, 'alert').mockImplementation(() => {
45
+ jest.spyOn(window, 'alert').mockReset();
46
+ jest.spyOn(window, 'alert').mockImplementation(() => undefined);
47
+ authEE.on(auth.AuthStatus.FAILURE, (reason) => {
48
+ expect(reason).toEqual(auth.AuthFailureType.NO_COOKIE_ACCESS);
43
49
  expect(window.alert).toBeCalledWith(
44
- 'Third party cookie access is blocked on this browser, please allow third party cookies for ThoughtSpot to work properly',
50
+ 'Third party cookie access is blocked on this browser, please allow third party cookies for this to work properly. \nYou can use `suppressNoCookieAccessAlert` to suppress this message.',
45
51
  );
46
52
  done();
47
53
  });
@@ -92,4 +98,50 @@ describe('Base TS Embed', () => {
92
98
  expect(getIFrameSrc()).toContain('disableLoginRedirect=true');
93
99
  });
94
100
  });
101
+
102
+ test('handleAuth notifies for SDK auth failure', (done) => {
103
+ jest.spyOn(auth, 'authenticate').mockResolvedValue(false);
104
+ const authEmitter = index.init({
105
+ thoughtSpotHost,
106
+ authType: index.AuthType.Basic,
107
+ username: 'test',
108
+ password: 'test',
109
+ });
110
+ authEmitter.on(auth.AuthStatus.FAILURE, (reason) => {
111
+ expect(reason).toBe(auth.AuthFailureType.SDK);
112
+ done();
113
+ });
114
+ });
115
+
116
+ test('Logout method should disable autoLogin', () => {
117
+ jest.spyOn(window, 'fetch').mockResolvedValue({
118
+ type: 'opaque',
119
+ });
120
+ index.init({
121
+ thoughtSpotHost,
122
+ authType: index.AuthType.None,
123
+ autoLogin: true,
124
+ });
125
+ index.logout();
126
+ expect(window.fetch).toHaveBeenCalledWith(
127
+ `http://${thoughtSpotHost}${auth.EndPoints.LOGOUT}`,
128
+ {
129
+ credentials: 'include',
130
+ mode: 'no-cors',
131
+ method: 'POST',
132
+ },
133
+ );
134
+ expect(base.getEmbedConfig().autoLogin).toBe(false);
135
+ });
136
+ });
137
+
138
+ describe('Base without init', () => {
139
+ test('notify should error when called without init', () => {
140
+ base.reset();
141
+ jest.spyOn(global.console, 'error').mockImplementation(() => undefined);
142
+ base.notifyAuthSuccess();
143
+ base.notifyAuthFailure(auth.AuthFailureType.SDK);
144
+ base.notifyLogout();
145
+ expect(global.console.error).toHaveBeenCalledTimes(3);
146
+ });
95
147
  });