@@ -2,12 +2,9 @@ var https = require('https'),
2
2
crypto = require ( 'crypto' ) ;
3
3
var Parse = require ( 'parse/node' ) . Parse ;
4
4
5
- var OAuth = function ( options ) {
5
+ var OAuth = function ( options ) {
6
6
if ( ! options ) {
7
- throw new Parse . Error (
8
- Parse . Error . INTERNAL_SERVER_ERROR ,
9
- 'No options passed to OAuth'
10
- ) ;
7
+ throw new Parse . Error ( Parse . Error . INTERNAL_SERVER_ERROR , 'No options passed to OAuth' ) ;
11
8
}
12
9
this . consumer_key = options . consumer_key ;
13
10
this . consumer_secret = options . consumer_secret ;
@@ -17,22 +14,22 @@ var OAuth = function(options) {
17
14
this . oauth_params = options . oauth_params || { } ;
18
15
} ;
19
16
20
- OAuth . prototype . send = function ( method , path , params , body ) {
17
+ OAuth . prototype . send = function ( method , path , params , body ) {
21
18
var request = this . buildRequest ( method , path , params , body ) ;
22
19
// Encode the body properly, the current Parse Implementation don't do it properly
23
- return new Promise ( function ( resolve , reject ) {
20
+ return new Promise ( function ( resolve , reject ) {
24
21
var httpRequest = https
25
- . request ( request , function ( res ) {
22
+ . request ( request , function ( res ) {
26
23
var data = '' ;
27
- res . on ( 'data' , function ( chunk ) {
24
+ res . on ( 'data' , function ( chunk ) {
28
25
data += chunk ;
29
26
} ) ;
30
- res . on ( 'end' , function ( ) {
27
+ res . on ( 'end' , function ( ) {
31
28
data = JSON . parse ( data ) ;
32
29
resolve ( data ) ;
33
30
} ) ;
34
31
} )
35
- . on ( 'error' , function ( ) {
32
+ . on ( 'error' , function ( ) {
36
33
reject ( 'Failed to make an OAuth request' ) ;
37
34
} ) ;
38
35
if ( request . body ) {
@@ -42,7 +39,7 @@ OAuth.prototype.send = function(method, path, params, body) {
42
39
} ) ;
43
40
} ;
44
41
45
- OAuth . prototype . buildRequest = function ( method , path , params , body ) {
42
+ OAuth . prototype . buildRequest = function ( method , path , params , body ) {
46
43
if ( path . indexOf ( '/' ) != 0 ) {
47
44
path = '/' + path ;
48
45
}
@@ -62,31 +59,26 @@ OAuth.prototype.buildRequest = function(method, path, params, body) {
62
59
oauth_params [ 'oauth_token' ] = this . auth_token ;
63
60
}
64
61
65
- request = OAuth . signRequest (
66
- request ,
67
- oauth_params ,
68
- this . consumer_secret ,
69
- this . auth_token_secret
70
- ) ;
62
+ request = OAuth . signRequest ( request , oauth_params , this . consumer_secret , this . auth_token_secret ) ;
71
63
72
64
if ( body && Object . keys ( body ) . length > 0 ) {
73
65
request . body = OAuth . buildParameterString ( body ) ;
74
66
}
75
67
return request ;
76
68
} ;
77
69
78
- OAuth . prototype . get = function ( path , params ) {
70
+ OAuth . prototype . get = function ( path , params ) {
79
71
return this . send ( 'GET' , path , params ) ;
80
72
} ;
81
73
82
- OAuth . prototype . post = function ( path , params , body ) {
74
+ OAuth . prototype . post = function ( path , params , body ) {
83
75
return this . send ( 'POST' , path , params , body ) ;
84
76
} ;
85
77
86
78
/*
87
79
Proper string %escape encoding
88
80
*/
89
- OAuth . encode = function ( str ) {
81
+ OAuth . encode = function ( str ) {
90
82
// discuss at: http://phpjs.org/functions/rawurlencode/
91
83
// original by: Brett Zamir (http://brett-zamir.me)
92
84
// input by: travc
@@ -126,25 +118,23 @@ OAuth.version = '1.0';
126
118
/*
127
119
Generate a nonce
128
120
*/
129
- OAuth . nonce = function ( ) {
121
+ OAuth . nonce = function ( ) {
130
122
var text = '' ;
131
- var possible =
132
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
123
+ var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
133
124
134
- for ( var i = 0 ; i < 30 ; i ++ )
135
- text += possible . charAt ( Math . floor ( Math . random ( ) * possible . length ) ) ;
125
+ for ( var i = 0 ; i < 30 ; i ++ ) text += possible . charAt ( Math . floor ( Math . random ( ) * possible . length ) ) ;
136
126
137
127
return text ;
138
128
} ;
139
129
140
- OAuth . buildParameterString = function ( obj ) {
130
+ OAuth . buildParameterString = function ( obj ) {
141
131
// Sort keys and encode values
142
132
if ( obj ) {
143
133
var keys = Object . keys ( obj ) . sort ( ) ;
144
134
145
135
// Map key=value, join them by &
146
136
return keys
147
- . map ( function ( key ) {
137
+ . map ( function ( key ) {
148
138
return key + '=' + OAuth . encode ( obj [ key ] ) ;
149
139
} )
150
140
. join ( '&' ) ;
@@ -157,33 +147,19 @@ OAuth.buildParameterString = function(obj) {
157
147
Build the signature string from the object
158
148
*/
159
149
160
- OAuth . buildSignatureString = function ( method , url , parameters ) {
161
- return [
162
- method . toUpperCase ( ) ,
163
- OAuth . encode ( url ) ,
164
- OAuth . encode ( parameters ) ,
165
- ] . join ( '&' ) ;
150
+ OAuth . buildSignatureString = function ( method , url , parameters ) {
151
+ return [ method . toUpperCase ( ) , OAuth . encode ( url ) , OAuth . encode ( parameters ) ] . join ( '&' ) ;
166
152
} ;
167
153
168
154
/*
169
155
Retuns encoded HMAC-SHA1 from key and text
170
156
*/
171
- OAuth . signature = function ( text , key ) {
157
+ OAuth . signature = function ( text , key ) {
172
158
crypto = require ( 'crypto' ) ;
173
- return OAuth . encode (
174
- crypto
175
- . createHmac ( 'sha1' , key )
176
- . update ( text )
177
- . digest ( 'base64' )
178
- ) ;
159
+ return OAuth . encode ( crypto . createHmac ( 'sha1' , key ) . update ( text ) . digest ( 'base64' ) ) ;
179
160
} ;
180
161
181
- OAuth . signRequest = function (
182
- request ,
183
- oauth_parameters ,
184
- consumer_secret ,
185
- auth_token_secret
186
- ) {
162
+ OAuth . signRequest = function ( request , oauth_parameters , consumer_secret , auth_token_secret ) {
187
163
oauth_parameters = oauth_parameters || { } ;
188
164
189
165
// Set default values
@@ -224,16 +200,9 @@ OAuth.signRequest = function(
224
200
// Build the signature string
225
201
var url = 'https://' + request . host + '' + request . path ;
226
202
227
- var signatureString = OAuth . buildSignatureString (
228
- request . method ,
229
- url ,
230
- parameterString
231
- ) ;
203
+ var signatureString = OAuth . buildSignatureString ( request . method , url , parameterString ) ;
232
204
// Hash the signature string
233
- var signatureKey = [
234
- OAuth . encode ( consumer_secret ) ,
235
- OAuth . encode ( auth_token_secret ) ,
236
- ] . join ( '&' ) ;
205
+ var signatureKey = [ OAuth . encode ( consumer_secret ) , OAuth . encode ( auth_token_secret ) ] . join ( '&' ) ;
237
206
238
207
var signature = OAuth . signature ( signatureString , signatureKey ) ;
239
208
@@ -246,7 +215,7 @@ OAuth.signRequest = function(
246
215
// Set the authorization header
247
216
var authHeader = Object . keys ( oauth_parameters )
248
217
. sort ( )
249
- . map ( function ( key ) {
218
+ . map ( function ( key ) {
250
219
var value = oauth_parameters [ key ] ;
251
220
return key + '="' + value + '"' ;
252
221
} )
0 commit comments