| 29 | } |
| 30 | |
| 31 | run(params, callback) { |
| 32 | |
| 33 | let activeToken = config.get('ACTIVE_LIBRARY_TOKEN'); |
| 34 | |
| 35 | let host = params.flags.h ? params.flags.h[0] : 'https://api.autocode.com'; |
| 36 | let port = params.flags.p && params.flags.p[0]; |
| 37 | let silent = !!(params.flags.s || params.vflags.silent); |
| 38 | let all = !!(params.flags.a || params.vflags.all); |
| 39 | |
| 40 | let resource = new APIResource(host, port); |
| 41 | let reqParams = {}; |
| 42 | all && (reqParams.all = true); |
| 43 | |
| 44 | resource.authorize(config.get('ACCESS_TOKEN')); |
| 45 | resource.request('/v1/library_tokens').index(reqParams, (err, response) => { |
| 46 | |
| 47 | if (err) { |
| 48 | return callback(err); |
| 49 | } |
| 50 | |
| 51 | let tokens = (response && response.data) || []; |
| 52 | |
| 53 | if (!tokens.length) { |
| 54 | console.log(); |
| 55 | console.log(chalk.bold.red('Oops!')); |
| 56 | console.log(); |
| 57 | console.log(`It doesn't look like you have any remotely generated Identity Tokens.`); |
| 58 | console.log(`This usually means you've removed them.`); |
| 59 | console.log(); |
| 60 | console.log(`Try typing `); |
| 61 | console.log(`\t${chalk.bold('lib tokens:create')}`); |
| 62 | console.log(); |
| 63 | console.log(`To create a new Identity Token (remote).`); |
| 64 | console.log(); |
| 65 | return callback(new Error('No remotely generated tokens.')); |
| 66 | } |
| 67 | |
| 68 | if (!silent) { |
| 69 | console.log(); |
| 70 | console.log(`Here's a list of your available ${chalk.bold('Identity Tokens')}.`); |
| 71 | console.log(`These are your API keys that provide authentication and access to functions.`); |
| 72 | } |
| 73 | |
| 74 | return callback( |
| 75 | null, |
| 76 | `\n` + tabler( |
| 77 | ['Active', 'User', 'Label', 'Token', 'Valid', 'Created'], |
| 78 | tokens.map(libraryToken => { |
| 79 | let label = libraryToken.label ? |
| 80 | libraryToken.label.length > 36 ? |
| 81 | libraryToken.label.substr(0, 33) + '...' : |
| 82 | libraryToken.label : |
| 83 | ''; |
| 84 | let Token = libraryToken.token.substr(0, 16) + '...'; |
| 85 | return { |
| 86 | 'Active': activeToken === libraryToken.token ? ['(active)', chalk.yellow] : '', |
| 87 | 'User': libraryToken.is_valid ? |
| 88 | libraryToken.user.username : |