UNPKG

@simbachain/truffle

Version:
161 lines 7.54 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.login = exports.handler = exports.builder = exports.describe = exports.command = void 0; const web3_suites_1 = require("@simbachain/web3-suites"); // import {default as prompt} from 'prompts'; const chalk_1 = __importDefault(require("chalk")); exports.command = 'login'; exports.describe = 'log in to SIMBAChain SCaaS'; exports.builder = { 'interactive': { 'string': true, 'type': 'boolean', 'describe': 'true or false for interactive export mode', 'default': true, }, 'org': { 'string': true, 'type': 'string', 'describe': 'the name of the SIMBA org to log into non-interactively', }, 'app': { 'string': true, 'type': 'string', 'describe': 'the name of the SIMBA app to log into non-interactively', }, }; /** * command for user to login (acquire auth token, and choose org and app from blocks) * @param args * args: * args.interactive * args.org * args.app * @returns */ exports.handler = async (args) => { web3_suites_1.SimbaConfig.log.debug(`:: ENTER : ${JSON.stringify(args)}`); const interactive = args.interactive; const org = args.org; const app = args.app; await login(interactive, org, app); web3_suites_1.SimbaConfig.log.debug(`:: EXIT :`); }; /** * command for user to login (acquire auth token, and choose org and app from blocks) * @param interactive - if true, then choose org and app from prompts * Also, if true, auth token will be generated through device login flow * If false, auth token will be generated through client creds flow, * so SIMBA_AUTH_CLIENT_ID and SIMBA_AUTH_CLIENT_SECRET need to be present * in .simbachain.env, simbachain.env, or .env in project root or SIMBA_HOME * @param org - if interactive is false, org must be specified, or be present in simba.json * @param app - if interactive is false, app must be specified or be present in simba.json * @returns */ async function login(interactive = true, org, app) { const entryParams = { interactive, org, app, }; web3_suites_1.SimbaConfig.log.debug(`:: ENTER : entryParams : ${JSON.stringify(entryParams)}`); const authStore = await web3_suites_1.SimbaConfig.authStore(); const simbaConfig = new web3_suites_1.SimbaConfig(); const previousSimbaJson = web3_suites_1.SimbaConfig.ProjectConfigStore.all; if (authStore == null) { web3_suites_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`\nsimba: authStore not set!`)}`); return; } if (!interactive) { if (org && !app) { const message = `\nsimba: if specifying an org in non-interactive mode, you must specify an app.`; web3_suites_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`${message}`)}`); throw new Error(message); } let appData; let orgData; if (!org || !app) { const orgFromSimbaJson = web3_suites_1.SimbaConfig.ProjectConfigStore.get("organisation"); try { const orgName = orgFromSimbaJson.name; web3_suites_1.SimbaConfig.log.info(`${chalk_1.default.cyanBright(`no org was specified in login command; logging in using org ${orgName} from simba.json`)}`); } catch (error) { const message = `no organisation specified in your login command, and no organisation present in your simba.json. Please login in interactive mode and choose your organisation, or use the --org <org> flag in your non-interactive login command.`; web3_suites_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`${message}`)}`); throw new Error(message); } const appFromSimbaJson = web3_suites_1.SimbaConfig.ProjectConfigStore.get("application"); try { const appName = appFromSimbaJson.name; web3_suites_1.SimbaConfig.log.info(`${chalk_1.default.cyanBright(`no app was specified in login command; logging in using app ${appName} from simba.json`)}`); } catch (error) { const message = `no app specified in your login command, and no application present in your simba.json. Please login in interactive mode and choose your application, or use the --app <app> flag in your non-interactive login command.`; web3_suites_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`${message}`)}`); throw new Error(message); } } authStore.logout(); try { await authStore.performLogin(interactive); if (org) { orgData = await web3_suites_1.chooseOrganisationFromName(simbaConfig, org); } if (app) { appData = await web3_suites_1.chooseApplicationFromName(simbaConfig, app); } // we only check .id here below if !app, because if app, then // the endpoint we hit does not return organisation data if (!app && (orgData.id != appData.organisation.id)) { web3_suites_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`the selected app is not part of the selected organisation. Please login in interactive mode and choose your application, or use the --app <app> flag in your non-interactive login command.`)}`); return; } web3_suites_1.SimbaConfig.resetSimbaJson(previousSimbaJson, org); web3_suites_1.SimbaConfig.organisation = orgData; web3_suites_1.SimbaConfig.application = appData; web3_suites_1.SimbaConfig.log.info(`${chalk_1.default.greenBright(`Logged in to SIMBA Chain!`)}`); web3_suites_1.SimbaConfig.log.debug(`:: EXIT :`); return; } catch (error) { return; } } try { authStore.logout(); if (!authStore.isLoggedIn()) { await authStore.performLogin(); } else { try { await authStore.refreshToken(); } catch (e) { await authStore.performLogin(); } } const org = await web3_suites_1.chooseOrganisationFromList(simbaConfig); if (!org) { web3_suites_1.SimbaConfig.log.debug(`:: EXIT :`); return Promise.resolve(new Error('No Organisation Selected!')); } const app = await web3_suites_1.chooseApplicationFromList(simbaConfig); if (!app) { web3_suites_1.SimbaConfig.log.debug(`:: ENTER :`); return Promise.resolve(new Error('No Application Selected!')); } web3_suites_1.SimbaConfig.resetSimbaJson(previousSimbaJson, org); web3_suites_1.SimbaConfig.organisation = org; web3_suites_1.SimbaConfig.application = app; web3_suites_1.SimbaConfig.log.info(`${chalk_1.default.cyanBright('\nsimba: Logged in with organisation')} ${chalk_1.default.greenBright(org.display_name)} ${chalk_1.default.cyanBright('and application')} ${chalk_1.default.greenBright(app.display_name)}`); } catch (e) { web3_suites_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`\nsimba: ${e}`)}`); } } exports.login = login; //# sourceMappingURL=login.js.map