UNPKG

@commercelayer/cli

Version:
128 lines (127 loc) 5.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.newAccessToken = void 0; const tslib_1 = require("tslib"); const base_1 = tslib_1.__importStar(require("../../base")); const config_1 = require("../../config"); const cli_core_1 = require("@commercelayer/cli-core"); const current_1 = require("./current"); const defaultTokenExpiration = cli_core_1.clConfig.api.token_expiration_mins; class ApplicationsToken extends base_1.default { static description = 'get a new access token from Commerce Layer API'; static aliases = ['app:token']; static hidden = true; static examples = [ '$ commercelayer applications:token', '$ commercelayer app:token --info' ]; static flags = { save: base_1.Flags.boolean({ char: 's', description: 'save access token' }), info: base_1.Flags.boolean({ char: 'i', description: 'show access token info' }), shared: base_1.Flags.string({ char: 'S', description: 'organization shared secret', hidden: true, exclusive: ['save'], deprecated: true }), minutes: base_1.Flags.integer({ char: 'M', description: `minutes to token expiration [2, ${defaultTokenExpiration}]`, hidden: true, dependsOn: ['shared'], deprecated: true }) }; async run() { const { flags } = await this.parse(ApplicationsToken); const app = this.appFilterEnabled(flags) ? await this.findApplication(flags) : (0, config_1.currentApplication)(); if (!app || !(0, config_1.configFileExists)(this.config, app)) this.error(`Unable to find configuration file for application${app ? ` ${app.name}` : ''}`, { suggestions: [`execute ${cli_core_1.clColor.cli.command('applications:login')} command to initialize application and get the first access token`] }); try { let expMinutes; let accessToken; let returnData; if (flags.shared) { const token = generateAccessToken(this.config, app, flags.shared, flags.minutes); accessToken = token.accessToken; returnData = token.info; expMinutes = token.expMinutes; } else { const token = await newAccessToken(this.config, app, flags.save); if (flags.save) this.log(`The new ${app.mode} access token has been locally saved for application ${cli_core_1.clColor.api.application(app.name)}`); accessToken = token?.accessToken; returnData = token; } if (accessToken) { this.log(`\nAccess token for application ${(0, current_1.printCurrent)(app)}`); this.log(`\n${cli_core_1.clColor.api.token(accessToken)}\n`); if (flags.shared && expMinutes) { this.warn(cli_core_1.clColor.italic(`this access token will expire in ${expMinutes} minutes`)); this.log(); } } if (flags.info) this.printAccessToken(accessToken); return returnData; } catch (error) { this.log(cli_core_1.clColor.msg.error.bold('FAILURE! ') + String(error.message)); } } printAccessToken(accessToken) { if (accessToken) { const info = cli_core_1.clToken.decodeAccessToken(accessToken); this.log(cli_core_1.clColor.style.title('Token Info:')); this.log(cli_core_1.clOutput.printObject(info)); this.log(); } } } exports.default = ApplicationsToken; const newAccessToken = async (config, app, save = false) => { const cfg = (0, config_1.readConfigFile)(config, app); const token = await cli_core_1.clToken.getAccessToken(cfg); if (token) { if (token.error) throw new Error(`Error getting new access token: ${token.error}`); // Check application type on each token refresh const info = cli_core_1.clToken.decodeAccessToken(token.accessToken); const typeCheck = (0, config_1.configParam)(config_1.ConfigParams.applicationTypeCheck); if (typeCheck) { if (!typeCheck.includes(info.application.kind)) throw new Error(`The locally saved credentials are associated to an application of type ${cli_core_1.clColor.msg.error(info.application.kind)} while the only allowed types are: ${cli_core_1.clColor.api.kind(typeCheck.join(','))}`); } if (save) (0, config_1.writeTokenFile)(config, app, token); } else throw new Error('Error getting new access token'); return token; }; exports.newAccessToken = newAccessToken; const generateAccessToken = (config, app, sharedSecret, valMinutes) => { const savedToken = (0, config_1.readTokenFile)(config, app); const tokenData = cli_core_1.clToken.decodeAccessToken(savedToken.accessToken); let minutes = (valMinutes === undefined) ? defaultTokenExpiration : valMinutes; if (minutes < 2) minutes = 2; else minutes = Math.min(minutes, defaultTokenExpiration); const payload = { application: tokenData?.application, // exp: Math.floor(Date.now() / 1000) + (minutes * 60), organization: tokenData?.organization, // rand: Math.random(), test: tokenData?.test, }; return cli_core_1.clToken.generateAccessToken(payload, sharedSecret, minutes); };