From 8eae6615744efeb3f15609de873a6fc2634c1a04 Mon Sep 17 00:00:00 2001 From: Richa Date: Sat, 14 Dec 2019 10:42:10 -0800 Subject: [PATCH] Adding https --- ExpressPassport-TokenAuth/app.js | 9 ++ ExpressPassport-TokenAuth/bin/cert.csr | 11 ++ ExpressPassport-TokenAuth/bin/certificate.pem | 13 +++ ExpressPassport-TokenAuth/bin/private.key | 15 +++ ExpressPassport-TokenAuth/bin/www | 100 +++++++++++------- 5 files changed, 107 insertions(+), 41 deletions(-) create mode 100644 ExpressPassport-TokenAuth/bin/cert.csr create mode 100644 ExpressPassport-TokenAuth/bin/certificate.pem create mode 100644 ExpressPassport-TokenAuth/bin/private.key diff --git a/ExpressPassport-TokenAuth/app.js b/ExpressPassport-TokenAuth/app.js index 7e663c2..5fef90b 100755 --- a/ExpressPassport-TokenAuth/app.js +++ b/ExpressPassport-TokenAuth/app.js @@ -32,6 +32,15 @@ connect.then((db) => { var app = express(); +// Redirect http to https +app.all("*", (req, res, next) => { + if (req.secure) { + return next(); + } else { + res.redirect(307, `https://${req.hostname}:${app.get("securePort")}${req.url}`); + } +}) + // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'pug'); diff --git a/ExpressPassport-TokenAuth/bin/cert.csr b/ExpressPassport-TokenAuth/bin/cert.csr new file mode 100644 index 0000000..b495d4a --- /dev/null +++ b/ExpressPassport-TokenAuth/bin/cert.csr @@ -0,0 +1,11 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIIBlDCB/gIBADA7MQswCQYDVQQGEwJVUzELMAkGA1UECAwCV0ExEDAOBgNVBAcM +B1NlYXR0bGUxDTALBgNVBAMMBHRlc3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ +AoGBAJ6hgd0UyrrINIRkAJRKKhuuojwFgBOatU800li0+YUEwm8n8VBDP/I78Zxu +Jj5yOYdg0Zk/PHiAbZadTkkoFCnv8DedA1ePFtKsnBR0v+A/bQqumhL00+TYdLOj +QygSm1NdMCkcJ6PStsCS22vAiEmG9pgxRrZBlDcgLmuwQ5INAgMBAAGgGjAYBgkq +hkiG9w0BCQcxCwwJcmljaGExMjM0MA0GCSqGSIb3DQEBCwUAA4GBABQ2bQQkZAUV +RvFPgtWv4Av/EgEUeiDliwSdFeh0Gsfy3of6mMEHg7K94iaGuOoN2oMUyG/HUDPG +rF1QyvM22CSZiMAPgf/S5ADcW0vc4WDRX052NVW7N50u961RI3XGcpzWHqmesajd +dQtGV76IyzrWX0CV3gZrOtD1nVyw8haj +-----END CERTIFICATE REQUEST----- diff --git a/ExpressPassport-TokenAuth/bin/certificate.pem b/ExpressPassport-TokenAuth/bin/certificate.pem new file mode 100644 index 0000000..769a745 --- /dev/null +++ b/ExpressPassport-TokenAuth/bin/certificate.pem @@ -0,0 +1,13 @@ +-----BEGIN CERTIFICATE----- +MIIB7TCCAVYCCQDJANNgw1M2fzANBgkqhkiG9w0BAQUFADA7MQswCQYDVQQGEwJV +UzELMAkGA1UECAwCV0ExEDAOBgNVBAcMB1NlYXR0bGUxDTALBgNVBAMMBHRlc3Qw +HhcNMTkxMjE0MTgyMDQ0WhcNMjAwMTEzMTgyMDQ0WjA7MQswCQYDVQQGEwJVUzEL +MAkGA1UECAwCV0ExEDAOBgNVBAcMB1NlYXR0bGUxDTALBgNVBAMMBHRlc3QwgZ8w +DQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAJ6hgd0UyrrINIRkAJRKKhuuojwFgBOa +tU800li0+YUEwm8n8VBDP/I78ZxuJj5yOYdg0Zk/PHiAbZadTkkoFCnv8DedA1eP +FtKsnBR0v+A/bQqumhL00+TYdLOjQygSm1NdMCkcJ6PStsCS22vAiEmG9pgxRrZB +lDcgLmuwQ5INAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAnD5F3yCQTzQV19//Ffc3 ++oVXWByHMFXFwAWzOUKo/KWqCTKmrtNf8db9/5SECSnuW16zwbTEKsIZxqc6ZuAe +E2DxNrWvsVR6XPiHDKKtdilgS9dxmCnQIweJ6MYwc8Jppf1eb+9LGeiohRFMjidZ +hOKTzUIEjR/Olt6GIYTlrFI= +-----END CERTIFICATE----- diff --git a/ExpressPassport-TokenAuth/bin/private.key b/ExpressPassport-TokenAuth/bin/private.key new file mode 100644 index 0000000..ac86e05 --- /dev/null +++ b/ExpressPassport-TokenAuth/bin/private.key @@ -0,0 +1,15 @@ +-----BEGIN RSA PRIVATE KEY----- +MIICXAIBAAKBgQCeoYHdFMq6yDSEZACUSiobrqI8BYATmrVPNNJYtPmFBMJvJ/FQ +Qz/yO/GcbiY+cjmHYNGZPzx4gG2WnU5JKBQp7/A3nQNXjxbSrJwUdL/gP20KrpoS +9NPk2HSzo0MoEptTXTApHCej0rbAkttrwIhJhvaYMUa2QZQ3IC5rsEOSDQIDAQAB +AoGAGzLsFmErRY1wW8bdxo9U2vw9evcid7oOHm+bJXSaV/Fx+ScrHCz7uOgITF8i +lZB2Rr0WEyWhk7PJloqQUsDZe/YrVWAm7viBqi8LkpJLSxhXwCmdBpX6JhJuNzfr +DjzNXmdAi8UxryKd7c+5e/Hjms2NWwhAatf7d8LpnP1uNgECQQDR4DNMvQlc6Zx1 +4QimHGJkZlYBSXQyQ0nRaWI3FHTKf6sTmw6QUkUPcBWQ+jaok08E0BxqLgLzhguJ +RQt0L/mNAkEAwX45Ijm0S1vgSYIrIqN1wOYdvR8K0H2at9kL46T1H+JbM8IwQ2ZH +OOW+fmYUMCOHaSeteVTpJvRdPs443diagQJAduLNrHaYyyz6GDw1LgN+KOlfZrM5 +Qw+izSEpJZJM5jiTrDN1eINh43DWhp12QN/RLyTEM9GEknYoKsYVyucyMQJBAKeK +jxgmlQnnnRapYoZUuYJ0/u0iMvACwnRMuPG9hQ69pjD0BWxm5GbM8BxscTCZoweX +dqsnrdawIwld8XmOs4ECQEybo4WKVWQ510Kr3Yw9MesuTLBmZXonkD+D0EX7Y0zg +bA+CM53Z4qVJWp9K75mhPJouCocgR7/Ae4yhg1QjvBw= +-----END RSA PRIVATE KEY----- diff --git a/ExpressPassport-TokenAuth/bin/www b/ExpressPassport-TokenAuth/bin/www index 2cdf9fa..00b4130 100755 --- a/ExpressPassport-TokenAuth/bin/www +++ b/ExpressPassport-TokenAuth/bin/www @@ -4,26 +4,44 @@ * Module dependencies. */ -var app = require('../app'); -var debug = require('debug')('confusionserver:server'); -var http = require('http'); +const app = require('../app'); +const debug = require('debug')('confusionserver:server'); +const http = require('http'); +const https = require('https'); +const fs = require('fs'); /** * Get port from environment and store in Express. */ -var port = normalizePort(process.env.PORT || '3000'); +const port = normalizePort(process.env.PORT || '3000'); app.set('port', port); +app.set('securePort', port + 443); /** * Create HTTP server. */ -var server = http.createServer(app); +const server = http.createServer(app); + +/** + * Create HTTPS server. + */ + +const options = { + key: fs.readFileSync(__dirname + '/private.key'), + cert: fs.readFileSync(__dirname + '/certificate.pem') +} +const secureServer = https.createServer(options, app); /** * Listen on provided port, on all network interfaces. */ +secureServer.listen(app.get('securePort'), () => { + console.log(`Server listening on port ${app.get('securePort')}`); +}); +secureServer.on('error', onError); +secureServer.on('listening', onListening); server.listen(port); server.on('error', onError); @@ -34,19 +52,19 @@ server.on('listening', onListening); */ function normalizePort(val) { - var port = parseInt(val, 10); + const port = parseInt(val, 10); - if (isNaN(port)) { - // named pipe - return val; - } + if (isNaN(port)) { + // named pipe + return val; + } - if (port >= 0) { - // port number - return port; - } + if (port >= 0) { + // port number + return port; + } - return false; + return false; } /** @@ -54,27 +72,27 @@ function normalizePort(val) { */ function onError(error) { - if (error.syscall !== 'listen') { - throw error; - } - - var bind = typeof port === 'string' - ? 'Pipe ' + port - : 'Port ' + port; - - // handle specific listen errors with friendly messages - switch (error.code) { - case 'EACCES': - console.error(bind + ' requires elevated privileges'); - process.exit(1); - break; - case 'EADDRINUSE': - console.error(bind + ' is already in use'); - process.exit(1); - break; - default: - throw error; - } + if (error.syscall !== 'listen') { + throw error; + } + + var bind = typeof port === 'string' + ? 'Pipe ' + port + : 'Port ' + port; + + // handle specific listen errors with friendly messages + switch (error.code) { + case 'EACCES': + console.error(bind + ' requires elevated privileges'); + process.exit(1); + break; + case 'EADDRINUSE': + console.error(bind + ' is already in use'); + process.exit(1); + break; + default: + throw error; + } } /** @@ -82,9 +100,9 @@ function onError(error) { */ function onListening() { - var addr = server.address(); - var bind = typeof addr === 'string' - ? 'pipe ' + addr - : 'port ' + addr.port; - debug('Listening on ' + bind); + const addr = server.address(); + const bind = typeof addr === 'string' + ? 'pipe ' + addr + : 'port ' + addr.port; + debug('Listening on ' + bind); }