Skip to content

Commit 4efa278

Browse files
authored
Merge pull request #861 from Timothyw0/main
fix (#859): Add check for azureProfile.json to prevent double login
2 parents 81adcab + 6eabbcd commit 4efa278

4 files changed

Lines changed: 54 additions & 2 deletions

File tree

src/cli/commands/login/login.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { existsSync, promises as fs } from "node:fs";
55
import path from "node:path";
66
import { logger, logGitHubIssueMessageAndExit } from "../../../core/utils/logger.js";
77
import { authenticateWithAzureIdentity, listSubscriptions, listTenants } from "../../../core/account.js";
8-
import { ENV_FILENAME } from "../../../core/constants.js";
8+
import { AZURE_LOGIN_CONFIG, ENV_FILENAME } from "../../../core/constants.js";
9+
import { pathExists, safeReadJson } from "../../../core/utils/file.js";
910
import { updateGitIgnore } from "../../../core/git.js";
1011
import { chooseSubscription, chooseTenant } from "../../../core/prompts.js";
1112
import { Environment } from "../../../core/swa-cli-persistence-plugin/impl/azure-environment.js";
@@ -43,6 +44,7 @@ export async function login(options: SWACLIConfig): Promise<any> {
4344
logger.log(chalk.green(`✔ Successfully logged into Azure!`));
4445
}
4546

47+
options = await tryGetAzTenantAndSubscription(options);
4648
return await setupProjectCredentials(options, credentialChain);
4749
}
4850

@@ -149,3 +151,24 @@ async function storeProjectCredentialsInEnvFile(
149151
await updateGitIgnore(ENV_FILENAME);
150152
}
151153
}
154+
155+
async function tryGetAzTenantAndSubscription(options: SWACLIConfig) {
156+
const doesAzureConfigExist = await pathExists(AZURE_LOGIN_CONFIG);
157+
if (!doesAzureConfigExist) {
158+
return options;
159+
} else {
160+
logger.silly(`Found an existing Azure config file, getting Tenant and Subscription Id from ${AZURE_LOGIN_CONFIG}`);
161+
162+
const azureProfile = await safeReadJson(AZURE_LOGIN_CONFIG);
163+
if (azureProfile) {
164+
const allSubscriptions = (azureProfile as AzureProfile).subscriptions;
165+
const defaultAzureInfo = allSubscriptions.find((subscription) => subscription.isDefault == true);
166+
if (defaultAzureInfo) {
167+
options.tenantId = defaultAzureInfo.tenantId;
168+
options.subscriptionId = defaultAzureInfo.id;
169+
}
170+
}
171+
172+
return options;
173+
}
174+
}

src/core/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,8 @@ export function IS_DATA_API_DEV_SERVER() {
272272
export function SWA_CLI_DATA_API_URI() {
273273
return IS_DATA_API_DEV_SERVER() ? DEFAULT_CONFIG.dataApiLocation : address(DEFAULT_CONFIG.host, DEFAULT_CONFIG.dataApiPort, "http");
274274
}
275+
276+
// Constants related to swa login
277+
const azureFolderName = ".azure";
278+
const azureProfileFilename = "azureProfile.json";
279+
export const AZURE_LOGIN_CONFIG = path.join(os.homedir(), azureFolderName, azureProfileFilename);

src/core/utils/file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export async function safeReadJson(path: string): Promise<JsonData | undefined>
77
try {
88
let contents = await fs.readFile(path, "utf8");
99
contents = stripJsonComments(contents);
10-
return JSON.parse(contents) as JsonData;
10+
return JSON.parse(contents.trim()) as JsonData;
1111
} catch (error) {
1212
logger.warn(`Failed to read JSON file at: ${path}`);
1313
return undefined;

src/swa.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,27 @@ declare type RolesSourceClaim = {
407407
typ: string;
408408
val: string;
409409
};
410+
411+
declare type SubscriptionState = "Enabled" | "Warned" | "PastDue" | "Disabled" | "Deleted";
412+
413+
declare type AzureLoginInfo = {
414+
id: string;
415+
name: string;
416+
state: SubscriptionState;
417+
user: {
418+
name: string;
419+
type: string;
420+
};
421+
isDefault: boolean;
422+
tenantId: string;
423+
environmentName: string;
424+
homeTenantId: string;
425+
tenantDefaultDomain: string;
426+
tenantDisplayName: string;
427+
managedByTenants: string[];
428+
};
429+
430+
declare interface AzureProfile {
431+
installationId: string;
432+
subscriptions: AzureLoginInfo[];
433+
}

0 commit comments

Comments
 (0)