-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkeygen.js
192 lines (167 loc) · 5.56 KB
/
keygen.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* eslint-disable @typescript-eslint/no-var-requires */
const dotenv = require('dotenv');
const path = require('path');
const crypto = require('crypto');
const chalk = require('chalk');
const prompts = require('prompts');
const fs = require('fs');
const envFile = path.join(__dirname, '.env');
// load dotenv if necessary.
dotenv.config({ path: envFile });
if (process.env.OPENID_JWT_ALGORITHM || process.env.OPENID_JWT_PUBLIC_KEY || process.env.OPENID_JWT_PRIVATE_KEY) {
console.error(chalk.redBright('It seems .env file is already configured.'));
console.error('If you want to continue, Please delete OPENID_JWT* env variables from .env');
process.exit(1);
}
(async () => {
const algorithm = (
await prompts({
type: 'select',
name: 'algorithm',
message: 'Which Algorithm do you want to use to sign ID Token?',
choices: [
{ title: 'RS256', description: 'The go-to signing algorithm for JWT', value: 'RS256' },
{ title: 'RS384', value: 'RS384' },
{ title: 'RS512', value: 'RS512' },
{ title: 'ES256K', description: 'The modern signing algorithm for JWT', value: 'ES256K' },
{ title: 'ES384', value: 'ES384' },
{ title: 'ES521', value: 'ES521' },
],
initial: 0,
})
).algorithm;
if (!algorithm) {
console.error(chalk.redBright(chalk.bold('Unexpected algorithm.'), 'Please report to Issues'));
throw new Error('unexepected algorithm');
}
const keyType = algorithm.startsWith('RS') ? 'rsa' : algorithm.startsWith('ES') ? 'ec' : undefined;
if (!keyType) {
console.error(chalk.redBright(chalk.bold('Unexpected algorithm.'), 'Please report to Issues'));
throw new Error('unexepected algorithm');
}
let keyGenData;
if (keyType === 'rsa') {
const modulusLength = (
await prompts({
type: 'select',
name: 'result',
message: 'Specify the key length',
choices: [
{ title: 'RSA-2048', description: 'Performance with decent cryptographic power', value: 2048 },
{ title: 'RSA-4096', description: 'Cryptographic power to extreme', value: 2048 },
],
initial: 0,
})
).result;
keyGenData = {
modulusLength,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem',
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
},
};
} else {
const namedCurve =
algorithm === 'ES256K'
? 'secp256k1'
: algorithm === 'ES384'
? 'secp384r1'
: algorithm === 'ES521'
? 'secp521r1'
: undefined;
if (!namedCurve) {
console.error(chalk.redBright(chalk.bold('Unexpected algorithm.'), 'Please report to Issues'));
throw new Error('unexepected algorithm');
}
keyGenData = {
namedCurve,
publicKeyEncoding: {
type: 'spki',
format: 'pem',
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
},
};
}
let pubPassphrase = (
await prompts({
type: 'confirm',
name: 'result',
message: 'Do you want to set passphrase for public key?',
})
).result;
if (pubPassphrase) {
pubPassphrase = (
await prompts({
type: 'password',
name: 'result',
message: 'Enter passphrase for public key',
})
).result;
} else {
pubPassphrase = undefined;
}
let privPassphrase = (
await prompts({
type: 'confirm',
name: 'result',
message: 'Do you want to set passphrase for private key?',
})
).result;
if (privPassphrase) {
privPassphrase = (
await prompts({
type: 'password',
name: 'result',
message: 'Enter passphrase for private key',
})
).result;
} else {
privPassphrase = undefined;
}
keyGenData.publicKeyEncoding.passphrase = pubPassphrase;
keyGenData.publicKeyEncoding.cipher = pubPassphrase ? 'aes-256-cbc' : undefined;
keyGenData.privateKeyEncoding.passphrase = privPassphrase;
keyGenData.privateKeyEncoding.cipher = privPassphrase ? 'aes-256-cbc' : undefined;
const generatedKey = crypto.generateKeyPairSync(keyType, keyGenData);
console.log(chalk.cyanBright(chalk.bold('Congratulations!')));
console.log('You have generated keys for signing your keys!');
console.log();
const pubKey = generatedKey.publicKey;
const privKey = generatedKey.privateKey;
const envFileOutput =
'\n\n' +
`# OPENID JWT CONFIGURATION\n` +
`OPENID_JWT_ALGORITHM="${algorithm}"\n` +
`OPENID_JWT_PUBLIC_KEY=${JSON.stringify(pubKey)}\n` +
(pubPassphrase ? `OPENID_JWT_PUBLIC_KEY_PASSPHRASE=${JSON.stringify(pubPassphrase)}\n` : '') +
`OPENID_JWT_PRIVATE_KEY=${JSON.stringify(privKey)}\n` +
(privPassphrase ? `OPENID_JWT_PRIVATE_KEY_PASSPHRASE=${JSON.stringify(privPassphrase)}\n` : '');
let applyNow = false;
if (fs.existsSync(envFile)) {
applyNow = (
await prompts({
type: 'confirm',
name: 'result',
message: '.env file was found., Update the .env file?',
})
).result;
}
if (applyNow) {
fs.appendFileSync(envFile, envFileOutput);
console.log(chalk.greenBright('Configuration was applied to ', chalk.bold('.env'), 'file!'));
} else {
console.log('Please manually configure the following:');
console.log('openid.jwt.algorithm:', algorithm);
console.log('openid.jwt.publicKey.key:', pubKey);
if (pubPassphrase) console.log('openid.jwt.publicKey.passphrase:', pubPassphrase);
console.log('openid.jwt.privateKey.key:', privKey);
if (privPassphrase) console.log('openid.jwt.privateKey.passphrase:', privPassphrase);
}
})();