2222 */
2323
2424var extend = require ( 'extend' ) ;
25- var GoogleAuth = require ( 'google-auth-library ' ) ;
25+ var googleAuth = require ( 'google-auto-auth ' ) ;
2626var is = require ( 'is' ) ;
2727var nodeutil = require ( 'util' ) ;
2828var request = require ( 'request' ) . defaults ( {
@@ -324,109 +324,6 @@ function shouldRetryRequest(err) {
324324
325325util . shouldRetryRequest = shouldRetryRequest ;
326326
327- /**
328- * Create an Auth Client from Google Auth Library, used to get an access token
329- * for authenticating API requests.
330- *
331- * @param {object } config - Configuration object.
332- * @param {object= } config.authClient - AuthClient object. If not provided,
333- * it will be created and cached here.
334- * @param {object= } config.credentials - Credentials object.
335- * @param {string= } config.email - Account email address, required for PEM/P12
336- * usage.
337- * @param {string= } config.keyFile - Path to a .json, .pem, or .p12 keyfile.
338- * @param {array } config.scopes - Array of scopes required for the API.
339- * @param {function } callback - The callback function.
340- */
341- function getAuthClient ( config , callback ) {
342- if ( config . authClient ) {
343- setImmediate ( function ( ) {
344- callback ( null , config . authClient ) ;
345- } ) ;
346- return ;
347- }
348- var googleAuth = new GoogleAuth ( ) ;
349-
350- if ( config . keyFile ) {
351- var authClient = new googleAuth . JWT ( ) ;
352- authClient . keyFile = config . keyFile ;
353- authClient . email = config . email ;
354- authClient . scopes = config . scopes ;
355- addScope ( null , authClient ) ;
356- } else if ( config . credentials ) {
357- googleAuth . fromJSON ( config . credentials , addScope ) ;
358- } else {
359- googleAuth . getApplicationDefault ( addScope ) ;
360- }
361-
362- function addScope ( err , authClient ) {
363- if ( err ) {
364- callback ( err ) ;
365- return ;
366- }
367-
368- if ( authClient . createScopedRequired && authClient . createScopedRequired ( ) ) {
369- authClient = authClient . createScoped ( config . scopes ) ;
370- }
371-
372- config . authClient = authClient ;
373- callback ( null , authClient ) ;
374- }
375- }
376-
377- util . getAuthClient = getAuthClient ;
378-
379- /**
380- * Authenticate a request by extending its headers object with an access token.
381- *
382- * @param {object } config - Configuration object.
383- * @param {object= } config.authClient - AuthClient object. If not provided,
384- * it will be created and cached here.
385- * @param {object= } config.credentials - Credentials object.
386- * @param {string= } config.email - Account email address, required for PEM/P12
387- * usage.
388- * @param {string= } config.keyFile - Path to a .json, .pem, or .p12 keyfile.
389- * @param {array } config.scopes - Array of scopes required for the API.
390- * @param {object } reqOpts - HTTP request options. Its `headers` object is
391- * created or extended with a valid access token.
392- * @param {function } callback - The callback function.
393- */
394- function authorizeRequest ( config , reqOpts , callback ) {
395- util . getAuthClient ( config , function ( err , authClient ) {
396- if ( err ) {
397- // google-auth-library returns a "Could not load..." error if it can't get
398- // an access token. However, it's possible an API request doesn't need to
399- // be authenticated, e.g. when downloading a file from a public bucket. We
400- // consider this error a warning, and allow the request to go through
401- // without authorization, relying on the upstream API to return an error
402- // the user would find more helpful, should one occur.
403- if ( err . message . indexOf ( 'Could not load' ) === 0 ) {
404- callback ( null , reqOpts ) ;
405- } else {
406- callback ( err ) ;
407- }
408- return ;
409- }
410-
411- authClient . getAccessToken ( function ( err , token ) {
412- if ( err ) {
413- callback ( err ) ;
414- return ;
415- }
416-
417- var authorizedReqOpts = extend ( true , { } , reqOpts , {
418- headers : {
419- Authorization : 'Bearer ' + token
420- }
421- } ) ;
422-
423- callback ( null , authorizedReqOpts ) ;
424- } ) ;
425- } ) ;
426- }
427-
428- util . authorizeRequest = authorizeRequest ;
429-
430327/**
431328 * Get a function for making authorized requests.
432329 *
@@ -450,6 +347,8 @@ util.authorizeRequest = authorizeRequest;
450347function makeAuthorizedRequestFactory ( config ) {
451348 config = config || { } ;
452349
350+ var authClient = googleAuth ( config ) ;
351+
453352 /**
454353 * The returned function that will make an authorized request.
455354 *
@@ -470,7 +369,13 @@ function makeAuthorizedRequestFactory(config) {
470369 }
471370
472371 function onAuthorized ( err , authorizedReqOpts ) {
473- if ( err ) {
372+ // google-auth-library returns a "Could not load..." error if it can't get
373+ // an access token. However, it's possible an API request doesn't need to
374+ // be authenticated, e.g. when downloading a file from a public bucket. We
375+ // consider this error a warning, and allow the request to go through
376+ // without authorization, relying on the upstream API to return an error
377+ // the user would find more helpful, should one occur.
378+ if ( err && err . message . indexOf ( 'Could not load' ) === - 1 ) {
474379 if ( stream ) {
475380 stream . destroy ( err ) ;
476381 } else {
@@ -490,38 +395,20 @@ function makeAuthorizedRequestFactory(config) {
490395 }
491396
492397 if ( reqConfig . customEndpoint ) {
493- // Using a custom API override. Do not use `google-auth-library ` for
398+ // Using a custom API override. Do not use `google-auto-auth ` for
494399 // authentication. (ex: connecting to a local Datastore server)
495400 onAuthorized ( null , reqOpts ) ;
496401 } else {
497- util . authorizeRequest ( reqConfig , reqOpts , onAuthorized ) ;
402+ authClient . authorizeRequest ( reqOpts , onAuthorized ) ;
498403 }
499404
500405 if ( stream ) {
501406 return stream ;
502407 }
503408 }
504409
505- makeAuthorizedRequest . getCredentials = function ( callback ) {
506- util . getAuthClient ( config , function ( err , authClient ) {
507- if ( err ) {
508- callback ( err ) ;
509- return ;
510- }
511-
512- authClient . authorize ( function ( err ) {
513- if ( err ) {
514- callback ( err ) ;
515- return ;
516- }
517-
518- callback ( null , {
519- client_email : authClient . email ,
520- private_key : authClient . key
521- } ) ;
522- } ) ;
523- } ) ;
524- } ;
410+ makeAuthorizedRequest . getCredentials =
411+ authClient . getCredentials . bind ( authClient ) ;
525412
526413 return makeAuthorizedRequest ;
527414}
0 commit comments