Skip to content

Commit 1124348

Browse files
Removes tenant management methods and forTenant from Auth class. (#619)
* Removes tenant management methods and `forTenant` from Auth class. Defines Auth#tenantManager(). Updates unit and integration tests. Updates index.d.ts type definitions.
1 parent 904e118 commit 1124348

4 files changed

Lines changed: 49 additions & 650 deletions

File tree

src/auth/auth.ts

Lines changed: 6 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
AuthProviderConfig, AuthProviderConfigFilter, ListProviderConfigResults, UpdateAuthProviderRequest,
3535
SAMLConfig, OIDCConfig, OIDCConfigServerResponse, SAMLConfigServerResponse,
3636
} from './auth-config';
37-
import {Tenant, TenantOptions, ListTenantsResult, TenantServerResponse} from './tenant';
37+
import {TenantManager} from './tenant-manager';
3838

3939

4040
/**
@@ -722,7 +722,7 @@ export class TenantAwareAuth extends BaseAuth<TenantAwareAuthRequestHandler> {
722722
*/
723723
export class Auth extends BaseAuth<AuthRequestHandler> implements FirebaseServiceInterface {
724724
public INTERNAL: AuthInternals = new AuthInternals();
725-
private readonly tenantsMap: {[key: string]: TenantAwareAuth};
725+
private readonly tenantManager_: TenantManager;
726726
private readonly app_: FirebaseApp;
727727

728728
/**
@@ -751,7 +751,7 @@ export class Auth extends BaseAuth<AuthRequestHandler> implements FirebaseServic
751751
new AuthRequestHandler(app),
752752
cryptoSignerFromApp(app));
753753
this.app_ = app;
754-
this.tenantsMap = {};
754+
this.tenantManager_ = new TenantManager(app);
755755
}
756756

757757
/**
@@ -763,107 +763,8 @@ export class Auth extends BaseAuth<AuthRequestHandler> implements FirebaseServic
763763
return this.app_;
764764
}
765765

766-
/**
767-
* Returns a TenantAwareAuth instance for the corresponding tenant ID.
768-
*
769-
* @param {string} tenantId The tenant ID whose TenantAwareAuth is to be returned.
770-
* @return {TenantAwareAuth} The corresponding TenantAwareAuth instance.
771-
*/
772-
public forTenant(tenantId: string): TenantAwareAuth {
773-
if (!validator.isNonEmptyString(tenantId)) {
774-
throw new FirebaseAuthError(AuthClientErrorCode.INVALID_TENANT_ID);
775-
}
776-
if (typeof this.tenantsMap[tenantId] === 'undefined') {
777-
this.tenantsMap[tenantId] = new TenantAwareAuth(this.app, tenantId);
778-
}
779-
return this.tenantsMap[tenantId];
780-
}
781-
782-
/**
783-
* Looks up the tenant identified by the provided tenant ID and returns a promise that is
784-
* fulfilled with the corresponding tenant if it is found.
785-
*
786-
* @param {string} tenantId The tenant ID of the tenant to look up.
787-
* @return {Promise<Tenant>} A promise that resolves with the corresponding tenant.
788-
*/
789-
public getTenant(tenantId: string): Promise<Tenant> {
790-
return this.authRequestHandler.getTenant(tenantId)
791-
.then((response: TenantServerResponse) => {
792-
return new Tenant(response);
793-
});
794-
}
795-
796-
/**
797-
* Exports a batch of tenant accounts. Batch size is determined by the maxResults argument.
798-
* Starting point of the batch is determined by the pageToken argument.
799-
*
800-
* @param {number=} maxResults The page size, 1000 if undefined. This is also the maximum
801-
* allowed limit.
802-
* @param {string=} pageToken The next page token. If not specified, returns users starting
803-
* without any offset.
804-
* @return {Promise<{users: Tenant[], pageToken?: string}>} A promise that resolves with
805-
* the current batch of downloaded tenants and the next page token. For the last page, an
806-
* empty list of tenants and no page token are returned.
807-
*/
808-
public listTenants(
809-
maxResults?: number,
810-
pageToken?: string): Promise<ListTenantsResult> {
811-
return this.authRequestHandler.listTenants(maxResults, pageToken)
812-
.then((response: {tenants: TenantServerResponse[], nextPageToken?: string}) => {
813-
// List of tenants to return.
814-
const tenants: Tenant[] = [];
815-
// Convert each user response to a Tenant.
816-
response.tenants.forEach((tenantResponse: TenantServerResponse) => {
817-
tenants.push(new Tenant(tenantResponse));
818-
});
819-
// Return list of tenants and the next page token if available.
820-
const result = {
821-
tenants,
822-
pageToken: response.nextPageToken,
823-
};
824-
// Delete result.pageToken if undefined.
825-
if (typeof result.pageToken === 'undefined') {
826-
delete result.pageToken;
827-
}
828-
return result;
829-
});
830-
}
831-
832-
/**
833-
* Deletes the tenant identified by the provided tenant ID and returns a promise that is
834-
* fulfilled when the tenant is found and successfully deleted.
835-
*
836-
* @param {string} tenantId The tenant ID of the tenant to delete.
837-
* @return {Promise<void>} A promise that resolves when the tenant is successfully deleted.
838-
*/
839-
public deleteTenant(tenantId: string): Promise<void> {
840-
return this.authRequestHandler.deleteTenant(tenantId);
841-
}
842-
843-
/**
844-
* Creates a new tenant with the properties provided.
845-
*
846-
* @param {TenantOptions} tenantOptions The properties to set on the new tenant to be created.
847-
* @return {Promise<Tenant>} A promise that resolves with the newly created tenant.
848-
*/
849-
public createTenant(tenantOptions: TenantOptions): Promise<Tenant> {
850-
return this.authRequestHandler.createTenant(tenantOptions)
851-
.then((response: TenantServerResponse) => {
852-
return new Tenant(response);
853-
});
854-
}
855-
856-
/**
857-
* Updates an existing tenant identified by the tenant ID with the properties provided.
858-
*
859-
* @param {string} tenantId The tenant identifier of the tenant to update.
860-
* @param {TenantOptions} tenantOptions The properties to update on the existing tenant.
861-
* @return {Promise<Tenant>} A promise that resolves with the modified tenant.
862-
*/
863-
public updateTenant(tenantId: string, tenantOptions: TenantOptions): Promise<Tenant> {
864-
return this.authRequestHandler.updateTenant(tenantId, tenantOptions)
865-
.then((response: TenantServerResponse) => {
866-
return new Tenant(response);
867-
});
766+
/** @return The current Auth instance's tenant manager. */
767+
public tenantManager(): TenantManager {
768+
return this.tenantManager_;
868769
}
869770
}

src/index.d.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,7 @@ declare namespace admin.auth {
19281928
* tenant.
19291929
*
19301930
* `TenantAwareAuth` instances for a specific `tenantId` can be instantiated by calling
1931-
* `auth.forTenant(tenantId)`.
1931+
* `auth.tenantManager().authForTenant(tenantId)`.
19321932
*/
19331933
interface TenantAwareAuth extends BaseAuth {
19341934

@@ -1943,12 +1943,30 @@ declare namespace admin.auth {
19431943
interface Auth extends admin.auth.BaseAuth {
19441944
app: admin.app.App;
19451945

1946+
/**
1947+
* @return The tenant manager instance associated with the current project.
1948+
*/
1949+
tenantManager(): admin.auth.TenantManager;
1950+
}
1951+
1952+
/**
1953+
* Defines the tenant manager used to help manage tenant related operations.
1954+
* This includes:
1955+
* <ul>
1956+
* <li>The ability to create, update, list, get and delete tenants for the underlying
1957+
* project.</li>
1958+
* <li>Getting a `TenantAwareAuth` instance for running Auth related operations
1959+
* (user management, provider configuration management, token verification,
1960+
* email link generation, etc) in the context of a specified tenant.</li>
1961+
* </ul>
1962+
*/
1963+
interface TenantManager {
19461964
/**
19471965
* @param tenantId The tenant ID whose `TenantAwareAuth` instance is to be returned.
19481966
*
19491967
* @return The `TenantAwareAuth` instance corresponding to this tenant identifier.
19501968
*/
1951-
forTenant(tenantId: string): admin.auth.TenantAwareAuth;
1969+
authForTenant(tenantId: string): admin.auth.TenantAwareAuth;
19521970

19531971
/**
19541972
* Gets the tenant configuration for the tenant corresponding to a given `tenantId`.

test/integration/auth.spec.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,14 @@ describe('admin.auth', () => {
486486
const promises: Array<Promise<any>> = [];
487487
createdTenants.forEach((tenantId) => {
488488
promises.push(
489-
admin.auth().deleteTenant(tenantId).catch((error) => {/** Ignore. */}));
489+
admin.auth().tenantManager().deleteTenant(tenantId)
490+
.catch((error) => {/** Ignore. */}));
490491
});
491492
return Promise.all(promises);
492493
});
493494

494495
it('createTenant() should resolve with a new tenant', () => {
495-
return admin.auth().createTenant(tenantOptions)
496+
return admin.auth().tenantManager().createTenant(tenantOptions)
496497
.then((actualTenant) => {
497498
createdTenantId = actualTenant.tenantId;
498499
createdTenants.push(createdTenantId);
@@ -520,7 +521,7 @@ describe('admin.auth', () => {
520521
const rawSalt = 'NaCl';
521522

522523
before(() => {
523-
tenantAwareAuth = admin.auth().forTenant(createdTenantId);
524+
tenantAwareAuth = admin.auth().tenantManager().authForTenant(createdTenantId);
524525
});
525526

526527
// Delete test user at the end of test suite.
@@ -678,7 +679,7 @@ describe('admin.auth', () => {
678679
};
679680

680681
before(() => {
681-
tenantAwareAuth = admin.auth().forTenant(createdTenantId);
682+
tenantAwareAuth = admin.auth().tenantManager().authForTenant(createdTenantId);
682683
});
683684

684685
// Delete SAML configuration at the end of test suite.
@@ -730,7 +731,7 @@ describe('admin.auth', () => {
730731
};
731732

732733
before(() => {
733-
tenantAwareAuth = admin.auth().forTenant(createdTenantId);
734+
tenantAwareAuth = admin.auth().tenantManager().authForTenant(createdTenantId);
734735
});
735736

736737
// Delete OIDC configuration at the end of test suite.
@@ -766,7 +767,7 @@ describe('admin.auth', () => {
766767
});
767768

768769
it('getTenant() should resolve with expected tenant', () => {
769-
return admin.auth().getTenant(createdTenantId)
770+
return admin.auth().tenantManager().getTenant(createdTenantId)
770771
.then((actualTenant) => {
771772
expect(actualTenant.toJSON()).to.deep.equal(expectedCreatedTenant);
772773
});
@@ -787,10 +788,10 @@ describe('admin.auth', () => {
787788
passwordRequired: false,
788789
},
789790
};
790-
return admin.auth().updateTenant(createdTenantId, updatedOptions)
791+
return admin.auth().tenantManager().updateTenant(createdTenantId, updatedOptions)
791792
.then((actualTenant) => {
792793
expect(actualTenant.toJSON()).to.deep.equal(expectedUpdatedTenant);
793-
return admin.auth().updateTenant(createdTenantId, updatedOptions2);
794+
return admin.auth().tenantManager().updateTenant(createdTenantId, updatedOptions2);
794795
})
795796
.then((actualTenant) => {
796797
expect(actualTenant.toJSON()).to.deep.equal(expectedUpdatedTenant2);
@@ -802,7 +803,7 @@ describe('admin.auth', () => {
802803
const tenantOptions2 = deepCopy(tenantOptions);
803804
tenantOptions2.displayName = 'testTenant2';
804805
const listAllTenantIds = (tenantIds: string[], nextPageToken?: string): Promise<void> => {
805-
return admin.auth().listTenants(100, nextPageToken)
806+
return admin.auth().tenantManager().listTenants(100, nextPageToken)
806807
.then((result) => {
807808
result.tenants.forEach((tenant) => {
808809
tenantIds.push(tenant.tenantId);
@@ -812,7 +813,7 @@ describe('admin.auth', () => {
812813
}
813814
});
814815
};
815-
return admin.auth().createTenant(tenantOptions2)
816+
return admin.auth().tenantManager().createTenant(tenantOptions2)
816817
.then((actualTenant) => {
817818
createdTenants.push(actualTenant.tenantId);
818819
// Test listTenants returns the expected tenants.
@@ -827,9 +828,9 @@ describe('admin.auth', () => {
827828
});
828829

829830
it('deleteTenant() should successfully delete the provided tenant', () => {
830-
return admin.auth().deleteTenant(createdTenantId)
831+
return admin.auth().tenantManager().deleteTenant(createdTenantId)
831832
.then(() => {
832-
return admin.auth().getTenant(createdTenantId);
833+
return admin.auth().tenantManager().getTenant(createdTenantId);
833834
})
834835
.then((result) => {
835836
throw new Error('unexpected success');

0 commit comments

Comments
 (0)