Skip to content

Commit a00ce05

Browse files
authored
Allow enabling of anonymous provider via tenant configuration (#802)
RELEASE NOTES: Allow enabling of anonymous provider via tenant configuration.
1 parent fc2f557 commit a00ce05

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

etc/firebase-admin.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ export namespace auth {
251251
expiresIn: number;
252252
}
253253
export interface Tenant {
254+
anonymousSignInEnabled: boolean;
254255
displayName?: string;
255256
emailSignInConfig?: {
256257
enabled: boolean;
@@ -301,6 +302,7 @@ export namespace auth {
301302
photoURL?: string | null;
302303
}
303304
export interface UpdateTenantRequest {
305+
anonymousSignInEnabled?: boolean;
304306
displayName?: string;
305307
emailSignInConfig?: EmailSignInProviderConfig;
306308
multiFactorConfig?: MultiFactorConfig;

src/auth/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,11 @@ export namespace auth {
10141014
passwordRequired?: boolean;
10151015
};
10161016

1017+
/**
1018+
* Whether the anonymous provider is enabled.
1019+
*/
1020+
anonymousSignInEnabled: boolean;
1021+
10171022
/**
10181023
* The multi-factor auth configuration on the current tenant.
10191024
*/
@@ -1089,6 +1094,11 @@ export namespace auth {
10891094
*/
10901095
emailSignInConfig?: EmailSignInProviderConfig;
10911096

1097+
/**
1098+
* Whether the anonymous provider is enabled.
1099+
*/
1100+
anonymousSignInEnabled?: boolean;
1101+
10921102
/**
10931103
* The multi-factor auth configuration to update on the tenant.
10941104
*/

src/auth/tenant.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import UpdateTenantRequest = auth.UpdateTenantRequest;
2929
/** The corresponding server side representation of a TenantOptions object. */
3030
export interface TenantOptionsServerRequest extends EmailSignInConfigServerRequest {
3131
displayName?: string;
32+
enableAnonymousUser?: boolean;
3233
mfaConfig?: MultiFactorAuthServerConfig;
3334
testPhoneNumbers?: {[key: string]: string};
3435
}
@@ -39,6 +40,7 @@ export interface TenantServerResponse {
3940
displayName?: string;
4041
allowPasswordSignup?: boolean;
4142
enableEmailLinkSignin?: boolean;
43+
enableAnonymousUser?: boolean;
4244
mfaConfig?: MultiFactorAuthServerConfig;
4345
testPhoneNumbers?: {[key: string]: string};
4446
}
@@ -50,6 +52,7 @@ export class Tenant implements TenantInterface {
5052
public readonly tenantId: string;
5153
public readonly displayName?: string;
5254
public readonly emailSignInConfig?: EmailSignInConfig;
55+
public readonly anonymousSignInEnabled: boolean;
5356
public readonly multiFactorConfig?: MultiFactorAuthConfig;
5457
public readonly testPhoneNumbers?: {[phoneNumber: string]: string};
5558

@@ -70,6 +73,9 @@ export class Tenant implements TenantInterface {
7073
if (typeof tenantOptions.displayName !== 'undefined') {
7174
request.displayName = tenantOptions.displayName;
7275
}
76+
if (typeof tenantOptions.anonymousSignInEnabled !== 'undefined') {
77+
request.enableAnonymousUser = tenantOptions.anonymousSignInEnabled;
78+
}
7379
if (typeof tenantOptions.multiFactorConfig !== 'undefined') {
7480
request.mfaConfig = MultiFactorAuthConfig.buildServerRequest(tenantOptions.multiFactorConfig);
7581
}
@@ -105,6 +111,7 @@ export class Tenant implements TenantInterface {
105111
const validKeys = {
106112
displayName: true,
107113
emailSignInConfig: true,
114+
anonymousSignInEnabled: true,
108115
multiFactorConfig: true,
109116
testPhoneNumbers: true,
110117
};
@@ -179,6 +186,7 @@ export class Tenant implements TenantInterface {
179186
allowPasswordSignup: false,
180187
});
181188
}
189+
this.anonymousSignInEnabled = !!response.enableAnonymousUser;
182190
if (typeof response.mfaConfig !== 'undefined') {
183191
this.multiFactorConfig = new MultiFactorAuthConfig(response.mfaConfig);
184192
}
@@ -193,6 +201,7 @@ export class Tenant implements TenantInterface {
193201
tenantId: this.tenantId,
194202
displayName: this.displayName,
195203
emailSignInConfig: this.emailSignInConfig?.toJSON(),
204+
anonymousSignInEnabled: this.anonymousSignInEnabled,
196205
multiFactorConfig: this.multiFactorConfig?.toJSON(),
197206
testPhoneNumbers: this.testPhoneNumbers,
198207
};

test/integration/auth.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ describe('admin.auth', () => {
886886
enabled: true,
887887
passwordRequired: true,
888888
},
889+
anonymousSignInEnabled: false,
889890
multiFactorConfig: {
890891
state: 'ENABLED',
891892
factorIds: ['phone'],
@@ -901,6 +902,7 @@ describe('admin.auth', () => {
901902
enabled: false,
902903
passwordRequired: true,
903904
},
905+
anonymousSignInEnabled: false,
904906
multiFactorConfig: {
905907
state: 'DISABLED',
906908
factorIds: [],
@@ -915,6 +917,7 @@ describe('admin.auth', () => {
915917
enabled: true,
916918
passwordRequired: false,
917919
},
920+
anonymousSignInEnabled: false,
918921
multiFactorConfig: {
919922
state: 'ENABLED',
920923
factorIds: ['phone'],
@@ -957,6 +960,20 @@ describe('admin.auth', () => {
957960
});
958961
});
959962

963+
it('createTenant() can enable anonymous users', async () => {
964+
const tenant = await admin.auth().tenantManager().createTenant({
965+
displayName: 'testTenantWithAnon',
966+
emailSignInConfig: {
967+
enabled: false,
968+
passwordRequired: true,
969+
},
970+
anonymousSignInEnabled: true,
971+
});
972+
createdTenants.push(tenant.tenantId);
973+
974+
expect(tenant.anonymousSignInEnabled).to.be.true;
975+
});
976+
960977
// Sanity check user management + email link generation + custom attribute APIs.
961978
// TODO: Confirm behavior in client SDK when it starts supporting it.
962979
describe('supports user management, email link generation, custom attribute and token revocation APIs', () => {
@@ -1300,6 +1317,25 @@ describe('admin.auth', () => {
13001317
});
13011318
});
13021319

1320+
it('updateTenant() should be able to enable/disable anon provider', async () => {
1321+
const tenantManager = admin.auth().tenantManager();
1322+
let tenant = await tenantManager.createTenant({
1323+
displayName: 'testTenantUpdateAnon',
1324+
});
1325+
createdTenants.push(tenant.tenantId);
1326+
expect(tenant.anonymousSignInEnabled).to.be.false;
1327+
1328+
tenant = await tenantManager.updateTenant(tenant.tenantId, {
1329+
anonymousSignInEnabled: true,
1330+
});
1331+
expect(tenant.anonymousSignInEnabled).to.be.true;
1332+
1333+
tenant = await tenantManager.updateTenant(tenant.tenantId, {
1334+
anonymousSignInEnabled: false,
1335+
});
1336+
expect(tenant.anonymousSignInEnabled).to.be.false;
1337+
});
1338+
13031339
it('listTenants() should resolve with expected number of tenants', () => {
13041340
const allTenantIds: string[] = [];
13051341
const tenantOptions2 = deepCopy(tenantOptions);

test/unit/auth/tenant-manager.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ describe('TenantManager', () => {
5252
displayName: 'TENANT-DISPLAY-NAME',
5353
allowPasswordSignup: true,
5454
enableEmailLinkSignin: false,
55+
enableAnonymousUser: true,
5556
};
5657

5758
before(() => {
@@ -388,6 +389,7 @@ describe('TenantManager', () => {
388389
enabled: true,
389390
passwordRequired: true,
390391
},
392+
anonymousSignInEnabled: true,
391393
};
392394
const expectedTenant = new Tenant(GET_TENANT_RESPONSE);
393395
const expectedError = new FirebaseAuthError(
@@ -480,6 +482,7 @@ describe('TenantManager', () => {
480482
enabled: true,
481483
passwordRequired: true,
482484
},
485+
anonymousSignInEnabled: true,
483486
};
484487
const expectedTenant = new Tenant(GET_TENANT_RESPONSE);
485488
const expectedError = new FirebaseAuthError(

test/unit/auth/tenant.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ describe('Tenant', () => {
351351
enabled: true,
352352
passwordRequired: false,
353353
},
354+
anonymousSignInEnabled: false,
354355
multiFactorConfig: deepCopy(clientRequest.multiFactorConfig),
355356
testPhoneNumbers: deepCopy(clientRequest.testPhoneNumbers),
356357
});
@@ -368,6 +369,7 @@ describe('Tenant', () => {
368369
enabled: true,
369370
passwordRequired: false,
370371
},
372+
anonymousSignInEnabled: false,
371373
});
372374
});
373375
});

0 commit comments

Comments
 (0)