@@ -113,6 +113,7 @@ export class ServiceAccountCredential implements Credential {
113
113
public readonly clientEmail : string ;
114
114
115
115
private googleAuth : GoogleAuth ;
116
+ private authClient : AnyAuthClient | undefined ;
116
117
117
118
/**
118
119
* Creates a new ServiceAccountCredential from the given parameters.
@@ -141,16 +142,19 @@ export class ServiceAccountCredential implements Credential {
141
142
if ( this . googleAuth ) {
142
143
return this . googleAuth ;
143
144
}
144
- this . googleAuth = populateGoogleAuth ( this . serviceAccountPathOrObject , this . httpAgent ) ;
145
+ const { auth, client } = populateGoogleAuth ( this . serviceAccountPathOrObject , this . httpAgent ) ;
146
+ this . googleAuth = auth ;
147
+ this . authClient = client ;
145
148
return this . googleAuth ;
146
149
}
147
150
148
151
public async getAccessToken ( ) : Promise < GoogleOAuthAccessToken > {
149
152
const googleAuth = this . getGoogleAuth ( ) ;
150
- const client = googleAuth . fromJSON ( this . serviceAccountPathOrObject as object ) ;
151
- //const client = await googleAuth.getClient();
152
- await client . getAccessToken ( ) ;
153
- const credentials = client . credentials ;
153
+ if ( this . authClient === undefined ) {
154
+ this . authClient = await googleAuth . getClient ( ) ;
155
+ }
156
+ await this . authClient . getAccessToken ( ) ;
157
+ const credentials = this . authClient . credentials ;
154
158
return populateCredential ( credentials ) ;
155
159
}
156
160
}
@@ -219,6 +223,7 @@ class ServiceAccount {
219
223
export class RefreshTokenCredential implements Credential {
220
224
221
225
private googleAuth : GoogleAuth ;
226
+ private authClient : AnyAuthClient | undefined ;
222
227
223
228
/**
224
229
* Creates a new RefreshTokenCredential from the given parameters.
@@ -245,15 +250,19 @@ export class RefreshTokenCredential implements Credential {
245
250
if ( this . googleAuth ) {
246
251
return this . googleAuth ;
247
252
}
248
- this . googleAuth = populateGoogleAuth ( this . refreshTokenPathOrObject , this . httpAgent ) ;
253
+ const { auth, client } = populateGoogleAuth ( this . refreshTokenPathOrObject , this . httpAgent ) ;
254
+ this . googleAuth = auth ;
255
+ this . authClient = client ;
249
256
return this . googleAuth ;
250
257
}
251
258
252
259
public async getAccessToken ( ) : Promise < GoogleOAuthAccessToken > {
253
260
const googleAuth = this . getGoogleAuth ( ) ;
254
- const client = await googleAuth . getClient ( ) ;
255
- await client . getAccessToken ( ) ;
256
- const credentials = client . credentials ;
261
+ if ( this . authClient === undefined ) {
262
+ this . authClient = await googleAuth . getClient ( ) ;
263
+ }
264
+ await this . authClient . getAccessToken ( ) ;
265
+ const credentials = this . authClient . credentials ;
257
266
return populateCredential ( credentials ) ;
258
267
}
259
268
}
@@ -313,6 +322,7 @@ class RefreshToken {
313
322
export class ImpersonatedServiceAccountCredential implements Credential {
314
323
315
324
private googleAuth : GoogleAuth ;
325
+ private authClient : AnyAuthClient | undefined ;
316
326
317
327
/**
318
328
* Creates a new ImpersonatedServiceAccountCredential from the given parameters.
@@ -339,15 +349,19 @@ export class ImpersonatedServiceAccountCredential implements Credential {
339
349
if ( this . googleAuth ) {
340
350
return this . googleAuth ;
341
351
}
342
- this . googleAuth = populateGoogleAuth ( this . impersonatedServiceAccountPathOrObject , this . httpAgent ) ;
352
+ const { auth, client } = populateGoogleAuth ( this . impersonatedServiceAccountPathOrObject , this . httpAgent ) ;
353
+ this . googleAuth = auth ;
354
+ this . authClient = client ;
343
355
return this . googleAuth ;
344
356
}
345
357
346
358
public async getAccessToken ( ) : Promise < GoogleOAuthAccessToken > {
347
359
const googleAuth = this . getGoogleAuth ( ) ;
348
- const client = await googleAuth . getClient ( ) ;
349
- await client . getAccessToken ( ) ;
350
- const credentials = client . credentials ;
360
+ if ( this . authClient === undefined ) {
361
+ this . authClient = await googleAuth . getClient ( ) ;
362
+ }
363
+ await this . authClient . getAccessToken ( ) ;
364
+ const credentials = this . authClient . credentials ;
351
365
return populateCredential ( credentials ) ;
352
366
}
353
367
}
@@ -430,8 +444,10 @@ function copyAttr(to: { [key: string]: any }, from: { [key: string]: any }, key:
430
444
/**
431
445
* Populate google-auth-library GoogleAuth credentials type.
432
446
*/
433
- function populateGoogleAuth ( keyFile : string | object , httpAgent ?: Agent ) : GoogleAuth {
434
- const googleAuth = new GoogleAuth ( {
447
+ function populateGoogleAuth ( keyFile : string | object , httpAgent ?: Agent )
448
+ : { auth : GoogleAuth , client : AnyAuthClient | undefined } {
449
+ let client : AnyAuthClient | undefined ;
450
+ const auth = new GoogleAuth ( {
435
451
scopes : SCOPES ,
436
452
clientOptions : {
437
453
transporterOptions : {
@@ -448,9 +464,9 @@ function populateGoogleAuth(keyFile: string | object, httpAgent?: Agent): Google
448
464
'Service account must be an object.' ,
449
465
) ;
450
466
}
451
- googleAuth . fromJSON ( keyFile ) ;
467
+ client = auth . fromJSON ( keyFile ) ;
452
468
}
453
- return googleAuth ;
469
+ return { auth , client } ;
454
470
}
455
471
456
472
/**
0 commit comments