dce-expresskit 4.0.0-beta.2 → 4.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/genEncodedSecret.ts +104 -78
- package/package.json +1 -1
package/genEncodedSecret.ts
CHANGED
|
@@ -1,84 +1,110 @@
|
|
|
1
1
|
// Import crypto lib
|
|
2
2
|
import crypto from 'crypto';
|
|
3
3
|
|
|
4
|
+
// Prompt
|
|
5
|
+
import readline from 'readline';
|
|
6
|
+
|
|
7
|
+
// Create a readline interface
|
|
8
|
+
const readlineInterface = readline.createInterface({
|
|
9
|
+
input: process.stdin,
|
|
10
|
+
output: process.stdout
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Prompt user for input
|
|
15
|
+
* @author Gabe Abrams
|
|
16
|
+
* @param question the question to ask the user
|
|
17
|
+
* @returns the text from the user
|
|
18
|
+
*/
|
|
19
|
+
const prompt = (question: string): Promise<string> => {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
readlineInterface.question(question, (answer: string) => {
|
|
22
|
+
if (!answer || answer.trim().length === 0) {
|
|
23
|
+
console.log('\nValue cannot be empty. Exiting...');
|
|
24
|
+
process.exit(0);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
resolve(answer);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// All chars for randomizer
|
|
4
33
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
5
34
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
console.log('
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
console.log('
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
console.log('
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
//
|
|
31
|
-
secret =
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
(async () => {
|
|
36
|
+
console.log('––––– Generate Encoded Secret –––––');
|
|
37
|
+
console.log('\nFirst, we need info on the *receiving* server');
|
|
38
|
+
console.log('This is the server that hosts the cross-server endpoint, the one that receives requests from the sending server.\n');
|
|
39
|
+
|
|
40
|
+
// Get salt
|
|
41
|
+
console.log('Encoding salt on the *receiving* server')
|
|
42
|
+
const DCEKIT_CRED_ENCODING_SALT = await prompt('Salt: ');
|
|
43
|
+
|
|
44
|
+
// Get host
|
|
45
|
+
console.log('Hostname of the *receiving* server');
|
|
46
|
+
const host = await prompt('Host: ');
|
|
47
|
+
|
|
48
|
+
console.log('\n\nSecond, we need info on the *sending* server');
|
|
49
|
+
console.log('This is the server that sends requests to the receiving server.\n');
|
|
50
|
+
|
|
51
|
+
// Get key
|
|
52
|
+
console.log('Short unique key for the *sending* server')
|
|
53
|
+
const key = await prompt('Key: ');
|
|
54
|
+
|
|
55
|
+
// Get description
|
|
56
|
+
console.log('Human-readable description of the *sending* server')
|
|
57
|
+
const description = await prompt('Description: ');
|
|
58
|
+
|
|
59
|
+
// Get secret
|
|
60
|
+
let secret = process.env.npm_config_secret;
|
|
61
|
+
if (!secret) {
|
|
62
|
+
// Generate a random secret
|
|
63
|
+
secret = '';
|
|
64
|
+
for (let i = 0; i < 32; i++) {
|
|
65
|
+
secret += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
66
|
+
}
|
|
67
|
+
console.log('Generated a random secret. If you have one in mind, use --secret=...');
|
|
34
68
|
}
|
|
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
|
-
console.log('
|
|
76
|
-
|
|
77
|
-
console.log('');
|
|
78
|
-
console.log('On the server *sending* the requests, append the following to the DCEKIT_CROSS_SERVER_CREDENTIALS env var:');
|
|
79
|
-
console.log(`|${host}:${key}:${secret}|`);
|
|
80
|
-
console.log('');
|
|
81
|
-
console.log('On the server *receiving* the requests, add an entry to the "CrossServerCredential" collection:');
|
|
82
|
-
console.log(`{ "description": "${description}", "key": "${key}", "encodedeSecret": "${encryptionPack}", "scopes": [] }`);
|
|
83
|
-
console.log('');
|
|
84
|
-
console.log('For all scopes that the server should have access to, add them to the "scopes" array.');
|
|
69
|
+
|
|
70
|
+
// Encryption process based on:
|
|
71
|
+
// https://medium.com/@tony.infisical/guide-to-nodes-crypto-module-for-encryption-decryption-65c077176980
|
|
72
|
+
|
|
73
|
+
// Create a random initialization vector
|
|
74
|
+
const iv = crypto.randomBytes(12).toString('base64');
|
|
75
|
+
|
|
76
|
+
// Create a cipher
|
|
77
|
+
const cipher = crypto.createCipheriv(
|
|
78
|
+
'aes-256-gcm',
|
|
79
|
+
Buffer.from(secret, 'base64'),
|
|
80
|
+
Buffer.from(iv, 'base64'),
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
// Encrypt the string
|
|
84
|
+
let ciphertext = cipher.update(secret, 'utf8', 'base64');
|
|
85
|
+
|
|
86
|
+
// Finalize the encryption
|
|
87
|
+
ciphertext += cipher.final('base64');
|
|
88
|
+
|
|
89
|
+
// Get the authentication tag
|
|
90
|
+
const tag = cipher.getAuthTag();
|
|
91
|
+
|
|
92
|
+
// JSONify the encrypted data
|
|
93
|
+
const encryptionPack = encodeURIComponent(JSON.stringify({
|
|
94
|
+
ciphertext,
|
|
95
|
+
iv,
|
|
96
|
+
tag,
|
|
97
|
+
}));
|
|
98
|
+
|
|
99
|
+
// Show the encrypted data
|
|
100
|
+
console.log('\n\n');
|
|
101
|
+
console.log('––––– Done! What\'s Next: –––––');
|
|
102
|
+
console.log('');
|
|
103
|
+
console.log('On the *sending* server, append the following to the DCEKIT_CROSS_SERVER_CREDENTIALS env var:');
|
|
104
|
+
console.log(`|${host}:${key}:${secret}|`);
|
|
105
|
+
console.log('');
|
|
106
|
+
console.log('On the *receiving* server, add an entry to its "CrossServerCredential" collection:');
|
|
107
|
+
console.log(`{ "description": "${description}", "key": "${key}", "encodedeSecret": "${encryptionPack}", "scopes": [] }`);
|
|
108
|
+
console.log('');
|
|
109
|
+
console.log('For all scopes that the server should have access to, add them to the "scopes" array.');
|
|
110
|
+
})();
|
package/package.json
CHANGED