Permalink
Browse files

Adding ID generation override to configuration

  • Loading branch information...
martinthomson committed May 15, 2012
1 parent 3b9715e commit d852c4ca51716a12ed2e3c9d180e7c0705b88db9
Showing with 137 additions and 5 deletions.
  1. +9 −5 lib/manager.js
  2. +128 −0 test/manager.test.js
View
@@ -705,8 +705,12 @@ Manager.prototype.handleClient = function (data, req) {
* @api private
*/
-Manager.prototype.generateId = function () {
- var rand = new Buffer(15); // multiple of 3 for base64
+Manager.prototype.generateId = function (data) {
+ var rand;
+ if (this.get('id generator')) {
+ return this.get('id generator').call(this, data);
+ }
+ rand = new Buffer(15); // multiple of 3 for base64
this.sequenceNumber = (this.sequenceNumber + 1) | 0;
rand.writeInt32BE(this.sequenceNumber, 11);
if (crypto.randomBytes) {
@@ -765,7 +769,7 @@ Manager.prototype.handleHandshake = function (data, req, res) {
if (err) return error(err);
if (authorized) {
- var id = self.generateId()
+ var id = self.generateId(newData || handshakeData)
, hs = [
id
, self.enabled('heartbeats') ? self.get('heartbeat timeout') || '' : ''
@@ -885,9 +889,9 @@ Manager.prototype.authorize = function (data, fn) {
if (this.get('authorization')) {
var self = this;
- this.get('authorization').call(this, data, function (err, authorized) {
+ this.get('authorization').call(this, data, function (err, authorized, newData) {
self.log.debug('client ' + authorized ? 'authorized' : 'unauthorized');
- fn(err, authorized);
+ fn(err, authorized, newData);
});
} else {
this.log.debug('client authorized');
View
@@ -220,6 +220,134 @@ module.exports = {
});
},
+ 'test authorization gets handshake data': function (done) {
+ var port = ++ports
+ , io = sio.listen(port)
+ , cl = client(port);
+
+ io.configure(function () {
+ function auth (data, fn) {
+ data.query.should.have.foo;
+ data.query.foo.should.eql('bar');
+ fn(null, false);
+ };
+
+ io.set('authorization', auth);
+ });
+
+ cl.get('/socket.io/{protocol}/?foo=bar', function (res, data) {
+ res.statusCode.should.eql(403);
+ data.should.match(/handshake unauthorized/);
+
+ cl.end();
+ io.server.close();
+ done();
+ });
+ },
+
+ 'test that authorization can view handshake data': function (done) {
+ var port = ++ports
+ , io = sio.listen(port)
+ , cl = client(port);
+
+ io.configure(function () {
+ function auth (data, fn) {
+ data.query.should.have.foo;
+ data.query.foo.should.eql('bar');
+ fn(null, true);
+ };
+
+ io.set('authorization', auth);
+ });
+
+ cl.get('/socket.io/{protocol}/?foo=bar', function (res, data) {
+ res.statusCode.should.eql(200);
+
+ cl.end();
+ io.server.close();
+ done();
+ });
+ },
+
+ 'test that authorization can change handshake data': function (done) {
+ var port = ++ports
+ , io = sio.listen(port)
+ , cl = client(port);
+
+ io.configure(function () {
+ function auth (data, fn) {
+ var replacement = { baz: 'qu' };
+ for (i in data) {
+ if (data.hasOwnProperty(i)) {
+ replacement[i] = data[i];
+ }
+ }
+ fn(null, true, replacement);
+ };
+
+ io.set('authorization', auth);
+ });
+
+ cl.get('/socket.io/{protocol}/', function (res, data) {
+ var id = data.split(':', 2)[0];
+ res.statusCode.should.eql(200);
+ io.handshaken[id].should.have.baz;
+ io.handshaken[id].baz.should.eql('qu');
+
+ cl.end();
+ io.server.close();
+ done();
+ });
+ },
+
+ 'test that id generation can be overridden': function (done) {
+ var port = ++ports
+ , io = sio.listen(port)
+ , cl = client(port);
+
+ io.configure(function () {
+ function idgen (data) {
+ return 'foo';
+ };
+
+ io.set('id generator', idgen);
+ });
+
+ cl.get('/socket.io/{protocol}/', function (res, data) {
+ var id = data.split(':', 2)[0];
+ id.should.eql('foo');
+
+ cl.end();
+ io.server.close();
+ done();
+ });
+ },
+
+ 'test that id generator can see handshake data': function (done) {
+ var port = ++ports
+ , io = sio.listen(port)
+ , cl = client(port);
+
+ io.configure(function () {
+ function idgen (data) {
+ data.query.should.have.foo;
+ data.query.foo.should.eql('bar');
+ return 'foo';
+ };
+
+ io.set('id generator', idgen);
+ });
+
+ cl.get('/socket.io/{protocol}/?foo=bar', function (res, data) {
+ var id = data.split(':', 2)[0];
+ id.should.eql('foo');
+
+ cl.end();
+ io.server.close();
+ done();
+ });
+ },
+
'test a handshake error': function (done) {
var port = ++ports
, io = sio.listen(port)

0 comments on commit d852c4c

Please sign in to comment.