@@ -184,42 +184,34 @@ var loadSslCert = function(callback) {
184
184
}
185
185
} ;
186
186
187
+ var storeSecret = crypto . randomBytes ( 16 ) . toString ( 'utf-8' ) ;
188
+ var sessionStore = new express . session . MemoryStore ( ) ;
189
+
187
190
var io ;
188
- var storesecret = crypto . randomBytes ( 16 ) . toString ( 'utf-8' )
189
191
var socketMap = { } ;
190
192
var initSocketIO = function ( server ) {
191
193
io = socketio . listen ( server ) ;
194
+ var sioCookieParser = express . cookieParser ( storeSecret ) ;
195
+
192
196
io . set ( 'log level' , 1 ) ; //TODO: hack to fix recursion problem since we are piping log info to a socket
193
197
194
- // sync session data with socket
195
- // via https://github.com/DanielBaulig/sioe-demo/blob/master/app.js
196
198
io . set ( 'authorization' , function ( handshake , accept ) {
197
- if ( ! handshake . headers . cookie ) {
198
- console . log ( 'no cookie sent with socket connection' ) ;
199
- return accept ( 'No cookie transmitted.' , false ) ;
200
- }
201
-
202
- handshake . cookie = cookie . parse ( handshake . headers . cookie ) ;
203
- handshake . sessionID = connect . utils . parseSignedCookie ( handshake . cookie [ 'connect.sid' ] , storesecret ) ;
204
-
205
- if ( handshake . cookie [ 'connect.sid' ] == handshake . sessionID ) {
206
- return accept ( 'Cookie is invalid' , false ) ;
207
- }
208
-
209
- handshake . sessionStore = sslapp . sessionStore ;
210
-
211
- if ( ! handshake . sessionID ) {
212
- return accept ( 'Session cookie could not be found' , false ) ;
213
- }
214
-
215
- handshake . sessionStore . get ( handshake . sessionID , function ( err , session ) {
199
+ sioCookieParser ( handshake , { } , function ( err ) {
216
200
if ( err ) {
217
- console . log ( 'error loading session' ) ;
218
- return accept ( 'Error' , false ) ;
201
+ accept ( err , false ) ;
202
+ }
203
+ else {
204
+ sessionStore . get ( handshake . signedCookies [ "connect.sid" ] , function ( err , sessionData ) {
205
+ if ( err || ! sessionData ) {
206
+ accept ( 'Session error' , false ) ;
207
+ }
208
+ else {
209
+ handshake . sessionStore = sessionStore ;
210
+ handshake . session = new express . session . Session ( handshake , sessionData ) ;
211
+ accept ( null , true ) ;
212
+ }
213
+ } ) ;
219
214
}
220
-
221
- var s = handshake . session = new express . session . Session ( handshake , session ) ;
222
- return accept ( null , true ) ;
223
215
} ) ;
224
216
} ) ;
225
217
@@ -237,7 +229,7 @@ var initSocketIO = function( server ) {
237
229
238
230
io . sockets . on ( 'connection' , function ( socket ) {
239
231
240
- var sess = socket . handshake . session ;
232
+ socket . session = socket . handshake . session ;
241
233
242
234
socket . socketID = genRandomID ( ) ;
243
235
socketMap [ socket . socketID ] = socket ;
@@ -248,7 +240,7 @@ var initSocketIO = function( server ) {
248
240
} ) ;
249
241
250
242
socket . on ( 'appdata' , function ( data ) {
251
- if ( ! sess . authenticated ) {
243
+ if ( ! socket . session . authenticated ) {
252
244
return ;
253
245
}
254
246
if ( data . appid !== undefined && data . appid . match ( / ^ \w + $ / ) && data . key !== undefined ) {
@@ -294,7 +286,7 @@ console.log = function(d) {
294
286
var clients = io . sockets . clients ( ) ;
295
287
for ( var x = 0 ; x < clients . length ; x ++ ) {
296
288
var c = clients [ x ] ;
297
- var sess = c . handshake . session ;
289
+ var sess = c . session ;
298
290
if ( sess . authenticated ) {
299
291
c . emit ( 'SERVERLOG' , d ) ;
300
292
}
@@ -373,8 +365,8 @@ coderapp.use( express.bodyParser() );
373
365
coderapp . use ( express . cookieParser ( ) ) ;
374
366
coderapp . use ( express . session ( {
375
367
key : 'connect.sid' ,
376
- secret : storesecret ,
377
- store : new express . session . MemoryStore ( )
368
+ secret : storeSecret ,
369
+ store : sessionStore
378
370
} ) ) ;
379
371
coderapp . use ( '/static' , express . static ( __dirname + '/static' ) ) ;
380
372
coderapp . get ( '/' , function ( req , res ) {
0 commit comments