From c4dfd669c89ff36d7d334921a1183bfc5efb8a82 Mon Sep 17 00:00:00 2001 From: richerd Date: Tue, 4 Feb 2014 22:57:57 -0500 Subject: [PATCH 1/2] Only encode body in oauth signature if the content-type is x-www-form-urlencoded --- superagent-oauth.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/superagent-oauth.js b/superagent-oauth.js index 6750547..7b42a7a 100644 --- a/superagent-oauth.js +++ b/superagent-oauth.js @@ -1,4 +1,16 @@ +/** + * Check if `obj` is an object. + * + * @param {Object} obj + * @return {Boolean} + * @api private + */ + +function isObject(obj) { + return null != obj && 'object' == typeof obj; +} + module.exports = function (superagent) { /** @@ -54,12 +66,30 @@ module.exports = function (superagent) { */ Request.prototype.signOAuth = function () { + + var req = this.request(); + var type = req.getHeader('Content-Type'); + var extra_params = this._oauth_query; + + if ('application/x-www-form-urlencoded' == type && isObject(this._data)) { + if(!extra_params) { + extra_params = this._data; + } else { + // merge + var keys = Object.keys(this._data), key; + for (var i = 0; i < keys.length; i++) { + key = keys[i]; + extra_params[key] = this._data[key]; + } + } + } + var params = this.oa._prepareParameters( this.token , this.secret , this.method , this.url - , this._data || this._oauth_query // XXX: what if there's query and body? merge? + , extra_params ); var header = this.oa._isEcho From 1c0077f4ec936bc950447df9e83e9b2f52a614d4 Mon Sep 17 00:00:00 2001 From: richerd Date: Wed, 5 Feb 2014 09:02:36 -0500 Subject: [PATCH 2/2] Reverse if so true is on top --- superagent-oauth.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/superagent-oauth.js b/superagent-oauth.js index 7b42a7a..60ab137 100644 --- a/superagent-oauth.js +++ b/superagent-oauth.js @@ -72,15 +72,15 @@ module.exports = function (superagent) { var extra_params = this._oauth_query; if ('application/x-www-form-urlencoded' == type && isObject(this._data)) { - if(!extra_params) { - extra_params = this._data; - } else { + if(extra_params) { // merge var keys = Object.keys(this._data), key; for (var i = 0; i < keys.length; i++) { key = keys[i]; extra_params[key] = this._data[key]; } + } else { + extra_params = this._data; } }