Permalink
Browse files

Merge pull request #84 from ozten/master

Add HTTP_PROXY_HOST, HTTP_PROXY_PORT and HTTPS variants
  • Loading branch information...
2 parents fb2ed15 + 96e3292 commit ead2635b1d51e8383ec497c7fc6bcf1adf66c67b @havard committed Jul 20, 2012
Showing with 52 additions and 0 deletions.
  1. +7 −0 README.md
  2. +45 −0 openid.js
View
@@ -127,6 +127,13 @@ The verification of a positive assertion (i.e. an authenticated user) can be spe
- When loading fails for some reason, `callback(error, null)` is called with `error` being an error string specifying why loading failed.
- When loading succeeds, `callback(null, provider)` is called with the exact provider object that was previously stored using `saveDiscoveredInformation`.
+## Proxy Support
+`node-openid` makes HTTP and HTTPS requests during authentication. You can have these
+requests go through a proxy server, by using the following environment variables:
+
+ - HTTP_PROXY_HOST and HTTP_PROXY_PORT control how http:// requests are sent
+ - HTTPS_PROXY_HOST and HTTPS_PROXY_PORT control how https:// requests are sent
+
## License
OpenID for node.js is licensed under the MIT license. See LICENSE for further details.
View
@@ -159,6 +159,46 @@ var _buildUrl = function(theUrl, params)
return url.format(theUrl);
}
+var _proxyRequest = function(protocol, options)
+{
+ /*
+ If process.env['HTTP_PROXY_HOST'] and the env variable `HTTP_PROXY_POST`
+ are set, make sure path and the header Host are set to target url.
+
+ Similarly, `HTTPS_PROXY_HOST` and `HTTPS_PROXY_PORT` can be used
+ to proxy HTTPS traffic.
+
+ Proxies Example:
+ export HTTP_PROXY_HOST=localhost
+ export HTTP_PROXY_PORT=8080
+ export HTTPS_PROXY_HOST=localhost
+ export HTTPS_PROXY_PORT=8442
+ */
+ var targetHost = options.host;
+ if (!targetHost) return;
+ var updateOptions = function (envPrefix) {
+ var proxyHostname = process.env[envPrefix + '_PROXY_HOST'].trim();
+ var proxyPort = parseInt(process.env[envPrefix + '_PROXY_PORT'], 10);
+ if (proxyHostname.length > 0 && ! isNaN(proxyPort)) {
+
+ if (! options.headers) options.headers = {};
+
+ options.host = proxyHostname;
+ options.port = proxyPort;
+ options.path = protocol + '//' + targetHost + options.path;
+ options.headers['Host'] = targetHost;
+ }
+ };
+ if ('https:' === protocol &&
+ !! process.env['HTTPS_PROXY_HOST'] &&
+ !! process.env['HTTPS_PROXY_PORT']) {
+ updateOptions('HTTPS');
+ } else if (!! process.env['HTTP_PROXY_HOST'] &&
+ !! process.env['HTTP_PROXY_PORT']) {
+ updateOptions('HTTP');
+ }
+}
+
var _get = function(getUrl, params, callback, redirects)
{
redirects = redirects || 5;
@@ -178,6 +218,8 @@ var _get = function(getUrl, params, callback, redirects)
path: path
};
+ _proxyRequest(getUrl.protocol, options);
+
(getUrl.protocol == 'https:' ? https : http).get(options, function(res)
{
var data = '';
@@ -236,6 +278,9 @@ var _post = function(postUrl, data, callback, redirects)
},
method: 'POST'
};
+
+ _proxyRequest(postUrl.protocol, options);
+
(postUrl.protocol == 'https:' ? https : http).request(options, function(res)
{
var data = '';

0 comments on commit ead2635

Please sign in to comment.