@@ -22,6 +22,7 @@ import { AuthorizedHttpClient, HttpRequestConfig, HttpClient, RequestResponseErr
2222
2323import { Algorithm } from 'jsonwebtoken' ;
2424import { ErrorInfo } from '../utils/error' ;
25+ import * as utils from '../utils/index' ;
2526import * as validator from '../utils/validator' ;
2627
2728const ALGORITHM_RS256 : Algorithm = 'RS256' as const ;
@@ -105,22 +106,23 @@ export class IAMSigner implements CryptoSigner {
105106
106107 private readonly httpClient : AuthorizedHttpClient ;
107108 private serviceAccountId ?: string ;
109+ private app ?: App ;
108110
109- constructor ( httpClient : AuthorizedHttpClient , serviceAccountId ?: string ) {
111+ constructor ( httpClient : AuthorizedHttpClient , app ?: App ) {
110112 if ( ! httpClient ) {
111113 throw new CryptoSignerError ( {
112114 code : CryptoSignerErrorCode . INVALID_ARGUMENT ,
113115 message : 'INTERNAL ASSERT: Must provide a HTTP client to initialize IAMSigner.' ,
114116 } ) ;
115117 }
116- if ( typeof serviceAccountId !== 'undefined' && ! validator . isNonEmptyString ( serviceAccountId ) ) {
118+ if ( app && ( typeof app !== 'object' || app === null || ! ( 'options' in app ) ) ) {
117119 throw new CryptoSignerError ( {
118120 code : CryptoSignerErrorCode . INVALID_ARGUMENT ,
119- message : 'INTERNAL ASSERT: Service account ID must be undefined or a non-empty string .' ,
121+ message : 'INTERNAL ASSERT: Must provide a valid Firebase app instance .' ,
120122 } ) ;
121123 }
122124 this . httpClient = httpClient ;
123- this . serviceAccountId = serviceAccountId ;
125+ this . app = app ;
124126 }
125127
126128 /**
@@ -152,9 +154,16 @@ export class IAMSigner implements CryptoSigner {
152154 /**
153155 * @inheritDoc
154156 */
155- public getAccountId ( ) : Promise < string > {
157+ public async getAccountId ( ) : Promise < string > {
156158 if ( validator . isNonEmptyString ( this . serviceAccountId ) ) {
157- return Promise . resolve ( this . serviceAccountId ) ;
159+ return this . serviceAccountId ;
160+ }
161+ if ( this . app ) {
162+ const accountId = await utils . findServiceAccountEmail ( this . app ! )
163+ if ( accountId ) {
164+ this . serviceAccountId = accountId ;
165+ return accountId ;
166+ }
158167 }
159168 const request : HttpRequestConfig = {
160169 method : 'GET' ,
@@ -197,7 +206,7 @@ export function cryptoSignerFromApp(app: App): CryptoSigner {
197206 return new ServiceAccountSigner ( credential ) ;
198207 }
199208
200- return new IAMSigner ( new AuthorizedHttpClient ( app as FirebaseApp ) , app . options . serviceAccountId ) ;
209+ return new IAMSigner ( new AuthorizedHttpClient ( app as FirebaseApp ) , app ) ;
201210}
202211
203212/**
0 commit comments