-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsession.js
224 lines (188 loc) · 7.23 KB
/
session.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Session + Redis Store
// (c)copyright 2014 by Gerald Wodni <[email protected]>
"use strict";
var crypto = require("crypto");
var moment = require("moment");
var _ = require("underscore");
var defaults = {
cookie: "kernSession",
externalCookie: "kernSessionExternal",
timeout: 3000, /* in seconds */
autostart: false
}
/* rdb: redis database */
module.exports = function _session( k, opts ) {
opts = _.extend( defaults, opts );
if( !( "key" in opts ) )
opts.key = crypto.pseudoRandomBytes( 256 );
if( process.env.KERN_SESSION_TIMEOUT ) {
opts.timeout = process.env.KERN_SESSION_TIMEOUT;
console.log( "Session-timeout-env:".bold.magenta, `${opts.timeout}s (${opts.timeout/60}min)` );
}
/* active sessions */
function activeSessions( website, opts = {} ) {
return new Promise( (fulfill, reject) => {
k.rdb.keys( `session:${website}:*`, (err, sessionKeys) => {
if( err ) return reject( err );
const multi = k.rdb.multi();
sessionKeys.forEach( sessionKey => multi.hgetall( sessionKey ) );
sessionKeys.forEach( sessionKey => multi.ttl( sessionKey ) );
/* fetch sessions */
const now = new moment();
multi.exec( (err, data ) => {
const sessions = data.slice( 0, data.length/2 );
const ttls = data.slice( data.length/2 );
if( err ) return reject( err );
for( var i = 0; i < sessions.length; i++ ) {
const lastActivity = moment( sessions[i]["session:activity"] );
sessions[i]["session:activityDuration"] = moment.utc( now.diff( lastActivity ) );
sessions[i]["session:ttl"] = ttls[i];
if( opts.addKey )
sessions[i]["session:key"] = sessionKeys[i];
}
fulfill( sessions );
});
});
});
}
/* callback( randomaHashed ) */
function randomHash( callback ) {
crypto.pseudoRandomBytes(256, function( ex, buf) {
var hash = crypto.createHmac( "sha256", opts.key );
hash.update( buf );
callback( hash.digest( "hex" ) );
});
}
function pRandomHash() {
return new Promise( (fulfill, reject) => {
randomHash( fulfill );
});
}
function sessionKey( req ) {
return "session:" + req.kern.website + ":" + req.sessionId;
}
function setCookie( req, res ) {
const cookieOpts = req.kern.getWebsiteConfig( "sessionCookies", { sameSite: 'strict' } );
cookieOpts.httpOnly = true;
cookieOpts.maxAge = opts.timeout * 1000;
res.cookie( opts.cookie, req.sessionId, cookieOpts );
}
function setExternalCookie( req, res ) {
console.log( "EXTERNAL COOKIE SETTYYYYYY!" );
const cookieOpts = {
sameSite: 'Lax',
//secure: true,
httpOnly: true,
maxAge: opts.timeout * 1000
};
res.cookie( opts.externalCookie, req.sessionId, cookieOpts );
}
function start( req, res, next ) {
if( typeof req.session !== "undefined" )
return next( new Error( "Session already started" ) );
console.log( "START session" );
randomHash( function( id ) {
req.sessionId = id;
req.session = {
}
var key = sessionKey( req );
k.rdb.hset( key, "session:started", moment().format( "YYYY-MM-DD hh:mm:ss" ) );
k.rdb.expire( key, opts.timeout );
setCookie( req, res );
next();
});
};
function load( req, res, next ) {
var key = sessionKey( req );
k.rdb.expire( key, opts.timeout );
setCookie( req, res );
k.rdb.hgetall( key, function( err, values) {
/* only assign a session containing data (i.e. cookie sent which does not match session) */
if( values ) {
req.session = values;
req.session["session:activity"] = moment().format( "YYYY-MM-DD hh:mm:ss" );
}
//console.log( "LOAD session", req.sessionId );
next();
});
};
function save( req, res, next ) {
/* start saving the session */
if( req.session ) {
//console.log( "SAVE session", req.sessionId );
var sKey = sessionKey( req );
_.map( req.session, function( value, key ) {
k.rdb.hset( sKey, key, value );
});
}
/* no need to wait for redis to finish */
next();
};
async function destroyByPseudoId( website, pseudoId ) {
const sessions = await activeSessions( website, { addKey: true } );
for( let session of sessions ) {
if( session.loggedInPseudoId == pseudoId ) {
await new Promise( (fulfill, reject) => k.rdb.del( session["session:key"], err => err ? reject(err) : fulfill() ) );
return;
}
}
throw new Error( `destroyByPseudoId: session with pseudoId ${pseudoId} in ${website} not found` );
}
function destroy( req, res, next ) {
if( req.session ) {
res.clearCookie( opts.cookie );
res.clearCookie( opts.externalCookie );
if( k.users.persistentLoginCookie )
res.clearCookie( k.users.persistentLoginCookie );
k.rdb.del( sessionKey( req ) );
req.session = undefined;
}
next();
}
function destroyExternalCookie( req, res, next ) {
if( req.session )
res.clearCookie( opts.externalCookie );
}
function route() {
k.app.use( function _route( req, res, next ) {
/* add interface hooks */
req.sessionInterface = {
start: start,
save: save,
setExternalCookie,
destroyExternalCookie,
destroy: destroy
}
/* if kernSession-cookie exists, attempt to load session */
if( req.cookies && opts.cookie in req.cookies ) {
req.sessionId = req.cookies[ opts.cookie ];
load( req, res, next );
}
/* external return? allow reload */
else if( req.cookies && opts.externalCookie in req.cookies ) {
console.log( "RESUME SESSION FROM external cookie".bold.cyan );
req.sessionId = req.cookies[ opts.externalCookie ];
load( req, res, next );
}
else if( opts.autostart )
start( req, res, next );
else
next();
});
}
function pushPostHook() {
k.hooks.add( "post", function( req, res ) {
/* save session (so there is one ) */
/* TODO: store sessionId in req.sessionId? */
if( req.sessionInterface )
req.sessionInterface.save( req, res, function() {} )
});
}
return {
getActive: activeSessions,
destroyByPseudoId,
randomHash: pRandomHash,
pushPostHook: pushPostHook,
route: route
}
}