3 const assert = require('assert');
6 * This code removes a possible session fixation. We regenerate the session
7 * upon logging in so an anonymous session cookie can't be reused.
10 * - https://github.com/jaredhanson/passport/issues/192#issuecomment-162836516
11 * - https://stackoverflow.com/a/26394156/606571
13 async function regeneratePassportSession(req) {
14 const passportSession = req.session.passport;
15 // parameter used for returning to URL user visited before authentication
16 // used for OAuth login flow (https://gitlab.com/gitterHQ/webapp/issues/2283)
17 const { returnTo } = req.session;
18 return new Promise((resolve, reject) =>
19 req.session.regenerate(function(err) {
21 assert(!req.session.passport);
22 req.session.passport = passportSession;
23 req.session.returnTo = returnTo;
24 req.session.save(function(err) {
33 * Adds user to passport, if this is the
34 * first time (user just logged in) we generate a new session
35 * and returns a user with identity object
37 module.exports = async function(req, user) {
38 // if user just logged in (session hasn't been authenticated before)
39 if (!req.user) await regeneratePassportSession(req);
40 await new Promise((resolve, reject) => {
41 req.login(user, err => {