-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
135 lines (122 loc) · 3.79 KB
/
cli.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env node
const fs = require('fs');
const { Command, Option } = require('commander');
const { get, getWithToken } = require('./lib/access_token_retriever');
const { generateToken } = require('./lib/generate_token');
// ------------------------------------------------------------
const program = new Command("github-app-installation-access-token");
program.command('get')
.description('Get an installation token given an application id, installation id and private key')
.addOption(new Option('-a, --appId <app-id>', 'Application ID')
.env('GAIAT_APPID')
.makeOptionMandatory(true)
.argParser(parseAppId))
.addOption(new Option('-i, --installationId <installation-id>', 'Installation ID')
.env('GAIAT_INSTID')
.makeOptionMandatory(true)
.argParser(parseInstallationId))
.addOption(new Option('-k, --privateKey <pem>', 'Private key in PEM format')
.env('GAIAT_PKEY')
.makeOptionMandatory(true)
.argParser(parsePrivateKey))
.action(get);
program.command('get-with-cred')
.description('Get an installation token given a base64 encoded credentials')
.addOption(new Option('-c, --credentials <credentials-token>', 'Base64 encoded credentials')
.env('GAIAT_CREDENTIALS')
.makeOptionMandatory(true)
.argParser(parseCredentials))
.action(getWithToken);
program.command('gen-creds')
.description('Generates a base64 encoded credentials given an application id, installation id and private key')
.addOption(new Option('-a, --appId <app-id>', 'Application ID')
.env('GAIAT_APPID')
.makeOptionMandatory(true)
.argParser(parseAppId))
.addOption(new Option('-i, --installationId <installation-id>', 'Installation ID')
.env('GAIAT_INSTID')
.makeOptionMandatory(true)
.argParser(parseInstallationId))
.addOption(new Option('-k, --privateKey <pem>', 'Private key in PEM format')
.env('GAIAT_PKEY')
.makeOptionMandatory(true)
.argParser(parsePrivateKey))
.action(generateToken);
program.parse();
// ------------------------------------------------------------
function getAppId(value) {
if (typeof value !== 'string') {
return false;
}
if (!((/^[0-9]+$/ui).test(value))) {
return false;
}
return parseInt(value, 10);
}
function parseAppId(value) {
value = getAppId(value);
if (value === false) {
program.error('invalid application id argument');
}
return value;
}
function getInstallationId(value) {
if (typeof value !== 'string') {
return false;
}
if (!((/^[0-9]+$/ui).test(value))) {
return false;
}
return parseInt(value, 10);
}
function parseInstallationId(value) {
value = getInstallationId(value);
if (value === false) {
program.error('invalid installation id argument');
}
return value;
}
function getPrivateKey(value) {
if (typeof value !== 'string') {
return false;
}
if (value.startsWith('@')) {
try {
value = fs.readFileSync(value.substring(1), 'utf8');
}
catch (err) {
return false;
}
}
const startIdx = value.indexOf('-----BEGIN RSA PRIVATE KEY-----');
const endIdx = value.indexOf('-----END RSA PRIVATE KEY-----');
if (startIdx < 0 || endIdx < 0 || endIdx <= startIdx) {
return false;
}
return value;
}
function parsePrivateKey(value) {
value = getPrivateKey(value);
if (value === false) {
program.error('invalid private key argument');
}
return value;
}
function parseCredentials(value) {
if (!value) {
program.error('invalid credentials argument');
}
try {
const payload = JSON.parse(Buffer.from(value, 'base64').toString());
if (getAppId(payload.appId) === false || getInstallationId(payload.installationId) === false) {
throw new Error("invalid payload");
}
if (typeof payload.privateKey !== 'string' || payload.privateKey.startsWith('@') || getPrivateKey(payload.privateKey) === false) {
throw new Error("invalid payload");
}
return payload;
}
catch (err) {
program.error('invalid credentials argument');
}
}