Skip to content

Commit

Permalink
Gentle refactor to improve testability of the authorization headers code
Browse files Browse the repository at this point in the history
  • Loading branch information
ciaranj committed Oct 17, 2010
1 parent 29b5abd commit 8a4b7e6
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/oauth.js
Expand Up @@ -70,6 +70,20 @@ exports.OAuth.prototype._normalizeUrl= function(url) {
return parsedUrl.protocol + "//" + parsedUrl.hostname + port + parsedUrl.pathname;
}

// build the OAuth request authorization header
exports.OAuth.prototype._buildAuthorizationHeaders= function(orderedParameters) {
var authHeader="OAuth ";
for( var i= 0 ; i < orderedParameters.length; i++) {
// Whilst the all the parameters should be included within the signature, only the oauth_ arguments
// should appear within the authorization header.
if( orderedParameters[i][0].match('^oauth_') == "oauth_") {
authHeader+= this._encodeData(orderedParameters[i][0])+"=\""+ this._encodeData(orderedParameters[i][1])+"\",";
}
}
authHeader= authHeader.substring(0, authHeader.length-1);
return authHeader;
}

// Takes a literal in, then returns a sorted array
exports.OAuth.prototype._sortRequestParams= function(argumentsHash) {
var argument_pairs= [];
Expand Down Expand Up @@ -196,19 +210,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
}

var headers= {};

// build request authorization header
var authHeader="OAuth ";
for( var i= 0 ; i < orderedParameters.length; i++) {
// Whilst the all the parameters should be included within the signature, only the oauth_ arguments
// should appear within the authorization header.
if( orderedParameters[i][0].match('^oauth_') == "oauth_") {
authHeader+= this._encodeData(orderedParameters[i][0])+"=\""+ this._encodeData(orderedParameters[i][1])+"\",";
}
}
authHeader= authHeader.substring(0, authHeader.length-1);

headers["Authorization"]= authHeader;
headers["Authorization"]= this._buildAuthorizationHeaders(orderedParameters);
headers["Host"] = parsedUrl.host

for( var key in this._headers ) {
Expand Down

0 comments on commit 8a4b7e6

Please sign in to comment.