forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth1_binding.js
197 lines (161 loc) · 5.91 KB
/
oauth1_binding.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var crypto = Npm.require("crypto");
var querystring = Npm.require("querystring");
var urlModule = Npm.require("url");
// An OAuth1 wrapper around http calls which helps get tokens and
// takes care of HTTP headers
//
// @param config {Object}
// - consumerKey (String): oauth consumer key
// - secret (String): oauth consumer secret
// @param urls {Object}
// - requestToken (String): url
// - authorize (String): url
// - accessToken (String): url
// - authenticate (String): url
OAuth1Binding = function(config, urls) {
this._config = config;
this._urls = urls;
};
OAuth1Binding.prototype.prepareRequestToken = function(callbackUrl) {
var self = this;
var headers = self._buildHeader({
oauth_callback: callbackUrl
});
var response = self._call('POST', self._urls.requestToken, headers);
var tokens = querystring.parse(response.content);
if (! tokens.oauth_callback_confirmed)
throw _.extend(new Error("oauth_callback_confirmed false when requesting oauth1 token"),
{response: response});
self.requestToken = tokens.oauth_token;
self.requestTokenSecret = tokens.oauth_token_secret;
};
OAuth1Binding.prototype.prepareAccessToken = function(query, requestTokenSecret) {
var self = this;
// support implementations that use request token secrets. This is
// read by self._call.
//
// XXX make it a param to call, not something stashed on self? It's
// kinda confusing right now, everything except this is passed as
// arguments, but this is stored.
if (requestTokenSecret)
self.accessTokenSecret = requestTokenSecret;
var headers = self._buildHeader({
oauth_token: query.oauth_token,
oauth_verifier: query.oauth_verifier
});
var response = self._call('POST', self._urls.accessToken, headers);
var tokens = querystring.parse(response.content);
if (! tokens.oauth_token || ! tokens.oauth_token_secret) {
var error = new Error("missing oauth token or secret");
// We provide response only if no token is available, we do not want to leak any tokens
if (! tokens.oauth_token && ! tokens.oauth_token_secret) {
_.extend(error, {response: response});
}
throw error;
}
self.accessToken = tokens.oauth_token;
self.accessTokenSecret = tokens.oauth_token_secret;
};
OAuth1Binding.prototype.call = function(method, url, params, callback) {
var self = this;
var headers = self._buildHeader({
oauth_token: self.accessToken
});
if(! params) {
params = {};
}
return self._call(method, url, headers, params, callback);
};
OAuth1Binding.prototype.get = function(url, params, callback) {
return this.call('GET', url, params, callback);
};
OAuth1Binding.prototype.post = function(url, params, callback) {
return this.call('POST', url, params, callback);
};
OAuth1Binding.prototype._buildHeader = function(headers) {
var self = this;
return _.extend({
oauth_consumer_key: self._config.consumerKey,
oauth_nonce: Random.secret().replace(/\W/g, ''),
oauth_signature_method: 'HMAC-SHA1',
oauth_timestamp: (new Date().valueOf()/1000).toFixed().toString(),
oauth_version: '1.0'
}, headers);
};
OAuth1Binding.prototype._getSignature = function(method, url, rawHeaders, accessTokenSecret, params) {
var self = this;
var headers = self._encodeHeader(_.extend({}, rawHeaders, params));
var parameters = _.map(headers, function(val, key) {
return key + '=' + val;
}).sort().join('&');
var signatureBase = [
method,
self._encodeString(url),
self._encodeString(parameters)
].join('&');
var secret = OAuth.openSecret(self._config.secret);
var signingKey = self._encodeString(secret) + '&';
if (accessTokenSecret)
signingKey += self._encodeString(accessTokenSecret);
return crypto.createHmac('SHA1', signingKey).update(signatureBase).digest('base64');
};
OAuth1Binding.prototype._call = function(method, url, headers, params, callback) {
var self = this;
// all URLs to be functions to support parameters/customization
if(typeof url === "function") {
url = url(self);
}
headers = headers || {};
params = params || {};
// Extract all query string parameters from the provided URL
var parsedUrl = urlModule.parse(url, true);
// Merge them in a way that params given to the method call have precedence
params = _.extend({}, parsedUrl.query, params);
// Reconstruct the URL back without any query string parameters
// (they are now in params)
parsedUrl.query = {};
parsedUrl.search = '';
url = urlModule.format(parsedUrl);
// Get the signature
headers.oauth_signature =
self._getSignature(method, url, headers, self.accessTokenSecret, params);
// Make a authorization string according to oauth1 spec
var authString = self._getAuthHeaderString(headers);
// Make signed request
try {
var response = HTTP.call(method, url, {
params: params,
headers: {
Authorization: authString
}
}, callback && function (error, response) {
if (! error) {
response.nonce = headers.oauth_nonce;
}
callback(error, response);
});
// We store nonce so that JWTs can be validated
if (response)
response.nonce = headers.oauth_nonce;
return response;
} catch (err) {
throw _.extend(new Error("Failed to send OAuth1 request to " + url + ". " + err.message),
{response: err.response});
}
};
OAuth1Binding.prototype._encodeHeader = function(header) {
var self = this;
return _.reduce(header, function(memo, val, key) {
memo[self._encodeString(key)] = self._encodeString(val);
return memo;
}, {});
};
OAuth1Binding.prototype._encodeString = function(str) {
return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
};
OAuth1Binding.prototype._getAuthHeaderString = function(headers) {
var self = this;
return 'OAuth ' + _.map(headers, function(val, key) {
return self._encodeString(key) + '="' + self._encodeString(val) + '"';
}).sort().join(', ');
};