From 420596ad243437cedabe1a12528dc4747924673f Mon Sep 17 00:00:00 2001 From: Sean Lee Date: Fri, 10 Mar 2017 17:09:48 -0500 Subject: [PATCH] Enable making request behind proxy --- lib/oauth.js | 33 ++++++++++++++++++--------------- package.json | 3 +++ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/oauth.js b/lib/oauth.js index 50dccf99..0c16d0be 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -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; @@ -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}; @@ -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 ) { @@ -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 ) { @@ -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; @@ -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); @@ -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 ); @@ -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) { @@ -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 ); } @@ -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); diff --git a/package.json b/package.json index fa35b6f7..4a31c8af 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,9 @@ , "main" : "index.js" , "author" : "Ciaran Jessup " , "repository" : { "type":"git", "url":"http://github.com/ciaranj/node-oauth.git" } +, "dependencies": { + "https-proxy-agent": "^1.0.0" + } , "devDependencies": { "vows": "0.5.x" }