UNPKG

@onboardbase/cli

Version:

[![Version](https://img.shields.io/npm/v/@onboardbase/cli.svg)](https://www.npmjs.com/package/@onboardbase/cli) [![Downloads/week](https://img.shields.io/npm/dw/@onboardbase/cli.svg)](https://www.npmjs.com/package/@onboardbase/cli) [![License](https://img

154 lines (153 loc) 7.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CompleteLoginProcess = void 0; const login_1 = require("../commands/login"); const os = require("os"); const open = require("open"); const config_1 = require("../configuration/config"); const utils_1 = require("../utils"); const config_2 = require("../config"); const inquirer = require("inquirer"); const http_1 = require("../http"); const chalk = require("chalk"); const CompleteLoginProcess = async ({ flags, shouldCloseProcess, additionalProcesses, oclifConfig, }) => { var _a, _b, _c, _d, _e; // Init config manager await config_1.default.init(login_1.default.name); const hostname = os.hostname(); const hostARCH = os.arch(); const fingerprint = await (0, utils_1.getMachineID)(); const hostOS = os.platform(); const allConfigs = config_1.default.getConfigs(); const { pollingCode, authCode } = await (0, http_1.generateAuthCode)(fingerprint, hostOS, hostname, hostARCH); let dashboardHost = "https://app.onboardbase.com"; const dashboardHostSettingForCurrentDir = (_a = allConfigs[process.cwd()]) === null || _a === void 0 ? void 0 : _a["dashboard-host"]; const dashboardHostSettingForRootDir = (_b = allConfigs["/"]) === null || _b === void 0 ? void 0 : _b["dashboard-host"]; if (dashboardHostSettingForCurrentDir) { dashboardHost = dashboardHostSettingForCurrentDir; } else if (dashboardHostSettingForRootDir) { dashboardHost = dashboardHostSettingForRootDir; } const authUrl = dashboardHost.concat(`/auth/cli?authCode=${authCode}`); /** * That is if the user has not authenticated before */ if (!(allConfigs === null || allConfigs === void 0 ? void 0 : allConfigs["/"]) || !((_c = allConfigs === null || allConfigs === void 0 ? void 0 : allConfigs["/"]) === null || _c === void 0 ? void 0 : _c.token)) { const { browserOption } = await inquirer.prompt([ { type: "list", name: "browserOption", message: "Open the authorization page in your browser?", choices: ["Yes", "No"], }, ]); if (browserOption === "Yes") { await open(authUrl); console.log("Complete authorization at " + authUrl); console.log(""); console.log("Waiting..."); } else { console.log("Complete authorization at " + authUrl); console.log("Your auth code is: %s", authCode); console.log(""); console.log("Waiting..."); } } let newConfig = { scope: "/", token: undefined, }; const scopedConfig = config_1.default.getScopedConfig(); /** * That is if the user has a global authentication befoire */ if (((_d = allConfigs === null || allConfigs === void 0 ? void 0 : allConfigs["/"]) === null || _d === void 0 ? void 0 : _d.token) && ((_e = allConfigs === null || allConfigs === void 0 ? void 0 : allConfigs["/"]) === null || _e === void 0 ? void 0 : _e.token) !== undefined) { console.log("You are already logged in."); const options = await inquirer.prompt([ { type: "list", name: "config", message: "Would you like to scope your new login to the current directory, or overwrite the existing global login?", choices: [ `Scope login to current directory (${process.cwd()})`, `Overwrite global login`, ], }, ]); if (options.config.startsWith("Scope login")) { if (!flags.overwrite && (scopedConfig === null || scopedConfig === void 0 ? void 0 : scopedConfig.token)) { console.log(`This scope is already authorized from a previous login. You can pass the ${chalk.greenBright.bold("--overwrite")} flag to overwrite this directory.`); process.exit(1); } } options.config.startsWith("Scope login") ? (newConfig.scope = process.cwd()) : "/"; const { browserOption } = await inquirer.prompt([ { type: "list", name: "browserOption", message: "Open the authorization page in your browser?", choices: ["Yes", "No"], }, ]); if (browserOption === "Yes") { await open(authUrl); console.log("Complete authorization at " + authUrl); } else { console.log("Complete authorization at " + authUrl); console.log("Your auth code is:\n%s", chalk.green(authCode)); } console.log(""); console.log("Waiting..."); } const pollingInterval = 4000; // 4secs const pollingTimeout = 300000; // 5mins let authTokenResponse = await (0, http_1.getAuthToken)(pollingCode); let isAuthenticated = false; if (authTokenResponse === null || authTokenResponse === void 0 ? void 0 : authTokenResponse.errors) { let intervalHandler; intervalHandler = setInterval(async () => { var _a; if (!isAuthenticated) { authTokenResponse = await (0, http_1.getAuthToken)(pollingCode); if (!(authTokenResponse === null || authTokenResponse === void 0 ? void 0 : authTokenResponse.errors)) { isAuthenticated = true; clearInterval(intervalHandler); const { token } = (_a = authTokenResponse === null || authTokenResponse === void 0 ? void 0 : authTokenResponse.data) === null || _a === void 0 ? void 0 : _a.verifyAuthCode; newConfig.token = token; await config_1.default.updateGlobalConfig(Object.assign(newConfig, { dashboardHost, apiHost: config_1.default.getAuthApiHost(), requirePassword: Boolean(flags.password), password: flags.password, requirePasswordForCurrentSession: Boolean(flags.password), })); // migrate the newly generated token to the config.json file in the datadir provided by @clif/dev const configMgr = new config_2.ConfigManager(); configMgr.loadAllConfig(oclifConfig); const _key = `scoped.${newConfig.scope}.token`; configMgr.setAndPersistGlobalConfig(_key, newConfig.token); // end migrate newly generated token to the config.json file console.log(`${chalk.green("√")} ${chalk.bold(chalk.gray("Authentication successful"))}`); if (shouldCloseProcess) { process.exit(0); } else if (!shouldCloseProcess && additionalProcesses) { await additionalProcesses.nextFunc(additionalProcesses.executor); } } } }, pollingInterval); const intervalTimeout = setTimeout(() => { clearInterval(intervalHandler); clearTimeout(intervalTimeout); console.log(chalk.bold.red("Authentication Timeout exceeded...")); process.exit(1); }, pollingTimeout); } }; exports.CompleteLoginProcess = CompleteLoginProcess;