Skip to content

Commit

Permalink
feat(https): add enforce https middleware (#9)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: changed enforce ssl behavior
  • Loading branch information
szeist authored and felin-arch committed May 19, 2016
1 parent 75403bf commit 93b8223
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,19 @@ export HTTPS_CERT="path/to/cert.crt"
node server.js
```

### Enforce HTTPS

Add the [koa-ssl](https://github.com/jclem/koa-ssl) middlware:
```
var app = new App(koaApp);
app.addEnforceSSLMiddleware();
```

If your application is running behind reverse proxy you should set the trustProxy configiration option for detecting the x-forwarded-proto header.
```
var app = new App(koaApp);
app.addEnforceSSLMiddleware({ trustProxy: true });
```

If you use this middleware this middleware should be the first you add.
9 changes: 6 additions & 3 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var methodOverride = require('koa-methodoverride');
var HookMiddlewareFactory = require('./middlewares/hook');
var bodyparser = require('koa-bodyparser');
var requestId = require('koa-request-id');
var sslify = require('koa-sslify');
var ssl = require('koa-ssl');
var SecurityMiddlewareFactory = require('../lib/security-middleware-factory');


Expand Down Expand Up @@ -98,6 +98,11 @@ App.prototype = {
},


addEnforceSSLMiddleware: function(options) {
this.addMiddleware(ssl(options));
},


listen: function(port, env) {
var httpPort = parseInt(port);
this._startHTTPServer(httpPort, env);
Expand All @@ -122,8 +127,6 @@ App.prototype = {
httpsOptions.cert = fs.readFileSync(process.env.HTTPS_CERT);
}

this.addMiddleware(sslify({ port: port }));

https.createServer(httpsOptions, this.koaApp.callback()).listen(port);
console.log('Application started (with SSL):', { port: port, env: env });
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"koa-methodoverride": "2.0.0",
"koa-request-id": "1.0.1",
"koa-router": "5.4.0",
"koa-sslify": "1.0.1",
"koa-ssl": "2.0.0",
"koa-static": "2.0.0",
"lodash": "4.6.0",
"logentries-logformat": "0.1.4"
Expand Down

0 comments on commit 93b8223

Please sign in to comment.