diff --git a/lib/peer_client.js b/lib/peer_client.js index c818677..77ec1d1 100644 --- a/lib/peer_client.js +++ b/lib/peer_client.js @@ -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 diff --git a/lib/peer_socket.js b/lib/peer_socket.js index 28a0981..1514559 100644 --- a/lib/peer_socket.js +++ b/lib/peer_socket.js @@ -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 = {}; // { : } this.connectionId = null; @@ -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); @@ -355,6 +360,10 @@ 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); @@ -362,6 +371,7 @@ PeerSocket.prototype.unsubscribe = function(event, cb) { }; PeerSocket.prototype.confirmConnection = function(connectionId, callback) { + var self = this; var timeout = setTimeout(function() { req.abort(); callback(new Error('Confirm connection timeout reached.')); @@ -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); @@ -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;