import { ensureError } from '@axinom/mosaic-service-common'; import chalk from 'chalk'; import { CommandModule } from 'yargs'; import { exitCode } from '../../common'; import { GetAccessTokenOptions } from './get-access-token-options'; import { getDevAccessToken } from './get-dev-access-token'; export const getAccessToken: CommandModule = { command: 'get-access-token', describe: 'Requests a long living access token for development purposes', builder: (yargs) => yargs .option('permissionsFile', { alias: 'f', describe: 'A JSON file containing the wanted permission structure.\nJSON Format:\n{"serviceId" : string, "permissions": string[]}[]', string: true, demandOption: true, }) .option('clientId', { alias: ['i', 'devServiceAccountClientId'], describe: "The service account client id.\nRequired permission:\nID SERVICE -> 'Dev Generate User Access Token With Permissions'", string: true, demandOption: true, }) .option('clientSecret', { alias: ['s', 'devServiceAccountClientSecret'], describe: 'The service account client secret.', string: true, demandOption: true, }) .option('authEndpoint', { alias: ['a', 'idServiceAuthBaseUrl'], describe: 'The authentication service endpoint url.', string: true, demandOption: true, }) .option('userEmail', { alias: ['e'], describe: 'The email of a user that exists on the environment. If unspecified, a pseudo-user will be used to generate the token for.', string: true, }) .option('failOnUnknownPermission', { alias: 'u', describe: 'Fails if an unknown permission is requested.', boolean: true, default: false, }) .option('expiration', { alias: 'x', describe: 'Token expiration time in seconds.', number: true, default: 2592000, }), handler: async (options: GetAccessTokenOptions): Promise => { try { const result = await getDevAccessToken(options); console.log(chalk.greenBright(result.accessToken)); } catch (e) { const error = ensureError(e); console.log(chalk.red('Command failed:'), error.message); process.exit(exitCode); } }, };