diff --git a/.gitignore b/.gitignore index 3c36febe..79f6cf28 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ public/lib app/tests/coverage/ .bower-*/ .idea/ +config/sslcert/*.pem diff --git a/config/env/all.js b/config/env/all.js index 33ca9948..fe23c146 100644 --- a/config/env/all.js +++ b/config/env/all.js @@ -7,6 +7,7 @@ module.exports = { keywords: 'mongodb, express, angularjs, node.js, mongoose, passport' }, port: process.env.PORT || 3000, + secure: process.env.SECURE || false, templateEngine: 'swig', sessionSecret: 'MEAN', sessionCollection: 'sessions', @@ -39,4 +40,4 @@ module.exports = { 'public/modules/*/tests/*.js' ] } -}; \ No newline at end of file +}; diff --git a/config/env/development.js b/config/env/development.js index 4fe43e1d..2cadbd2a 100644 --- a/config/env/development.js +++ b/config/env/development.js @@ -40,4 +40,4 @@ module.exports = { } } } -}; \ No newline at end of file +}; diff --git a/config/env/production.js b/config/env/production.js index 92882f96..cc3e02cc 100644 --- a/config/env/production.js +++ b/config/env/production.js @@ -55,4 +55,4 @@ module.exports = { } } } -}; \ No newline at end of file +}; diff --git a/config/env/secure.js b/config/env/secure.js new file mode 100644 index 00000000..0b52f3a4 --- /dev/null +++ b/config/env/secure.js @@ -0,0 +1,60 @@ +'use strict'; + +module.exports = { + secure: true, + port: 443, + db: process.env.MONGOHQ_URL || process.env.MONGOLAB_URI || 'mongodb://localhost/mean', + assets: { + lib: { + css: [ + 'public/lib/bootstrap/dist/css/bootstrap.min.css', + 'public/lib/bootstrap/dist/css/bootstrap-theme.min.css', + ], + js: [ + 'public/lib/angular/angular.min.js', + 'public/lib/angular-resource/angular-resource.min.js', + 'public/lib/angular-animate/angular-animate.min.js', + 'public/lib/angular-ui-router/release/angular-ui-router.min.js', + 'public/lib/angular-ui-utils/ui-utils.min.js', + 'public/lib/angular-bootstrap/ui-bootstrap-tpls.min.js' + ] + }, + css: 'public/dist/application.min.css', + js: 'public/dist/application.min.js' + }, + facebook: { + clientID: process.env.FACEBOOK_ID || 'APP_ID', + clientSecret: process.env.FACEBOOK_SECRET || 'APP_SECRET', + callbackURL: 'https://localhost:443/auth/facebook/callback' + }, + twitter: { + clientID: process.env.TWITTER_KEY || 'CONSUMER_KEY', + clientSecret: process.env.TWITTER_SECRET || 'CONSUMER_SECRET', + callbackURL: 'https://localhost:443/auth/twitter/callback' + }, + google: { + clientID: process.env.GOOGLE_ID || 'APP_ID', + clientSecret: process.env.GOOGLE_SECRET || 'APP_SECRET', + callbackURL: 'https://localhost:443/auth/google/callback' + }, + linkedin: { + clientID: process.env.LINKEDIN_ID || 'APP_ID', + clientSecret: process.env.LINKEDIN_SECRET || 'APP_SECRET', + callbackURL: 'https://localhost:443/auth/linkedin/callback' + }, + github: { + clientID: process.env.GITHUB_ID || 'APP_ID', + clientSecret: process.env.GITHUB_SECRET || 'APP_SECRET', + callbackURL: 'https://localhost:443/auth/github/callback' + }, + mailer: { + from: process.env.MAILER_FROM || 'MAILER_FROM', + options: { + service: process.env.MAILER_SERVICE_PROVIDER || 'MAILER_SERVICE_PROVIDER', + auth: { + user: process.env.MAILER_EMAIL_ID || 'MAILER_EMAIL_ID', + pass: process.env.MAILER_PASSWORD || 'MAILER_PASSWORD' + } + } + } +}; diff --git a/config/express.js b/config/express.js index 5e2e8bb1..383c8898 100755 --- a/config/express.js +++ b/config/express.js @@ -3,7 +3,9 @@ /** * Module dependencies. */ -var express = require('express'), +var fs = require('fs'), + http = require('http'), + express = require('express'), morgan = require('morgan'), bodyParser = require('body-parser'), session = require('express-session'), @@ -36,6 +38,7 @@ module.exports = function(db) { app.locals.facebookAppId = config.facebook.clientID; app.locals.jsFiles = config.getJavaScriptAssets(); app.locals.cssFiles = config.getCSSAssets(); + app.locals.secure = config.secure; // Passing the request url to environment locals app.use(function(req, res, next) { @@ -137,5 +140,17 @@ module.exports = function(db) { }); }); - return app; -}; \ No newline at end of file + if (app.locals.secure) { + console.log('Securely using https protocol'); + var https = require('https'), + privateKey = fs.readFileSync('./config/sslcert/key.pem', 'utf8'), + certificate = fs.readFileSync('./config/sslcert/cert.pem', 'utf8'), + credentials = {key: privateKey, cert: certificate}, + httpsServer = https.createServer(credentials, app); + return httpsServer; + } else { + console.log('Insecurely using http protocol'); + var httpServer = http.createServer(app); + return httpServer; + } +}; diff --git a/config/sslcert/gen-certs b/config/sslcert/gen-certs new file mode 100755 index 00000000..70519705 --- /dev/null +++ b/config/sslcert/gen-certs @@ -0,0 +1,7 @@ +#!/bin/bash +echo "Generating self-signed certificates..." +openssl genrsa -out key.pem -aes256 1024 +openssl req -new -key key.pem -out csr.pem +openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out cert.pem +rm csr.pem +chmod 600 key.pem cert.pem