@@ -2,7 +2,6 @@ var extend = require('xtend')
2
2
var popsicle = require ( 'popsicle' )
3
3
var parseQuery = require ( 'querystring' ) . parse
4
4
var parseUrl = require ( 'url' ) . parse
5
- var omit = require ( 'object.omit' )
6
5
7
6
var btoa = typeof Buffer === 'function' ? btoaBuffer : window . btoa
8
7
@@ -200,6 +199,18 @@ function string (str) {
200
199
return str == null ? '' : String ( str )
201
200
}
202
201
202
+ /**
203
+ * Merge request options from an options object.
204
+ */
205
+ function requestOptions ( requestOptions , options ) {
206
+ return extend ( requestOptions , {
207
+ body : extend ( options . body , requestOptions . body ) ,
208
+ query : extend ( options . query , requestOptions . query ) ,
209
+ headers : extend ( options . headers , requestOptions . headers ) ,
210
+ options : extend ( options . options , requestOptions . options )
211
+ } )
212
+ }
213
+
203
214
/**
204
215
* Construct an object that can handle the multiple OAuth 2.0 flows.
205
216
*
@@ -246,11 +257,11 @@ ClientOAuth2.prototype.createToken = function (access, refresh, type, data) {
246
257
* Using the built-in request method, we'll automatically attempt to parse
247
258
* the response.
248
259
*
249
- * @param {Object } options
260
+ * @param {Object } requestObject
250
261
* @return {Promise }
251
262
*/
252
- ClientOAuth2 . prototype . _request = function ( options ) {
253
- return this . request ( this . _requestOptions ( options ) )
263
+ ClientOAuth2 . prototype . _request = function ( requestObject ) {
264
+ return this . request ( requestObject )
254
265
. then ( function ( res ) {
255
266
if ( res . status < 200 || res . status >= 399 ) {
256
267
var err = new Error ( 'HTTP status ' + res . status )
@@ -263,15 +274,6 @@ ClientOAuth2.prototype._request = function (options) {
263
274
} )
264
275
}
265
276
266
- ClientOAuth2 . prototype . _requestOptions = function ( options ) {
267
- return extend ( options , {
268
- body : extend ( this . options . body , options . body ) ,
269
- query : extend ( this . options . query , options . query ) ,
270
- headers : extend ( this . options . headers , options . headers ) ,
271
- options : extend ( this . options . options , options . options )
272
- } )
273
- }
274
-
275
277
/**
276
278
* Set `popsicle` as the default request method.
277
279
*/
@@ -285,12 +287,7 @@ ClientOAuth2.prototype.request = popsicle.request
285
287
*/
286
288
function ClientOAuth2Token ( client , data ) {
287
289
this . client = client
288
-
289
- this . data = omit ( data , [
290
- 'access_token' , 'refresh_token' , 'token_type' , 'expires_in' , 'scope' ,
291
- 'state' , 'error' , 'error_description' , 'error_uri'
292
- ] )
293
-
290
+ this . data = data
294
291
this . tokenType = data . token_type && data . token_type . toLowerCase ( )
295
292
this . accessToken = data . access_token
296
293
this . refreshToken = data . refresh_token
@@ -318,60 +315,61 @@ ClientOAuth2Token.prototype.expiresIn = function (duration) {
318
315
/**
319
316
* Sign a standardised request object with user authentication information.
320
317
*
321
- * @param {Object } opts
318
+ * @param {Object } requestOptions
322
319
* @return {Object }
323
320
*/
324
- ClientOAuth2Token . prototype . sign = function ( opts ) {
321
+ ClientOAuth2Token . prototype . sign = function ( requestObject ) {
325
322
if ( ! this . accessToken ) {
326
323
throw new Error ( 'Unable to sign without access token' )
327
324
}
328
325
329
- opts . headers = opts . headers || { }
326
+ requestObject . headers = requestObject . headers || { }
330
327
331
328
if ( this . tokenType === 'bearer' ) {
332
- opts . headers . Authorization = 'Bearer ' + this . accessToken
329
+ requestObject . headers . Authorization = 'Bearer ' + this . accessToken
333
330
} else {
334
- var parts = opts . url . split ( '#' )
331
+ var parts = requestObject . url . split ( '#' )
335
332
var token = 'access_token=' + this . accessToken
336
333
var url = parts [ 0 ] . replace ( / [ ? & ] a c c e s s _ t o k e n = [ ^ & # ] / , '' )
337
334
var fragment = parts [ 1 ] ? '#' + parts [ 1 ] : ''
338
335
339
336
// Prepend the correct query string parameter to the url.
340
- opts . url = url + ( url . indexOf ( '?' ) > - 1 ? '&' : '?' ) + token + fragment
337
+ requestObject . url = url + ( url . indexOf ( '?' ) > - 1 ? '&' : '?' ) + token + fragment
341
338
342
339
// Attempt to avoid storing the url in proxies, since the access token
343
340
// is exposed in the query parameters.
344
- opts . headers . Pragma = 'no-store'
345
- opts . headers [ 'Cache-Control' ] = 'no-store'
341
+ requestObject . headers . Pragma = 'no-store'
342
+ requestObject . headers [ 'Cache-Control' ] = 'no-store'
346
343
}
347
344
348
- return opts
345
+ return requestObject
349
346
}
350
347
351
348
/**
352
349
* Make a HTTP request as the user.
353
350
*
354
- * @param {Object } opts
351
+ * @param {Object } options
355
352
* @return {Promise }
356
353
*/
357
- ClientOAuth2Token . prototype . request = function ( opts ) {
358
- return this . client . request ( this . client . _requestOptions ( this . sign ( opts ) ) )
354
+ ClientOAuth2Token . prototype . request = function ( options ) {
355
+ return this . client . request ( requestOptions ( this . sign ( options ) , this . client . options ) )
359
356
}
360
357
361
358
/**
362
359
* Refresh a user access token with the supplied token.
363
360
*
364
361
* @return {Promise }
365
362
*/
366
- ClientOAuth2Token . prototype . refresh = function ( ) {
363
+ ClientOAuth2Token . prototype . refresh = function ( options ) {
367
364
var self = this
368
- var options = this . client . options
365
+
366
+ options = extend ( this . client . options , options )
369
367
370
368
if ( ! this . refreshToken ) {
371
369
return Promise . reject ( new Error ( 'No refresh token set' ) )
372
370
}
373
371
374
- return this . client . _request ( {
372
+ return this . client . _request ( requestOptions ( {
375
373
url : options . accessTokenUri ,
376
374
method : 'POST' ,
377
375
headers : extend ( DEFAULT_HEADERS , {
@@ -381,7 +379,7 @@ ClientOAuth2Token.prototype.refresh = function () {
381
379
refresh_token : this . refreshToken ,
382
380
grant_type : 'refresh_token'
383
381
}
384
- } )
382
+ } , options ) )
385
383
. then ( handleAuthResponse )
386
384
. then ( function ( data ) {
387
385
self . accessToken = data . access_token
@@ -429,7 +427,7 @@ OwnerFlow.prototype.getToken = function (username, password, options) {
429
427
430
428
options = extend ( this . client . options , options )
431
429
432
- return this . client . _request ( {
430
+ return this . client . _request ( requestOptions ( {
433
431
url : options . accessTokenUri ,
434
432
method : 'POST' ,
435
433
headers : extend ( DEFAULT_HEADERS , {
@@ -441,7 +439,7 @@ OwnerFlow.prototype.getToken = function (username, password, options) {
441
439
password : password ,
442
440
grant_type : 'password'
443
441
}
444
- } )
442
+ } , options ) )
445
443
. then ( handleAuthResponse )
446
444
. then ( function ( data ) {
447
445
return new ClientOAuth2Token ( self . client , data )
@@ -476,10 +474,11 @@ TokenFlow.prototype.getUri = function (options) {
476
474
*
477
475
* @param {String } uri
478
476
* @param {String } [state]
477
+ * @param {Object } [options]
479
478
* @return {Promise }
480
479
*/
481
- TokenFlow . prototype . getToken = function ( uri , state ) {
482
- var options = this . client . options
480
+ TokenFlow . prototype . getToken = function ( uri , state , options ) {
481
+ options = extend ( this . client . options , options )
483
482
484
483
// Make sure the uri matches our expected redirect uri.
485
484
if ( uri . substr ( 0 , options . redirectUri . length ) !== options . redirectUri ) {
@@ -546,7 +545,7 @@ CredentialsFlow.prototype.getToken = function (options) {
546
545
'accessTokenUri'
547
546
] )
548
547
549
- return this . client . _request ( {
548
+ return this . client . _request ( requestOptions ( {
550
549
url : options . accessTokenUri ,
551
550
method : 'POST' ,
552
551
headers : extend ( DEFAULT_HEADERS , {
@@ -556,7 +555,7 @@ CredentialsFlow.prototype.getToken = function (options) {
556
555
scope : sanitizeScope ( options . scopes ) ,
557
556
grant_type : 'client_credentials'
558
557
}
559
- } )
558
+ } , options ) )
560
559
. then ( handleAuthResponse )
561
560
. then ( function ( data ) {
562
561
return new ClientOAuth2Token ( self . client , data )
@@ -591,11 +590,13 @@ CodeFlow.prototype.getUri = function (options) {
591
590
*
592
591
* @param {String } uri
593
592
* @param {String } [state]
593
+ * @param {Object } [options]
594
594
* @return {Promise }
595
595
*/
596
- CodeFlow . prototype . getToken = function ( uri , state ) {
596
+ CodeFlow . prototype . getToken = function ( uri , state , options ) {
597
597
var self = this
598
- var options = this . client . options
598
+
599
+ options = extend ( this . client . options , options )
599
600
600
601
expects ( options , [
601
602
'clientId' ,
@@ -631,7 +632,7 @@ CodeFlow.prototype.getToken = function (uri, state) {
631
632
return Promise . reject ( new Error ( 'Missing code, unable to request token' ) )
632
633
}
633
634
634
- return this . client . _request ( {
635
+ return this . client . _request ( requestOptions ( {
635
636
url : options . accessTokenUri ,
636
637
method : 'POST' ,
637
638
headers : extend ( DEFAULT_HEADERS ) ,
@@ -642,7 +643,7 @@ CodeFlow.prototype.getToken = function (uri, state) {
642
643
client_id : options . clientId ,
643
644
client_secret : options . clientSecret
644
645
}
645
- } )
646
+ } , options ) )
646
647
. then ( handleAuthResponse )
647
648
. then ( function ( data ) {
648
649
return new ClientOAuth2Token ( self . client , data )
@@ -684,7 +685,7 @@ JwtBearerFlow.prototype.getToken = function (token, options) {
684
685
headers [ 'Authorization' ] = auth ( options . clientId , options . clientSecret )
685
686
}
686
687
687
- return this . client . _request ( {
688
+ return this . client . _request ( requestOptions ( {
688
689
url : options . accessTokenUri ,
689
690
method : 'POST' ,
690
691
headers : headers ,
@@ -693,7 +694,7 @@ JwtBearerFlow.prototype.getToken = function (token, options) {
693
694
grant_type : 'urn:ietf:params:oauth:grant-type:jwt-bearer' ,
694
695
assertion : token
695
696
}
696
- } )
697
+ } , options ) )
697
698
. then ( handleAuthResponse )
698
699
. then ( function ( data ) {
699
700
return new ClientOAuth2Token ( self . client , data )
0 commit comments