From 1d3dff2d5a99c7994657bc94f2fbcb85b51408d9 Mon Sep 17 00:00:00 2001 From: Rich Gowman Date: Thu, 5 Mar 2020 15:14:10 -0500 Subject: [PATCH 1/4] Allow enabling of anonymous provider via tenant configuration --- src/auth/tenant.ts | 10 ++++++++ src/index.d.ts | 10 ++++++++ test/integration/auth.spec.ts | 36 +++++++++++++++++++++++++++ test/unit/auth/tenant-manager.spec.ts | 3 +++ test/unit/auth/tenant.spec.ts | 1 + 5 files changed, 60 insertions(+) diff --git a/src/auth/tenant.ts b/src/auth/tenant.ts index 0b2ed1dca9..58369226c7 100755 --- a/src/auth/tenant.ts +++ b/src/auth/tenant.ts @@ -24,11 +24,13 @@ import { export interface TenantOptions { displayName?: string; emailSignInConfig?: EmailSignInProviderConfig; + anonymousSignInEnabled?: boolean; } /** The corresponding server side representation of a TenantOptions object. */ export interface TenantOptionsServerRequest extends EmailSignInConfigServerRequest { displayName?: string; + enableAnonymousUser?: boolean; } /** The tenant server response interface. */ @@ -37,6 +39,7 @@ export interface TenantServerResponse { displayName?: string; allowPasswordSignup?: boolean; enableEmailLinkSignin?: boolean; + enableAnonymousUser?: boolean; } /** The interface representing the listTenant API response. */ @@ -53,6 +56,7 @@ export class Tenant { public readonly tenantId: string; public readonly displayName?: string; public readonly emailSignInConfig?: EmailSignInConfig; + public readonly anonymousSignInEnabled?: boolean; /** * Builds the corresponding server request for a TenantOptions object. @@ -71,6 +75,9 @@ export class Tenant { if (typeof tenantOptions.displayName !== 'undefined') { request.displayName = tenantOptions.displayName; } + if (typeof tenantOptions.anonymousSignInEnabled !== 'undefined') { + request.enableAnonymousUser = tenantOptions.anonymousSignInEnabled; + } return request; } @@ -99,6 +106,7 @@ export class Tenant { const validKeys = { displayName: true, emailSignInConfig: true, + anonymousSignInEnabled: true, }; const label = createRequest ? 'CreateTenantRequest' : 'UpdateTenantRequest'; if (!validator.isNonNullObject(request)) { @@ -155,6 +163,7 @@ export class Tenant { allowPasswordSignup: false, }); } + this.anonymousSignInEnabled = !!response.enableAnonymousUser; } /** @return {object} The plain object representation of the tenant. */ @@ -163,6 +172,7 @@ export class Tenant { tenantId: this.tenantId, displayName: this.displayName, emailSignInConfig: this.emailSignInConfig && this.emailSignInConfig.toJSON(), + anonymousSignInEnabled: this.anonymousSignInEnabled, }; } } diff --git a/src/index.d.ts b/src/index.d.ts index af8a938766..5550536392 100755 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1150,6 +1150,11 @@ declare namespace admin.auth { passwordRequired?: boolean; }; + /** + * Whether anonymous provider is enabled. + */ + anonymousSignInEnabled?: boolean; + /** * @return A JSON-serializable representation of this object. */ @@ -1182,6 +1187,11 @@ declare namespace admin.auth { */ passwordRequired?: boolean; }; + + /** + * Whether anonymous provider is enabled. + */ + anonymousSignInEnabled?: boolean; } /** diff --git a/test/integration/auth.spec.ts b/test/integration/auth.spec.ts index 3bd65422cd..8fba21d20e 100755 --- a/test/integration/auth.spec.ts +++ b/test/integration/auth.spec.ts @@ -510,6 +510,7 @@ describe('admin.auth', () => { enabled: true, passwordRequired: true, }, + anonymousSignInEnabled: false, }; const expectedUpdatedTenant: any = { displayName: 'testTenantUpdated', @@ -517,6 +518,7 @@ describe('admin.auth', () => { enabled: false, passwordRequired: true, }, + anonymousSignInEnabled: false, }; const expectedUpdatedTenant2: any = { displayName: 'testTenantUpdated', @@ -524,6 +526,7 @@ describe('admin.auth', () => { enabled: true, passwordRequired: false, }, + anonymousSignInEnabled: false, }; // https://mochajs.org/ @@ -562,6 +565,20 @@ describe('admin.auth', () => { }); }); + it('createTenant() can enable anonymous users', async () => { + const tenant = await admin.auth().tenantManager().createTenant({ + displayName: 'testTenantWithAnon', + emailSignInConfig: { + enabled: false, + passwordRequired: true, + }, + anonymousSignInEnabled: true, + }); + createdTenants.push(tenant.tenantId); + + expect(tenant.anonymousSignInEnabled).to.be.true; + }); + // Sanity check user management + email link generation + custom attribute APIs. // TODO: Confirm behavior in client SDK when it starts supporting it. describe('supports user management, email link generation, custom attribute and token revocation APIs', () => { @@ -900,6 +917,25 @@ describe('admin.auth', () => { }); }); + it('updateTenant() should be able to enable/disable anon provider', async () => { + const tenantManager = admin.auth().tenantManager(); + let tenant = await tenantManager.createTenant({ + displayName: 'testTenantUpdateAnon', + }); + createdTenants.push(tenant.tenantId); + expect(tenant.anonymousSignInEnabled).to.be.false; + + tenant = await tenantManager.updateTenant(tenant.tenantId, { + anonymousSignInEnabled: true, + }); + expect(tenant.anonymousSignInEnabled).to.be.true; + + tenant = await tenantManager.updateTenant(tenant.tenantId, { + anonymousSignInEnabled: false, + }); + expect(tenant.anonymousSignInEnabled).to.be.false; + }); + it('listTenants() should resolve with expected number of tenants', () => { const allTenantIds: string[] = []; const tenantOptions2 = deepCopy(tenantOptions); diff --git a/test/unit/auth/tenant-manager.spec.ts b/test/unit/auth/tenant-manager.spec.ts index 82f89599d9..7fec667683 100644 --- a/test/unit/auth/tenant-manager.spec.ts +++ b/test/unit/auth/tenant-manager.spec.ts @@ -47,6 +47,7 @@ describe('TenantManager', () => { displayName: 'TENANT-DISPLAY-NAME', allowPasswordSignup: true, enableEmailLinkSignin: false, + enableAnonymousUser: true, }; before(() => { @@ -383,6 +384,7 @@ describe('TenantManager', () => { enabled: true, passwordRequired: true, }, + anonymousSignInEnabled: true, }; const expectedTenant = new Tenant(GET_TENANT_RESPONSE); const expectedError = new FirebaseAuthError( @@ -475,6 +477,7 @@ describe('TenantManager', () => { enabled: true, passwordRequired: true, }, + anonymousSignInEnabled: true, }; const expectedTenant = new Tenant(GET_TENANT_RESPONSE); const expectedError = new FirebaseAuthError( diff --git a/test/unit/auth/tenant.spec.ts b/test/unit/auth/tenant.spec.ts index 1fb1e24dfa..968e161db0 100755 --- a/test/unit/auth/tenant.spec.ts +++ b/test/unit/auth/tenant.spec.ts @@ -233,6 +233,7 @@ describe('Tenant', () => { enabled: true, passwordRequired: false, }, + anonymousSignInEnabled: false, }); }); }); From c644b73f370750c9a4cd90a441c2c36effc02317 Mon Sep 17 00:00:00 2001 From: Rich Gowman Date: Wed, 23 Dec 2020 09:47:44 -0500 Subject: [PATCH 2/4] api-extractor --- etc/firebase-admin.api.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/etc/firebase-admin.api.md b/etc/firebase-admin.api.md index b5b810a5e6..e7064c47ea 100644 --- a/etc/firebase-admin.api.md +++ b/etc/firebase-admin.api.md @@ -250,6 +250,7 @@ export namespace auth { expiresIn: number; } export interface Tenant { + anonymousSignInEnabled?: boolean; displayName?: string; emailSignInConfig?: { enabled: boolean; @@ -300,6 +301,7 @@ export namespace auth { photoURL?: string | null; } export interface UpdateTenantRequest { + anonymousSignInEnabled?: boolean; displayName?: string; emailSignInConfig?: EmailSignInProviderConfig; multiFactorConfig?: MultiFactorConfig; From 8f249521cb5852f7652e4d8adc6fa88a1084ae22 Mon Sep 17 00:00:00 2001 From: Rich Gowman Date: Fri, 5 Feb 2021 10:33:49 -0500 Subject: [PATCH 3/4] make Tenant.anonymousSignInEnabled non-optional. --- src/auth/index.ts | 2 +- src/auth/tenant.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/auth/index.ts b/src/auth/index.ts index 5913895060..3adfc3f3c9 100644 --- a/src/auth/index.ts +++ b/src/auth/index.ts @@ -1017,7 +1017,7 @@ export namespace auth { /** * Whether the anonymous provider is enabled. */ - anonymousSignInEnabled?: boolean; + anonymousSignInEnabled: boolean; /** * The multi-factor auth configuration on the current tenant. diff --git a/src/auth/tenant.ts b/src/auth/tenant.ts index 8156e01681..392494739b 100644 --- a/src/auth/tenant.ts +++ b/src/auth/tenant.ts @@ -52,7 +52,7 @@ export class Tenant implements TenantInterface { public readonly tenantId: string; public readonly displayName?: string; public readonly emailSignInConfig?: EmailSignInConfig; - public readonly anonymousSignInEnabled?: boolean; + public readonly anonymousSignInEnabled: boolean; public readonly multiFactorConfig?: MultiFactorAuthConfig; public readonly testPhoneNumbers?: {[phoneNumber: string]: string}; From 1f3814e53c2471f32b3ae6b4d46fa876b5257705 Mon Sep 17 00:00:00 2001 From: Rich Gowman Date: Fri, 5 Feb 2021 11:14:02 -0500 Subject: [PATCH 4/4] api-extractor --- etc/firebase-admin.api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/firebase-admin.api.md b/etc/firebase-admin.api.md index e7064c47ea..c258711562 100644 --- a/etc/firebase-admin.api.md +++ b/etc/firebase-admin.api.md @@ -250,7 +250,7 @@ export namespace auth { expiresIn: number; } export interface Tenant { - anonymousSignInEnabled?: boolean; + anonymousSignInEnabled: boolean; displayName?: string; emailSignInConfig?: { enabled: boolean;