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

Added method for XAuth access token requests #100

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 50 additions & 28 deletions lib/oauth.js
Expand Up @@ -84,15 +84,15 @@ exports.OAuth.prototype._getSignature= function(method, url, parameters, tokenSe
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;
}
}

if( !parsedUrl.pathname || parsedUrl.pathname == "" ) parsedUrl.pathname ="/";

return parsedUrl.protocol + "//" + parsedUrl.hostname + port + parsedUrl.pathname;
}

Expand Down Expand Up @@ -141,17 +141,17 @@ exports.OAuth.prototype._makeArrayOfArgumentsHash= function(argumentsHash) {
argument_pairs[argument_pairs.length]= [key, value];
}
}
return argument_pairs;
}
return argument_pairs;
}

// Sorts the encoded key value pairs by encoded name, then encoded value
exports.OAuth.prototype._sortRequestParams= function(argument_pairs) {
// 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;
Expand All @@ -164,30 +164,30 @@ exports.OAuth.prototype._normaliseRequestParams= function(arguments) {
argument_pairs[i][0]= this._encodeData( argument_pairs[i][0] );
argument_pairs[i][1]= this._encodeData( argument_pairs[i][1] );
}

// Then sort them #3.4.1.3.2 .2
argument_pairs= this._sortRequestParams( argument_pairs );

// Then concatenate together #3.4.1.3.2 .3 & .4
var args= "";
for(var i=0;i<argument_pairs.length;i++) {
args+= argument_pairs[i][0];
args+= "="
args+= 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;

Expand All @@ -200,7 +200,7 @@ exports.OAuth.prototype._createSignature= function(signatureBase, tokenSecret) {
hash = crypto.createHmac("sha1", key).update(signatureBase).digest("base64");
}
else {
hash= sha1.HMACSHA1(key, signatureBase);
hash= sha1.HMACSHA1(key, signatureBase);
}
}
return hash;
Expand All @@ -216,7 +216,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];
Expand All @@ -238,7 +238,7 @@ exports.OAuth.prototype._createClient= function( port, hostname, method, path, h
} else {
httpModel= http;
}
return httpModel.request(options);
return httpModel.request(options);
}

exports.OAuth.prototype._prepareParameters= function( oauth_token, oauth_token_secret, method, url, extra_params ) {
Expand Down Expand Up @@ -330,7 +330,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke

headers["Content-length"]= post_body ? Buffer.byteLength(post_body) : 0;
headers["Content-Type"]= post_content_type;

var path;
if( !parsedUrl.pathname || parsedUrl.pathname == "" ) parsedUrl.pathname ="/";
if( parsedUrl.query ) path= parsedUrl.pathname + "?"+ parsedUrl.query ;
Expand All @@ -345,7 +345,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
}

if( callback ) {
var data="";
var data="";
var self= this;

// Some hosts *cough* google appear to close the connection early / send no content-length header
Expand Down Expand Up @@ -383,12 +383,12 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
}
});
});

request.on("error", function(err) {
callbackCalled= true;
callback( err )
});

if( (method == "POST" || method =="PUT") && post_body != null && post_body != "" ) {
request.write(post_body);
}
Expand All @@ -400,7 +400,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
}
return request;
}

return;
}

Expand All @@ -420,14 +420,36 @@ exports.OAuth.prototype.setClientOptions= function(options) {
this._clientOptions= mergedOptions;
};

exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_secret, oauth_verifier, callback) {
exports.OAuth.prototype.getXAuthAccessToken= function(x_auth_username, x_auth_password, callback) {
var x_auth_params = {
'x_auth_mode': 'client_auth',
'x_auth_password': x_auth_password,
'x_auth_username': x_auth_username
};

var extraParams = x_auth_params;

this._performSecureRequest(null, null, this._clientOptions.accessTokenHttpMethod, this._accessUrl, extraParams, null, null, function(error, data, response) {
if( error ) callback(error);
else {
var results= querystring.parse( data );
var oauth_access_token= results["oauth_token"];
delete results["oauth_token"];
var oauth_access_token_secret= results["oauth_token_secret"];
delete results["oauth_token_secret"];
callback(null, oauth_access_token, oauth_access_token_secret, results );
}
});
}

exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_secret, oauth_verifier, callback, extraParams) {
var extraParams= {};
if( typeof oauth_verifier == "function" ) {
callback= oauth_verifier;
} else {
extraParams.oauth_verifier= oauth_verifier;
}

this._performSecureRequest( oauth_token, oauth_token_secret, this._clientOptions.accessTokenHttpMethod, this._accessUrl, extraParams, null, null, function(error, data, response) {
if( error ) callback(error);
else {
Expand All @@ -438,8 +460,8 @@ exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_s
delete results["oauth_token_secret"];
callback(null, oauth_access_token, oauth_access_token_secret, results );
}
})
}
});
};

// Deprecated
exports.OAuth.prototype.getProtectedResource= function(url, method, oauth_token, oauth_token_secret, callback) {
Expand Down Expand Up @@ -467,7 +489,7 @@ exports.OAuth.prototype._putOrPost= function(method, url, oauth_token, oauth_tok
}
return this._performSecureRequest( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback );
}


exports.OAuth.prototype.put= function(url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) {
return this._putOrPost("PUT", url, oauth_token, oauth_token_secret, post_body, post_content_type, callback);
Expand All @@ -483,7 +505,7 @@ exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, pos
*
* The callback should expect a function of the following form:
*
* function(err, token, token_secret, parsedQueryString) {}
* function(err, token, token_secret, parsedQueryString) {}
*
* This method has optional parameters so can be called in the following 2 ways:
*
Expand All @@ -502,7 +524,7 @@ exports.OAuth.prototype.getOAuthRequestToken= function( extraParams, callback )
callback = extraParams;
extraParams = {};
}
// Callbacks are 1.0A related
// Callbacks are 1.0A related
if( this._authorize_callback ) {
extraParams["oauth_callback"]= this._authorize_callback;
}
Expand All @@ -529,12 +551,12 @@ exports.OAuth.prototype.signUrl= function(url, oauth_token, oauth_token_secret,
var orderedParameters= this._prepareParameters(oauth_token, oauth_token_secret, method, url, {});
var parsedUrl= URL.parse( url, false );

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;
};

Expand Down