diff --git a/lib/oauth.js b/lib/oauth.js index 74382b1..2721195 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -1,5 +1,6 @@ var sha1= require('./sha1'), - http= require('http'); + http= require('http'), + URL= require('url'); exports.OAuth= function(requestUrl, accessUrl, authorizeUrl, consumerKey, consumerSecret, version, signatureMethod) { this._requestUrl= requestUrl; @@ -41,6 +42,18 @@ exports.OAuth.prototype._getSignature= function(method, url, parameters, tokenSe return this._createSignature( signatureBase, tokenSecret ); } +exports.OAuth.prototype._normalizeUrl= function(url) { + var parsedUrl= URL.parse(url, true) + var port =""; + if( parsedUrl.port ) { + if( (parsedUrl.protocol == "http:" && parsedUrl.port != "80" ) || + (parsedUrl.protocol == "https:" && parsedUrl.port != "443") ) { + port= ":" + parsedUrl.port; + } + } + return parsedUrl.protocol + "//" + parsedUrl.hostname + port + parsedUrl.pathname; +} + exports.OAuth.prototype._splitQueryString= function(stringToSplit) { var result= {}; var parameters= stringToSplit.split("&"); @@ -81,7 +94,7 @@ exports.OAuth.prototype._normaliseRequestParams= function(arguments) { } exports.OAuth.prototype._createSignatureBase= function(method, url, parameters) { - url= this._encodeData(url); + url= this._encodeData( this._normalizeUrl(url) ); parameters= this._encodeData(parameters); return method.toUpperCase() + "&" + url + "&" + parameters; } diff --git a/spec/spec.oauth.js b/spec/spec.oauth.js index 96cd826..6e379a7 100644 --- a/spec/spec.oauth.js +++ b/spec/spec.oauth.js @@ -9,6 +9,17 @@ describe 'node-oauth' "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" end + describe 'Url normalisation' + it 'should strip default ports from urls for use in signature generation' + var oa= new OAuth(); + oa._normalizeUrl("https://somehost.com:443/foo/bar").should_be "https://somehost.com/foo/bar" + end + it 'should leave in non-default ports from urls for use in signature generation' + var oa= new OAuth(); + oa._normalizeUrl("https://somehost.com:446/foo/bar").should_be "https://somehost.com:446/foo/bar" + oa._normalizeUrl("http://somehost.com:81/foo/bar").should_be "http://somehost.com:81/foo/bar" + end + end end end end \ No newline at end of file