Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Changed the post method to support passing of either a POST body stri…
…ng and optional content-type,

or a hash of query parameters that will be url form encoded.
  • Loading branch information
ciaranj committed Aug 6, 2010
1 parent ca370f3 commit 752c441
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/oauth.js
Expand Up @@ -48,7 +48,7 @@ exports.OAuth.prototype._decodeData= function(toDecode) {
}

exports.OAuth.prototype._getSignature= function(method, url, parameters, tokenSecret) {
var signatureBase= this._createSignatureBase(method, url, parameters);
var signatureBase= this._createSignatureBase(method, url, parameters);
return this._createSignature( signatureBase, tokenSecret );
}

Expand Down Expand Up @@ -141,7 +141,7 @@ exports.OAuth.prototype._createClient= function( port, hostname, sshEnabled, cre
return http.createClient(port, hostname, sshEnabled, credentials);
}

exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, callback ) {
exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback ) {
var oauthParameters= {
"oauth_timestamp": this._getTimestamp(),
"oauth_nonce": this._getNonce(this._nonceSize),
Expand All @@ -158,6 +158,9 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
oauthParameters[key]= extra_params[key];
}
}
if( !post_content_type ) {
post_content_type= "application/x-www-form-urlencoded";
}

var parsedUrl= URL.parse( url, false );
if( parsedUrl.protocol == "http:" && !parsedUrl.port ) parsedUrl.port= 80;
Expand Down Expand Up @@ -208,7 +211,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
headers["Connection"]= "close"
headers["User-Agent"]= "Express authentication"
headers["Content-length"]= post_body ? post_body.length : 0; //Probably going to fail if not posting ascii
headers["Content-Type"]= "application/x-www-form-urlencoded"
headers["Content-Type"]= post_content_type;

var path;
if( !parsedUrl.pathname || parsedUrl.pathname == "" ) parsedUrl.pathname ="/";
Expand Down Expand Up @@ -247,7 +250,7 @@ exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_s
extraParams.oauth_verifier= oauth_verifier;
}

this._performSecureRequest( oauth_token, oauth_token_secret, "GET", this._accessUrl, extraParams, "", function(error, data, response) {
this._performSecureRequest( oauth_token, oauth_token_secret, "GET", this._accessUrl, extraParams, "", null, function(error, data, response) {
if( error ) callback(error);
else {
var results= querystring.parse( data );
Expand All @@ -262,15 +265,25 @@ exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_s

// Deprecated
exports.OAuth.prototype.getProtectedResource= function(url, method, oauth_token, oauth_token_secret, callback) {
this._performSecureRequest( oauth_token, oauth_token_secret, method, url, null, "", callback );
this._performSecureRequest( oauth_token, oauth_token_secret, method, url, null, "", null, callback );
}

exports.OAuth.prototype.get= function(url, oauth_token, oauth_token_secret, callback) {
this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, "", callback );
this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, "", null, callback );
}

exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, post_body, callback) {
this._performSecureRequest( oauth_token, oauth_token_secret, "POST", url, null, post_body, callback );
exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) {
var extra_params= null;
if( typeof post_content_type == "function" ) {
callback= post_content_type;
post_content_type= null;
}
if( typeof post_body != "string" ) {
post_content_type= "application/x-www-form-urlencoded"
extra_params= post_body;
post_body= querystring.stringify(post_body);
}
this._performSecureRequest( oauth_token, oauth_token_secret, "POST", url, extra_params, post_body, post_content_type, callback );
}

exports.OAuth.prototype.getOAuthRequestToken= function(extraParams, callback) {
Expand All @@ -283,7 +296,7 @@ exports.OAuth.prototype.getOAuthRequestToken= function(extraParams, callback) {
if( this._authorize_callback ) {
extraParams["oauth_callback"]= this._authorize_callback;
}
this._performSecureRequest( null, null, "POST", this._requestUrl, extraParams, "", function(error, data, response) {
this._performSecureRequest( null, null, "POST", this._requestUrl, extraParams, "", null, function(error, data, response) {
if( error ) callback(error);
else {
var results= querystring.parse(data);
Expand Down

0 comments on commit 752c441

Please sign in to comment.