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

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 (91) hide show
  1. package/CHANGELOG.md +2 -2
  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/search.d.ts +4 -0
  7. package/dist/src/embed/ts-embed.d.ts +1 -1
  8. package/dist/src/index.d.ts +3 -2
  9. package/dist/src/types.d.ts +29 -0
  10. package/dist/src/utils/authService.d.ts +1 -0
  11. package/dist/src/utils/plugin.d.ts +0 -0
  12. package/dist/src/utils/processData.d.ts +1 -1
  13. package/dist/src/utils.d.ts +1 -0
  14. package/dist/src/v1/api.d.ts +19 -0
  15. package/dist/tsembed.es.js +543 -39
  16. package/dist/tsembed.js +541 -38
  17. package/lib/package.json +2 -1
  18. package/lib/src/auth.d.ts +18 -5
  19. package/lib/src/auth.js +51 -12
  20. package/lib/src/auth.js.map +1 -1
  21. package/lib/src/auth.spec.js +69 -11
  22. package/lib/src/auth.spec.js.map +1 -1
  23. package/lib/src/embed/base.d.ts +21 -5
  24. package/lib/src/embed/base.js +64 -10
  25. package/lib/src/embed/base.js.map +1 -1
  26. package/lib/src/embed/base.spec.js +49 -3
  27. package/lib/src/embed/base.spec.js.map +1 -1
  28. package/lib/src/embed/embed.spec.js +1 -1
  29. package/lib/src/embed/embed.spec.js.map +1 -1
  30. package/lib/src/embed/events.spec.js +30 -1
  31. package/lib/src/embed/events.spec.js.map +1 -1
  32. package/lib/src/embed/pinboard.d.ts +6 -0
  33. package/lib/src/embed/pinboard.js +4 -1
  34. package/lib/src/embed/pinboard.js.map +1 -1
  35. package/lib/src/embed/search.d.ts +4 -0
  36. package/lib/src/embed/search.js +1 -1
  37. package/lib/src/embed/search.js.map +1 -1
  38. package/lib/src/embed/ts-embed.d.ts +1 -1
  39. package/lib/src/embed/ts-embed.js +18 -14
  40. package/lib/src/embed/ts-embed.js.map +1 -1
  41. package/lib/src/embed/ts-embed.spec.js +16 -6
  42. package/lib/src/embed/ts-embed.spec.js.map +1 -1
  43. package/lib/src/index.d.ts +3 -2
  44. package/lib/src/index.js +3 -2
  45. package/lib/src/index.js.map +1 -1
  46. package/lib/src/test/test-utils.js +1 -1
  47. package/lib/src/test/test-utils.js.map +1 -1
  48. package/lib/src/types.d.ts +29 -0
  49. package/lib/src/types.js +16 -0
  50. package/lib/src/types.js.map +1 -1
  51. package/lib/src/utils/authService.d.ts +1 -0
  52. package/lib/src/utils/authService.js +21 -3
  53. package/lib/src/utils/authService.js.map +1 -1
  54. package/lib/src/utils/authService.spec.js +21 -5
  55. package/lib/src/utils/authService.spec.js.map +1 -1
  56. package/lib/src/utils/plugin.d.ts +0 -0
  57. package/lib/src/utils/plugin.js +1 -0
  58. package/lib/src/utils/plugin.js.map +1 -0
  59. package/lib/src/utils/processData.d.ts +1 -1
  60. package/lib/src/utils/processData.js +37 -3
  61. package/lib/src/utils/processData.js.map +1 -1
  62. package/lib/src/utils/processData.spec.js +106 -4
  63. package/lib/src/utils/processData.spec.js.map +1 -1
  64. package/lib/src/utils.d.ts +1 -0
  65. package/lib/src/utils.js +4 -0
  66. package/lib/src/utils.js.map +1 -1
  67. package/lib/src/utils.spec.js +14 -1
  68. package/lib/src/utils.spec.js.map +1 -1
  69. package/lib/src/visual-embed-sdk.d.ts +119 -8
  70. package/package.json +2 -1
  71. package/src/auth.spec.ts +90 -11
  72. package/src/auth.ts +74 -16
  73. package/src/embed/base.spec.ts +56 -4
  74. package/src/embed/base.ts +83 -16
  75. package/src/embed/embed.spec.ts +1 -1
  76. package/src/embed/events.spec.ts +32 -0
  77. package/src/embed/search.ts +5 -0
  78. package/src/embed/ts-embed.spec.ts +19 -9
  79. package/src/embed/ts-embed.ts +24 -15
  80. package/src/index.ts +5 -1
  81. package/src/test/test-utils.ts +1 -1
  82. package/src/types.ts +31 -0
  83. package/src/utils/authService.spec.ts +31 -5
  84. package/src/utils/authService.ts +27 -3
  85. package/src/utils/processData.spec.ts +139 -4
  86. package/src/utils/processData.ts +54 -4
  87. package/src/utils.spec.ts +26 -0
  88. package/src/utils.ts +5 -0
  89. package/lib/src/utils/fetchAnswers.d.ts +0 -3
  90. package/lib/src/utils/fetchAnswers.js +0 -49
  91. 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' {
@@ -306,6 +325,10 @@ declare module '@thoughtspot/visual-embed-sdk/embed/search' {
306
325
  * using raw answer data.
307
326
  */
308
327
  hideResults?: boolean;
328
+ /**
329
+ * If set to true, expands all the data sources panel.
330
+ */
331
+ expandAllDataSource?: boolean;
309
332
  /**
310
333
  * If set to true, the Search Assist feature is enabled.
311
334
  */
@@ -353,6 +376,65 @@ declare module '@thoughtspot/visual-embed-sdk/embed/search' {
353
376
  export {};
354
377
  }
355
378
 
379
+ declare module '@thoughtspot/visual-embed-sdk/auth' {
380
+ import { EmbedConfig } from '@thoughtspot/visual-embed-sdk/types';
381
+ export let loggedInStatus: boolean;
382
+ export let samlAuthWindow: Window;
383
+ export let samlCompletionPromise: Promise<void>;
384
+ export let sessionInfo: any;
385
+ export const SSO_REDIRECTION_MARKER_GUID = "5e16222e-ef02-43e9-9fbd-24226bf3ce5b";
386
+ export const EndPoints: {
387
+ AUTH_VERIFICATION: string;
388
+ SAML_LOGIN_TEMPLATE: (targetUrl: string) => string;
389
+ OIDC_LOGIN_TEMPLATE: (targetUrl: string) => string;
390
+ TOKEN_LOGIN: string;
391
+ BASIC_LOGIN: string;
392
+ LOGOUT: string;
393
+ };
394
+ export enum AuthFailureType {
395
+ SDK = "SDK",
396
+ NO_COOKIE_ACCESS = "NO_COOKIE_ACCESS",
397
+ EXPIRY = "EXPIRY",
398
+ OTHER = "OTHER"
399
+ }
400
+ export enum AuthStatus {
401
+ FAILURE = "FAILURE",
402
+ SUCCESS = "SUCCESS",
403
+ LOGOUT = "LOGOUT"
404
+ }
405
+ /**
406
+ * Return sessionInfo if available else make a loggedIn check to fetch the sessionInfo
407
+ */
408
+ export function getSessionInfo(): any;
409
+ export function initSession(sessionDetails: any): void;
410
+ /**
411
+ * Perform token based authentication
412
+ * @param embedConfig The embed configuration
413
+ */
414
+ export const doTokenAuth: (embedConfig: EmbedConfig) => Promise<boolean>;
415
+ /**
416
+ * Perform basic authentication to the ThoughtSpot cluster using the cluster
417
+ * credentials.
418
+ *
419
+ * Warning: This feature is primarily intended for developer testing. It is
420
+ * strongly advised not to use this authentication method in production.
421
+ * @param embedConfig The embed configuration
422
+ */
423
+ export const doBasicAuth: (embedConfig: EmbedConfig) => Promise<boolean>;
424
+ export const doSamlAuth: (embedConfig: EmbedConfig) => Promise<boolean>;
425
+ export const doOIDCAuth: (embedConfig: EmbedConfig) => Promise<boolean>;
426
+ export const logout: (embedConfig: EmbedConfig) => Promise<boolean>;
427
+ /**
428
+ * Perform authentication on the ThoughtSpot cluster
429
+ * @param embedConfig The embed configuration
430
+ */
431
+ export const authenticate: (embedConfig: EmbedConfig) => Promise<boolean>;
432
+ /**
433
+ * Check if we are authenticated to the ThoughtSpot cluster
434
+ */
435
+ export const isAuthenticated: () => boolean;
436
+ }
437
+
356
438
  declare module '@thoughtspot/visual-embed-sdk/types' {
357
439
  /**
358
440
  * The authentication mechanism for allowing access to the
@@ -438,6 +520,14 @@ declare module '@thoughtspot/visual-embed-sdk/types' {
438
520
  * @default false
439
521
  */
440
522
  noRedirect?: boolean;
523
+ /**
524
+ * [SSO] For SSO Authentication, one can supply an optional path param,
525
+ * this will be the path on the host origin where the SAML flow will be
526
+ * terminated.
527
+ *
528
+ * @version SDK: 1.10.2 | ThoughtSpot: *
529
+ */
530
+ redirectPath?: string;
441
531
  /** @internal */
442
532
  basepath?: string;
443
533
  /**
@@ -467,6 +557,11 @@ declare module '@thoughtspot/visual-embed-sdk/types' {
467
557
  * @default false
468
558
  */
469
559
  disableLoginRedirect?: boolean;
560
+ /**
561
+ * This message is displayed on the embed view when the login fails.
562
+ * @version 1.10.1 | ThoughtSpot: *
563
+ */
564
+ loginFailedMessage?: string;
470
565
  /**
471
566
  * Calls the prefetch method internally when set to true
472
567
  * @default false
@@ -680,6 +775,16 @@ declare module '@thoughtspot/visual-embed-sdk/types' {
680
775
  * The ThoughtSpot auth session has expired.
681
776
  */
682
777
  AuthExpire = "ThoughtspotAuthExpired",
778
+ /**
779
+ * ThoughtSpot failed to validate the auth session.
780
+ * @hidden
781
+ */
782
+ AuthFailure = "ThoughtspotAuthFailure",
783
+ /**
784
+ * ThoughtSpot failed to validate the auth session.
785
+ * @hidden
786
+ */
787
+ AuthLogout = "ThoughtspotAuthLogout",
683
788
  /**
684
789
  * The height of the embedded Liveboard or visualization has been computed.
685
790
  * @return data - The height of the embedded Liveboard or visualization
@@ -731,6 +836,12 @@ declare module '@thoughtspot/visual-embed-sdk/types' {
731
836
  * @version 1.9.1 or later
732
837
  */
733
838
  LiveboardRendered = "PinboardRendered",
839
+ /**
840
+ * This can be used to register an event listener which
841
+ * is triggered on all events.
842
+ * @version SDK: 1.10.0 | ThoughtSpot: any
843
+ */
844
+ ALL = "*",
734
845
  /**
735
846
  * Emitted when answer is saved in the app
736
847
  * @version SDK: 1.11.0 | ThoughtSpot: 8.3.0.cl
@@ -1323,7 +1434,7 @@ declare module '@thoughtspot/visual-embed-sdk/embed/ts-embed' {
1323
1434
  * @param iframeSrc
1324
1435
  */
1325
1436
  protected renderV1Embed(iframeSrc: string): void;
1326
- on(messageType: EmbedEvent, callback: MessageCallback): typeof TsEmbed.prototype;
1437
+ on(messageType: EmbedEvent, callback: MessageCallback, options?: MessageOptions): typeof TsEmbed.prototype;
1327
1438
  }
1328
1439
  }
1329
1440
 
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.10.2",
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
@@ -1,12 +1,13 @@
1
1
  import { initMixpanel } from './mixpanel-service';
2
2
  import { AuthType, EmbedConfig, EmbedEvent } from './types';
3
- import { appendToUrlHash } from './utils';
3
+ import { getRedirectUrl } from './utils';
4
4
  // eslint-disable-next-line import/no-cycle
5
5
  import {
6
6
  fetchSessionInfoService,
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
 
@@ -210,7 +249,11 @@ export const doSamlAuth = async (embedConfig: EmbedConfig) => {
210
249
  // again and the same JS will execute again.
211
250
  const ssoRedirectUrl = embedConfig.noRedirect
212
251
  ? `${thoughtSpotHost}/v2/#/embed/saml-complete`
213
- : appendToUrlHash(window.location.href, SSO_REDIRECTION_MARKER_GUID);
252
+ : getRedirectUrl(
253
+ window.location.href,
254
+ SSO_REDIRECTION_MARKER_GUID,
255
+ embedConfig.redirectPath,
256
+ );
214
257
 
215
258
  // bring back the page to the same URL
216
259
  const ssoEndPoint = `${EndPoints.SAML_LOGIN_TEMPLATE(
@@ -218,6 +261,7 @@ export const doSamlAuth = async (embedConfig: EmbedConfig) => {
218
261
  )}`;
219
262
 
220
263
  await doSSOAuth(embedConfig, ssoEndPoint);
264
+ return loggedInStatus;
221
265
  };
222
266
 
223
267
  export const doOIDCAuth = async (embedConfig: EmbedConfig) => {
@@ -226,7 +270,11 @@ export const doOIDCAuth = async (embedConfig: EmbedConfig) => {
226
270
  // again and the same JS will execute again.
227
271
  const ssoRedirectUrl = embedConfig.noRedirect
228
272
  ? `${thoughtSpotHost}/v2/#/embed/saml-complete`
229
- : appendToUrlHash(window.location.href, SSO_REDIRECTION_MARKER_GUID);
273
+ : getRedirectUrl(
274
+ window.location.href,
275
+ SSO_REDIRECTION_MARKER_GUID,
276
+ embedConfig.redirectPath,
277
+ );
230
278
 
231
279
  // bring back the page to the same URL
232
280
  const ssoEndPoint = `${EndPoints.OIDC_LOGIN_TEMPLATE(
@@ -234,13 +282,23 @@ export const doOIDCAuth = async (embedConfig: EmbedConfig) => {
234
282
  )}`;
235
283
 
236
284
  await doSSOAuth(embedConfig, ssoEndPoint);
285
+ return loggedInStatus;
286
+ };
287
+
288
+ export const logout = async (embedConfig: EmbedConfig): Promise<boolean> => {
289
+ const { thoughtSpotHost } = embedConfig;
290
+ const response = await fetchLogoutService(thoughtSpotHost);
291
+ loggedInStatus = false;
292
+ return loggedInStatus;
237
293
  };
238
294
 
239
295
  /**
240
296
  * Perform authentication on the ThoughtSpot cluster
241
297
  * @param embedConfig The embed configuration
242
298
  */
243
- export const authenticate = async (embedConfig: EmbedConfig): Promise<void> => {
299
+ export const authenticate = async (
300
+ embedConfig: EmbedConfig,
301
+ ): Promise<boolean> => {
244
302
  const { authType } = embedConfig;
245
303
  switch (authType) {
246
304
  case AuthType.SSO:
@@ -252,7 +310,7 @@ export const authenticate = async (embedConfig: EmbedConfig): Promise<void> => {
252
310
  case AuthType.Basic:
253
311
  return doBasicAuth(embedConfig);
254
312
  default:
255
- return Promise.resolve();
313
+ return Promise.resolve(true);
256
314
  }
257
315
  };
258
316