diff --git a/lib/http-proxy.js b/lib/http-proxy.js index b1ad646f4..8d37dd212 100644 --- a/lib/http-proxy.js +++ b/lib/http-proxy.js @@ -41,6 +41,7 @@ module.exports.createProxyServer = * prependPath: * localAddress : * changeOrigin: + * auth : Basic authentication i.e. 'user:password' to compute an Authorization header. * hostRewrite: rewrites the location hostname on (301/302/307/308) redirects, Default: null. * } * diff --git a/lib/http-proxy/common.js b/lib/http-proxy/common.js index 66e80f9a1..59ece4dfb 100644 --- a/lib/http-proxy/common.js +++ b/lib/http-proxy/common.js @@ -46,6 +46,10 @@ common.setupOutgoing = function(outgoing, options, req, forward) { extend(outgoing.headers, options.headers); } + if (options.auth) { + outgoing.auth = options.auth; + } + if (isSSL.test(options[forward || 'target'].protocol)) { outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure; } diff --git a/test/lib-http-proxy-common-test.js b/test/lib-http-proxy-common-test.js index 5cf36c232..007ba39a4 100644 --- a/test/lib-http-proxy-common-test.js +++ b/test/lib-http-proxy-common-test.js @@ -17,6 +17,7 @@ describe('lib/http-proxy/common.js', function () { }, headers: {'fizz': 'bang', 'overwritten':true}, localAddress: 'local.address', + auth:'username:pass' }, { method : 'i', @@ -37,6 +38,7 @@ describe('lib/http-proxy/common.js', function () { expect(outgoing.headers.fizz).to.eql('bang'); expect(outgoing.headers.overwritten).to.eql(true); expect(outgoing.localAddress).to.eql('local.address'); + expect(outgoing.auth).to.eql('username:pass'); }); it('should not override agentless upgrade header', function () { diff --git a/test/lib-http-proxy-passes-web-incoming-test.js b/test/lib-http-proxy-passes-web-incoming-test.js index f2c6f1304..cf9bf6b75 100644 --- a/test/lib-http-proxy-passes-web-incoming-test.js +++ b/test/lib-http-proxy-passes-web-incoming-test.js @@ -299,4 +299,31 @@ describe('#createProxyServer.web() using own http server', function () { http.request('http://127.0.0.1:8081', function() {}).end(); }); -}); \ No newline at end of file + + it('should proxy the request with the Authorization header set', function (done) { + var proxy = httpProxy.createProxyServer({ + target: 'http://127.0.0.1:8080', + auth: 'user:pass' + }); + + function requestHandler(req, res) { + proxy.web(req, res); + } + + var proxyServer = http.createServer(requestHandler); + + var source = http.createServer(function(req, res) { + source.close(); + proxyServer.close(); + var auth = new Buffer(req.headers.authorization.split(' ')[1], 'base64'); + expect(req.method).to.eql('GET'); + expect(auth.toString()).to.eql('user:pass'); + done(); + }); + + proxyServer.listen('8081'); + source.listen('8080'); + + http.request('http://127.0.0.1:8081', function() {}).end(); + }); +});