|
34 | 34 | accessTokenSecret: options.accessTokenSecret || empty,
|
35 | 35 | verifier: empty,
|
36 | 36 |
|
37 |
| - signatureMethod: options.signatureMethod || 'HMAC-SHA1' |
| 37 | + signatureMethod: options.signatureMethod || 'HMAC-SHA1', |
| 38 | + timeStampFormat: options.timeStampFormat || 'ms' |
38 | 39 | };
|
39 | 40 |
|
40 | 41 | this.realm = options.realm || empty;
|
41 | 42 | this.requestTokenUrl = options.requestTokenUrl || empty;
|
42 | 43 | this.authorizationUrl = options.authorizationUrl || empty;
|
43 | 44 | 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 | + } |
44 | 151 |
|
45 | 152 | this.getAccessToken = function () {
|
46 | 153 | return [oauth.accessTokenKey, oauth.accessTokenSecret];
|
|
90 | 197 | this.request = function (options) {
|
91 | 198 | var method, url, data, headers, success, failure, xhr, i,
|
92 | 199 | headerParams, signatureMethod, signatureString, signature,
|
93 |
| - query = [], appendQueryString, signatureData = {}, params, withFile; |
| 200 | + query = [], appendQueryString, signatureData = {}, params, withFile, urlString; |
94 | 201 |
|
95 | 202 | method = options.method || 'GET';
|
96 | 203 | url = URI(options.url);
|
|
159 | 266 | }
|
160 | 267 | };
|
161 | 268 |
|
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); |
236 | 270 |
|
237 | 271 | xhr.open(method, url+'', true);
|
238 | 272 |
|
|
354 | 388 |
|
355 | 389 | success(data);
|
356 | 390 | }, 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 | + } |
357 | 407 | }
|
358 | 408 | };
|
359 | 409 |
|
|
458 | 508 | ].join('&');
|
459 | 509 | }
|
460 | 510 |
|
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 | + |
468 | 512 | /**
|
469 | 513 | * Generate a nonce for the request
|
470 | 514 | *
|
|
0 commit comments