1
1
const restana = require ( 'restana' )
2
2
const opensslService = require ( './openssl-service' )
3
- const fs = require ( 'fs/promises' )
4
3
const { v4 : uuidv4 } = require ( 'uuid' )
5
4
const assert = require ( 'assert' )
6
5
const { AssertionError } = require ( 'assert' )
7
6
8
7
const ALLOWED_BITS = [ 1024 , 2048 , 3072 , 4096 ]
9
-
10
8
let OPENSSL_VERSION
11
9
12
- async function getAndDeleteKeyPair ( filePrivKey , filePubKey ) {
13
- const [ publicKey , privateKey ] = await Promise . all ( [
14
- fs . readFile ( filePubKey , 'utf8' ) ,
15
- fs . readFile ( filePrivKey , 'utf8' )
16
- ] )
17
- await Promise . all ( [
18
- fs . unlink ( filePubKey ) ,
19
- fs . unlink ( filePrivKey )
20
- ] )
21
-
22
- return { publicKey, privateKey }
23
- }
24
-
25
10
const app = restana ( { } )
26
11
27
12
app . get ( [ '/api/openssl-version' , '/api/health/status' ] , async ( req , res ) => {
@@ -48,10 +33,10 @@ app.get('/api/generate/:algorithm', async (req, res) => {
48
33
case 'PS256' :
49
34
case 'PS384' :
50
35
case 'PS512' : {
51
- await opensslService ( `genrsa -out ${ filePrivKey } ${ bits } ` )
52
- await opensslService ( `rsa -in ${ filePrivKey } -pubout -out ${ filePubKey } ` )
36
+ await opensslService . exec ( `genrsa -out ${ filePrivKey } ${ bits } ` )
37
+ await opensslService . exec ( `rsa -in ${ filePrivKey } -pubout -out ${ filePubKey } ` )
53
38
54
- const { privateKey, publicKey } = await getAndDeleteKeyPair ( filePrivKey , filePubKey )
39
+ const { privateKey, publicKey } = await opensslService . getAndDeleteKeyPair ( filePrivKey , filePubKey )
55
40
56
41
res . send ( {
57
42
privateKey,
@@ -65,10 +50,10 @@ app.get('/api/generate/:algorithm', async (req, res) => {
65
50
case 'HS256' :
66
51
case 'HS384' :
67
52
case 'HS512' : {
68
- const secret = ( await opensslService ( `rand -base64 ${ bytes } ` ) ) . toString ( ) . trim ( )
53
+ const secret = await opensslService . exec ( `rand -base64 ${ bytes } ` )
69
54
70
55
res . send ( {
71
- secret,
56
+ secret : secret . toString ( ) . trim ( ) ,
72
57
algorithm,
73
58
bytes,
74
59
openssl : OPENSSL_VERSION
@@ -85,10 +70,10 @@ app.get('/api/generate/:algorithm', async (req, res) => {
85
70
curve = 'secp384r1'
86
71
}
87
72
88
- await opensslService ( `ecparam -genkey -name ${ curve } -noout -out ${ filePrivKey } ` )
89
- await opensslService ( `ec -in ${ filePrivKey } -pubout -out ${ filePubKey } ` )
73
+ await opensslService . exec ( `ecparam -genkey -name ${ curve } -noout -out ${ filePrivKey } ` )
74
+ await opensslService . exec ( `ec -in ${ filePrivKey } -pubout -out ${ filePubKey } ` )
90
75
91
- const { privateKey, publicKey } = await getAndDeleteKeyPair ( filePrivKey , filePubKey )
76
+ const { privateKey, publicKey } = await opensslService . getAndDeleteKeyPair ( filePrivKey , filePubKey )
92
77
93
78
res . send ( {
94
79
privateKey,
@@ -111,10 +96,12 @@ app.get('/api/generate/:algorithm', async (req, res) => {
111
96
}
112
97
} )
113
98
114
- opensslService ( 'version' ) . then ( version => {
99
+ opensslService . exec ( 'version' ) . then ( version => {
115
100
OPENSSL_VERSION = version . toString ( ) . trim ( )
116
101
117
- app . start ( process . env . PORT || 3000 )
102
+ const PORT = process . env . PORT || 3000
103
+ app . start ( PORT )
104
+ console . log ( 'API successfully running on port: ' + PORT )
118
105
} ) . catch ( err => {
119
106
console . error ( 'OpenSSL integration failed: ' + err . message )
120
107
} )
0 commit comments