@@ -88,7 +88,9 @@ const MAX_LIST_TENANT_PAGE_SIZE = 1000;
8888
8989/** Defines a base utility to help with resource URL construction. */
9090class AuthResourceUrlBuilder {
91+
9192 protected urlFormat : string ;
93+ private projectId : string ;
9294
9395 /**
9496 * The resource URL builder constructor.
@@ -97,7 +99,7 @@ class AuthResourceUrlBuilder {
9799 * @param {string } version The endpoint API version.
98100 * @constructor
99101 */
100- constructor ( protected projectId : string | null , protected version : string = 'v1' ) {
102+ constructor ( protected app : FirebaseApp , protected version : string = 'v1' ) {
101103 this . urlFormat = FIREBASE_AUTH_BASE_URL_FORMAT ;
102104 }
103105
@@ -107,17 +109,41 @@ class AuthResourceUrlBuilder {
107109 * @param {string= } api The backend API name.
108110 * @param {object= } params The optional additional parameters to substitute in the
109111 * URL path.
110- * @return {string } The corresponding resource URL.
112+ * @return {Promise< string> } The corresponding resource URL.
111113 */
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+ } ) ;
121147 }
122148}
123149
@@ -132,8 +158,8 @@ class TenantAwareAuthResourceUrlBuilder extends AuthResourceUrlBuilder {
132158 * @param {string } tenantId The tenant ID.
133159 * @constructor
134160 */
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 ) ;
137163 this . urlFormat = FIREBASE_AUTH_TENANT_URL_FORMAT ;
138164 }
139165
@@ -143,10 +169,13 @@ class TenantAwareAuthResourceUrlBuilder extends AuthResourceUrlBuilder {
143169 * @param {string= } api The backend API name.
144170 * @param {object= } params The optional additional parameters to substitute in the
145171 * URL path.
146- * @return {string } The corresponding resource URL.
172+ * @return {Promise< string> } The corresponding resource URL.
147173 */
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+ } ) ;
150179 }
151180}
152181
@@ -683,7 +712,7 @@ const LIST_INBOUND_SAML_CONFIGS = new ApiSettings('/inboundSamlConfigs', 'GET')
683712 * Class that provides the mechanism to send requests to the Firebase Auth backend endpoints.
684713 */
685714export abstract class AbstractAuthRequestHandler {
686- protected readonly projectId : string | null ;
715+
687716 protected readonly httpClient : AuthorizedHttpClient ;
688717 private authUrlBuilder : AuthResourceUrlBuilder ;
689718 private projectConfigUrlBuilder : AuthResourceUrlBuilder ;
@@ -700,17 +729,14 @@ export abstract class AbstractAuthRequestHandler {
700729 * @param {FirebaseApp } app The app used to fetch access tokens to sign API requests.
701730 * @constructor
702731 */
703- constructor ( app : FirebaseApp ) {
732+ constructor ( protected readonly app : FirebaseApp ) {
704733 if ( typeof app !== 'object' || app === null || ! ( 'options' in app ) ) {
705734 throw new FirebaseAuthError (
706735 AuthClientErrorCode . INVALID_ARGUMENT ,
707736 'First argument passed to admin.auth() must be a valid Firebase app instance.' ,
708737 ) ;
709738 }
710739
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 ) ;
714740 this . httpClient = new AuthorizedHttpClient ( app ) ;
715741 }
716742
@@ -1357,15 +1383,15 @@ export abstract class AbstractAuthRequestHandler {
13571383 protected invokeRequestHandler (
13581384 urlBuilder : AuthResourceUrlBuilder , apiSettings : ApiSettings ,
13591385 requestData : object , additionalResourceParams ?: object ) : Promise < object > {
1360- return Promise . resolve ( )
1361- . then ( ( ) => {
1386+ return urlBuilder . getUrl ( apiSettings . getEndpoint ( ) , additionalResourceParams )
1387+ . then ( ( url ) => {
13621388 // Validate request.
13631389 const requestValidator = apiSettings . getRequestValidator ( ) ;
13641390 requestValidator ( requestData ) ;
13651391 // Process request.
13661392 const req : HttpRequestConfig = {
13671393 method : apiSettings . getHttpMethod ( ) ,
1368- url : urlBuilder . getUrl ( apiSettings . getEndpoint ( ) , additionalResourceParams ) ,
1394+ url,
13691395 headers : FIREBASE_AUTH_HEADER ,
13701396 data : requestData ,
13711397 timeout : FIREBASE_AUTH_TIMEOUT ,
@@ -1512,21 +1538,21 @@ export class AuthRequestHandler extends AbstractAuthRequestHandler {
15121538 */
15131539 constructor ( app : FirebaseApp ) {
15141540 super ( app ) ;
1515- this . tenantMgmtResourceBuilder = new AuthResourceUrlBuilder ( utils . getProjectId ( app ) , 'v2' ) ;
1541+ this . tenantMgmtResourceBuilder = new AuthResourceUrlBuilder ( app , 'v2' ) ;
15161542 }
15171543
15181544 /**
15191545 * @return {AuthResourceUrlBuilder } A new Auth user management resource URL builder instance.
15201546 */
15211547 protected newAuthUrlBuilder ( ) : AuthResourceUrlBuilder {
1522- return new AuthResourceUrlBuilder ( this . projectId , 'v1' ) ;
1548+ return new AuthResourceUrlBuilder ( this . app , 'v1' ) ;
15231549 }
15241550
15251551 /**
15261552 * @return {AuthResourceUrlBuilder } A new project config resource URL builder instance.
15271553 */
15281554 protected newProjectConfigUrlBuilder ( ) : AuthResourceUrlBuilder {
1529- return new AuthResourceUrlBuilder ( this . projectId , 'v2' ) ;
1555+ return new AuthResourceUrlBuilder ( this . app , 'v2' ) ;
15301556 }
15311557
15321558 /**
@@ -1662,14 +1688,14 @@ export class TenantAwareAuthRequestHandler extends AbstractAuthRequestHandler {
16621688 * @return {AuthResourceUrlBuilder } A new Auth user management resource URL builder instance.
16631689 */
16641690 protected newAuthUrlBuilder ( ) : AuthResourceUrlBuilder {
1665- return new TenantAwareAuthResourceUrlBuilder ( this . projectId , 'v1' , this . tenantId ) ;
1691+ return new TenantAwareAuthResourceUrlBuilder ( this . app , 'v1' , this . tenantId ) ;
16661692 }
16671693
16681694 /**
16691695 * @return {AuthResourceUrlBuilder } A new project config resource URL builder instance.
16701696 */
16711697 protected newProjectConfigUrlBuilder ( ) : AuthResourceUrlBuilder {
1672- return new TenantAwareAuthResourceUrlBuilder ( this . projectId , 'v2' , this . tenantId ) ;
1698+ return new TenantAwareAuthResourceUrlBuilder ( this . app , 'v2' , this . tenantId ) ;
16731699 }
16741700
16751701 /**
0 commit comments