Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change HttpClient to axios call #17809

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 28 additions & 8 deletions src/azure/msal/msalAzureAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ErrorResponseBody>;

// tslint:disable:no-null-keyword
export abstract class MsalAzureAuth {
Expand Down Expand Up @@ -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<any>(tenantUri, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
});
const data = tenantResponse.body;
if (data.error) {
const tenantResponse = await this.makeGetRequest<GetTenantsResponseData>(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}`);
}
Expand Down Expand Up @@ -281,6 +283,24 @@ export abstract class MsalAzureAuth {
}
}

private isErrorResponseBodyWithError(body: any): body is ErrorResponseBodyWithError {
return 'error' in body && body.error;
}

private async makeGetRequest<T>(uri: string, token: string): Promise<AxiosResponse<T>> {
const config: AxiosRequestConfig = {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
validateStatus: () => true // Never throw
};

const response: AxiosResponse = await axios.get<T>(uri, config);
this.logger.piiSanitized('GET request ', [{ name: 'response', objOrArray: response.data?.value as ITenantResponse[] ?? response.data as GetTenantsResponseData }], [], uri);
return response;
}

//#region interaction handling
public async handleInteractionRequired(tenant: ITenant, settings: IAADResource, promptUser: boolean = true): Promise<AuthenticationResult | null> {
let shouldOpen: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/azure/msal/msalAzureController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class MsalAzureController extends AzureController {
public async getAccountSecurityToken(account: IAccount, tenantId: string, settings: IAADResource): Promise<IToken | undefined> {
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) {
Expand Down
2 changes: 1 addition & 1 deletion src/models/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down