• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thoughtspot / visual-embed-sdk / #2421

16 Sep 2025 08:56AM UTC coverage: 93.944% (-0.1%) from 94.081%
#2421

Pull #311

shivam-kumar-ts
SCAL-116862 added console log
Pull Request #311: SCAL-116862 FOR TEST added console log

1176 of 1335 branches covered (88.09%)

Branch coverage included in aggregate %.

47 of 50 new or added lines in 6 files covered. (94.0%)

54 existing lines in 6 files now uncovered.

2888 of 2991 relevant lines covered (96.56%)

84.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.55
/src/authToken.ts
1
import { ERROR_MESSAGE } from './errors';
26✔
2
import { EmbedConfig } from './types';
3
import { getValueFromWindow, storeValueInWindow } from './utils';
26✔
4
import { fetchAuthTokenService, verifyTokenService } from './utils/authService/authService';
26✔
5
import { logger } from './utils/logger';
26✔
6

7
const cacheAuthTokenKey = 'cachedAuthToken';
26✔
8

9
const getCacheAuthToken = (): string | null => getValueFromWindow(cacheAuthTokenKey);
44✔
10
const storeAuthTokenInCache = (token: string): void => {
26✔
11
    storeValueInWindow(cacheAuthTokenKey, token);
293✔
12
};
13

14
// This method can be used to get the authToken using the embedConfig
15
/**
16
 *
17
 * @param embedConfig
18
 */
19
export async function getAuthenticationToken(embedConfig: EmbedConfig): Promise<string> {
26✔
20
    logger.log('40.getAuthenticationToken: getting authentication token');
25✔
21
    const cachedAuthToken = getCacheAuthToken();
25✔
22
    // Since we don't have token validation enabled , we cannot tell if the
23
    // cached token is valid or not. So we will always fetch a new token.
24
    if (cachedAuthToken && !embedConfig.disableTokenVerification) {
25✔
25
        logger.log('41.getAuthenticationToken: cached authentication token');
3✔
26
        let isCachedTokenStillValid;
27
        try {
3✔
28
            logger.log('42.getAuthenticationToken: validating cached authentication token');
3✔
29
            isCachedTokenStillValid = await validateAuthToken(embedConfig, cachedAuthToken, true);
3✔
30
        } catch {
31
            logger.log('43.getAuthenticationToken: error validating cached authentication token');
1✔
32
            isCachedTokenStillValid = false;
1✔
33
        }
34

35
        if (isCachedTokenStillValid) return cachedAuthToken;
3✔
36
    }
37

38
    const { authEndpoint, getAuthToken } = embedConfig;
23✔
39

40
    let authToken = null;
23✔
41
    logger.log('44.getAuthenticationToken: getting authentication token');
23✔
42
    if (getAuthToken) {
23✔
43
        logger.log('45.getAuthenticationToken: getting authentication token');
21✔
44
        logger.log("555" , getAuthToken);
21✔
45
        authToken = await getAuthToken();
21✔
46
    } else {
47
        logger.log('46.getAuthenticationToken: getting authentication token');
2✔
48
        const response = await fetchAuthTokenService(authEndpoint);
2✔
49
        authToken = await response.text();
2✔
50
    }
51

52
    try {
16✔
53
        // this will throw error if the token is not valid
54
        await validateAuthToken(embedConfig, authToken);
16✔
55
        logger.log('47.getAuthenticationToken: validating authentication token');
14✔
56
    } catch (e) {
57
        logger.log('48.getAuthenticationToken: error validating authentication token');
2✔
58
        logger.error(`${ERROR_MESSAGE.INVALID_TOKEN_ERROR} Error : ${e.message}`);
2✔
59
        throw e;
2✔
60
    }
61

62
    storeAuthTokenInCache(authToken);
14✔
63
    logger.log('49.getAuthenticationToken: storing authentication token');
14✔
64
    return authToken;
14✔
65
}
66

67
const validateAuthToken = async (
26✔
68
    embedConfig: EmbedConfig,
69
    authToken: string,
70
    suppressAlert?: boolean,
71
): Promise<boolean> => {
72
    const cachedAuthToken = getCacheAuthToken();
19✔
73
    if (embedConfig.disableTokenVerification) {
19✔
74
        logger.info('Token verification is disabled. Assuming token is valid.');
1✔
75
        return true;
1✔
76
    }
77
    try {
18✔
78
        const isTokenValid = await verifyTokenService(embedConfig.thoughtSpotHost, authToken);
18✔
79
        if (isTokenValid) return true;
18✔
80
    } catch {
UNCOV
81
        return false;
×
82
    }
83

84
    if (cachedAuthToken && cachedAuthToken === authToken) {
3✔
85
        if (!embedConfig.suppressErrorAlerts && !suppressAlert) {
2✔
86
            // eslint-disable-next-line no-alert
87
            alert(ERROR_MESSAGE.DUPLICATE_TOKEN_ERR);
1✔
88
        }
89
        throw new Error(ERROR_MESSAGE.DUPLICATE_TOKEN_ERR);
2✔
90
    } else {
91
        throw new Error(ERROR_MESSAGE.INVALID_TOKEN_ERROR);
1✔
92
    }
93
};
94

95
/**
96
 * Resets the auth token and a new token will be fetched on the next request.
97
 * @example
98
 * ```js
99
 * resetCachedAuthToken();
100
 * ```
101
 * @version SDK: 1.28.0 | ThoughtSpot: *
102
 * @group Authentication / Init
103
 */
104
export const resetCachedAuthToken = (): void => {
26✔
105
    storeAuthTokenInCache(null);
279✔
106
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc