@@ -88,7 +88,9 @@ const MAX_LIST_TENANT_PAGE_SIZE = 1000;
88
88
89
89
/** Defines a base utility to help with resource URL construction. */
90
90
class AuthResourceUrlBuilder {
91
+
91
92
protected urlFormat : string ;
93
+ private projectId : string ;
92
94
93
95
/**
94
96
* The resource URL builder constructor.
@@ -97,7 +99,7 @@ class AuthResourceUrlBuilder {
97
99
* @param {string } version The endpoint API version.
98
100
* @constructor
99
101
*/
100
- constructor ( protected projectId : string | null , protected version : string = 'v1' ) {
102
+ constructor ( protected app : FirebaseApp , protected version : string = 'v1' ) {
101
103
this . urlFormat = FIREBASE_AUTH_BASE_URL_FORMAT ;
102
104
}
103
105
@@ -107,17 +109,41 @@ class AuthResourceUrlBuilder {
107
109
* @param {string= } api The backend API name.
108
110
* @param {object= } params The optional additional parameters to substitute in the
109
111
* URL path.
110
- * @return {string } The corresponding resource URL.
112
+ * @return {Promise< string> } The corresponding resource URL.
111
113
*/
112
- public getUrl ( api ?: string , params ?: object ) : string {
113
- const baseParams = {
114
- version : this . version ,
115
- projectId : this . projectId ,
116
- api : api || '' ,
117
- } ;
118
- const baseUrl = utils . formatString ( this . urlFormat , baseParams ) ;
119
- // Substitute additional api related parameters.
120
- return utils . formatString ( baseUrl , params || { } ) ;
114
+ public getUrl ( api ?: string , params ?: object ) : Promise < string > {
115
+ return this . getProjectId ( )
116
+ . then ( ( projectId ) => {
117
+ const baseParams = {
118
+ version : this . version ,
119
+ projectId,
120
+ api : api || '' ,
121
+ } ;
122
+ const baseUrl = utils . formatString ( this . urlFormat , baseParams ) ;
123
+ // Substitute additional api related parameters.
124
+ return utils . formatString ( baseUrl , params || { } ) ;
125
+ } ) ;
126
+ }
127
+
128
+ private getProjectId ( ) : Promise < string > {
129
+ if ( this . projectId ) {
130
+ return Promise . resolve ( this . projectId ) ;
131
+ }
132
+
133
+ return utils . findProjectId ( this . app )
134
+ . then ( ( projectId ) => {
135
+ if ( ! validator . isNonEmptyString ( projectId ) ) {
136
+ throw new FirebaseAuthError (
137
+ AuthClientErrorCode . INVALID_CREDENTIAL ,
138
+ 'Failed to determine project ID for Auth. Initialize the '
139
+ + 'SDK with service account credentials or set project ID as an app option. '
140
+ + 'Alternatively set the GOOGLE_CLOUD_PROJECT environment variable.' ,
141
+ ) ;
142
+ }
143
+
144
+ this . projectId = projectId ;
145
+ return projectId ;
146
+ } ) ;
121
147
}
122
148
}
123
149
@@ -132,8 +158,8 @@ class TenantAwareAuthResourceUrlBuilder extends AuthResourceUrlBuilder {
132
158
* @param {string } tenantId The tenant ID.
133
159
* @constructor
134
160
*/
135
- constructor ( protected projectId : string | null , protected version : string , protected tenantId : string ) {
136
- super ( projectId , version ) ;
161
+ constructor ( protected app : FirebaseApp , protected version : string , protected tenantId : string ) {
162
+ super ( app , version ) ;
137
163
this . urlFormat = FIREBASE_AUTH_TENANT_URL_FORMAT ;
138
164
}
139
165
@@ -143,10 +169,13 @@ class TenantAwareAuthResourceUrlBuilder extends AuthResourceUrlBuilder {
143
169
* @param {string= } api The backend API name.
144
170
* @param {object= } params The optional additional parameters to substitute in the
145
171
* URL path.
146
- * @return {string } The corresponding resource URL.
172
+ * @return {Promise< string> } The corresponding resource URL.
147
173
*/
148
- public getUrl ( api ?: string , params ?: object ) {
149
- return utils . formatString ( super . getUrl ( api , params ) , { tenantId : this . tenantId } ) ;
174
+ public getUrl ( api ?: string , params ?: object ) : Promise < string > {
175
+ return super . getUrl ( api , params )
176
+ . then ( ( url ) => {
177
+ return utils . formatString ( url , { tenantId : this . tenantId } ) ;
178
+ } ) ;
150
179
}
151
180
}
152
181
@@ -683,7 +712,7 @@ const LIST_INBOUND_SAML_CONFIGS = new ApiSettings('/inboundSamlConfigs', 'GET')
683
712
* Class that provides the mechanism to send requests to the Firebase Auth backend endpoints.
684
713
*/
685
714
export abstract class AbstractAuthRequestHandler {
686
- protected readonly projectId : string | null ;
715
+
687
716
protected readonly httpClient : AuthorizedHttpClient ;
688
717
private authUrlBuilder : AuthResourceUrlBuilder ;
689
718
private projectConfigUrlBuilder : AuthResourceUrlBuilder ;
@@ -700,17 +729,14 @@ export abstract class AbstractAuthRequestHandler {
700
729
* @param {FirebaseApp } app The app used to fetch access tokens to sign API requests.
701
730
* @constructor
702
731
*/
703
- constructor ( app : FirebaseApp ) {
732
+ constructor ( protected readonly app : FirebaseApp ) {
704
733
if ( typeof app !== 'object' || app === null || ! ( 'options' in app ) ) {
705
734
throw new FirebaseAuthError (
706
735
AuthClientErrorCode . INVALID_ARGUMENT ,
707
736
'First argument passed to admin.auth() must be a valid Firebase app instance.' ,
708
737
) ;
709
738
}
710
739
711
- // TODO(rsgowman): Trace utils.getProjectId() throughout and figure out where a null return
712
- // value will cause troubles. (Such as AuthResourceUrlBuilder::getUrl()).
713
- this . projectId = utils . getProjectId ( app ) ;
714
740
this . httpClient = new AuthorizedHttpClient ( app ) ;
715
741
}
716
742
@@ -1357,15 +1383,15 @@ export abstract class AbstractAuthRequestHandler {
1357
1383
protected invokeRequestHandler (
1358
1384
urlBuilder : AuthResourceUrlBuilder , apiSettings : ApiSettings ,
1359
1385
requestData : object , additionalResourceParams ?: object ) : Promise < object > {
1360
- return Promise . resolve ( )
1361
- . then ( ( ) => {
1386
+ return urlBuilder . getUrl ( apiSettings . getEndpoint ( ) , additionalResourceParams )
1387
+ . then ( ( url ) => {
1362
1388
// Validate request.
1363
1389
const requestValidator = apiSettings . getRequestValidator ( ) ;
1364
1390
requestValidator ( requestData ) ;
1365
1391
// Process request.
1366
1392
const req : HttpRequestConfig = {
1367
1393
method : apiSettings . getHttpMethod ( ) ,
1368
- url : urlBuilder . getUrl ( apiSettings . getEndpoint ( ) , additionalResourceParams ) ,
1394
+ url,
1369
1395
headers : FIREBASE_AUTH_HEADER ,
1370
1396
data : requestData ,
1371
1397
timeout : FIREBASE_AUTH_TIMEOUT ,
@@ -1512,21 +1538,21 @@ export class AuthRequestHandler extends AbstractAuthRequestHandler {
1512
1538
*/
1513
1539
constructor ( app : FirebaseApp ) {
1514
1540
super ( app ) ;
1515
- this . tenantMgmtResourceBuilder = new AuthResourceUrlBuilder ( utils . getProjectId ( app ) , 'v2' ) ;
1541
+ this . tenantMgmtResourceBuilder = new AuthResourceUrlBuilder ( app , 'v2' ) ;
1516
1542
}
1517
1543
1518
1544
/**
1519
1545
* @return {AuthResourceUrlBuilder } A new Auth user management resource URL builder instance.
1520
1546
*/
1521
1547
protected newAuthUrlBuilder ( ) : AuthResourceUrlBuilder {
1522
- return new AuthResourceUrlBuilder ( this . projectId , 'v1' ) ;
1548
+ return new AuthResourceUrlBuilder ( this . app , 'v1' ) ;
1523
1549
}
1524
1550
1525
1551
/**
1526
1552
* @return {AuthResourceUrlBuilder } A new project config resource URL builder instance.
1527
1553
*/
1528
1554
protected newProjectConfigUrlBuilder ( ) : AuthResourceUrlBuilder {
1529
- return new AuthResourceUrlBuilder ( this . projectId , 'v2' ) ;
1555
+ return new AuthResourceUrlBuilder ( this . app , 'v2' ) ;
1530
1556
}
1531
1557
1532
1558
/**
@@ -1662,14 +1688,14 @@ export class TenantAwareAuthRequestHandler extends AbstractAuthRequestHandler {
1662
1688
* @return {AuthResourceUrlBuilder } A new Auth user management resource URL builder instance.
1663
1689
*/
1664
1690
protected newAuthUrlBuilder ( ) : AuthResourceUrlBuilder {
1665
- return new TenantAwareAuthResourceUrlBuilder ( this . projectId , 'v1' , this . tenantId ) ;
1691
+ return new TenantAwareAuthResourceUrlBuilder ( this . app , 'v1' , this . tenantId ) ;
1666
1692
}
1667
1693
1668
1694
/**
1669
1695
* @return {AuthResourceUrlBuilder } A new project config resource URL builder instance.
1670
1696
*/
1671
1697
protected newProjectConfigUrlBuilder ( ) : AuthResourceUrlBuilder {
1672
- return new TenantAwareAuthResourceUrlBuilder ( this . projectId , 'v2' , this . tenantId ) ;
1698
+ return new TenantAwareAuthResourceUrlBuilder ( this . app , 'v2' , this . tenantId ) ;
1673
1699
}
1674
1700
1675
1701
/**
0 commit comments