-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathcmd-token.js
75 lines (63 loc) · 2.28 KB
/
cmd-token.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
const commander = require('commander');
const token = require('./lib/token')();
const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog;
const CONSOLE_LOG_TAG_COMP = 'microgateway cmd token';
const setup = function setup() {
commander
.command('decode')
.option('-f, --file <file>', 'file containing jwt')
.description('decode a token without verifying it')
.action((options)=>{
options.error = optionError(options);
if (!options.file) {return options.error( 'file is required' );}
token.decodeToken(options)
});
commander
.command('verify')
.option('-f, --file <file>', 'file containing jwt')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.description('verify a jwt token against the public key')
.action((options)=> {
options.error = optionError(options);
if (!options.file) { return options.error('file is required'); }
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
token.verifyToken(options)
});
commander
.command('get')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-i, --id <id>', 'the client id')
.option('-s, --secret <secret>', 'the client secret')
.description('create a client_credentials oauth token')
.action((options)=>{
options.error = optionError(options);
if (!options.org) { return options.error('id is required'); }
if (!options.secret) { return options.error('client secret is required'); }
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
token.getToken(options)
});
commander.parse(process.argv);
var running = false;
commander.commands.forEach(function (command) {
if (command._name === commander.rawArgs[2]) {
running = true;
}
});
if (!running) {
commander.help();
}
};
function optionError(caller) {
return(((obj) => {
return((message) => {
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},message);
obj.help();
});
})(caller))
}
module.exports = setup;