@thoughtspot/visual-embed-sdk
Version:
ThoughtSpot Embed SDK
123 lines • 4.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchBasicAuthService = exports.fetchAuthPostService = exports.fetchAuthService = exports.fetchAuthTokenService = exports.verifyTokenService = exports.EndPoints = void 0;
const logger_1 = require("../logger");
exports.EndPoints = {
AUTH_VERIFICATION: '/callosum/v1/session/info',
SESSION_INFO: '/callosum/v1/session/info',
PREAUTH_INFO: '/prism/preauth/info',
SAML_LOGIN_TEMPLATE: (targetUrl) => `/callosum/v1/saml/login?targetURLPath=${targetUrl}`,
OIDC_LOGIN_TEMPLATE: (targetUrl) => `/callosum/v1/oidc/login?targetURLPath=${targetUrl}`,
TOKEN_LOGIN: '/callosum/v1/session/login/token',
BASIC_LOGIN: '/callosum/v1/session/login',
LOGOUT: '/callosum/v1/session/logout',
EXECUTE_TML: '/api/rest/2.0/metadata/tml/import',
EXPORT_TML: '/api/rest/2.0/metadata/tml/export',
IS_ACTIVE: '/callosum/v1/session/isactive',
};
/**
*
* @param url
* @param options
*/
function failureLoggedFetch(url, options = {}) {
return fetch(url, options).then(async (r) => {
var _a;
if (!r.ok && r.type !== 'opaqueredirect' && r.type !== 'opaque') {
logger_1.logger.error('Failure', await ((_a = r.text) === null || _a === void 0 ? void 0 : _a.call(r)));
}
return r;
});
}
/**
* Service to validate a auth token against a ThoughtSpot host.
* @param thoughtSpotHost : ThoughtSpot host to verify the token against.
* @param authToken : Auth token to verify.
*/
async function verifyTokenService(thoughtSpotHost, authToken) {
const authVerificationUrl = `${thoughtSpotHost}${exports.EndPoints.IS_ACTIVE}`;
try {
const res = await fetch(authVerificationUrl, {
headers: {
Authorization: `Bearer ${authToken}`,
'x-requested-by': 'ThoughtSpot',
},
credentials: 'omit',
});
return res.ok;
}
catch (e) {
logger_1.logger.warn(`Token Verification Service failed : ${e.message}`);
}
return false;
}
exports.verifyTokenService = verifyTokenService;
/**
*
* @param authEndpoint
*/
async function fetchAuthTokenService(authEndpoint) {
return fetch(authEndpoint);
}
exports.fetchAuthTokenService = fetchAuthTokenService;
/**
*
* @param thoughtSpotHost
* @param username
* @param authToken
*/
async function fetchAuthService(thoughtSpotHost, username, authToken) {
const fetchUrlParams = username
? `username=${username}&auth_token=${authToken}`
: `auth_token=${authToken}`;
return failureLoggedFetch(`${thoughtSpotHost}${exports.EndPoints.TOKEN_LOGIN}?${fetchUrlParams}`, {
credentials: 'include',
// We do not want to follow the redirect, as it starts giving a CORS
// error
redirect: 'manual',
});
}
exports.fetchAuthService = fetchAuthService;
/**
*
* @param thoughtSpotHost
* @param username
* @param authToken
*/
async function fetchAuthPostService(thoughtSpotHost, username, authToken) {
const bodyPrams = username
? `username=${encodeURIComponent(username)}&auth_token=${encodeURIComponent(authToken)}`
: `auth_token=${encodeURIComponent(authToken)}`;
return failureLoggedFetch(`${thoughtSpotHost}${exports.EndPoints.TOKEN_LOGIN}`, {
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'x-requested-by': 'ThoughtSpot',
},
body: bodyPrams,
credentials: 'include',
// We do not want to follow the redirect, as it starts giving a CORS
// error
redirect: 'manual',
});
}
exports.fetchAuthPostService = fetchAuthPostService;
/**
*
* @param thoughtSpotHost
* @param username
* @param password
*/
async function fetchBasicAuthService(thoughtSpotHost, username, password) {
return failureLoggedFetch(`${thoughtSpotHost}${exports.EndPoints.BASIC_LOGIN}`, {
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'x-requested-by': 'ThoughtSpot',
},
body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`,
credentials: 'include',
});
}
exports.fetchBasicAuthService = fetchBasicAuthService;
//# sourceMappingURL=authService.js.map