@@ -2,12 +2,9 @@ var https = require('https'),
22 crypto = require ( 'crypto' ) ;
33var Parse = require ( 'parse/node' ) . Parse ;
44
5- var OAuth = function ( options ) {
5+ var OAuth = function ( options ) {
66 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' ) ;
118 }
129 this . consumer_key = options . consumer_key ;
1310 this . consumer_secret = options . consumer_secret ;
@@ -17,22 +14,22 @@ var OAuth = function(options) {
1714 this . oauth_params = options . oauth_params || { } ;
1815} ;
1916
20- OAuth . prototype . send = function ( method , path , params , body ) {
17+ OAuth . prototype . send = function ( method , path , params , body ) {
2118 var request = this . buildRequest ( method , path , params , body ) ;
2219 // 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 ) {
2421 var httpRequest = https
25- . request ( request , function ( res ) {
22+ . request ( request , function ( res ) {
2623 var data = '' ;
27- res . on ( 'data' , function ( chunk ) {
24+ res . on ( 'data' , function ( chunk ) {
2825 data += chunk ;
2926 } ) ;
30- res . on ( 'end' , function ( ) {
27+ res . on ( 'end' , function ( ) {
3128 data = JSON . parse ( data ) ;
3229 resolve ( data ) ;
3330 } ) ;
3431 } )
35- . on ( 'error' , function ( ) {
32+ . on ( 'error' , function ( ) {
3633 reject ( 'Failed to make an OAuth request' ) ;
3734 } ) ;
3835 if ( request . body ) {
@@ -42,7 +39,7 @@ OAuth.prototype.send = function(method, path, params, body) {
4239 } ) ;
4340} ;
4441
45- OAuth . prototype . buildRequest = function ( method , path , params , body ) {
42+ OAuth . prototype . buildRequest = function ( method , path , params , body ) {
4643 if ( path . indexOf ( '/' ) != 0 ) {
4744 path = '/' + path ;
4845 }
@@ -62,31 +59,26 @@ OAuth.prototype.buildRequest = function(method, path, params, body) {
6259 oauth_params [ 'oauth_token' ] = this . auth_token ;
6360 }
6461
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 ) ;
7163
7264 if ( body && Object . keys ( body ) . length > 0 ) {
7365 request . body = OAuth . buildParameterString ( body ) ;
7466 }
7567 return request ;
7668} ;
7769
78- OAuth . prototype . get = function ( path , params ) {
70+ OAuth . prototype . get = function ( path , params ) {
7971 return this . send ( 'GET' , path , params ) ;
8072} ;
8173
82- OAuth . prototype . post = function ( path , params , body ) {
74+ OAuth . prototype . post = function ( path , params , body ) {
8375 return this . send ( 'POST' , path , params , body ) ;
8476} ;
8577
8678/*
8779 Proper string %escape encoding
8880*/
89- OAuth . encode = function ( str ) {
81+ OAuth . encode = function ( str ) {
9082 // discuss at: http://phpjs.org/functions/rawurlencode/
9183 // original by: Brett Zamir (http://brett-zamir.me)
9284 // input by: travc
@@ -126,25 +118,23 @@ OAuth.version = '1.0';
126118/*
127119 Generate a nonce
128120*/
129- OAuth . nonce = function ( ) {
121+ OAuth . nonce = function ( ) {
130122 var text = '' ;
131- var possible =
132- 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
123+ var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
133124
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 ) ) ;
136126
137127 return text ;
138128} ;
139129
140- OAuth . buildParameterString = function ( obj ) {
130+ OAuth . buildParameterString = function ( obj ) {
141131 // Sort keys and encode values
142132 if ( obj ) {
143133 var keys = Object . keys ( obj ) . sort ( ) ;
144134
145135 // Map key=value, join them by &
146136 return keys
147- . map ( function ( key ) {
137+ . map ( function ( key ) {
148138 return key + '=' + OAuth . encode ( obj [ key ] ) ;
149139 } )
150140 . join ( '&' ) ;
@@ -157,33 +147,19 @@ OAuth.buildParameterString = function(obj) {
157147 Build the signature string from the object
158148*/
159149
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 ( '&' ) ;
166152} ;
167153
168154/*
169155 Retuns encoded HMAC-SHA1 from key and text
170156*/
171- OAuth . signature = function ( text , key ) {
157+ OAuth . signature = function ( text , key ) {
172158 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' ) ) ;
179160} ;
180161
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 ) {
187163 oauth_parameters = oauth_parameters || { } ;
188164
189165 // Set default values
@@ -224,16 +200,9 @@ OAuth.signRequest = function(
224200 // Build the signature string
225201 var url = 'https://' + request . host + '' + request . path ;
226202
227- var signatureString = OAuth . buildSignatureString (
228- request . method ,
229- url ,
230- parameterString
231- ) ;
203+ var signatureString = OAuth . buildSignatureString ( request . method , url , parameterString ) ;
232204 // 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 ( '&' ) ;
237206
238207 var signature = OAuth . signature ( signatureString , signatureKey ) ;
239208
@@ -246,7 +215,7 @@ OAuth.signRequest = function(
246215 // Set the authorization header
247216 var authHeader = Object . keys ( oauth_parameters )
248217 . sort ( )
249- . map ( function ( key ) {
218+ . map ( function ( key ) {
250219 var value = oauth_parameters [ key ] ;
251220 return key + '="' + value + '"' ;
252221 } )
0 commit comments