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

thoughtspot / visual-embed-sdk / #1337

03 Jan 2025 06:00AM UTC coverage: 90.769% (-2.9%) from 93.701%
#1337

Pull #65

yinstardev
SCAL-233454-exp temp-testcases skip
Pull Request #65: test-exported memb

918 of 1087 branches covered (84.45%)

Branch coverage included in aggregate %.

76 of 135 new or added lines in 7 files covered. (56.3%)

19 existing lines in 2 files now uncovered.

2376 of 2542 relevant lines covered (93.47%)

52.69 hits per line

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

20.9
/src/native/handleAuth.ts
1
import EventEmitter from 'eventemitter3';
2
import { AuthEvent, AuthFailureType, AuthStatus } from '../auth';
1✔
3
import { getAuthenticationToken, resetCachedAuthToken } from '../authToken';
1✔
4
import { getMobileEmbedConfig } from '../embed/embedConfig';
1✔
5
import { AuthType } from '../types';
1✔
6
import { isActiveService } from '../utils/authService/tokenizedAuthService';
1✔
7
import { fetchBasicAuthService } from '../utils/authService';
1✔
8
import { WebViewConfig } from './types';
9
import { logger } from '../utils/logger';
1✔
10

11
let loggedInStatus = false;
1✔
12
let authPromise: Promise<boolean> | null = null;
1✔
13
let authEE: EventEmitter<AuthStatus | AuthEvent> | null = null;
1✔
14

15
/**
16
 * Set or get the global EventEmitter used for auth events.
17
 * @param eventEmitter
18
 */
19
export function setAuthEE(eventEmitter: EventEmitter<AuthStatus | AuthEvent>) {
1✔
NEW
20
    authEE = eventEmitter;
×
21
}
22
/**
23
 *
24
 */
25
export function getAuthEE() {
1✔
NEW
26
    return authEE;
×
27
}
28

29
/**
30
 * Notify that SDK-based auth has succeeded.
31
 */
32
function notifyAuthSDKSuccess() {
NEW
33
    if (!authEE) {
×
NEW
34
        logger.error('SDK not initialized');
×
NEW
35
        return;
×
36
    }
NEW
37
    authEE.emit(AuthStatus.SDK_SUCCESS);
×
38
}
39

40
/**
41
 * Notify that authentication has failed.
42
 * @param failureType
43
 */
44
function notifyAuthFailure(failureType: AuthFailureType) {
NEW
45
    if (!authEE) {
×
NEW
46
        logger.error('SDK not initialized');
×
NEW
47
        return;
×
48
    }
NEW
49
    authEE.emit(AuthStatus.FAILURE, failureType);
×
50
}
51

52
/**
53
 * The main handleAuth function to trigger authentication.
54
 */
55
export function handleAuth(): Promise<boolean> {
1✔
NEW
56
    const embedConfig = getMobileEmbedConfig();
×
57
    // Clear any existing tokens if needed
NEW
58
    resetCachedAuthToken();
×
59

60
    // Start authentication
NEW
61
    authPromise = authenticate(embedConfig);
×
NEW
62
    authPromise.then(
×
63
        (isLoggedIn) => {
NEW
64
            if (!isLoggedIn) {
×
NEW
65
                notifyAuthFailure(AuthFailureType.SDK);
×
66
            } else {
NEW
67
                notifyAuthSDKSuccess();
×
68
            }
69
        },
70
        () => {
NEW
71
            notifyAuthFailure(AuthFailureType.SDK);
×
72
        },
73
    );
74

NEW
75
    return authPromise;
×
76
}
77

78
/**
79
 * Returns whether we are currently authenticated.
80
 */
81
export function isAuthenticated(): boolean {
1✔
NEW
82
    return loggedInStatus;
×
83
}
84

85
/**
86
 * The main authenticate function. Only supports the following:
87
 * - TrustedAuthTokenCookieless
88
 * - Basic
89
 * - None
90
 * @param embedConfig
91
 */
92
async function authenticate(embedConfig: WebViewConfig): Promise<boolean> {
NEW
93
    switch (embedConfig.authType) {
×
94
        case AuthType.TrustedAuthTokenCookieless:
NEW
95
            return doCookielessTokenAuth(embedConfig);
×
96
        case AuthType.Basic:
NEW
97
            return doBasicAuth(embedConfig);
×
98
        case AuthType.None:
99
        default:
NEW
100
            loggedInStatus = true;
×
NEW
101
            return true;
×
102
    }
103
}
104

105
/**
106
 * Cookieless token-based auth.
107
 * Checks if we already have a valid session;
108
 * if not, fetches a token and ensures it's usable.
109
 * @param embedConfig
110
 */
111
async function doCookielessTokenAuth(embedConfig: WebViewConfig): Promise<boolean> {
NEW
112
    const { thoughtSpotHost } = embedConfig;
×
NEW
113
    if (await isActiveService(thoughtSpotHost)) {
×
NEW
114
        loggedInStatus = true;
×
NEW
115
        return true;
×
116
    }
117

NEW
118
    const authToken = await getAuthenticationToken(embedConfig);
×
NEW
119
    if (!authToken) {
×
NEW
120
        loggedInStatus = false;
×
NEW
121
        return false;
×
122
    }
123

NEW
124
    loggedInStatus = true;
×
NEW
125
    return loggedInStatus;
×
126
}
127

128
/**
129
 * Basic auth flow. To be Used only during Development.
130
 * Warning: Avoid using in Production.
131
 * @param embedConfig
132
 */
133
async function doBasicAuth(embedConfig: WebViewConfig): Promise<boolean> {
NEW
134
    const { thoughtSpotHost, username, password } = embedConfig;
×
135

NEW
136
    if (await isActiveService(thoughtSpotHost)) {
×
NEW
137
        loggedInStatus = true;
×
NEW
138
        return true;
×
139
    }
140

NEW
141
    const response = await fetchBasicAuthService(thoughtSpotHost, username, password);
×
NEW
142
    loggedInStatus = response.ok;
×
NEW
143
    return loggedInStatus;
×
144
}
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

© 2025 Coveralls, Inc