Permalink
Browse files

Split request signing into sub methods for easier unit testing.

  • Loading branch information...
1 parent 34dcb03 commit 8213e0b35a2f337e282bcddbac63d3bf44e0e8fb @novemberborn novemberborn committed Jan 2, 2011
Showing with 40 additions and 15 deletions.
  1. +40 −15 lib/oauth.js
View
@@ -85,6 +85,10 @@ Client.makeNonceGenerator = function(nonceSize){
}
};
+Client.getTimestamp = function(){
+ return Math.floor(Date.now() / 1000).toString();
+};
+
// Binds the client against a set of token credentials.
// The resulting object can be used to make signed requests.
// The secret won't be exposed on the object itself.
@@ -98,8 +102,12 @@ Client.prototype.bind = function(tokenIdentifier, tokenSecret){
generateNonce: this.generateNonce
};
bound._createSignature = this._createSignature.bind(this, encodeRfc3986(tokenSecret));
- bound._signRequest = this._signRequest.bind(bound);
- bound.request = this.request.bind(bound);
+ bound._createSignatureBase = this._createSignatureBase;
+ bound._normalizeUrl = this._normalizeUrl;
+ bound._collectOAuthParams = this._collectOAuthParams;
+ bound._normalizeParams = this._normalizeParams;
+ bound._signRequest = this._signRequest;
+ bound.request = this.request;
return bound;
};
@@ -175,19 +183,18 @@ Client.prototype.request = function(originalRequest){
}.bind(this));
};
-Client.prototype._signRequest = function(request, requestParams){
- // Calculate base URI string
- var baseUri = request.protocol + "//" + request.hostname;
+Client.prototype._normalizeUrl = function(request){
+ var normalized = request.protocol + "//" + request.hostname;
if(request.protocol == "http" && request.port && (request.port + "") != "80"){
- baseUri += ":" + request.port;
+ normalized += ":" + request.port;
}
if(request.protocol == "https" && request.port && (request.port + "") != "443"){
- baseUri += ":" + request.port;
+ normalized += ":" + request.port;
}
- baseUri += request.pathname;
-
- // Register OAuth parameters and add to the request parameters
- // Additional parameters can be specified via the `request.oauthParams` object
+ return normalized + request.pathname;
+};
+
+Client.prototype._collectOAuthParams = function(request, requestParams){
var oauthParams = {};
if(request.oauthParams){
for(var p in request.oauthParams){
@@ -202,7 +209,7 @@ Client.prototype._signRequest = function(request, requestParams){
}
oauthParams.oauth_consumer_key = this.identifier;
oauthParams.oauth_signature_method = this.signatureMethod;
- oauthParams.oauth_timestamp = Math.floor(Date.now() / 1000);
+ oauthParams.oauth_timestamp = Client.getTimestamp();
oauthParams.oauth_nonce = this.generateNonce();
if(this.tokenIdentifier){
oauthParams.oauth_token = this.tokenIdentifier;
@@ -213,7 +220,10 @@ Client.prototype._signRequest = function(request, requestParams){
for(var i in oauthParams){
requestParams.push(i, oauthParams[i]);
}
-
+ return oauthParams;
+};
+
+Client.prototype._normalizeParams = function(requestParams){
// Encode requestParams
requestParams = requestParams.map(encodeRfc3986);
// Unflatten the requestParams for sorting
@@ -231,11 +241,26 @@ Client.prototype._signRequest = function(request, requestParams){
return a[0] < b[0] ? -1 : 1;
}
});
+ return requestParams.map(function(pair){ return pair.join("="); }).join("&");
+};
+
+Client.prototype._createSignatureBase = function(requestMethod, baseUri, params){
+ return [requestMethod, baseUri, params].map(encodeRfc3986).join("&");
+};
+
+Client.prototype._signRequest = function(request, requestParams){
+ // Calculate base URI string
+ var baseUri = this._normalizeUrl(request);
+
+ // Register OAuth parameters and add to the request parameters
+ // Additional parameters can be specified via the `request.oauthParams` object
+ var oauthParams = this._collectOAuthParams(request, requestParams);
+
// Generate parameter string
- var params = requestParams.map(function(pair){ return pair.join("="); }).join("&");
+ var params = this._normalizeParams(requestParams);
// Sign the base string
- var baseString = [request.method, baseUri, params].map(encodeRfc3986).join("&");
+ var baseString = this._createSignatureBase(request.method, baseUri, params);
oauthParams.oauth_signature = this._createSignature(baseString);
// Add Authorization header

0 comments on commit 8213e0b

Please sign in to comment.