From bd7f98e6112f2b7e2610adb6620ea4404b106b4f Mon Sep 17 00:00:00 2001 From: Christopher Suh Date: Mon, 18 Sep 2023 11:40:58 -0700 Subject: [PATCH 1/3] change to axios call --- package.json | 1 + src/azure/msal/msalAzureAuth.ts | 36 +++++++++++++++++++++------ src/azure/msal/msalAzureController.ts | 2 +- src/models/logger.ts | 2 +- yarn.lock | 13 ++++++++++ 5 files changed, 44 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 39082cd35b..ed8303f3cf 100644 --- a/package.json +++ b/package.json @@ -116,6 +116,7 @@ "@azure/msal-common": "^11.0.0", "@azure/msal-node": "^1.16.0", "@microsoft/ads-extension-telemetry": "^3.0.2", + "axios": "^0.27.2", "http-proxy-agent": "5.0.0", "https-proxy-agent": "5.0.0", "core-js": "^2.4.1", diff --git a/src/azure/msal/msalAzureAuth.ts b/src/azure/msal/msalAzureAuth.ts index 9ab6933701..4fc788ce59 100644 --- a/src/azure/msal/msalAzureAuth.ts +++ b/src/azure/msal/msalAzureAuth.ts @@ -17,6 +17,13 @@ import { AzureAuthError } from '../azureAuthError'; import * as Constants from '../constants'; import * as azureUtils from '../utils'; import { HttpClient } from './httpClient'; +import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'; +import { ErrorResponseBody } from '@azure/arm-subscriptions'; + +export type GetTenantsResponseData = { + value: ITenantResponse[]; +} +export type ErrorResponseBodyWithError = Required; // tslint:disable:no-null-keyword export abstract class MsalAzureAuth { @@ -241,14 +248,9 @@ export abstract class MsalAzureAuth { try { this.logger.verbose('Fetching tenants with uri {0}', tenantUri); let tenantList: string[] = []; - const tenantResponse = await this.httpClient.sendGetRequestAsync(tenantUri, { - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${token}` - } - }); - const data = tenantResponse.body; - if (data.error) { + const tenantResponse = await this.makeGetRequest(tenantUri, token); + const data = tenantResponse.data; + if (this.isErrorResponseBodyWithError(data)) { this.logger.error(`Error fetching tenants :${data.error.code} - ${data.error.message}`); throw new Error(`${data.error.code} - ${data.error.message}`); } @@ -281,6 +283,24 @@ export abstract class MsalAzureAuth { } } + private isErrorResponseBodyWithError(body: any): body is ErrorResponseBodyWithError { + return 'error' in body && body.error; + } + + private async makeGetRequest(url: string, token: string): Promise> { + const config: AxiosRequestConfig = { + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}` + }, + validateStatus: () => true // Never throw + }; + + const response: AxiosResponse = await axios.get(url, config); + this.logger.piiSanitized('GET request ', [{ name: 'response', objOrArray: response.data?.value as ITenantResponse[] ?? response.data as GetTenantsResponseData }], [], url,); + return response; + } + //#region interaction handling public async handleInteractionRequired(tenant: ITenant, settings: IAADResource, promptUser: boolean = true): Promise { let shouldOpen: boolean; diff --git a/src/azure/msal/msalAzureController.ts b/src/azure/msal/msalAzureController.ts index 614a6ef910..d66785c312 100644 --- a/src/azure/msal/msalAzureController.ts +++ b/src/azure/msal/msalAzureController.ts @@ -105,7 +105,7 @@ export class MsalAzureController extends AzureController { public async getAccountSecurityToken(account: IAccount, tenantId: string, settings: IAADResource): Promise { let azureAuth = await this.getAzureAuthInstance(getAzureActiveDirectoryConfig()); if (azureAuth) { - this.logger.piiSantized(`Getting account security token for ${JSON.stringify(account?.key)} (tenant ${tenantId}). Auth Method = ${AzureAuthType[account?.properties.azureAuthType]}`, [], []); + this.logger.piiSanitized(`Getting account security token for ${JSON.stringify(account?.key)} (tenant ${tenantId}). Auth Method = ${AzureAuthType[account?.properties.azureAuthType]}`, [], []); tenantId = tenantId || account.properties.owningTenant.id; let result = await azureAuth.getToken(account, tenantId, settings); if (!result || !result.account || !result.account.idTokenClaims) { diff --git a/src/models/logger.ts b/src/models/logger.ts index e867ffdffe..b5cbcb41a5 100644 --- a/src/models/logger.ts +++ b/src/models/logger.ts @@ -44,7 +44,7 @@ export class Logger implements ILogger { * @param stringsToShorten Set of strings to shorten * @param vals Any other values to add on to the end of the log message */ - public piiSantized(msg: any, objsToSanitize: { name: string, objOrArray: any | any[] }[], + public piiSanitized(msg: any, objsToSanitize: { name: string, objOrArray: any | any[] }[], stringsToShorten: { name: string, value: string }[], ...vals: any[]): void { if (this.piiLogging) { msg = [ diff --git a/yarn.lock b/yarn.lock index efa3d87942..bdc787af0c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -731,6 +731,14 @@ aws4@^1.8.0: resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.11.0.tgz#d61f46d83b2519250e2784daf5b09479a8b41c59" integrity sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA== +axios@^0.27.2: + version "0.27.2" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972" + integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ== + dependencies: + follow-redirects "^1.14.9" + form-data "^4.0.0" + babel-code-frame@^6.26.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b" @@ -2167,6 +2175,11 @@ fmerge@1.2.0: resolved "https://registry.yarnpkg.com/fmerge/-/fmerge-1.2.0.tgz#36e99d2ae255e3ee1af666b4df780553671cf692" integrity sha1-NumdKuJV4+4a9ma033gFU2cc9pI= +follow-redirects@^1.14.9: + version "1.15.2" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13" + integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA== + for-in@^1.0.1, for-in@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80" From 86d0c52f784f52b78d84e0bb2c752b6efb656b12 Mon Sep 17 00:00:00 2001 From: Christopher Suh Date: Mon, 18 Sep 2023 12:30:53 -0700 Subject: [PATCH 2/3] fix lint error --- src/azure/msal/msalAzureAuth.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/azure/msal/msalAzureAuth.ts b/src/azure/msal/msalAzureAuth.ts index 4fc788ce59..f9701e1abb 100644 --- a/src/azure/msal/msalAzureAuth.ts +++ b/src/azure/msal/msalAzureAuth.ts @@ -22,7 +22,7 @@ import { ErrorResponseBody } from '@azure/arm-subscriptions'; export type GetTenantsResponseData = { value: ITenantResponse[]; -} +}; export type ErrorResponseBodyWithError = Required; // tslint:disable:no-null-keyword From 86c59366229cb413ec3c40032b9502d0a7d70b6b Mon Sep 17 00:00:00 2001 From: Christopher Suh Date: Mon, 18 Sep 2023 14:51:35 -0700 Subject: [PATCH 3/3] fix linter --- src/azure/msal/msalAzureAuth.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/azure/msal/msalAzureAuth.ts b/src/azure/msal/msalAzureAuth.ts index f9701e1abb..5d2896b3ed 100644 --- a/src/azure/msal/msalAzureAuth.ts +++ b/src/azure/msal/msalAzureAuth.ts @@ -287,7 +287,7 @@ export abstract class MsalAzureAuth { return 'error' in body && body.error; } - private async makeGetRequest(url: string, token: string): Promise> { + private async makeGetRequest(uri: string, token: string): Promise> { const config: AxiosRequestConfig = { headers: { 'Content-Type': 'application/json', @@ -296,8 +296,8 @@ export abstract class MsalAzureAuth { validateStatus: () => true // Never throw }; - const response: AxiosResponse = await axios.get(url, config); - this.logger.piiSanitized('GET request ', [{ name: 'response', objOrArray: response.data?.value as ITenantResponse[] ?? response.data as GetTenantsResponseData }], [], url,); + const response: AxiosResponse = await axios.get(uri, config); + this.logger.piiSanitized('GET request ', [{ name: 'response', objOrArray: response.data?.value as ITenantResponse[] ?? response.data as GetTenantsResponseData }], [], uri); return response; }