Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/peer_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ PeerClient.prototype.checkServerReq = function() {
self.log.emit('log', 'peer-client', 'Peer connection established (' + self.url + ')');

res.statusCode = 200;

// If the auth flag is set, then that means this connection is authenticated.
// We need to pass back the authorization so that the server we're peering to
// can connect back to us for subscriptions
if ((typeof res.socket.socket.authorized !== 'undefined') && (res.socket.socket.authorized)) {
var headers = {
'X-Authorization-Required': true,
'authorization': res.socket.socket._httpMessage._headers.authorization
};

res.writeHead(res.statusCode, headers);
}

res.end();

// remove request listener
Expand Down
22 changes: 22 additions & 0 deletions lib/peer_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var PeerSocket = module.exports = function(ws, name, peerRegistry, opts) {
var self = this;
this.state = STATES.DISCONNECTED;
this.name = name; // peers local id
this.authorization = null;
this.agent = null;
this.subscriptions = {}; // { <topic>: <subscribed_count> }
this.connectionId = null;
Expand Down Expand Up @@ -311,6 +312,10 @@ PeerSocket.prototype.subscribe = function(event, cb) {
agent: this.agent
};

if (this.authorization !== null) {
opts.headers.Authorization = this.authorization;
}

var req = http.request(opts, function(res) {
cb();
}).on('error', cb);
Expand Down Expand Up @@ -355,13 +360,18 @@ PeerSocket.prototype.unsubscribe = function(event, cb) {
agent: this.agent
};

if (this.authorization !== null) {
opts.headers.Authorization = this.authorization;
}

var req = http.request(opts, function(res) {
cb();
}).on('error', cb);
req.end(body);
};

PeerSocket.prototype.confirmConnection = function(connectionId, callback) {
var self = this;
var timeout = setTimeout(function() {
req.abort();
callback(new Error('Confirm connection timeout reached.'));
Expand All @@ -373,6 +383,14 @@ PeerSocket.prototype.confirmConnection = function(connectionId, callback) {
if (res.statusCode !== 200) {
return callback(new Error('Unexpected status code'));
}

// If the peer has responded with an auth flag, then it is passing back the
// authorization to use for future requests. So we need to store this value
// for requests like observe subscribe.
if (res.headers['x-authorization-required']) {
self.authorization = res.headers.authorization;
}

callback();
}).on('error', function(err) {
clearTimeout(timeout);
Expand Down Expand Up @@ -404,6 +422,10 @@ PeerSocket.prototype.transition = function(action, args, cb) {
}
};

if (this.authorization !== null) {
opts.headers.Authorization = this.authorization;
}

var req = http.request(opts, function(res) {
var buffer = [];
var len = 0;
Expand Down