Skip to content

Commit 872033e

Browse files
imlucaskangas
authored andcommitted
fix(brain,client,server): finally the real fix
1 parent 774eb9a commit 872033e

File tree

3 files changed

+47
-53
lines changed

3 files changed

+47
-53
lines changed

scout-brain/lib/actions/token.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ var jwt = require('jsonwebtoken');
33
var boom = require('boom');
44
var debug = require('debug')('scout-brain:token');
55
var _ = require('underscore');
6-
var assert = require('assert');
76

87
var getDeployment = require('./deployment').get;
98
var createSession = require('./session').create;
@@ -26,6 +25,7 @@ function verify(token, fn) {
2625
}
2726

2827
function mount(tokenData, ctx, next) {
28+
debug('mounting token data');
2929
if (!tokenData.session_id) {
3030
return next(boom.badRequest('Bad token: missing session id'));
3131
}
@@ -57,23 +57,19 @@ function mount(tokenData, ctx, next) {
5757
'to `' + ctx.instance_id + '` but it is not in ' +
5858
'deployment `' + tokenData.deployment_id + '`'));
5959
}
60+
}
6061

61-
debug('getting connection for session', tokenData.session_id);
62-
getSession(tokenData.session_id, function(err, connection) {
63-
if (err) return next(err);
64-
65-
ctx.mongo = connection;
66-
return next();
67-
});
62+
debug('getting connection for session', tokenData.session_id);
63+
getSession(tokenData.session_id, function(err, connection) {
64+
if (err) return next(err);
6865

69-
} else {
70-
getSession(tokenData.session_id, function(err, connection) {
71-
if (err) return next(err);
66+
if (!connection) {
67+
return next(boom.forbidden('No session for this token.'));
68+
}
7269

73-
ctx.mongo = connection;
74-
return next();
75-
});
76-
}
70+
ctx.mongo = connection;
71+
return next();
72+
});
7773
});
7874
}
7975

scout-client/lib/client.js

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ Client.prototype.connect = function() {
124124
return this;
125125
}
126126
this.token = new Token(this.config)
127-
.on('readable', this.onTokenReadable.bind(this))
128-
.on('error', this.onTokenError.bind(this));
127+
.on('data', this.onTokenReadable.bind(this))
128+
.on('error', this.onError.bind(this));
129129
return this;
130130
};
131131

@@ -558,7 +558,7 @@ Client.prototype.read = function(path, params, fn) {
558558

559559
if (!this.readable) {
560560
debug('%s not readable. queueing read', this._id, path, params);
561-
return this.on('readable', this.read.bind(this, path, params, fn));
561+
return this.once('readable', this.read.bind(this, path, params, fn));
562562
}
563563

564564
if (typeof params === 'function') {
@@ -618,14 +618,14 @@ Client.prototype.ender = function(fn) {
618618
* @api private
619619
*/
620620
Client.prototype.onTokenReadable = function() {
621-
debug('token now readable');
622-
this.readable = true;
623-
this.context.set(this.token.session);
624621
if (!this.io) {
625622
this._initSocketio();
626623
}
627-
this.emit('readable', this.token.session);
628-
debug('emitted readable on client');
624+
debug('authenticating with scout-server socket.io transport...');
625+
this.io.emit('authenticate', {
626+
token: this.token.toString()
627+
});
628+
629629

630630
if (!this.original) {
631631
this.emit('change');
@@ -642,30 +642,27 @@ Client.prototype.onTokenReadable = function() {
642642
}
643643
};
644644

645-
/**
646-
* We couldn't get a token.
647-
*
648-
* @api private
649-
*/
650-
Client.prototype.onTokenError = function(err) {
651-
debug('Could not get token. Server not running?', err);
652-
this.emit('error', err);
653-
};
654-
655645
Client.prototype._initSocketio = function() {
656646
if (this.io) return;
657647

658-
this.io = socketio(this.config.scout, {
659-
query: 'token=' + this.token.toString()
660-
});
648+
this.io = socketio(this.config.scout);
661649
this.io.on('reconnecting', this.emit.bind(this, 'reconnecting'))
662650
.on('reconnect', this.onReconnect.bind(this))
663651
.on('reconnect_attempt', this.emit.bind(this, 'reconnect_attempt'))
664652
.on('reconnect_failed', this.emit.bind(this, 'reconnect_failed'))
665653
.on('disconnect', this.emit.bind(this, 'disconnect'));
666654
this.io.on('connect', function() {
667655
debug('connected to scout-server socket');
668-
});
656+
this.io.on('authenticated', function() {
657+
debug('Success! Now authenticated with scout-server socket.io transport');
658+
659+
this.readable = true;
660+
this.context.set(this.token.session);
661+
debug('now ready for use');
662+
this.emit('readable', this.token.session);
663+
}.bind(this));
664+
}.bind(this));
665+
this.io.on('error', this.onError.bind(this));
669666
};
670667

671668
/**
@@ -689,20 +686,19 @@ Client.prototype.onUnload = function() {
689686
* @param {Error} err
690687
* @api private
691688
*/
692-
Client.prototype.onTokenError = function(err) {
693-
this.dead = err;
694-
if (err >= 500) {
695-
this.dead.message += ' (scout-server dead at ' + this.config.scout + '?)';
689+
Client.prototype.onError = function(err) {
690+
if (err.message === 'Origin is not allowed by Access-Control-Allow-Origin') {
691+
err.message = 'scout-server not running at ' + this.config.scout + '?';
696692
}
693+
// @todo: Exponential back-off to retry connecting.
694+
this.dead = err;
697695
this.emit('error', err);
698696
};
699697

700698
Client.prototype.onReconnect = function() {
701699
debug('reconnected. getting new token');
702700
this.token = new Token(this.config)
703701
.on('readable', function() {
704-
this.readable = true;
705-
this.context.set(this.token.session);
706702
this._initSocketio();
707703
}.bind(this))
708704
.on('error', this.emit.bind(this, 'error'));

scout-server/lib/io.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ var _idToDocument = require('./streams/id-to-document');
1818
var EJSON = require('mongodb-extended-json');
1919
var typedParams = require('./middleware/typed-params');
2020

21-
io.use(require('socketio-jwt').authorize({
21+
22+
io.on('connection', require('socketio-jwt').authorize({
2223
secret: config.get('token:secret').toString('utf-8'),
23-
handshake: true
24-
}));
24+
timeout: 15000
25+
})).on('authenticated', function(socket) {
26+
debug('authenticated with token data', socket.decoded_token);
27+
});
2528

2629
function prepare(socket, req, done) {
30+
debug('preparing socket.io request');
2731
req.params = _.extend({
2832
ns: req.ns,
2933
size: req.size,
@@ -35,10 +39,7 @@ function prepare(socket, req, done) {
3539

3640
var tasks = {};
3741
tasks.token = function(next) {
38-
brain.loadToken(socket.decoded_token, req, function() {
39-
debug('load token returned', arguments);
40-
next();
41-
});
42+
brain.loadToken(socket.decoded_token, req, next);
4243
};
4344

4445
if (req.params.ns) {
@@ -55,8 +56,8 @@ function prepare(socket, req, done) {
5556
done();
5657
});
5758
}
58-
5959
async.series(tasks, function(err) {
60+
debug('socket.io request now prepared');
6061
if (err) return done(err);
6162
done();
6263
});
@@ -65,7 +66,9 @@ function prepare(socket, req, done) {
6566

6667
io.on('connection', function(socket) {
6768
ss(socket).on('collection:sample', function(stream, req) {
68-
prepare(socket, req, function() {
69+
prepare(socket, req, function(err) {
70+
if (err) return stream.emit('error', err);
71+
6972
debug('collection:sample got req %j', Object.keys(req));
7073
var db = req.mongo.db(req.params.database_name);
7174
createSampleStream(db, req.params.collection_name, {
@@ -79,5 +82,4 @@ io.on('connection', function(socket) {
7982
.pipe(stream);
8083
});
8184
});
82-
debug('token data %j', socket.decoded_token);
8385
});

0 commit comments

Comments
 (0)