This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
158 lines (132 loc) · 4.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use strict';
var hashes = require('jshashes'),
sha1 = new hashes.SHA1();
// # xtend
var hasOwnProperty = Object.prototype.hasOwnProperty;
function xtend() {
var target = {};
for (var i = 0; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
}
var ohauth = {};
ohauth.qsString = function(obj) {
return Object.keys(obj).sort().map(function(key) {
return ohauth.percentEncode(key) + '=' +
ohauth.percentEncode(obj[key]);
}).join('&');
};
ohauth.stringQs = function(str) {
return str.split('&').filter(function (pair) {
return pair !== '';
}).reduce(function(obj, pair){
var parts = pair.split('=');
obj[decodeURIComponent(parts[0])] = (null === parts[1]) ?
'' : decodeURIComponent(parts[1]);
return obj;
}, {});
};
ohauth.rawxhr = function(method, url, data, headers, callback) {
var xhr = new XMLHttpRequest(),
twoHundred = /^20\d$/;
xhr.onreadystatechange = function() {
if (4 === xhr.readyState && 0 !== xhr.status) {
if (twoHundred.test(xhr.status)) callback(null, xhr);
else return callback(xhr, null);
}
};
xhr.onerror = function(e) { return callback(e, null); };
xhr.open(method, url, true);
for (var h in headers) xhr.setRequestHeader(h, headers[h]);
xhr.send(data);
return xhr;
};
ohauth.xhr = function(method, url, auth, data, options, callback) {
var headers = (options && options.header) || {
'Content-Type': 'application/x-www-form-urlencoded'
};
headers.Authorization = 'OAuth ' + ohauth.authHeader(auth);
return ohauth.rawxhr(method, url, data, headers, callback);
};
ohauth.nonce = function() {
for (var o = ''; o.length < 6;) {
o += '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'[Math.floor(Math.random() * 61)];
}
return o;
};
ohauth.authHeader = function(obj) {
return Object.keys(obj).sort().map(function(key) {
return encodeURIComponent(key) + '="' + encodeURIComponent(obj[key]) + '"';
}).join(', ');
};
ohauth.timestamp = function() { return ~~((+new Date()) / 1000); };
ohauth.percentEncode = function(s) {
return encodeURIComponent(s)
.replace(/\!/g, '%21').replace(/\'/g, '%27')
.replace(/\*/g, '%2A').replace(/\(/g, '%28').replace(/\)/g, '%29');
};
ohauth.baseString = function(method, url, params) {
if (params.oauth_signature) delete params.oauth_signature;
return [
method,
ohauth.percentEncode(url),
ohauth.percentEncode(ohauth.qsString(params))].join('&');
};
ohauth.signature = function(oauth_secret, token_secret, baseString) {
return sha1.b64_hmac(
ohauth.percentEncode(oauth_secret) + '&' +
ohauth.percentEncode(token_secret),
baseString);
};
/**
* Takes an options object for configuration (consumer_key,
* consumer_secret, version, signature_method, token, token_secret)
* and returns a function that generates the Authorization header
* for given data.
*
* The returned function takes these parameters:
* - method: GET/POST/...
* - uri: full URI with protocol, port, path and query string
* - extra_params: any extra parameters (that are passed in the POST data),
* can be an object or a from-urlencoded string.
*
* Returned function returns full OAuth header with "OAuth" string in it.
*/
ohauth.headerGenerator = function(options) {
options = options || {};
var consumer_key = options.consumer_key || '',
consumer_secret = options.consumer_secret || '',
signature_method = options.signature_method || 'HMAC-SHA1',
version = options.version || '1.0',
token = options.token || '',
token_secret = options.token_secret || '';
return function(method, uri, extra_params) {
method = method.toUpperCase();
if (typeof extra_params === 'string' && extra_params.length > 0) {
extra_params = ohauth.stringQs(extra_params);
}
var uri_parts = uri.split('?', 2),
base_uri = uri_parts[0];
var query_params = uri_parts.length === 2 ?
ohauth.stringQs(uri_parts[1]) : {};
var oauth_params = {
oauth_consumer_key: consumer_key,
oauth_signature_method: signature_method,
oauth_version: version,
oauth_timestamp: ohauth.timestamp(),
oauth_nonce: ohauth.nonce()
};
if (token) oauth_params.oauth_token = token;
var all_params = xtend({}, oauth_params, query_params, extra_params),
base_str = ohauth.baseString(method, base_uri, all_params);
oauth_params.oauth_signature = ohauth.signature(consumer_secret, token_secret, base_str);
return 'OAuth ' + ohauth.authHeader(oauth_params);
};
};
module.exports = ohauth;