-
Notifications
You must be signed in to change notification settings - Fork 0
/
django-auth.js
executable file
·335 lines (288 loc) · 10.8 KB
/
django-auth.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* Export the wrapper.
*/
var connect = require('connect')
,parseCookie = require('connect').utils.parseCookie
,redis = require('redis');
exports = module.exports = SocketAuthentication;
function SocketAuthentication(socket, options, onSessionLoadedCB) {
var redis = options.rc ? options.rc : redis.createClient();
var session_timeout = (options && options.session_timeout) ? options.session_timeout * 60 : 20 * 60;
this.debug_mode = options && options.debug || false;
var self = this;
/**
* Convienience function. Allows me to put the initialisations at
* the top of the file before all the functions are defined. This
* is called at the bottom of this class.
*/
var _init = function() {
// We assume we're passed a connected socket. There's probably
// a way to detect this...
//if (socket) socket.on('connection', onConnect);
if (socket) onConnect();
};
/**
* This receives the handshake function built into socket-io. This is
* largely modeled on http://www.danielbaulig.de/socket-ioexpress/.
*
* It extracts the sessionid from the headers, loads the session
* data from redis, and stores it in the handshake object so it's
* accessible from the socket object.
*
* @param handshake:io.handshake
* @param accept:Function(err:String, acceptConnection:Boolean)
*/
this.onAuthorization = function(handshake, accept) {
//~ console.log('*** AUTHORIZATION() ***', handshake);
// Parse the cookie if it's present.
if (handshake.headers.cookie) {
handshake.cookie = parseCookie(handshake.headers.cookie);
// Django stores its session ID as 'sessionid'.
var sessionID = handshake.cookie['sessionid'];
handshake.sessionID = sessionID;
loadSession(sessionID, function(err, session) {
if (!err) {
handshake.session = session;
onSessionLoaded(err, session);
}
});
// Accept the incoming connection whether or not the session
// loads successfully.
//~ accept(null, true);
//~ }
//~ else {
//~ // No cookie? Still connect.
//~ accept('No cookie transmitted.', true);
}
// Accept the incoming connection whether or not the session
// loads successfully.
accept(null, true);
}
/**
* Adds more handlers and stores the session->socket link for valid
* connections.
* @param socket:io.Socket
*/
var onConnect = function() {
// Ask for the session on session-less connections.
if (!socket.handshake.sessionID) {
socket.emit('request_session_id');
socket.on('session', onSessionIdReceived);
}
else {
linkSocketToSession();
}
socket.on('disconnect', function() {
unlinkSocketFromSession();
});
}
/**
*
*/
var onSessionLoaded = function(err, session) {
// Reset the expire on the session key for good sessions.
if (session) redis.expire(buildSessionKey(session.id), session_timeout);
if (session) {
socket.emit('session_auth');
}
if (onSessionLoadedCB) onSessionLoadedCB(err, session)
}
/**
* Handler for accepting a session id.
*/
var onSessionIdReceived = function(data) {
if (self.debug_mode)
console.log('*** onSessionIdReceived ***', 'sessionid:', data.sessionid);
loadSession(data.sessionid, onSessionLoaded);
}
//~ var onConnect = function (sck) {
//~ socket = sck;
//~
//~ socket.emit('authorize');
//~ console.log('socket.id', socket.id)
//~
//~ // Ping event to refresh session of opened browser window
//~ // --------------------------------------------
//~ // socket.on('auth_ping', onAuthPing);
//~
//~ socket.on('auth', onAuth);
//~
//~ }
//~
//~
//~ /**
//~ * Ping event to refresh session of opened browser window.
//~ */
//~ var onAuthPing = function (data) {
//~ console.log("ping sessionid:", data.sessionid);
//~ self.ping(data.sessionid, function(err, session) {
//~ console.log("Pinged: ", session, data.sessionid)
//~ });
//~ }
//~
//~
//~ this.ping = function(sessionid, callback) {
//~ console.log("pinging session ", sessionid)
//~ self.authorize(sessionid, callback);
//~ };
/**
*
* The functions below serve more-or-less as a session model and
* might go better in an explicit model class but they're fine here
* for now.
*
* Only the currently externally accessed functions are exposed.
* Private functions can be made public as needed.
*
*/
/**
* Gets all the sessions associated with a user.
* @param userid:Int
* @param callback:Function(err:String, sessions:Array)
*/
var userSessions = function(userid, callback) {
userSessionKeys(userid, function(err, sessionids) {
loadSessions(sessionids, callback);
});
};
/**
* Gets all the session keys associated with a user.
* @param userid:Int
* @param callback:Function(err:String, sessionKeys:Array)
*/
var userSessionKeys = function(userid, callback) {
redis.smembers(buildUsersSessionsKey(userid), callback)
};
/**
* Centralises session key creation.
* @param sessionid:String
*/
var buildSessionKey = function(sessionid) {
return 'session:' + sessionid;
};
/**
* Centralises the socket.session list key creation.
* @param sessionid:String
*/
var buildSessionSocketKey = function() {
return 'socket.session:' + socket.sessionID;
};
/**
* Centralises the session.user list key creation.
* @param userid:Int
*/
var buildUsersSessionsKey = function(userid) {
return 'session.user:' + userid;
};
/**
* This is an optimisation to avoid having to retrieve a session id
* in self.userSockets().
* @param sessionKey:String
*/
var buildSocketKeyFromSessionKey = function(sessionKey) {
return sessionKey.replace('session:', 'socket.session:');
};
/**
* Returns the active socketids for a user in a callback.
* @param user_id:Int
* @param callback:Function(err, socketids)
*/
this.userSockets = function(user_id, callback) {
if (!callback) return;
userSessionKeys(user_id, function(err, sessionKeys) {
if (err) {
callback(err);
}
else {
var sockets = [], cmds = sessionKeys.map(function(key) {
//console.log('fetching sockets for key:', key);
return ['smembers', buildSocketKeyFromSessionKey(key)];
});
//console.log('cmds:', cmds);
redis.multi(cmds).exec(function(err, results) {
if (self.debug_mode)
console.log('found sockets:', err, results);
results.forEach(function(r) {
sockets = sockets.concat(r);
});
callback(err, sockets);
});
}
});
};
/**
* Associates the passed socket with the session so we can
* broadcast events out to all sockets associated with a given
* session. The result array passed to the callback is not
* terribly helpful. It will generally be [0,1] or [1,1] to show
* that the addition was or wasn't new and that the expire was
* successful, respectfully.
* @param socket:io.Socket
* @param callback:Function(err:String, result:Array)
*/
var linkSocketToSession = function(sessionid,callback) {
socket.sessionID = sessionid;
var socketKey = buildSessionSocketKey(sessionid);
if (self.debug_mode)
console.log('*********** link to *************',socketKey)
redis.multi()
.sadd(socketKey, socket.id)
.expire(socketKey, session_timeout)
.exec(function(err, result) {
if (self.debug_mode)
console.log("User connected with sessionID:", socket.handshake.sessionID);
callback && callback(err, result);
});
callback && callback("Not a valid session.", null);
};
/**
* Disassociates the passed socket from its session.
* @param socket:io.Socket
*/
var unlinkSocketFromSession = function() {
redis.srem(buildSessionSocketKey(), socket.id);
};
/**
* Stores a session object in a Redis hash. We could get the
* sessionid from the session object...
* @param userid:Int
* @param sessionid:String
* @param session:Object
*/
this.addSession = function(userid, sessionid, session) {
var sessionKey = buildSessionKey(sessionid);
redis.multi([
['hmset', sessionKey, session],
['expire', sessionKey, session_timeout],
['sadd', buildUsersSessionsKey(userid), sessionKey],
['expire', buildUsersSessionsKey(userid), session_timeout]
]).exec(function(err, result) {
if (self.debug_mode)
console.log('SocketAuthentication.addSession()', err, result);
});
};
/**
* Returns the session as the second argument in the callback.
* @param sessionid:String
* @param callback:Function(err:String, session:Object)
*/
var loadSession = function(sessionid, callback) {
redis.hgetall(buildSessionKey(sessionid), function(err, session) {
linkSocketToSession(sessionid);
callback && callback(err, session);
});
};
/**
* Basically mget for session hashes.
* @param sessionids:Array
* @param callback:Function(err:String, sessions:Array)
*/
var loadSessions = function(sessionids, callback) {
var cmds = sessionids.map(function(id) {
return ['hgetall', buildSessionKey(id)];
});
redis.multi(cmds).exec(callback);
};
_init();
return this;
}