Skip to content

Commit f84527b

Browse files
committed
Edge case handling of tenant mfaConfig
1 parent a9572dd commit f84527b

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

src/emulator/auth/operations.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
UsageMode,
3333
AgentProjectState,
3434
TenantProjectState,
35+
MfaConfig,
3536
} from "./state";
3637
import { MfaEnrollments, Schemas } from "./types";
3738

@@ -2692,14 +2693,22 @@ function createTenant(
26922693
throw new InternalError("INTERNAL_ERROR: Can only create tenant in agent project", "INTERNAL");
26932694
}
26942695

2696+
const mfaConfig = reqBody.mfaConfig ?? {};
2697+
if (!("state" in mfaConfig)) {
2698+
mfaConfig.state = "DISABLED";
2699+
}
2700+
if (!("enabledProviders" in mfaConfig)) {
2701+
mfaConfig.enabledProviders = [];
2702+
}
2703+
26952704
// Default to production settings if unset
26962705
const tenant = {
26972706
displayName: reqBody.displayName,
26982707
allowPasswordSignup: reqBody.allowPasswordSignup ?? false,
26992708
enableEmailLinkSignin: reqBody.enableEmailLinkSignin ?? false,
27002709
enableAnonymousUser: reqBody.enableAnonymousUser ?? false,
27012710
disableAuth: reqBody.disableAuth ?? false,
2702-
mfaConfig: reqBody.mfaConfig ?? { state: "DISABLED" },
2711+
mfaConfig: mfaConfig as MfaConfig,
27032712
tenantId: "", // Placeholder until one is generated
27042713
};
27052714

src/emulator/auth/state.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -753,16 +753,27 @@ export class TenantProjectState extends ProjectState {
753753
this.parentProject.deleteTenant(this.tenantId);
754754
}
755755

756-
updateTenant(update: Partial<Tenant>, updateMask: string | undefined): Tenant {
756+
updateTenant(
757+
update: Schemas["GoogleCloudIdentitytoolkitAdminV2Tenant"],
758+
updateMask: string | undefined
759+
): Tenant {
757760
// Empty masks indicate a full update
758761
if (!updateMask) {
762+
const mfaConfig = update.mfaConfig ?? {};
763+
if (!("state" in mfaConfig)) {
764+
mfaConfig.state = "DISABLED";
765+
}
766+
if (!("enabledProviders" in mfaConfig)) {
767+
mfaConfig.enabledProviders = [];
768+
}
769+
759770
// Default to production defaults if unset
760771
this._tenantConfig = {
761772
tenantId: this.tenantId,
762773
name: this.tenantConfig.name,
763774
allowPasswordSignup: update.allowPasswordSignup ?? false,
764775
disableAuth: update.disableAuth ?? false,
765-
mfaConfig: update.mfaConfig ?? {},
776+
mfaConfig: mfaConfig as MfaConfig,
766777
enableAnonymousUser: update.enableAnonymousUser ?? false,
767778
enableEmailLinkSignin: update.enableEmailLinkSignin ?? false,
768779
displayName: update.displayName,
@@ -830,14 +841,17 @@ export type UserInfo = Omit<
830841
localId: string;
831842
providerUserInfo?: ProviderUserInfo[];
832843
};
833-
export type Tenant = MakeRequired<
834-
Omit<Schemas["GoogleCloudIdentitytoolkitAdminV2Tenant"], "testPhoneNumbers">,
835-
| "allowPasswordSignup"
836-
| "disableAuth"
837-
| "mfaConfig"
838-
| "enableAnonymousUser"
839-
| "enableEmailLinkSignin"
840-
> & { tenantId: string };
844+
export type MfaConfig = MakeRequired<
845+
Schemas["GoogleCloudIdentitytoolkitAdminV2MultiFactorAuthConfig"],
846+
"enabledProviders" | "state"
847+
>;
848+
export type Tenant = Omit<
849+
MakeRequired<
850+
Schemas["GoogleCloudIdentitytoolkitAdminV2Tenant"],
851+
"allowPasswordSignup" | "disableAuth" | "enableAnonymousUser" | "enableEmailLinkSignin"
852+
>,
853+
"testPhoneNumbers" | "mfaConfig"
854+
> & { tenantId: string; mfaConfig: MfaConfig };
841855

842856
interface RefreshTokenRecord {
843857
localId: string;

src/test/emulators/auth/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { expect, AssertionError } from "chai";
55
import { IdpJwtPayload } from "../../../emulator/auth/operations";
66
import { OobRecord, PhoneVerificationRecord, Tenant, UserInfo } from "../../../emulator/auth/state";
77
import { TestAgent, PROJECT_ID } from "./setup";
8-
import { MfaEnrollments } from "../../../emulator/auth/types";
8+
import { MfaEnrollments, Schemas } from "../../../emulator/auth/types";
99

1010
export { PROJECT_ID };
1111
export const TEST_PHONE_NUMBER = "+15555550100";
@@ -407,7 +407,7 @@ export function deleteAccount(testAgent: TestAgent, reqBody: {}): Promise<string
407407
export function registerTenant(
408408
testAgent: TestAgent,
409409
projectId: string,
410-
tenant?: Partial<Tenant>
410+
tenant?: Schemas["GoogleCloudIdentitytoolkitAdminV2Tenant"]
411411
): Promise<Tenant> {
412412
return testAgent
413413
.post(`/identitytoolkit.googleapis.com/v2/projects/${projectId}/tenants`)

src/test/emulators/auth/tenant.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describeAuthEmulator("tenant management", ({ authApi }) => {
5353
expect(res.body.enableEmailLinkSignin).to.be.false;
5454
expect(res.body.mfaConfig).to.eql({
5555
state: "DISABLED",
56+
enabledProviders: [],
5657
});
5758
});
5859
});
@@ -367,7 +368,10 @@ describeAuthEmulator("tenant management", ({ authApi }) => {
367368
expect(res.body.disableAuth).to.be.false;
368369
expect(res.body.enableAnonymousUser).to.be.false;
369370
expect(res.body.enableEmailLinkSignin).to.be.false;
370-
expect(res.body.mfaConfig).to.eql({});
371+
expect(res.body.mfaConfig).to.eql({
372+
enabledProviders: [],
373+
state: "DISABLED",
374+
});
371375
});
372376
});
373377
});

0 commit comments

Comments
 (0)