Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Reversed breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Griffiths committed Sep 18, 2012
1 parent b4686b2 commit 402593f
Showing 1 changed file with 82 additions and 127 deletions.
209 changes: 82 additions & 127 deletions src/OAuth/Consumer.js
Expand Up @@ -33,121 +33,13 @@
accessTokenKey: options.accessTokenKey || empty,
accessTokenSecret: options.accessTokenSecret || empty,
verifier: empty,

signatureMethod: options.signatureMethod || 'HMAC-SHA1',
timeStampFormat: options.timeStampFormat || 'ms'
signatureMethod: options.signatureMethod || 'HMAC-SHA1'
};

this.realm = options.realm || empty;
this.requestTokenUrl = options.requestTokenUrl || empty;
this.authorizationUrl = options.authorizationUrl || empty;
this.accessTokenUrl = options.accessTokenUrl || empty;
this.headerParams = {};

this.getTimeStampFormat= function () {
return oauth.timeStampFormat;
}

//pulled this out of this.request to be accessible from not-closure context
this.getHeaderParams = function (options) {
if (typeof options == "undefined")
var options = {};
var url, headers, data, urlString, method, signature, signatureString, signatureMethod, urlString, appendQueryString, signatureData = {}, withFile = false;

method = options.method || 'GET';
url = options.url ? URI(options.url) : '';
data = options.data || {};
headers = options.headers || {};
appendQueryString = options.appendQueryString ? options.appendQueryString : false;

headerParams = {
'oauth_callback': oauth.callbackUrl,
'oauth_consumer_key': oauth.consumerKey,
'oauth_token': oauth.accessTokenKey,
'oauth_signature_method': oauth.signatureMethod,
'oauth_timestamp': this.getTimestamp(),
'oauth_nonce': getNonce(),
'oauth_verifier': oauth.verifier,
'oauth_version': OAUTH_VERSION_1_0
};

this.setHeaderParams(headerParams);
signatureMethod = oauth.signatureMethod;

// Handle GET params first
params = url.query.toObject();
for (i in params) {
signatureData[i] = params[i];
}

// According to the OAuth spec
// if data is transfered using
// multipart the POST data doesn't
// have to be signed:
// http://www.mail-archive.com/oauth@googlegroups.com/msg01556.html
if((!('Content-Type' in headers) || headers['Content-Type'] == 'application/x-www-form-urlencoded') && !withFile) {
for (i in data) {
signatureData[i] = data[i];
}
}

urlString = url.scheme + '://' + url.host + url.path;

signatureString = toSignatureBaseString(method, urlString, headerParams, signatureData);

signature = OAuth.signatureMethod[signatureMethod](oauth.consumerSecret, oauth.accessTokenSecret, signatureString);

headerParams.oauth_signature = signature;

if (this.realm)
{
headerParams['realm'] = this.realm;
}

if (oauth.proxyUrl) {
url = URI(oauth.proxyUrl + url.path);
}

if(appendQueryString || method == 'GET') {
url.query.setQueryParams(data);
query = null;
} else if(!withFile){
if (typeof data == 'string') {
query = data;
if (!('Content-Type' in headers)) {
headers['Content-Type'] = 'text/plain';
}
} else {
for(i in data) {
query.push(OAuth.urlEncode(i) + '=' + OAuth.urlEncode(data[i] + ''));
}
query = query.sort().join('&');
if (!('Content-Type' in headers)) {
headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
}

} else if(withFile) {
// When using FormData multipart content type
// is used by default and required header
// is set to multipart/form-data etc
query = new FormData();
for(i in data) {
query.append(i, data[i]);
}
}

return oauth.headerParams;
}

//transforms params to string
this.getHeaderString = function (url){
return toHeaderString(this.getHeaderParams({url:url}));
}

this.setHeaderParams = function (headerParams) {
oauth.headerParams = headerParams;
}

this.getAccessToken = function () {
return [oauth.accessTokenKey, oauth.accessTokenSecret];
Expand Down Expand Up @@ -266,7 +158,80 @@
}
};

headerParams = this.getHeaderParams(options);
headerParams = {
'oauth_callback': oauth.callbackUrl,
'oauth_consumer_key': oauth.consumerKey,
'oauth_token': oauth.accessTokenKey,
'oauth_signature_method': oauth.signatureMethod,
'oauth_timestamp': getTimestamp(),
'oauth_nonce': getNonce(),
'oauth_verifier': oauth.verifier,
'oauth_version': OAUTH_VERSION_1_0
};

signatureMethod = oauth.signatureMethod;

// Handle GET params first
params = url.query.toObject();
for (i in params) {
signatureData[i] = params[i];
}

// According to the OAuth spec
// if data is transfered using
// multipart the POST data doesn't
// have to be signed:
// http://www.mail-archive.com/oauth@googlegroups.com/msg01556.html
if((!('Content-Type' in headers) || headers['Content-Type'] == 'application/x-www-form-urlencoded') && !withFile) {
for (i in data) {
signatureData[i] = data[i];
}
}

urlString = url.scheme + '://' + url.host + url.path;
signatureString = toSignatureBaseString(method, urlString, headerParams, signatureData);

signature = OAuth.signatureMethod[signatureMethod](oauth.consumerSecret, oauth.accessTokenSecret, signatureString);

headerParams.oauth_signature = signature;

if (this.realm)
{
headerParams['realm'] = this.realm;
}

if (oauth.proxyUrl) {
url = URI(oauth.proxyUrl + url.path);
}

if(appendQueryString || method == 'GET') {
url.query.setQueryParams(data);
query = null;
} else if(!withFile){
if (typeof data == 'string') {
query = data;
if (!('Content-Type' in headers)) {
headers['Content-Type'] = 'text/plain';
}
} else {
for(i in data) {
query.push(OAuth.urlEncode(i) + '=' + OAuth.urlEncode(data[i] + ''));
}
query = query.sort().join('&');
if (!('Content-Type' in headers)) {
headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
}

} else if(withFile) {
// When using FormData multipart content type
// is used by default and required header
// is set to multipart/form-data etc
query = new FormData();
for(i in data) {
query.append(i, data[i]);
}
}

xhr.open(method, url+'', true);

Expand Down Expand Up @@ -388,22 +353,6 @@

success(data);
}, failure);
},

/**
* Generate a timestamp for the request
*
* moved function into prototype to have oauth.getTimeStampFormat() of instance avalable
*/
getTimestamp: function() {
var oauth = this;

switch (oauth.getTimeStampFormat()){
case ('ms'):
return parseInt(+new Date() / 1000, 10); // use short form of getting a milliseconds-timestamp
default:
return parseInt(+new Date() / 100000, 10); // use short form of getting a seconds-timestamp
}
}
};

Expand Down Expand Up @@ -508,7 +457,13 @@
].join('&');
}


/**
* Generate a timestamp for the request
*/
function getTimestamp() {
return parseInt(+new Date() / 1000, 10); // use short form of getting a timestamp
}

/**
* Generate a nonce for the request
*
Expand Down

2 comments on commit 402593f

@axeff
Copy link

@axeff axeff commented on 402593f Oct 18, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happend?

@bytespider
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes that you made broke the functionality. I had to back them out but haven't had chance to check them into a branch and bug fix.

If you are willing to investigate, this is the reported issue

Please sign in to comment.