Skip to content

Commit

Permalink
Adding support for HTTP(S) proxies
Browse files Browse the repository at this point in the history
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 ciaranj#102.
  • Loading branch information
ozten committed Jul 20, 2012
1 parent a7c1e94 commit 46a5ba0
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Expand Up @@ -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
==============
Expand Down
42 changes: 42 additions & 0 deletions lib/oauth.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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= {},
Expand Down
42 changes: 42 additions & 0 deletions lib/oauth2.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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 || {};
Expand Down

1 comment on commit 46a5ba0

@ciaranj
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.