1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-13 11:17:26 +02:00

fix: add TTL to sessions

This commit is contained in:
Ivar Conradi Østhus 2020-10-02 16:32:05 +02:00
parent ef5b67974d
commit ce0c66d127
6 changed files with 14 additions and 16 deletions

View File

@ -21,12 +21,6 @@ function basicAuthentication(app) {
.set({ 'WWW-Authenticate': 'Basic realm="example"' }) .set({ 'WWW-Authenticate': 'Basic realm="example"' })
.end('access denied'); .end('access denied');
}); });
app.use((req, res, next) => {
// Updates active sessions every hour
req.session.nowInHours = Math.floor(Date.now() / 3600e3);
next();
});
} }
module.exports = basicAuthentication; module.exports = basicAuthentication;

View File

@ -4,7 +4,7 @@ const User = require('../user');
function noneAuthentication(basePath = '', app) { function noneAuthentication(basePath = '', app) {
app.use(`${basePath}/api/admin/`, (req, res, next) => { app.use(`${basePath}/api/admin/`, (req, res, next) => {
req.user = new User({ email: 'none@unknown.com' }); req.user = new User({ username: 'unknown' });
next(); next();
}); });
} }

View File

@ -24,6 +24,6 @@ test('should add dummy user object to all requests', t => {
.get('/api/admin/test') .get('/api/admin/test')
.expect(200) .expect(200)
.expect(res => { .expect(res => {
t.true(res.body.email === 'none@unknown.com'); t.true(res.body.username === 'unknown');
}); });
}); });

View File

@ -3,11 +3,21 @@
const cookieSession = require('cookie-session'); const cookieSession = require('cookie-session');
module.exports = function(config) { module.exports = function(config) {
return cookieSession({ const sessionMiddleware = cookieSession({
name: 'unleash-session', name: 'unleash-session',
keys: [config.secret], keys: [config.secret],
maxAge: config.sessionAge, maxAge: config.sessionAge,
secureProxy: !!config.secureHeaders, secureProxy: !!config.secureHeaders,
path: config.baseUriPath === '' ? '/' : config.baseUriPath, path: config.baseUriPath === '' ? '/' : config.baseUriPath,
}); });
const extendTTL = (req, res, next) => {
// Updates active sessions every hour
req.session.nowInHours = Math.floor(Date.now() / 3600e3);
next();
};
return (req, res, next) => {
sessionMiddleware(req, res, () => extendTTL(req, res, next));
};
}; };

View File

@ -35,12 +35,6 @@ function unsecureAuthentication(basePath = '', app) {
) )
.end(); .end();
}); });
app.use((req, res, next) => {
// Updates active sessions every hour
req.session.nowInHours = Math.floor(Date.now() / 3600e3);
next();
});
} }
module.exports = unsecureAuthentication; module.exports = unsecureAuthentication;

View File

@ -96,7 +96,7 @@ test.serial('creates new feature toggle with createdBy unknown', async t => {
strategies: [{ name: 'default' }], strategies: [{ name: 'default' }],
}); });
await request.get('/api/admin/events').expect(res => { await request.get('/api/admin/events').expect(res => {
t.true(res.body.events[0].createdBy === 'none@unknown.com'); t.is(res.body.events[0].createdBy, 'unknown');
}); });
}); });