11const restana = require ( 'restana' )
22const opensslService = require ( './openssl-service' )
3- const fs = require ( 'fs/promises' )
43const { v4 : uuidv4 } = require ( 'uuid' )
54const assert = require ( 'assert' )
65const { AssertionError } = require ( 'assert' )
76
87const ALLOWED_BITS = [ 1024 , 2048 , 3072 , 4096 ]
9-
108let OPENSSL_VERSION
119
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-
2510const app = restana ( { } )
2611
2712app . get ( [ '/api/openssl-version' , '/api/health/status' ] , async ( req , res ) => {
@@ -48,10 +33,10 @@ app.get('/api/generate/:algorithm', async (req, res) => {
4833 case 'PS256' :
4934 case 'PS384' :
5035 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 } ` )
5338
54- const { privateKey, publicKey } = await getAndDeleteKeyPair ( filePrivKey , filePubKey )
39+ const { privateKey, publicKey } = await opensslService . getAndDeleteKeyPair ( filePrivKey , filePubKey )
5540
5641 res . send ( {
5742 privateKey,
@@ -65,10 +50,10 @@ app.get('/api/generate/:algorithm', async (req, res) => {
6550 case 'HS256' :
6651 case 'HS384' :
6752 case 'HS512' : {
68- const secret = ( await opensslService ( `rand -base64 ${ bytes } ` ) ) . toString ( ) . trim ( )
53+ const secret = await opensslService . exec ( `rand -base64 ${ bytes } ` )
6954
7055 res . send ( {
71- secret,
56+ secret : secret . toString ( ) . trim ( ) ,
7257 algorithm,
7358 bytes,
7459 openssl : OPENSSL_VERSION
@@ -85,10 +70,10 @@ app.get('/api/generate/:algorithm', async (req, res) => {
8570 curve = 'secp384r1'
8671 }
8772
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 } ` )
9075
91- const { privateKey, publicKey } = await getAndDeleteKeyPair ( filePrivKey , filePubKey )
76+ const { privateKey, publicKey } = await opensslService . getAndDeleteKeyPair ( filePrivKey , filePubKey )
9277
9378 res . send ( {
9479 privateKey,
@@ -111,10 +96,12 @@ app.get('/api/generate/:algorithm', async (req, res) => {
11196 }
11297} )
11398
114- opensslService ( 'version' ) . then ( version => {
99+ opensslService . exec ( 'version' ) . then ( version => {
115100 OPENSSL_VERSION = version . toString ( ) . trim ( )
116101
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 )
118105} ) . catch ( err => {
119106 console . error ( 'OpenSSL integration failed: ' + err . message )
120107} )
0 commit comments