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

Commit b0ab234

Browse files
author
Rob Griffiths
committed
Merge commit 'ae3ac7e264d3d679cc7e3ee8cf82594d18f4913f'
2 parents cef72f8 + ae3ac7e commit b0ab234

File tree

1 file changed

+127
-83
lines changed

1 file changed

+127
-83
lines changed

src/OAuth/Consumer.js

Lines changed: 127 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,120 @@
3434
accessTokenSecret: options.accessTokenSecret || empty,
3535
verifier: empty,
3636

37-
signatureMethod: options.signatureMethod || 'HMAC-SHA1'
37+
signatureMethod: options.signatureMethod || 'HMAC-SHA1',
38+
timeStampFormat: options.timeStampFormat || 'ms'
3839
};
3940

4041
this.realm = options.realm || empty;
4142
this.requestTokenUrl = options.requestTokenUrl || empty;
4243
this.authorizationUrl = options.authorizationUrl || empty;
4344
this.accessTokenUrl = options.accessTokenUrl || empty;
45+
this.headerParams = {};
46+
47+
this.getTimeStampFormat= function () {
48+
return oauth.timeStampFormat;
49+
}
50+
51+
//pulled this out of this.request to be accessible from not-closure context
52+
this.getHeaderParams = function (options) {
53+
if (typeof options == "undefined")
54+
var options = {};
55+
var url, headers, data, urlString, method, signature, signatureString, signatureMethod, urlString, appendQueryString, signatureData = {}, withFile = false;
56+
57+
method = options.method || 'GET';
58+
url = options.url ? URI(options.url) : '';
59+
data = options.data || {};
60+
headers = options.headers || {};
61+
appendQueryString = options.appendQueryString ? options.appendQueryString : false;
62+
63+
headerParams = {
64+
'oauth_callback': oauth.callbackUrl,
65+
'oauth_consumer_key': oauth.consumerKey,
66+
'oauth_token': oauth.accessTokenKey,
67+
'oauth_signature_method': oauth.signatureMethod,
68+
'oauth_timestamp': this.getTimestamp(),
69+
'oauth_nonce': getNonce(),
70+
'oauth_verifier': oauth.verifier,
71+
'oauth_version': OAUTH_VERSION_1_0
72+
};
73+
74+
this.setHeaderParams(headerParams);
75+
signatureMethod = oauth.signatureMethod;
76+
77+
// Handle GET params first
78+
params = url.query.toObject();
79+
for (i in params) {
80+
signatureData[i] = params[i];
81+
}
82+
83+
// According to the OAuth spec
84+
// if data is transfered using
85+
// multipart the POST data doesn't
86+
// have to be signed:
87+
// http://www.mail-archive.com/[email protected]/msg01556.html
88+
if((!('Content-Type' in headers) || headers['Content-Type'] == 'application/x-www-form-urlencoded') && !withFile) {
89+
for (i in data) {
90+
signatureData[i] = data[i];
91+
}
92+
}
93+
94+
urlString = url.scheme + '://' + url.host + url.path;
95+
96+
signatureString = toSignatureBaseString(method, urlString, headerParams, signatureData);
97+
98+
signature = OAuth.signatureMethod[signatureMethod](oauth.consumerSecret, oauth.accessTokenSecret, signatureString);
99+
100+
headerParams.oauth_signature = signature;
101+
102+
if (this.realm)
103+
{
104+
headerParams['realm'] = this.realm;
105+
}
106+
107+
if (oauth.proxyUrl) {
108+
url = URI(oauth.proxyUrl + url.path);
109+
}
110+
111+
if(appendQueryString || method == 'GET') {
112+
url.query.setQueryParams(data);
113+
query = null;
114+
} else if(!withFile){
115+
if (typeof data == 'string') {
116+
query = data;
117+
if (!('Content-Type' in headers)) {
118+
headers['Content-Type'] = 'text/plain';
119+
}
120+
} else {
121+
for(i in data) {
122+
query.push(OAuth.urlEncode(i) + '=' + OAuth.urlEncode(data[i] + ''));
123+
}
124+
query = query.sort().join('&');
125+
if (!('Content-Type' in headers)) {
126+
headers['Content-Type'] = 'application/x-www-form-urlencoded';
127+
}
128+
}
129+
130+
} else if(withFile) {
131+
// When using FormData multipart content type
132+
// is used by default and required header
133+
// is set to multipart/form-data etc
134+
query = new FormData();
135+
for(i in data) {
136+
query.append(i, data[i]);
137+
}
138+
}
139+
140+
return oauth.headerParams;
141+
}
142+
143+
//transforms params to string
144+
this.getHeaderString = function (url){
145+
return toHeaderString(this.getHeaderParams({url:url}));
146+
}
147+
148+
this.setHeaderParams = function (headerParams) {
149+
oauth.headerParams = headerParams;
150+
}
44151

45152
this.getAccessToken = function () {
46153
return [oauth.accessTokenKey, oauth.accessTokenSecret];
@@ -90,7 +197,7 @@
90197
this.request = function (options) {
91198
var method, url, data, headers, success, failure, xhr, i,
92199
headerParams, signatureMethod, signatureString, signature,
93-
query = [], appendQueryString, signatureData = {}, params, withFile;
200+
query = [], appendQueryString, signatureData = {}, params, withFile, urlString;
94201

95202
method = options.method || 'GET';
96203
url = URI(options.url);
@@ -159,80 +266,7 @@
159266
}
160267
};
161268

162-
headerParams = {
163-
'oauth_callback': oauth.callbackUrl,
164-
'oauth_consumer_key': oauth.consumerKey,
165-
'oauth_token': oauth.accessTokenKey,
166-
'oauth_signature_method': oauth.signatureMethod,
167-
'oauth_timestamp': getTimestamp(),
168-
'oauth_nonce': getNonce(),
169-
'oauth_verifier': oauth.verifier,
170-
'oauth_version': OAUTH_VERSION_1_0
171-
};
172-
173-
signatureMethod = oauth.signatureMethod;
174-
175-
// Handle GET params first
176-
params = url.query.toObject();
177-
for (i in params) {
178-
signatureData[i] = params[i];
179-
}
180-
181-
// According to the OAuth spec
182-
// if data is transfered using
183-
// multipart the POST data doesn't
184-
// have to be signed:
185-
// http://www.mail-archive.com/[email protected]/msg01556.html
186-
if((!('Content-Type' in headers) || headers['Content-Type'] == 'application/x-www-form-urlencoded') && !withFile) {
187-
for (i in data) {
188-
signatureData[i] = data[i];
189-
}
190-
}
191-
192-
urlString = url.scheme + '://' + url.host + url.path;
193-
signatureString = toSignatureBaseString(method, urlString, headerParams, signatureData);
194-
195-
signature = OAuth.signatureMethod[signatureMethod](oauth.consumerSecret, oauth.accessTokenSecret, signatureString);
196-
197-
headerParams.oauth_signature = signature;
198-
199-
if (this.realm)
200-
{
201-
headerParams['realm'] = this.realm;
202-
}
203-
204-
if (oauth.proxyUrl) {
205-
url = URI(oauth.proxyUrl + url.path);
206-
}
207-
208-
if(appendQueryString || method == 'GET') {
209-
url.query.setQueryParams(data);
210-
query = null;
211-
} else if(!withFile){
212-
if (typeof data == 'string') {
213-
query = data;
214-
if (!('Content-Type' in headers)) {
215-
headers['Content-Type'] = 'text/plain';
216-
}
217-
} else {
218-
for(i in data) {
219-
query.push(OAuth.urlEncode(i) + '=' + OAuth.urlEncode(data[i] + ''));
220-
}
221-
query = query.sort().join('&');
222-
if (!('Content-Type' in headers)) {
223-
headers['Content-Type'] = 'application/x-www-form-urlencoded';
224-
}
225-
}
226-
227-
} else if(withFile) {
228-
// When using FormData multipart content type
229-
// is used by default and required header
230-
// is set to multipart/form-data etc
231-
query = new FormData();
232-
for(i in data) {
233-
query.append(i, data[i]);
234-
}
235-
}
269+
headerParams = this.getHeaderParams(options);
236270

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

@@ -354,6 +388,22 @@
354388

355389
success(data);
356390
}, failure);
391+
},
392+
393+
/**
394+
* Generate a timestamp for the request
395+
*
396+
* moved function into prototype to have oauth.getTimeStampFormat() of instance avalable
397+
*/
398+
getTimestamp: function() {
399+
var oauth = this;
400+
401+
switch (oauth.getTimeStampFormat()){
402+
case ('ms'):
403+
return parseInt(+new Date() / 1000, 10); // use short form of getting a milliseconds-timestamp
404+
default:
405+
return parseInt(+new Date() / 100000, 10); // use short form of getting a seconds-timestamp
406+
}
357407
}
358408
};
359409

@@ -458,13 +508,7 @@
458508
].join('&');
459509
}
460510

461-
/**
462-
* Generate a timestamp for the request
463-
*/
464-
function getTimestamp() {
465-
return parseInt(+new Date() / 1000, 10); // use short form of getting a timestamp
466-
}
467-
511+
468512
/**
469513
* Generate a nonce for the request
470514
*

0 commit comments

Comments
 (0)