1919var Auth2Client = require ( './oauth2client.js' ) ;
2020var gToken = require ( 'gtoken' ) ;
2121var JWTAccess = require ( './jwtaccess.js' ) ;
22+ var noop = require ( 'lodash.noop' ) ;
2223var util = require ( 'util' ) ;
2324
25+
2426/**
2527 * JWT service account credentials.
2628 *
@@ -53,13 +55,6 @@ function JWT(email, keyFile, key, scopes, subject) {
5355 */
5456util . inherits ( JWT , Auth2Client ) ;
5557
56- // Executes the given callback if it is not null.
57- function callback ( c , err , res ) {
58- if ( c ) {
59- c ( err , res ) ;
60- }
61- }
62-
6358/**
6459 * Creates a copy of the credential with the specified scopes.
6560 * @param {(string|array)= } scopes List of requested scopes or a single scope.
@@ -73,16 +68,16 @@ JWT.prototype.createScoped = function(scopes) {
7368 * Obtains the metadata to be sent with the request.
7469 *
7570 * @param {string } opt_uri the URI being authorized.
76- * @param {function } cb_with_metadata
71+ * @param {function } metadataFn
7772 */
78- JWT . prototype . getRequestMetadata = function ( opt_uri , cb_with_metadata ) {
73+ JWT . prototype . getRequestMetadata = function ( opt_uri , metadataFn ) {
7974 if ( this . createScopedRequired ( ) && opt_uri ) {
8075 // no scopes have been set, but a uri has been provided. Use JWTAccess credentials.
8176 var alt = new JWTAccess ( this . email , this . key ) ;
82- alt . getRequestMetadata ( opt_uri , cb_with_metadata ) ;
77+ alt . getRequestMetadata ( opt_uri , metadataFn ) ;
8378 } else {
8479 JWT . super_ . prototype . getRequestMetadata . call (
85- this , opt_uri , cb_with_metadata ) ;
80+ this , opt_uri , metadataFn ) ;
8681 }
8782} ;
8883
@@ -112,6 +107,7 @@ JWT.prototype.createScopedRequired = function() {
112107 */
113108JWT . prototype . authorize = function ( opt_callback ) {
114109 var that = this ;
110+ var done = opt_callback || noop ;
115111
116112 that . refreshToken_ ( null , function ( err , result ) {
117113 if ( ! err ) {
@@ -120,7 +116,7 @@ JWT.prototype.authorize = function(opt_callback) {
120116 that . key = that . gtoken . key ;
121117 that . email = that . gtoken . iss ;
122118 }
123- callback ( opt_callback , err , result ) ;
119+ done ( err , result ) ;
124120 } ) ;
125121} ;
126122
@@ -132,12 +128,14 @@ JWT.prototype.authorize = function(opt_callback) {
132128 * @private
133129 */
134130JWT . prototype . refreshToken_ = function ( ignored_ , opt_callback ) {
131+ var done = opt_callback || noop ;
132+
135133 this . _createGToken ( function ( err , gToken ) {
136134 if ( err ) {
137- callback ( opt_callback , err ) ;
135+ done ( err ) ;
138136 } else {
139137 gToken . getToken ( function ( err , token ) {
140- callback ( opt_callback , err , {
138+ done ( err , {
141139 access_token : token ,
142140 token_type : 'Bearer' ,
143141 expiry_date : gToken . expires_at
@@ -155,25 +153,26 @@ JWT.prototype.refreshToken_ = function(ignored_, opt_callback) {
155153 */
156154JWT . prototype . fromJSON = function ( json , opt_callback ) {
157155 var that = this ;
156+ var done = opt_callback || noop ;
158157 if ( ! json ) {
159- callback ( opt_callback , new Error (
158+ done ( new Error (
160159 'Must pass in a JSON object containing the service account auth settings.' ) ) ;
161160 return ;
162161 }
163162 if ( ! json . client_email ) {
164- callback ( opt_callback , new Error (
163+ done ( new Error (
165164 'The incoming JSON object does not contain a client_email field' ) ) ;
166165 return ;
167166 }
168167 if ( ! json . private_key ) {
169- callback ( opt_callback , new Error (
168+ done ( new Error (
170169 'The incoming JSON object does not contain a private_key field' ) ) ;
171170 return ;
172171 }
173172 // Extract the relevant information from the json key file.
174173 that . email = json . client_email ;
175174 that . key = json . private_key ;
176- callback ( opt_callback ) ;
175+ done ( ) ;
177176} ;
178177
179178/**
@@ -183,10 +182,11 @@ JWT.prototype.fromJSON = function(json, opt_callback) {
183182 */
184183JWT . prototype . fromStream = function ( stream , opt_callback ) {
185184 var that = this ;
185+ var done = opt_callback || noop ;
186+
186187 if ( ! stream ) {
187188 process . nextTick ( function ( ) {
188- callback (
189- opt_callback ,
189+ done (
190190 new Error ( 'Must pass in a stream containing the service account auth settings.' ) ) ;
191191 } ) ;
192192 return ;
@@ -201,7 +201,7 @@ JWT.prototype.fromStream = function(stream, opt_callback) {
201201 var data = JSON . parse ( s ) ;
202202 that . fromJSON ( data , opt_callback ) ;
203203 } catch ( err ) {
204- callback ( opt_callback , err ) ;
204+ done ( err ) ;
205205 }
206206 } ) ;
207207} ;
0 commit comments