UNPKG

liveperson-functions-cli

Version:
214 lines 8.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LoginController = void 0; const file_service_1 = require("../service/file.service"); const login_service_1 = require("../service/login.service"); const login_view_1 = require("../view/login.view"); class LoginController { constructor({ loginView = new login_view_1.LoginView(), loginService = new login_service_1.LoginService(), fileService = new file_service_1.FileService(), } = {}) { this.fileService = fileService; this.loginView = loginView; this.loginService = loginService; this.accountId = ''; this.tempFile = {}; } /** * Runs the login process if you call the login command (lpf login) * @param {ILoginFlags} - passed flags * @returns {Promise<void>} - login view * @memberof LoginController */ async loginByCommand({ inputFlags } = {}) { var _a, _b; this.tempFile = await this.fileService.getTempFile(); const accountIds = this.tempFile ? Object.keys(this.tempFile) : []; if (inputFlags === null || inputFlags === void 0 ? void 0 : inputFlags.token) { await this.performSSOLogin({ inputFlags }); this.loginView.showWelcomeBanner(true); return; } if (inputFlags === null || inputFlags === void 0 ? void 0 : inputFlags.accountId) { this.accountId = inputFlags.accountId; } else { const answer = await this.loginView.chooseOrEnterAccountId(accountIds); this.accountId = answer.accountId; } try { let tokenValid; if (this.tempFile) { tokenValid = await this.loginService.isTokenValid({ accountId: this.accountId, csrf: (_a = this.tempFile[this.accountId]) === null || _a === void 0 ? void 0 : _a.csrf, sessionId: (_b = this.tempFile[this.accountId]) === null || _b === void 0 ? void 0 : _b.sessionId, }); } if (tokenValid) { this.updateTempFile(); this.loginView.showWelcomeBanner(true); } else { await this.askForUsernameAndPassword({ showBanner: true, username: inputFlags === null || inputFlags === void 0 ? void 0 : inputFlags.username, password: inputFlags === null || inputFlags === void 0 ? void 0 : inputFlags.password, }); } } catch (error) { this.loginView.errorDuringLogin(); throw new Error(error); } } /** * Run the login process if you call a command and you're token is expired. * Will be invoked from the factory. * @returns {Promise<void>} - login view * @memberof LoginController */ async loginByFaasFactory() { this.tempFile = await this.fileService.getTempFile(); const accountIds = this.tempFile ? Object.keys(this.tempFile) : []; const answer = await this.loginView.chooseOrEnterAccountId(accountIds); this.accountId = answer.accountId; await this.askForUsernameAndPassword({ showBanner: false }); } async askForUsernameAndPassword( /* istanbul ignore next */ { showBanner, password, username, } = { showBanner: false, displayAccountId: false }) { const promptAnswer = await this.loginView.askForUsernameAndPassword({ password, username, }); try { const response = await this.loginService.login({ accountId: this.accountId, username: promptAnswer.username || username, password: promptAnswer.password || password, }); await this.fileService.writeTempFile({ ...(this.tempFile && { ...this.tempFile }), [this.accountId]: { token: response.bearer, userId: response.config.userId, username: response.config.loginName, csrf: response.csrf, sessionId: response.sessionId, active: true, }, }); this.tempFile = await this.fileService.getTempFile(); await this.updateTempFile(); this.loginView.showWelcomeBanner(showBanner); } catch (error) { throw new Error(error); } } /** * Gets the login information from the temp file. * If the token is invalid, it will starts the login process. * @param {validToken} - used for the recursive call * @returns {Promise<ILoginInformation>} - login view * @memberof LoginController */ async getLoginInformation({ validToken = false, } = {}) { try { this.tempFile = await this.fileService.getTempFile(); const activeAccountId = Object.keys(this.tempFile).find((e) => this.tempFile[e].active); if (this.checkIfSSOLogin(activeAccountId)) { return { accountId: activeAccountId, token: this.tempFile[activeAccountId].token, userId: this.tempFile[activeAccountId].userId, username: '', }; } const { token, userId, username, csrf, sessionId } = this.tempFile[activeAccountId]; if (validToken || (await this.loginService.isTokenValid({ accountId: activeAccountId, csrf, sessionId, }))) { return { accountId: activeAccountId, token, userId, username, }; } throw new Error('Token not valid'); } catch { try { await this.loginByFaasFactory(); } catch { this.loginView.errorDuringLogin(); } return this.getLoginInformation({ validToken: true }); } } checkIfSSOLogin(activeAccountId) { return (this.tempFile[activeAccountId].token && !this.tempFile[activeAccountId].csrf && !this.tempFile[activeAccountId].sessionId); } async performSSOLogin({ inputFlags }) { this.accountId = inputFlags === null || inputFlags === void 0 ? void 0 : inputFlags.accountId; this.tempFile = await this.fileService.getTempFile(); await this.fileService.writeTempFile({ ...(this.tempFile && { ...this.tempFile }), [this.accountId]: { token: inputFlags === null || inputFlags === void 0 ? void 0 : inputFlags.token, userId: inputFlags === null || inputFlags === void 0 ? void 0 : inputFlags.userId, csrf: null, sessionId: null, active: true, }, }); this.tempFile = await this.fileService.getTempFile(); await this.updateTempFile(); } async updateTempFile() { /* istanbul ignore else */ if (this.tempFile) { Object.keys(this.tempFile).forEach((entry) => { this.tempFile[entry].active = false; }); this.tempFile[this.accountId].active = true; await this.fileService.writeTempFile(this.tempFile); } } /** * Returns Promise which resolves to true/false if the user is currently logged in * @returns {Promise<boolean>} - isUserLoggedIn * @memberof LoginController */ async isUserLoggedIn() { let activeAccountId; try { this.tempFile = await this.fileService.getTempFile(); if (!this.tempFile) { return false; } activeAccountId = Object.keys(this.tempFile).find((e) => this.tempFile[e].active); } catch (error) { // eslint-disable-next-line no-console console.log(error); return false; } if (this.checkIfSSOLogin(activeAccountId)) { return true; } const { csrf, sessionId } = this.tempFile[activeAccountId]; return this.loginService.isTokenValid({ accountId: activeAccountId, csrf, sessionId, }); } } exports.LoginController = LoginController; //# sourceMappingURL=login.controller.js.map