diff --git a/lib/oauth.js b/lib/oauth.js index 6ff3aea..6418144 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -2,7 +2,7 @@ var crypto= require('crypto'), sha1= require('./sha1'), http= require('http'), URL= require('url'), - querystring= require('querystring'); + querystring= require('querystring'); exports.OAuth= function(requestUrl, accessUrl, consumerKey, consumerSecret, version, authorize_callback, signatureMethod, nonceSize, customHeaders) { this._requestUrl= requestUrl; @@ -51,14 +51,14 @@ exports.OAuth.prototype._decodeData= function(toDecode) { } exports.OAuth.prototype._getSignature= function(method, url, parameters, tokenSecret) { - var signatureBase= this._createSignatureBase(method, url, parameters); - return this._createSignature( signatureBase, tokenSecret ); + var signatureBase= this._createSignatureBase(method, url, parameters); + return this._createSignature( signatureBase, tokenSecret ); } exports.OAuth.prototype._normalizeUrl= function(url) { var parsedUrl= URL.parse(url, true) var port =""; - if( parsedUrl.port ) { + if( parsedUrl.port ) { if( (parsedUrl.protocol == "http:" && parsedUrl.port != "80" ) || (parsedUrl.protocol == "https:" && parsedUrl.port != "443") ) { port= ":" + parsedUrl.port; @@ -66,7 +66,7 @@ exports.OAuth.prototype._normalizeUrl= function(url) { } if( !parsedUrl.pathname || parsedUrl.pathname == "" ) parsedUrl.pathname ="/"; - + return parsedUrl.protocol + "//" + parsedUrl.hostname + port + parsedUrl.pathname; } @@ -77,7 +77,7 @@ exports.OAuth.prototype._buildAuthorizationHeaders= function(orderedParameters) // Whilst the all the parameters should be included within the signature, only the oauth_ arguments // should appear within the authorization header. var m = orderedParameters[i][0].match('^oauth_'); - if( m && m[0] === "oauth_") { + if( m && ( m[0] === "oauth_" || m[0] === 'scope' ) ) { authHeader+= this._encodeData(orderedParameters[i][0])+"=\""+ this._encodeData(orderedParameters[i][1])+"\","; } } @@ -88,15 +88,15 @@ exports.OAuth.prototype._buildAuthorizationHeaders= function(orderedParameters) // Takes a literal in, then returns a sorted array exports.OAuth.prototype._sortRequestParams= function(argumentsHash) { var argument_pairs= []; - for(var key in argumentsHash ) { + for(var key in argumentsHash ) { argument_pairs[argument_pairs.length]= [key, argumentsHash[key]]; } // Sort by name, then value. argument_pairs.sort(function(a,b) { if ( a[0]== b[0] ) { - return a[1] < b[1] ? -1 : 1; + return a[1] < b[1] ? -1 : 1; } - else return a[0] < b[0] ? -1 : 1; + else return a[0] < b[0] ? -1 : 1; }); return argument_pairs; @@ -110,19 +110,19 @@ exports.OAuth.prototype._normaliseRequestParams= function(arguments) { args+= "=" args+= this._encodeData( argument_pairs[i][1] ); if( i < argument_pairs.length-1 ) args+= "&"; - } + } return args; } exports.OAuth.prototype._createSignatureBase= function(method, url, parameters) { - url= this._encodeData( this._normalizeUrl(url) ); + url= this._encodeData( this._normalizeUrl(url) ); parameters= this._encodeData( parameters ); return method.toUpperCase() + "&" + url + "&" + parameters; } exports.OAuth.prototype._createSignature= function(signatureBase, tokenSecret) { if( tokenSecret === undefined ) var tokenSecret= ""; - else tokenSecret= this._encodeData( tokenSecret ); + else tokenSecret= this._encodeData( tokenSecret ); // consumerSecret is already encoded var key= this._consumerSecret + "&" + tokenSecret; @@ -147,7 +147,7 @@ exports.OAuth.prototype._getNonce= function(nonceSize) { var chars= this.NONCE_CHARS; var char_pos; var nonce_chars_length= chars.length; - + for (var i = 0; i < nonceSize; i++) { char_pos= Math.floor(Math.random() * nonce_chars_length); result[i]= chars[char_pos]; @@ -194,8 +194,8 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke var sig= this._getSignature( method, url, this._normaliseRequestParams(oauthParameters), oauth_token_secret); var orderedParameters= this._sortRequestParams( oauthParameters ); orderedParameters[orderedParameters.length]= ["oauth_signature", sig]; - - var query=""; + + var query=""; for( var i= 0 ; i < orderedParameters.length; i++) { query+= this._encodeData(orderedParameters[i][0])+"="+ this._encodeData(orderedParameters[i][1]) + "&"; } @@ -222,7 +222,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke headers["Content-length"]= post_body ? post_body.length : 0; //Probably going to fail if not posting ascii headers["Content-Type"]= post_content_type; - + var path; if( !parsedUrl.pathname || parsedUrl.pathname == "" ) parsedUrl.pathname ="/"; if( parsedUrl.query ) path= parsedUrl.pathname + "?"+ parsedUrl.query ; @@ -230,7 +230,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke var request = oauthProvider.request(method, path , headers); if( callback ) { - var data=""; + var data=""; var self= this; request.addListener('response', function (response) { response.setEncoding('utf8'); @@ -245,7 +245,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke } }); }); - + request.socket.addListener("error",callback); if( method == "POST" && post_body != null && post_body != "" ) { request.write(post_body); @@ -258,7 +258,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke } return request; } - + return; } @@ -269,7 +269,7 @@ exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_s } else { extraParams.oauth_verifier= oauth_verifier; } - + this._performSecureRequest( oauth_token, oauth_token_secret, "GET", this._accessUrl, extraParams, "", null, function(error, data, response) { if( error ) callback(error); else { @@ -305,14 +305,14 @@ exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, pos } return this._performSecureRequest( oauth_token, oauth_token_secret, "POST", url, extra_params, post_body, post_content_type, callback ); } - + exports.OAuth.prototype.getOAuthRequestToken= function(extraParams, callback) { if( typeof extraParams == "function" ){ callback = extraParams; extraParams = {}; } - // Callbacks are 1.0A related + // Callbacks are 1.0A related if( this._authorize_callback ) { extraParams["oauth_callback"]= this._authorize_callback; } @@ -361,12 +361,12 @@ exports.OAuth.prototype.signUrl= function(url, oauth_token, oauth_token_secret, var orderedParameters= this._sortRequestParams( collectedParameters ); orderedParameters[orderedParameters.length]= ["oauth_signature", sig]; - var query=""; + var query=""; for( var i= 0 ; i < orderedParameters.length; i++) { query+= orderedParameters[i][0]+"="+ this._encodeData(orderedParameters[i][1]) + "&"; } query= query.substring(0, query.length-1); - + return parsedUrl.protocol + "//"+ parsedUrl.host + parsedUrl.pathname + "?" + query; };