@@ -22,6 +22,7 @@ import { AuthorizedHttpClient, HttpRequestConfig, HttpClient, RequestResponseErr
22
22
23
23
import { Algorithm } from 'jsonwebtoken' ;
24
24
import { ErrorInfo } from '../utils/error' ;
25
+ import * as utils from '../utils/index' ;
25
26
import * as validator from '../utils/validator' ;
26
27
27
28
const ALGORITHM_RS256 : Algorithm = 'RS256' as const ;
@@ -105,22 +106,23 @@ export class IAMSigner implements CryptoSigner {
105
106
106
107
private readonly httpClient : AuthorizedHttpClient ;
107
108
private serviceAccountId ?: string ;
109
+ private app ?: App ;
108
110
109
- constructor ( httpClient : AuthorizedHttpClient , serviceAccountId ?: string ) {
111
+ constructor ( httpClient : AuthorizedHttpClient , app ?: App ) {
110
112
if ( ! httpClient ) {
111
113
throw new CryptoSignerError ( {
112
114
code : CryptoSignerErrorCode . INVALID_ARGUMENT ,
113
115
message : 'INTERNAL ASSERT: Must provide a HTTP client to initialize IAMSigner.' ,
114
116
} ) ;
115
117
}
116
- if ( typeof serviceAccountId !== 'undefined' && ! validator . isNonEmptyString ( serviceAccountId ) ) {
118
+ if ( app && ( typeof app !== 'object' || app === null || ! ( 'options' in app ) ) ) {
117
119
throw new CryptoSignerError ( {
118
120
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 .' ,
120
122
} ) ;
121
123
}
122
124
this . httpClient = httpClient ;
123
- this . serviceAccountId = serviceAccountId ;
125
+ this . app = app ;
124
126
}
125
127
126
128
/**
@@ -152,9 +154,16 @@ export class IAMSigner implements CryptoSigner {
152
154
/**
153
155
* @inheritDoc
154
156
*/
155
- public getAccountId ( ) : Promise < string > {
157
+ public async getAccountId ( ) : Promise < string > {
156
158
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
+ }
158
167
}
159
168
const request : HttpRequestConfig = {
160
169
method : 'GET' ,
@@ -197,7 +206,7 @@ export function cryptoSignerFromApp(app: App): CryptoSigner {
197
206
return new ServiceAccountSigner ( credential ) ;
198
207
}
199
208
200
- return new IAMSigner ( new AuthorizedHttpClient ( app as FirebaseApp ) , app . options . serviceAccountId ) ;
209
+ return new IAMSigner ( new AuthorizedHttpClient ( app as FirebaseApp ) , app ) ;
201
210
}
202
211
203
212
/**
0 commit comments