Added support in for dealing with servers using non-default ports.
This commit is contained in:
		
							
								
								
									
										13
									
								
								lib/oauth.js
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								lib/oauth.js
									
									
									
									
									
								
							| @ -119,6 +119,10 @@ exports.OAuth.prototype.NONCE_CHARS= ['a','b','c','d','e','f','g','h','i','j','k | |||||||
|               'Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3', |               'Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3', | ||||||
|               '4','5','6','7','8','9']; |               '4','5','6','7','8','9']; | ||||||
|  |  | ||||||
|  | exports.OAuth.prototype._createClient= function( port, hostname ) { | ||||||
|  |   return http.createClient(port, hostname); | ||||||
|  | } | ||||||
|  |  | ||||||
| exports.OAuth.prototype._getNonce= function(nonceSize) { | exports.OAuth.prototype._getNonce= function(nonceSize) { | ||||||
|    var result = []; |    var result = []; | ||||||
|    var chars= this.NONCE_CHARS; |    var chars= this.NONCE_CHARS; | ||||||
| @ -166,9 +170,8 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke | |||||||
|   } |   } | ||||||
|   query= query.substring(0, query.length-1); |   query= query.substring(0, query.length-1); | ||||||
|  |  | ||||||
|  |   var oauthProvider=  this._createClient(parsedUrl.port, parsedUrl.hostname) | ||||||
|   var oauthProvider=  http.createClient(parsedUrl.port, parsedUrl.hostname); |   var headers= {'Host': parsedUrl.host}  | ||||||
|   var headers= {'Host': parsedUrl.hostname} |  | ||||||
|   var request = oauthProvider.request(method,  parsedUrl.pathname + "?" + query, headers); |   var request = oauthProvider.request(method,  parsedUrl.pathname + "?" + query, headers); | ||||||
|  |  | ||||||
|   var data="";  |   var data="";  | ||||||
| @ -236,14 +239,14 @@ exports.OAuth.prototype.getOAuthRequestToken= function(callback) { | |||||||
|   authHeader= authHeader.substring(0, authHeader.length-1); |   authHeader= authHeader.substring(0, authHeader.length-1); | ||||||
|  |  | ||||||
|   headers["Authorization"]= authHeader; |   headers["Authorization"]= authHeader; | ||||||
|   headers["Host"] = parsedUrl.hostname |   headers["Host"] = parsedUrl.host | ||||||
|   headers["Accept"]= "*/*" |   headers["Accept"]= "*/*" | ||||||
|   headers["Connection"]= "close" |   headers["Connection"]= "close" | ||||||
|   headers["User-Agent"]= "Express authentication" |   headers["User-Agent"]= "Express authentication" | ||||||
|   headers["Content-length"]= 0 |   headers["Content-length"]= 0 | ||||||
|   headers["Content-Type"]= "application/x-www-form-urlencoded" |   headers["Content-Type"]= "application/x-www-form-urlencoded" | ||||||
|  |  | ||||||
|   var oauthProvider=  http.createClient(parsedUrl.port, parsedUrl.hostname); |   var oauthProvider=  this._createClient(parsedUrl.port, parsedUrl.hostname); | ||||||
|   var request = oauthProvider.request(method, parsedUrl.pathname, headers); |   var request = oauthProvider.request(method, parsedUrl.pathname, headers); | ||||||
|   var data="";  |   var data="";  | ||||||
|   var self= this; |   var self= this; | ||||||
|  | |||||||
| @ -1,16 +1,20 @@ | |||||||
| describe 'node-oauth' | describe 'node-oauth' | ||||||
|   before_each |   before_each | ||||||
|     OAuth= require('oauth').OAuth |     OAuth= require('oauth').OAuth | ||||||
|     oa= new OAuth(null, null, null, null, null, "HMAC-SHA1"); |  | ||||||
|   end |   end | ||||||
|   describe 'Auth' |   describe 'Auth' | ||||||
|     describe 'OAuth' |     describe 'OAuth' | ||||||
|       it 'should generate the signature base string described in http://oauth.net/core/1.0/#sig_base_example' |       it 'should generate the signature base string described in http://oauth.net/core/1.0/#sig_base_example' | ||||||
|  |          oa= new OAuth(null, null, null, null, null, "HMAC-SHA1"); | ||||||
|  |        | ||||||
|         var result= oa._createSignatureBase("GET", "http://photos.example.net/photos",  |         var result= oa._createSignatureBase("GET", "http://photos.example.net/photos",  | ||||||
|                                             "file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original") |                                             "file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original") | ||||||
|         result.should.eql "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal" |         result.should.eql "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal" | ||||||
|       end |       end | ||||||
|       describe 'Url normalisation' |       describe 'Url normalisation' | ||||||
|  |         before_each | ||||||
|  |          oa= new OAuth(null, null, null, null, null, "HMAC-SHA1"); | ||||||
|  |         end | ||||||
|         it 'should strip default ports from urls for use in signature generation' |         it 'should strip default ports from urls for use in signature generation' | ||||||
|           oa._normalizeUrl("https://somehost.com:443/foo/bar").should_be "https://somehost.com/foo/bar" |           oa._normalizeUrl("https://somehost.com:443/foo/bar").should_be "https://somehost.com/foo/bar" | ||||||
|         end |         end | ||||||
| @ -22,6 +26,55 @@ describe 'node-oauth' | |||||||
|           oa._normalizeUrl("http://somehost.com").should_be "http://somehost.com/" |           oa._normalizeUrl("http://somehost.com").should_be "http://somehost.com/" | ||||||
|         end |         end | ||||||
|       end |       end | ||||||
|  |       describe 'host headers for non default ports should contain the port' | ||||||
|  |         before_each | ||||||
|  |         end | ||||||
|  |  | ||||||
|  |         describe 'when getProtectedResource is called' | ||||||
|  |           it 'should set the correct Host header when provided with an unusual port' | ||||||
|  |             oa2= new OAuth(null, null, null, null, null, "HMAC-SHA1"); | ||||||
|  |             var mockProvider= {},  | ||||||
|  |                 mockRequest= {addListener:function(){}, | ||||||
|  |                               end:function(){}}; | ||||||
|  |  | ||||||
|  |             stub(mockProvider, 'request').and_return(mockRequest) | ||||||
|  |             mockProvider.should.receive('request', 'once').with_args('GET', an_instance_of(String), {Host:"somehost.com:8080"}) | ||||||
|  |             stub(oa2, '_createClient').and_return(mockProvider) | ||||||
|  |             oa2.should.receive('_createClient', 'once').with_args('8080', 'somehost.com') | ||||||
|  |             oa2.getProtectedResource("http://somehost.com:8080", "GET", "oauth_token", null, function(){}) | ||||||
|  |           end | ||||||
|  |         end | ||||||
|  |  | ||||||
|  |         describe 'when getOAuthRequestToken is called' | ||||||
|  |           it 'should set the correct Host header when provided with an unusual port' | ||||||
|  |             oa2= new OAuth(null, null, null, null, null, "HMAC-SHA1"); | ||||||
|  |             var mockProvider= {},  | ||||||
|  |                 mockRequest= {addListener:function(){}, | ||||||
|  |                               end:function(){}}; | ||||||
|  |  | ||||||
|  |             stub(mockProvider, 'request').and_return(mockRequest) | ||||||
|  |             mockProvider.should.receive('request', 'once').with_args('GET', an_instance_of(String), {Host:"somehost.com:8080"}) | ||||||
|  |             stub(oa2, '_createClient').and_return(mockProvider) | ||||||
|  |             oa2.should.receive('_createClient', 'once').with_args('8080', 'somehost.com') | ||||||
|  |             oa2.getProtectedResource("http://somehost.com:8080", "GET", "oauth_token", null, function(){}) | ||||||
|  |           end | ||||||
|  |         end | ||||||
|  |  | ||||||
|  |         describe 'when getOauthAccessToken is called' | ||||||
|  |           it 'should set the correct Host header when provided with an unusual port' | ||||||
|  |             oa2= new OAuth(null, null, null, null, null, "HMAC-SHA1"); | ||||||
|  |             var mockProvider= {},  | ||||||
|  |                 mockRequest= {addListener:function(){}, | ||||||
|  |                               end:function(){}}; | ||||||
|  |  | ||||||
|  |             stub(mockProvider, 'request').and_return(mockRequest) | ||||||
|  |             mockProvider.should.receive('request', 'once').with_args('GET', an_instance_of(String), {Host:"somehost.com:8080"}) | ||||||
|  |             stub(oa2, '_createClient').and_return(mockProvider) | ||||||
|  |             oa2.should.receive('_createClient', 'once').with_args('8080', 'somehost.com') | ||||||
|  |             oa2.getProtectedResource("http://somehost.com:8080", "GET", "oauth_token", null, function(){}) | ||||||
|  |           end | ||||||
|  |         end | ||||||
|  |       end  | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
| end | end | ||||||
		Reference in New Issue
	
	Block a user