@@ -21,7 +21,7 @@ import path = require('path');
21
21
22
22
import { AppErrorCodes , FirebaseAppError } from '../utils/error' ;
23
23
import { HttpClient , HttpRequestConfig } from '../utils/api-request' ;
24
-
24
+ import { Agent } from 'http' ;
25
25
26
26
const GOOGLE_TOKEN_AUDIENCE = 'https://accounts.google.com/o/oauth2/token' ;
27
27
const GOOGLE_AUTH_TOKEN_HOST = 'accounts.google.com' ;
@@ -216,13 +216,16 @@ function requestAccessToken(client: HttpClient, request: HttpRequestConfig): Pro
216
216
* Implementation of Credential that uses a service account certificate.
217
217
*/
218
218
export class CertCredential implements Credential {
219
+
219
220
private readonly certificate : Certificate ;
220
221
private readonly httpClient : HttpClient ;
222
+ private readonly httpAgent : Agent ;
221
223
222
- constructor ( serviceAccountPathOrObject : string | object ) {
224
+ constructor ( serviceAccountPathOrObject : string | object , httpAgent ?: Agent ) {
223
225
this . certificate = ( typeof serviceAccountPathOrObject === 'string' ) ?
224
226
Certificate . fromPath ( serviceAccountPathOrObject ) : new Certificate ( serviceAccountPathOrObject ) ;
225
227
this . httpClient = new HttpClient ( ) ;
228
+ this . httpAgent = httpAgent ;
226
229
}
227
230
228
231
public getAccessToken ( ) : Promise < GoogleOAuthAccessToken > {
@@ -236,6 +239,7 @@ export class CertCredential implements Credential {
236
239
'Content-Type' : 'application/x-www-form-urlencoded' ,
237
240
} ,
238
241
data : postData ,
242
+ httpAgent : this . httpAgent ,
239
243
} ;
240
244
return requestAccessToken ( this . httpClient , request ) ;
241
245
}
@@ -278,13 +282,16 @@ export interface Credential {
278
282
* Implementation of Credential that gets access tokens from refresh tokens.
279
283
*/
280
284
export class RefreshTokenCredential implements Credential {
285
+
281
286
private readonly refreshToken : RefreshToken ;
282
287
private readonly httpClient : HttpClient ;
288
+ private readonly httpAgent : Agent ;
283
289
284
- constructor ( refreshTokenPathOrObject : string | object ) {
290
+ constructor ( refreshTokenPathOrObject : string | object , httpAgent ?: Agent ) {
285
291
this . refreshToken = ( typeof refreshTokenPathOrObject === 'string' ) ?
286
292
RefreshToken . fromPath ( refreshTokenPathOrObject ) : new RefreshToken ( refreshTokenPathOrObject ) ;
287
293
this . httpClient = new HttpClient ( ) ;
294
+ this . httpAgent = httpAgent ;
288
295
}
289
296
290
297
public getAccessToken ( ) : Promise < GoogleOAuthAccessToken > {
@@ -300,6 +307,7 @@ export class RefreshTokenCredential implements Credential {
300
307
'Content-Type' : 'application/x-www-form-urlencoded' ,
301
308
} ,
302
309
data : postData ,
310
+ httpAgent : this . httpAgent ,
303
311
} ;
304
312
return requestAccessToken ( this . httpClient , request ) ;
305
313
}
@@ -318,11 +326,17 @@ export class RefreshTokenCredential implements Credential {
318
326
export class MetadataServiceCredential implements Credential {
319
327
320
328
private readonly httpClient = new HttpClient ( ) ;
329
+ private readonly httpAgent : Agent ;
330
+
331
+ constructor ( httpAgent ?: Agent ) {
332
+ this . httpAgent = httpAgent ;
333
+ }
321
334
322
335
public getAccessToken ( ) : Promise < GoogleOAuthAccessToken > {
323
336
const request : HttpRequestConfig = {
324
337
method : 'GET' ,
325
338
url : `http://${ GOOGLE_METADATA_SERVICE_HOST } ${ GOOGLE_METADATA_SERVICE_PATH } ` ,
339
+ httpAgent : this . httpAgent ,
326
340
} ;
327
341
return requestAccessToken ( this . httpClient , request ) ;
328
342
}
@@ -340,21 +354,21 @@ export class MetadataServiceCredential implements Credential {
340
354
export class ApplicationDefaultCredential implements Credential {
341
355
private credential_ : Credential ;
342
356
343
- constructor ( ) {
357
+ constructor ( httpAgent ?: Agent ) {
344
358
if ( process . env . GOOGLE_APPLICATION_CREDENTIALS ) {
345
359
const serviceAccount = Certificate . fromPath ( process . env . GOOGLE_APPLICATION_CREDENTIALS ) ;
346
- this . credential_ = new CertCredential ( serviceAccount ) ;
360
+ this . credential_ = new CertCredential ( serviceAccount , httpAgent ) ;
347
361
return ;
348
362
}
349
363
350
364
// It is OK to not have this file. If it is present, it must be valid.
351
365
const refreshToken = RefreshToken . fromPath ( GCLOUD_CREDENTIAL_PATH ) ;
352
366
if ( refreshToken ) {
353
- this . credential_ = new RefreshTokenCredential ( refreshToken ) ;
367
+ this . credential_ = new RefreshTokenCredential ( refreshToken , httpAgent ) ;
354
368
return ;
355
369
}
356
370
357
- this . credential_ = new MetadataServiceCredential ( ) ;
371
+ this . credential_ = new MetadataServiceCredential ( httpAgent ) ;
358
372
}
359
373
360
374
public getAccessToken ( ) : Promise < GoogleOAuthAccessToken > {
0 commit comments