Skip to content

Commit 296ad27

Browse files
committed
import classes explcitely from msal-node instead of importing everything
This is part of trying to make auth flows more treeshakable. This change doesn't bring any direct benefits, because the same imports are still happening in other places
1 parent 350c2f6 commit 296ad27

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

sdk/identity/identity/src/msal/msal.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

sdk/identity/identity/src/msal/nodeFlows/msalClient.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
import * as msal from "@azure/msal-node";
4+
import { ConfidentialClientApplication, PublicClientApplication } from "@azure/msal-node";
5+
import type {
6+
AccountInfo,
7+
AuthenticationResult,
8+
Configuration,
9+
DeviceCodeRequest,
10+
InteractiveRequest,
11+
SilentFlowRequest,
12+
UsernamePasswordRequest,
13+
} from "@azure/msal-node";
514

615
import type { AccessToken, GetTokenOptions } from "@azure/core-auth";
716
import type { AuthenticationRecord, CertificateParts } from "../types.js";
@@ -29,8 +38,8 @@ import type { TokenCachePersistenceOptions } from "./tokenCachePersistenceOption
2938
import { calculateRegionalAuthority } from "../../regionalAuthority.js";
3039
import { getLogLevel } from "@azure/logger";
3140
import { resolveTenantId } from "../../util/tenantIdUtils.js";
32-
import { CommonClientOptions } from "@azure/core-client";
33-
import { LogPolicyOptions } from "@azure/core-rest-pipeline";
41+
import type { CommonClientOptions } from "@azure/core-client";
42+
import type { LogPolicyOptions } from "@azure/core-rest-pipeline";
3443
import { getAuthorityHost } from "../../util/authorityHost.js";
3544

3645
/**
@@ -280,7 +289,7 @@ export function generateMsalConfiguration(
280289
clientId: string,
281290
tenantId: string,
282291
msalClientOptions: MsalClientOptions = {},
283-
): msal.Configuration {
292+
): Configuration {
284293
const resolvedTenant = resolveTenantId(
285294
msalClientOptions.logger ?? msalLogger,
286295
tenantId,
@@ -295,7 +304,7 @@ export function generateMsalConfiguration(
295304
loggingOptions: msalClientOptions.loggingOptions,
296305
});
297306

298-
const msalConfig: msal.Configuration = {
307+
const msalConfig: Configuration = {
299308
auth: {
300309
clientId,
301310
authority,
@@ -323,10 +332,10 @@ export function generateMsalConfiguration(
323332
*/
324333
interface MsalClientState {
325334
/** The configuration for the MSAL client. */
326-
msalConfig: msal.Configuration;
335+
msalConfig: Configuration;
327336

328337
/** The cached account information, or null if no account information is cached. */
329-
cachedAccount: msal.AccountInfo | null;
338+
cachedAccount: AccountInfo | null;
330339

331340
/** Configured plugins */
332341
pluginConfiguration: PluginConfiguration;
@@ -362,10 +371,10 @@ export function createMsalClient(
362371
logger: createMsalClientOptions.logger ?? msalLogger,
363372
};
364373

365-
const publicApps: Map<string, msal.PublicClientApplication> = new Map();
374+
const publicApps: Map<string, PublicClientApplication> = new Map();
366375
async function getPublicApp(
367376
options: GetTokenOptions = {},
368-
): Promise<msal.PublicClientApplication> {
377+
): Promise<PublicClientApplication> {
369378
const appKey = options.enableCae ? "CAE" : "default";
370379

371380
let publicClientApp = publicApps.get(appKey);
@@ -385,7 +394,7 @@ export function createMsalClient(
385394

386395
state.msalConfig.auth.clientCapabilities = options.enableCae ? ["cp1"] : undefined;
387396

388-
publicClientApp = new msal.PublicClientApplication({
397+
publicClientApp = new PublicClientApplication({
389398
...state.msalConfig,
390399
broker: { nativeBrokerPlugin: state.pluginConfiguration.broker.nativeBrokerPlugin },
391400
cache: { cachePlugin: await cachePlugin },
@@ -396,10 +405,10 @@ export function createMsalClient(
396405
return publicClientApp;
397406
}
398407

399-
const confidentialApps: Map<string, msal.ConfidentialClientApplication> = new Map();
408+
const confidentialApps: Map<string, ConfidentialClientApplication> = new Map();
400409
async function getConfidentialApp(
401410
options: GetTokenOptions = {},
402-
): Promise<msal.ConfidentialClientApplication> {
411+
): Promise<ConfidentialClientApplication> {
403412
const appKey = options.enableCae ? "CAE" : "default";
404413

405414
let confidentialClientApp = confidentialApps.get(appKey);
@@ -423,7 +432,7 @@ export function createMsalClient(
423432

424433
state.msalConfig.auth.clientCapabilities = options.enableCae ? ["cp1"] : undefined;
425434

426-
confidentialClientApp = new msal.ConfidentialClientApplication({
435+
confidentialClientApp = new ConfidentialClientApplication({
427436
...state.msalConfig,
428437
broker: { nativeBrokerPlugin: state.pluginConfiguration.broker.nativeBrokerPlugin },
429438
cache: { cachePlugin: await cachePlugin },
@@ -435,10 +444,10 @@ export function createMsalClient(
435444
}
436445

437446
async function getTokenSilent(
438-
app: msal.ConfidentialClientApplication | msal.PublicClientApplication,
447+
app: ConfidentialClientApplication | PublicClientApplication,
439448
scopes: string[],
440449
options: GetTokenOptions = {},
441-
): Promise<msal.AuthenticationResult> {
450+
): Promise<AuthenticationResult> {
442451
if (state.cachedAccount === null) {
443452
state.logger.getToken.info("No cached account found in local state.");
444453
throw new AuthenticationRequiredError({ scopes });
@@ -449,7 +458,7 @@ export function createMsalClient(
449458
state.cachedClaims = options.claims;
450459
}
451460

452-
const silentRequest: msal.SilentFlowRequest = {
461+
const silentRequest: SilentFlowRequest = {
453462
account: state.cachedAccount,
454463
scopes,
455464
claims: state.cachedClaims,
@@ -498,12 +507,12 @@ export function createMsalClient(
498507
* @returns A promise that resolves to an AccessToken object containing the access token and its expiration timestamp.
499508
*/
500509
async function withSilentAuthentication(
501-
msalApp: msal.ConfidentialClientApplication | msal.PublicClientApplication,
510+
msalApp: ConfidentialClientApplication | PublicClientApplication,
502511
scopes: Array<string>,
503512
options: GetTokenWithSilentAuthOptions,
504-
onAuthenticationRequired: () => Promise<msal.AuthenticationResult | null>,
513+
onAuthenticationRequired: () => Promise<AuthenticationResult | null>,
505514
): Promise<AccessToken> {
506-
let response: msal.AuthenticationResult | null = null;
515+
let response: AuthenticationResult | null = null;
507516
try {
508517
response = await getTokenSilent(msalApp, scopes, options);
509518
} catch (e: any) {
@@ -647,7 +656,7 @@ export function createMsalClient(
647656
const msalApp = await getPublicApp(options);
648657

649658
return withSilentAuthentication(msalApp, scopes, options, () => {
650-
const requestOptions: msal.DeviceCodeRequest = {
659+
const requestOptions: DeviceCodeRequest = {
651660
scopes,
652661
cancel: options?.abortSignal?.aborted ?? false,
653662
deviceCodeCallback,
@@ -676,7 +685,7 @@ export function createMsalClient(
676685
const msalApp = await getPublicApp(options);
677686

678687
return withSilentAuthentication(msalApp, scopes, options, () => {
679-
const requestOptions: msal.UsernamePasswordRequest = {
688+
const requestOptions: UsernamePasswordRequest = {
680689
scopes,
681690
username,
682691
password,
@@ -704,7 +713,7 @@ export function createMsalClient(
704713
): Promise<AccessToken> {
705714
state.logger.getToken.info(`Attempting to acquire token using authorization code`);
706715

707-
let msalApp: msal.ConfidentialClientApplication | msal.PublicClientApplication;
716+
let msalApp: ConfidentialClientApplication | PublicClientApplication;
708717
if (clientSecret) {
709718
// If a client secret is provided, we need to use a confidential client application
710719
// See https://learn.microsoft.com/entra/identity-platform/v2-oauth2-auth-code-flow#request-an-access-token-with-a-client_secret
@@ -776,7 +785,7 @@ export function createMsalClient(
776785
function createBaseInteractiveRequest(
777786
scopes: string[],
778787
options: GetTokenInteractiveOptions,
779-
): msal.InteractiveRequest {
788+
): InteractiveRequest {
780789
return {
781790
openBrowser: async (url) => {
782791
const open = await import("open");
@@ -799,7 +808,7 @@ export function createMsalClient(
799808
scopes: string[],
800809
useDefaultBrokerAccount: boolean,
801810
options: GetTokenInteractiveOptions = {},
802-
): Promise<msal.AuthenticationResult> {
811+
): Promise<AuthenticationResult> {
803812
msalLogger.verbose("Authentication will resume through the broker");
804813

805814
const app = await getPublicApp(options);

0 commit comments

Comments
 (0)