Skip to content
This repository has been archived by the owner on Dec 28, 2018. It is now read-only.

Commit

Permalink
Auto merge of #640 - saneyuki:session, r=saneyuki
Browse files Browse the repository at this point in the history
feat(server): set session id for request.

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/karen-irc/karen/640)
<!-- Reviewable:end -->
  • Loading branch information
dokidokivisual committed May 4, 2016
2 parents ea17f5a + 35e907a commit ba9e1e6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@
"scripts": {
"prestart": "npm run build",
"start": "node index.js",

"clean": "gulp clean",

"build": "gulp build",
"build_client": "gulp build:client",
"build_server": "gulp build:server",

"test": "npm run lint",
"lint": "gulp jslint",

"tsc": "gulp tsc",
"gulp": "gulp"
},
Expand Down Expand Up @@ -59,6 +55,7 @@
"cookies-js": "^1.2.2",
"event-stream": "^3.3.2",
"express": "^4.13.4",
"express-session": "^1.13.0",
"mkdirp": "^0.5.1",
"moment": "^2.13.0",
"option-t": "^0.18.3",
Expand Down
10 changes: 6 additions & 4 deletions src/server/app/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ import {confirmAuth, initializeConnection} from '../route/socketio';

import {ClientManager} from '../ClientManager';

import {applyGenericSecurityHeader} from './security';
import {applyGenericSecurityHeader, setSessionMiddleware} from './security';

export class KarenServer {

constructor(options) {
const config = ConfigDriver.getConfig();

this._config = Object.assign(config, options);
this._express = createExpress();
this._express = createExpress(this._config);
this._server = createServer(this._express, this._config);
this._socketIo = createSocketIo(this._server, this._config);
this._manager = new ClientManager();
Expand Down Expand Up @@ -102,12 +102,14 @@ export class KarenServer {
}
}

function createExpress() {
function createExpress(config) {
const app = express();
app.set('x-powered-by', false);
app.use(applyGenericSecurityHeader);
app.use(compression());
app.enable('trust proxy');

setSessionMiddleware(app, config);

return app;
}

Expand Down
21 changes: 21 additions & 0 deletions src/server/app/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import expressSession from 'express-session';

const STRICT_TRANSPORT_SECURITY_EXPIRE_TIME = String(60 * 24 * 365 * 1000);

export function applyGenericSecurityHeader(req, res, next) {
Expand Down Expand Up @@ -53,4 +55,23 @@ const cspDirectiveStr = [...cspDirective.entries()].map(function([key, value]){
export function applyHtmlSecurtyHeader(req, res) {
res.setHeader('Content-Security-Policy', cspDirectiveStr);
res.setHeader('X-Frame-Options', 'DENY');
}

export function setSessionMiddleware(express, config) {
express.enable('trust proxy');

const httpsOptions = config.https || {};
const sessionOption = {
cookie: {
path: '/',
httpOnly: true,
secure: !!httpsOptions.enable,
maxAge: null,
},
secret: String(Date.now() * Math.random),
resave: false,
name: 'karen.sessionid',
saveUninitialized: config.public,
};
express.use(expressSession(sessionOption));
}

0 comments on commit ba9e1e6

Please sign in to comment.