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