Skip to content
Browse files

Adding support for HTTP(S) proxies

Adding support for the following env variables:
* HTTP_PROXY_HOST
* HTTP_PROXY_PORT
* HTTPS_PROXY_HOST
* HTTPS_PROXY_PORT

Compatible with node-openid.

Proposed fix for Issues #102.
  • Loading branch information...
1 parent a7c1e94 commit 46a5ba0bdb8981a6d468146a67adeaae48bcdba7 @ozten committed Jun 18, 2012
Showing with 86 additions and 0 deletions.
  1. +2 −0 Readme.md
  2. +42 −0 lib/oauth.js
  3. +42 −0 lib/oauth2.js
View
2 Readme.md
@@ -6,6 +6,8 @@ Tested against Twitter (http://twitter.com), term.ie (http://term.ie/oauth/examp
Also provides rudimentary OAuth2 support, tested against facebook, github, foursquare, google and Janrain. For more complete usage examples please take a look at connect-auth (http://github.com/ciaranj/connect-auth)
+Requests may use a HTTP or HTTPS proxy if desired. Set the environment variables `HTTP_PROXY_HOST` and `HTTP_PROXY_PORT` to proxy http traffic. Similarly `HTTPS_PROXY_HOST` and `HTTPS_PROXY_PORT` for https traffic.
+
Installation
==============
View
42 lib/oauth.js
@@ -232,6 +232,7 @@ exports.OAuth.prototype._createClient= function( port, hostname, method, path, h
method: method,
headers: headers
};
+ this._proxyRequest(sslEnabled, options);
var httpModel;
if( sslEnabled ) {
httpModel= https;
@@ -404,6 +405,47 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
return;
}
+exports.OAuth.prototype._proxyRequest = function (secure, 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,
+ protocol = secure ? 'https:' : 'http:';
+ 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');
+ }
+}
+
exports.OAuth.prototype.setClientOptions= function(options) {
var key,
mergedOptions= {},
View
42 lib/oauth2.js
@@ -66,6 +66,8 @@ exports.OAuth2.prototype._request= function(method, url, headers, post_body, acc
headers: realHeaders
};
+ this._proxyRequest(parsedUrl.protocol, options);
+
// Some hosts *cough* google appear to close the connection early / send no content-length header
// allow this behaviour.
var allowEarlyClose= OAuthUtils.isAnEarlyCloseHost(options.host);
@@ -105,6 +107,46 @@ exports.OAuth2.prototype._request= function(method, url, headers, post_body, acc
request.end();
}
+exports.OAuth2.prototype._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');
+ }
+}
+
exports.OAuth2.prototype.getAuthorizeUrl= function( params ) {
var params= params || {};

1 comment on commit 46a5ba0

@ciaranj

This looks good, any chance you could provide some tests to go with it though ?

Please sign in to comment.
Something went wrong with that request. Please try again.