Fixed the parameter ordering, and added support for repeated arguments. Fixes issue 14
This commit is contained in:
40
lib/oauth.js
40
lib/oauth.js
@ -95,12 +95,26 @@ exports.OAuth.prototype._buildAuthorizationHeaders= function(orderedParameters)
|
||||
return authHeader;
|
||||
}
|
||||
|
||||
// Takes a literal in, then returns a sorted array
|
||||
exports.OAuth.prototype._sortRequestParams= function(argumentsHash) {
|
||||
// Takes an object literal that represents the arguments, and returns an array
|
||||
// of argument/value pairs.
|
||||
exports.OAuth.prototype._makeArrayOfArgumentsHash= function(argumentsHash) {
|
||||
var argument_pairs= [];
|
||||
for(var key in argumentsHash ) {
|
||||
argument_pairs[argument_pairs.length]= [key, argumentsHash[key]];
|
||||
for(var key in argumentsHash ) {
|
||||
var value= argumentsHash[key];
|
||||
if( Array.isArray(value) ) {
|
||||
for(var i=0;i<value.length;i++) {
|
||||
argument_pairs[argument_pairs.length]= [key, value[i]];
|
||||
}
|
||||
}
|
||||
else {
|
||||
argument_pairs[argument_pairs.length]= [key, value];
|
||||
}
|
||||
}
|
||||
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] ) {
|
||||
@ -113,12 +127,22 @@ exports.OAuth.prototype._sortRequestParams= function(argumentsHash) {
|
||||
}
|
||||
|
||||
exports.OAuth.prototype._normaliseRequestParams= function(arguments) {
|
||||
var argument_pairs= this._sortRequestParams( arguments );
|
||||
var argument_pairs= this._makeArrayOfArgumentsHash(arguments);
|
||||
// First encode them #3.4.1.3.2 .1
|
||||
for(var i=0;i<argument_pairs.length;i++) {
|
||||
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+= this._encodeData( argument_pairs[i][0] );
|
||||
args+= argument_pairs[i][0];
|
||||
args+= "="
|
||||
args+= this._encodeData( argument_pairs[i][1] );
|
||||
args+= argument_pairs[i][1];
|
||||
if( i < argument_pairs.length-1 ) args+= "&";
|
||||
}
|
||||
return args;
|
||||
@ -196,7 +220,7 @@ exports.OAuth.prototype._prepareParameters= function( oauth_token, oauth_token_s
|
||||
}
|
||||
|
||||
var sig= this._getSignature( method, url, this._normaliseRequestParams(oauthParameters), oauth_token_secret);
|
||||
var orderedParameters= this._sortRequestParams( oauthParameters );
|
||||
var orderedParameters= this._sortRequestParams( this._makeArrayOfArgumentsHash(oauthParameters) );
|
||||
orderedParameters[orderedParameters.length]= ["oauth_signature", sig];
|
||||
return orderedParameters;
|
||||
}
|
||||
|
Reference in New Issue
Block a user