Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable making request behind proxy #327

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ var crypto= require('crypto'),
https= require('https'),
URL= require('url'),
querystring= require('querystring'),
OAuthUtils= require('./_utils');
OAuthUtils= require('./_utils'),
HttpsProxyAgent= require('https-proxy-agent');

exports.OAuth= function(requestUrl, accessUrl, consumerKey, consumerSecret, version, authorize_callback, signatureMethod, nonceSize, customHeaders) {
exports.OAuth= function(requestUrl, accessUrl, consumerKey, consumerSecret, version, authorize_callback, signatureMethod, nonceSize, customHeaders, proxyUrl) {
this._isEcho = false;

this._requestUrl= requestUrl;
Expand All @@ -30,7 +31,8 @@ exports.OAuth= function(requestUrl, accessUrl, consumerKey, consumerSecret, vers
this._nonceSize= nonceSize || 32;
this._headers= customHeaders || {"Accept" : "*/*",
"Connection" : "close",
"User-Agent" : "Node authentication"}
"User-Agent" : "Node authentication"};
this._proxyUrl= proxyUrl;
this._clientOptions= this._defaultClientOptions= {"requestTokenHttpMethod": "POST",
"accessTokenHttpMethod": "POST",
"followRedirects": true};
Expand Down Expand Up @@ -239,13 +241,14 @@ exports.OAuth.prototype._getNonce= function(nonceSize) {
return result.join('');
}

exports.OAuth.prototype._createClient= function( port, hostname, method, path, headers, sslEnabled ) {
exports.OAuth.prototype._createClient= function( port, hostname, method, path, headers, proxy_url, sslEnabled ) {
var options = {
host: hostname,
port: port,
path: path,
method: method,
headers: headers
headers: headers,
agent: proxy_url ? new HttpsProxyAgent(proxy_url) : null
};
var httpModel;
if( sslEnabled ) {
Expand Down Expand Up @@ -305,7 +308,7 @@ exports.OAuth.prototype._prepareParameters= function( oauth_token, oauth_token_s
return orderedParameters;
}

exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback ) {
exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, proxy_url, callback ) {
var orderedParameters= this._prepareParameters(oauth_token, oauth_token_secret, method, url, extra_params);

if( !post_content_type ) {
Expand Down Expand Up @@ -368,10 +371,10 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke

var request;
if( parsedUrl.protocol == "https:" ) {
request= this._createClient(parsedUrl.port, parsedUrl.hostname, method, path, headers, true);
request= this._createClient(parsedUrl.port, parsedUrl.hostname, method, path, headers, proxy_url, true);
}
else {
request= this._createClient(parsedUrl.port, parsedUrl.hostname, method, path, headers);
request= this._createClient(parsedUrl.port, parsedUrl.hostname, method, path, headers, proxy_url);
}

var clientOptions = this._clientOptions;
Expand All @@ -391,7 +394,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
} else {
// Follow 301 or 302 redirects with Location HTTP header
if((response.statusCode == 301 || response.statusCode == 302) && clientOptions.followRedirects && response.headers && response.headers.location) {
self._performSecureRequest( oauth_token, oauth_token_secret, method, response.headers.location, extra_params, post_body, post_content_type, callback);
self._performSecureRequest( oauth_token, oauth_token_secret, method, response.headers.location, extra_params, post_body, post_content_type, proxy_url, callback);
}
else {
callback({ statusCode: response.statusCode, data: data }, data, response);
Expand Down Expand Up @@ -461,7 +464,7 @@ exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_s
extraParams.oauth_verifier= oauth_verifier;
}

this._performSecureRequest( oauth_token, oauth_token_secret, this._clientOptions.accessTokenHttpMethod, this._accessUrl, extraParams, null, null, function(error, data, response) {
this._performSecureRequest( oauth_token, oauth_token_secret, this._clientOptions.accessTokenHttpMethod, this._accessUrl, extraParams, null, null, this._proxyUrl, function(error, data, response) {
if( error ) callback(error);
else {
var results= querystring.parse( data );
Expand All @@ -476,15 +479,15 @@ exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_s

// Deprecated
exports.OAuth.prototype.getProtectedResource= function(url, method, oauth_token, oauth_token_secret, callback) {
this._performSecureRequest( oauth_token, oauth_token_secret, method, url, null, "", null, callback );
this._performSecureRequest( oauth_token, oauth_token_secret, method, url, null, "", null, null, callback );
}

exports.OAuth.prototype.delete= function(url, oauth_token, oauth_token_secret, callback) {
return this._performSecureRequest( oauth_token, oauth_token_secret, "DELETE", url, null, "", null, callback );
return this._performSecureRequest( oauth_token, oauth_token_secret, "DELETE", url, null, "", null, null, callback );
}

exports.OAuth.prototype.get= function(url, oauth_token, oauth_token_secret, callback) {
return this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, "", null, callback );
return this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, "", null, null, callback );
}

exports.OAuth.prototype._putOrPost= function(method, url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) {
Expand All @@ -498,7 +501,7 @@ exports.OAuth.prototype._putOrPost= function(method, url, oauth_token, oauth_tok
extra_params= post_body;
post_body= null;
}
return this._performSecureRequest( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback );
return this._performSecureRequest( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, null, callback );
}


Expand Down Expand Up @@ -539,7 +542,7 @@ exports.OAuth.prototype.getOAuthRequestToken= function( extraParams, callback )
if( this._authorize_callback ) {
extraParams["oauth_callback"]= this._authorize_callback;
}
this._performSecureRequest( null, null, this._clientOptions.requestTokenHttpMethod, this._requestUrl, extraParams, null, null, function(error, data, response) {
this._performSecureRequest( null, null, this._clientOptions.requestTokenHttpMethod, this._requestUrl, extraParams, null, null, this._proxyUrl, function(error, data, response) {
if( error ) callback(error);
else {
var results= querystring.parse(data);
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
, "main" : "index.js"
, "author" : "Ciaran Jessup <[email protected]>"
, "repository" : { "type":"git", "url":"http://github.com/ciaranj/node-oauth.git" }
, "dependencies": {
"https-proxy-agent": "^1.0.0"
}
, "devDependencies": {
"vows": "0.5.x"
}
Expand Down