UNPKG

@cto.ai/ops

Version:

💻 CTO.ai - The CLI built for Teams 🚀

106 lines (105 loc) • 4.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const axios_1 = tslib_1.__importDefault(require("axios")); const querystring_1 = tslib_1.__importDefault(require("querystring")); const cli_sdk_1 = require("@cto.ai/cli-sdk"); const base_1 = tslib_1.__importStar(require("../../base")); const env_1 = require("../../constants/env"); const utils_1 = require("../../utils"); const CustomErrors_1 = require("../../errors/CustomErrors"); const { callOutCyan, reset } = cli_sdk_1.ux.colors; class GenerateToken extends base_1.default { constructor() { super(...arguments); this.promptForInputs = async (inputs) => { const { clientId, clientSecret, password } = await cli_sdk_1.ux.prompt([ { type: 'input', name: 'clientId', message: `\n Insert Client Id ${reset.green('→')}`, validate: input => { if (!input) return 'Please provide your client id.'; return true; }, }, { type: 'password', name: 'clientSecret', message: `Insert Client Secret ${reset.green('→')}`, mask: '*', validate: input => { if (!input) return 'Please provide your client secret.'; return true; }, }, { type: 'password', name: 'password', message: `Insert Account Password ${reset.green('→')}`, mask: '*', validate: input => { if (!input) return 'Please provide your password.'; return true; }, }, ]); return Object.assign(Object.assign({}, inputs), { clientId, clientSecret, password }); }; this.requestToken = async (inputs) => { try { const { clientId, clientSecret, password, config: { user: { username }, }, } = inputs; const { data } = await axios_1.default.post(`${env_1.OPS_KEYCLOAK_HOST}/realms/ops/protocol/openid-connect/token`, querystring_1.default.stringify({ grant_type: 'password', client_id: clientId, client_secret: clientSecret, username, password, }), { headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, }); return Object.assign(Object.assign({}, inputs), { accessToken: data.access_token }); } catch (err) { this.debug('%O', err); throw new CustomErrors_1.GenerateTokenFailed(); } }; this.logToken = async (inputs) => { this.log(callOutCyan(`\n Your Access Token ${reset.green('→')}\n ${inputs.accessToken}`)); return inputs; }; this.sendAnalytics = (inputs) => { const { config, clientId } = inputs; this.services.analytics.track('Ops CLI Generate:Token', { username: config.user.username, clientId, }, config); }; } async run() { this.parse(GenerateToken); const config = await this.isLoggedIn(); try { const tokenPipeline = (0, utils_1.asyncPipe)(this.promptForInputs, this.requestToken, this.logToken, this.sendAnalytics); await tokenPipeline({ config }); } catch (err) { // this.debug('%O', err) this.config.runHook('error', { err, accessToken: config.tokens.accessToken, }); } } } exports.default = GenerateToken; GenerateToken.description = 'Generate a long live access token.'; GenerateToken.flags = { help: base_1.flags.help({ char: 'h' }), };