Skip to content

Commit aa1fe7a

Browse files
authored
Merge pull request #14271 from HuihuiWu-Microsoft/fix-m365-tenant-not-match
fix: instruct user to switch account when meet m365 tenant id mismatc…
2 parents 7cc88a2 + b74bdeb commit aa1fe7a

File tree

6 files changed

+373
-5
lines changed

6 files changed

+373
-5
lines changed

packages/api/src/utils/login.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ export interface M365TokenProvider {
183183
* @param tokenRequest permission scopes or show user interactive UX
184184
*/
185185
getStatus(tokenRequest: TokenRequest): Promise<Result<LoginStatus, FxError>>;
186+
187+
/**
188+
* Sign out from current M365 account
189+
*/
190+
signout(): Promise<boolean>;
186191
/**
187192
* Switch to specified tenant for current user account
188193
* @param tenantId id of tenant that user wants to switch to

packages/api/tests/login.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class M365Provider extends BasicLogin implements M365TokenProvider {
2424
async switchTenant(tenantId: string): Promise<Result<string, FxError>> {
2525
return ok("fakeToken");
2626
}
27+
28+
async signout(): Promise<boolean> {
29+
return true;
30+
}
2731
}
2832

2933
describe("m365Login", function () {

packages/cli/src/commonlib/m365LoginUserPassword.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ export class M365ProviderUserPassword extends BasicLogin implements M365TokenPro
133133
throw new Error("Method not implemented.");
134134
}
135135

136-
signout(): boolean {
137-
return true;
136+
signout(): Promise<boolean> {
137+
return Promise.resolve(true);
138138
}
139139

140140
switchTenant(tenantId: string): Promise<Result<string, FxError>> {

packages/fx-core/resource/package.nls.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"core.provision.confirmEnvAndCostNotice": "Costs may apply based on usage. Do you want to provision resources in %s environment using listed accounts?",
1717
"core.deploy.confirmEnvNoticeV3": "Do you want to deploy resources in %s environment?",
1818
"core.provision.viewResources": "View provisioned resources",
19+
"core.provision.switchAccount": "You're signed in with a Microsoft 365 account that doesn't match this environment. Please sign out and sign in with the correct one.",
20+
"core.provision.switchAccount.continue": "Continue",
1921
"core.deploy.aadManifestSuccessNotice": "Your Microsoft Entra app has been deployed successfully. To view that, click \"More info\"",
2022
"core.deploy.aadManifestOnCLISuccessNotice": "Your Microsoft Entra app has been updated successfully.",
2123
"core.deploy.aadManifestLearnMore": "More info",

packages/fx-core/src/component/coordinator/index.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
InputValidationError,
3737
MissingEnvironmentVariablesError,
3838
MissingRequiredInputError,
39+
UserCancelError,
3940
assembleError,
4041
} from "../../error/common";
4142
import { LifeCycleUndefinedError } from "../../error/yml";
@@ -439,10 +440,32 @@ class Coordinator {
439440

440441
const checkM365TenatRes = provisionUtils.ensureM365TenantMatchesV3(
441442
tenantSwitchCheckActions,
442-
m365tenantInfo?.tenantIdInToken
443+
m365tenantInfo.tenantIdInToken
443444
);
444445
if (checkM365TenatRes.isErr()) {
445-
return err(checkM365TenatRes.error);
446+
const msg = getLocalizedString("core.provision.switchAccount");
447+
const continueItem = getLocalizedString("core.provision.switchAccount.continue");
448+
const userCofirmRes = await ctx.ui?.showMessage("warn", msg, true, continueItem);
449+
if (userCofirmRes?.isOk() && userCofirmRes.value === continueItem) {
450+
await ctx.m365TokenProvider.signout();
451+
452+
const tenantInfoInTokenRes1 = await provisionUtils.getM365TenantId(ctx.m365TokenProvider);
453+
if (tenantInfoInTokenRes1.isErr()) {
454+
return err(tenantInfoInTokenRes1.error);
455+
}
456+
m365tenantInfo = tenantInfoInTokenRes1.value;
457+
458+
const checkM365TenatRes1 = provisionUtils.ensureM365TenantMatchesV3(
459+
tenantSwitchCheckActions,
460+
m365tenantInfo.tenantIdInToken
461+
);
462+
463+
if (checkM365TenatRes1.isErr()) {
464+
return err(checkM365TenatRes1.error);
465+
}
466+
} else {
467+
return err(new UserCancelError("coordinator"));
468+
}
446469
}
447470
}
448471

0 commit comments

Comments
 (0)