@@ -238,8 +238,8 @@ export class RefreshTokenCredential implements Credential {
238
238
readonly implicit : boolean = false ) {
239
239
240
240
( typeof refreshTokenPathOrObject === 'string' ) ?
241
- RefreshToken . fromPath ( refreshTokenPathOrObject )
242
- : new RefreshToken ( refreshTokenPathOrObject ) ;
241
+ RefreshToken . validateFromPath ( refreshTokenPathOrObject )
242
+ : RefreshToken . validateFromJSON ( refreshTokenPathOrObject ) ;
243
243
}
244
244
245
245
private getGoogleAuth ( ) : GoogleAuth {
@@ -261,18 +261,18 @@ export class RefreshTokenCredential implements Credential {
261
261
262
262
class RefreshToken {
263
263
264
- public readonly clientId : string ;
265
- public readonly clientSecret : string ;
266
- public readonly refreshToken : string ;
267
- public readonly type : string ;
264
+ // public readonly clientId: string;
265
+ // public readonly clientSecret: string;
266
+ // public readonly refreshToken: string;
267
+ // public readonly type: string;
268
268
269
269
/*
270
270
* Tries to load a RefreshToken from a path. Throws if the path doesn't exist or the
271
271
* data at the path is invalid.
272
272
*/
273
- public static fromPath ( filePath : string ) : RefreshToken {
273
+ public static validateFromPath ( filePath : string ) : void {
274
274
try {
275
- return new RefreshToken ( JSON . parse ( fs . readFileSync ( filePath , 'utf8' ) ) ) ;
275
+ RefreshToken . validateFromJSON ( JSON . parse ( fs . readFileSync ( filePath , 'utf8' ) ) ) ;
276
276
} catch ( error ) {
277
277
// Throw a nicely formed error message if the file contents cannot be parsed
278
278
throw new FirebaseAppError (
@@ -282,20 +282,20 @@ class RefreshToken {
282
282
}
283
283
}
284
284
285
- constructor ( json : object ) {
286
- copyAttr ( this , json , 'clientId' , 'client_id' ) ;
287
- copyAttr ( this , json , 'clientSecret' , 'client_secret' ) ;
288
- copyAttr ( this , json , ' refreshToken' , 'refresh_token' ) ;
289
- copyAttr ( this , json , 'type' , 'type' ) ;
285
+ public static validateFromJSON ( json : object ) : void {
286
+
287
+ const {
288
+ client_id : clientId , client_secret : clientSecret , refresh_token : refreshToken , type
289
+ } = ( json as { [ key : string ] : any } ) ;
290
290
291
291
let errorMessage ;
292
- if ( ! util . isNonEmptyString ( this . clientId ) ) {
292
+ if ( ! util . isNonEmptyString ( clientId ) ) {
293
293
errorMessage = 'Refresh token must contain a "client_id" property.' ;
294
- } else if ( ! util . isNonEmptyString ( this . clientSecret ) ) {
294
+ } else if ( ! util . isNonEmptyString ( clientSecret ) ) {
295
295
errorMessage = 'Refresh token must contain a "client_secret" property.' ;
296
- } else if ( ! util . isNonEmptyString ( this . refreshToken ) ) {
296
+ } else if ( ! util . isNonEmptyString ( refreshToken ) ) {
297
297
errorMessage = 'Refresh token must contain a "refresh_token" property.' ;
298
- } else if ( ! util . isNonEmptyString ( this . type ) ) {
298
+ } else if ( ! util . isNonEmptyString ( type ) ) {
299
299
errorMessage = 'Refresh token must contain a "type" property.' ;
300
300
}
301
301
@@ -329,8 +329,8 @@ export class ImpersonatedServiceAccountCredential implements Credential {
329
329
readonly implicit : boolean = false ) {
330
330
331
331
( typeof impersonatedServiceAccountPathOrObject === 'string' ) ?
332
- ImpersonatedServiceAccount . fromPath ( impersonatedServiceAccountPathOrObject )
333
- : new ImpersonatedServiceAccount ( impersonatedServiceAccountPathOrObject ) ;
332
+ ImpersonatedServiceAccount . validateFromPath ( impersonatedServiceAccountPathOrObject )
333
+ : ImpersonatedServiceAccount . validateFromJSON ( impersonatedServiceAccountPathOrObject ) ;
334
334
}
335
335
336
336
private getGoogleAuth ( ) : GoogleAuth {
@@ -351,22 +351,17 @@ export class ImpersonatedServiceAccountCredential implements Credential {
351
351
}
352
352
353
353
/**
354
- * A struct containing the properties necessary to use impersonated service account JSON credentials.
354
+ * A helper class to validate the properties necessary to use impersonated service account credentials.
355
355
*/
356
356
class ImpersonatedServiceAccount {
357
357
358
- public readonly clientId : string ;
359
- public readonly clientSecret : string ;
360
- public readonly refreshToken : string ;
361
- public readonly type : string ;
362
-
363
358
/*
364
359
* Tries to load a ImpersonatedServiceAccount from a path. Throws if the path doesn't exist or the
365
360
* data at the path is invalid.
366
361
*/
367
- public static fromPath ( filePath : string ) : ImpersonatedServiceAccount {
362
+ public static validateFromPath ( filePath : string ) : void {
368
363
try {
369
- return new ImpersonatedServiceAccount ( JSON . parse ( fs . readFileSync ( filePath , 'utf8' ) ) ) ;
364
+ ImpersonatedServiceAccount . validateFromJSON ( JSON . parse ( fs . readFileSync ( filePath , 'utf8' ) ) ) ;
370
365
} catch ( error ) {
371
366
// Throw a nicely formed error message if the file contents cannot be parsed
372
367
throw new FirebaseAppError (
@@ -376,23 +371,19 @@ class ImpersonatedServiceAccount {
376
371
}
377
372
}
378
373
379
- constructor ( json : object ) {
380
- const sourceCredentials = ( json as { [ key : string ] : any } ) [ 'source_credentials' ]
381
- if ( sourceCredentials ) {
382
- copyAttr ( this , sourceCredentials , 'clientId' , 'client_id' ) ;
383
- copyAttr ( this , sourceCredentials , 'clientSecret' , 'client_secret' ) ;
384
- copyAttr ( this , sourceCredentials , 'refreshToken' , 'refresh_token' ) ;
385
- copyAttr ( this , sourceCredentials , 'type' , 'type' ) ;
386
- }
374
+ public static validateFromJSON ( json : object ) : void {
375
+ const {
376
+ client_id : clientId , client_secret : clientSecret , refresh_token : refreshToken , type
377
+ } = ( json as { [ key : string ] : any } ) [ 'source_credentials' ] ;
387
378
388
379
let errorMessage ;
389
- if ( ! util . isNonEmptyString ( this . clientId ) ) {
380
+ if ( ! util . isNonEmptyString ( clientId ) ) {
390
381
errorMessage = 'Impersonated Service Account must contain a "source_credentials.client_id" property.' ;
391
- } else if ( ! util . isNonEmptyString ( this . clientSecret ) ) {
382
+ } else if ( ! util . isNonEmptyString ( clientSecret ) ) {
392
383
errorMessage = 'Impersonated Service Account must contain a "source_credentials.client_secret" property.' ;
393
- } else if ( ! util . isNonEmptyString ( this . refreshToken ) ) {
384
+ } else if ( ! util . isNonEmptyString ( refreshToken ) ) {
394
385
errorMessage = 'Impersonated Service Account must contain a "source_credentials.refresh_token" property.' ;
395
- } else if ( ! util . isNonEmptyString ( this . type ) ) {
386
+ } else if ( ! util . isNonEmptyString ( type ) ) {
396
387
errorMessage = 'Impersonated Service Account must contain a "source_credentials.type" property.' ;
397
388
}
398
389
0 commit comments