@@ -18,6 +18,7 @@ import {AuthClientErrorCode, FirebaseAuthError} from '../utils/error';
18
18
19
19
import * as validator from '../utils/validator' ;
20
20
import * as jwt from 'jsonwebtoken' ;
21
+ import { HttpClient , HttpRequestConfig , HttpError } from '../utils/api-request' ;
21
22
22
23
// Audience to use for Firebase Auth Custom tokens
23
24
const FIREBASE_AUDIENCE = 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit' ;
@@ -264,46 +265,45 @@ export class FirebaseTokenVerifier {
264
265
return Promise . resolve ( this . publicKeys ) ;
265
266
}
266
267
267
- return new Promise ( ( resolve , reject ) => {
268
- const https = require ( 'https' ) ;
269
- https . get ( this . clientCertUrl , ( res ) => {
270
- const buffers : Buffer [ ] = [ ] ;
271
-
272
- res . on ( 'data' , ( buffer ) => buffers . push ( buffer as Buffer ) ) ;
273
-
274
- res . on ( 'end' , ( ) => {
275
- try {
276
- const response = JSON . parse ( Buffer . concat ( buffers ) . toString ( ) ) ;
277
- if ( response . error ) {
278
- let errorMessage = 'Error fetching public keys for Google certs: ' + response . error ;
279
- /* istanbul ignore else */
280
- if ( response . error_description ) {
281
- errorMessage += ' (' + response . error_description + ')' ;
282
- }
283
- reject ( new FirebaseAuthError ( AuthClientErrorCode . INTERNAL_ERROR , errorMessage ) ) ;
284
- } else {
285
- /* istanbul ignore else */
286
- if ( res . headers . hasOwnProperty ( 'cache-control' ) ) {
287
- const cacheControlHeader : string = res . headers [ 'cache-control' ] as string ;
288
- const parts = cacheControlHeader . split ( ',' ) ;
289
- parts . forEach ( ( part ) => {
290
- const subParts = part . trim ( ) . split ( '=' ) ;
291
- if ( subParts [ 0 ] === 'max-age' ) {
292
- const maxAge : number = + subParts [ 1 ] ;
293
- this . publicKeysExpireAt = Date . now ( ) + ( maxAge * 1000 ) ;
294
- }
295
- } ) ;
296
- }
297
-
298
- this . publicKeys = response ;
299
- resolve ( response ) ;
300
- }
301
- } catch ( e ) {
302
- /* istanbul ignore next */
303
- reject ( e ) ;
268
+ const client = new HttpClient ( ) ;
269
+ const request : HttpRequestConfig = {
270
+ method : 'GET' ,
271
+ url : this . clientCertUrl ,
272
+ } ;
273
+ return client . send ( request ) . then ( ( resp ) => {
274
+ if ( ! resp . isJson ( ) || resp . data . error ) {
275
+ // Treat all non-json messages and messages with an 'error' field as
276
+ // error responses.
277
+ throw new HttpError ( resp ) ;
278
+ }
279
+ if ( resp . headers . hasOwnProperty ( 'cache-control' ) ) {
280
+ const cacheControlHeader : string = resp . headers [ 'cache-control' ] ;
281
+ const parts = cacheControlHeader . split ( ',' ) ;
282
+ parts . forEach ( ( part ) => {
283
+ const subParts = part . trim ( ) . split ( '=' ) ;
284
+ if ( subParts [ 0 ] === 'max-age' ) {
285
+ const maxAge : number = + subParts [ 1 ] ;
286
+ this . publicKeysExpireAt = Date . now ( ) + ( maxAge * 1000 ) ;
304
287
}
305
288
} ) ;
306
- } ) . on ( 'error' , reject ) ;
289
+ }
290
+ this . publicKeys = resp . data ;
291
+ return resp . data ;
292
+ } ) . catch ( ( err ) => {
293
+ if ( err instanceof HttpError ) {
294
+ let errorMessage = 'Error fetching public keys for Google certs: ' ;
295
+ const resp = err . response ;
296
+ if ( resp . isJson ( ) && resp . data . error ) {
297
+ errorMessage += `${ resp . data . error } ` ;
298
+ if ( resp . data . error_description ) {
299
+ errorMessage += ' (' + resp . data . error_description + ')' ;
300
+ }
301
+ } else {
302
+ errorMessage += `${ resp . text } ` ;
303
+ }
304
+ throw new FirebaseAuthError ( AuthClientErrorCode . INTERNAL_ERROR , errorMessage ) ;
305
+ }
306
+ throw err ;
307
307
} ) ;
308
308
}
309
309
}
0 commit comments