Skip to content

Commit 507f818

Browse files
committed
Merge pull request #792 from ashubham/master
Adding the nodejs0.12 auth option
2 parents 245d73a + e907d7b commit 507f818

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

lib/http-proxy.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module.exports.createProxyServer =
4141
* prependPath: <true/false, Default: true - specify whether you want to prepend the target's path to the proxy path>
4242
* localAddress : <Local interface string to bind for outgoing connections>
4343
* changeOrigin: <true/false, Default: false - changes the origin of the host header to the target URL>
44+
* auth : Basic authentication i.e. 'user:password' to compute an Authorization header.
4445
* hostRewrite: rewrites the location hostname on (301/302/307/308) redirects, Default: null.
4546
* }
4647
*

lib/http-proxy/common.js

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
4646
extend(outgoing.headers, options.headers);
4747
}
4848

49+
if (options.auth) {
50+
outgoing.auth = options.auth;
51+
}
52+
4953
if (isSSL.test(options[forward || 'target'].protocol)) {
5054
outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure;
5155
}

test/lib-http-proxy-common-test.js

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('lib/http-proxy/common.js', function () {
1717
},
1818
headers: {'fizz': 'bang', 'overwritten':true},
1919
localAddress: 'local.address',
20+
auth:'username:pass'
2021
},
2122
{
2223
method : 'i',
@@ -37,6 +38,7 @@ describe('lib/http-proxy/common.js', function () {
3738
expect(outgoing.headers.fizz).to.eql('bang');
3839
expect(outgoing.headers.overwritten).to.eql(true);
3940
expect(outgoing.localAddress).to.eql('local.address');
41+
expect(outgoing.auth).to.eql('username:pass');
4042
});
4143

4244
it('should not override agentless upgrade header', function () {

test/lib-http-proxy-passes-web-incoming-test.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,31 @@ describe('#createProxyServer.web() using own http server', function () {
299299

300300
http.request('http://127.0.0.1:8081', function() {}).end();
301301
});
302-
});
302+
303+
it('should proxy the request with the Authorization header set', function (done) {
304+
var proxy = httpProxy.createProxyServer({
305+
target: 'http://127.0.0.1:8080',
306+
auth: 'user:pass'
307+
});
308+
309+
function requestHandler(req, res) {
310+
proxy.web(req, res);
311+
}
312+
313+
var proxyServer = http.createServer(requestHandler);
314+
315+
var source = http.createServer(function(req, res) {
316+
source.close();
317+
proxyServer.close();
318+
var auth = new Buffer(req.headers.authorization.split(' ')[1], 'base64');
319+
expect(req.method).to.eql('GET');
320+
expect(auth.toString()).to.eql('user:pass');
321+
done();
322+
});
323+
324+
proxyServer.listen('8081');
325+
source.listen('8080');
326+
327+
http.request('http://127.0.0.1:8081', function() {}).end();
328+
});
329+
});

0 commit comments

Comments
 (0)