Skip to content

Commit 04f0175

Browse files
committed
refactor msalClient to export a context and flows directly
Exporting one client with all flows in it as a function makes it inherently not tree shakable increasing bundle size. This commit splits all those flows into functions taking the context as a parameter. This resulted in a bundle size reduction from 790kb -> 753kb on an artifical test bundle with only ClientCertificateCredential
1 parent 296ad27 commit 04f0175

14 files changed

+689
-628
lines changed

sdk/identity/identity/src/credentials/authorizationCodeCredential.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import { checkTenantId } from "../util/tenantIdUtils.js";
1111
import { credentialLogger } from "../util/logging.js";
1212
import { ensureScopes } from "../util/scopeUtils.js";
1313
import { tracingClient } from "../util/tracing.js";
14-
import type { MsalClient } from "../msal/nodeFlows/msalClient.js";
15-
import { createMsalClient } from "../msal/nodeFlows/msalClient.js";
14+
import {
15+
createMsalClientContext,
16+
getTokenByAuthorizationCode,
17+
type MsalClientContext,
18+
} from "../msal/nodeFlows/msalClient.js";
1619

1720
const logger = credentialLogger("AuthorizationCodeCredential");
1821

@@ -24,7 +27,7 @@ const logger = credentialLogger("AuthorizationCodeCredential");
2427
* https://learn.microsoft.com/entra/identity-platform/v2-oauth2-auth-code-flow
2528
*/
2629
export class AuthorizationCodeCredential implements TokenCredential {
27-
private msalClient: MsalClient;
30+
private msalContext: MsalClientContext;
2831
private disableAutomaticAuthentication?: boolean;
2932
private authorizationCode: string;
3033
private redirectUri: string;
@@ -124,7 +127,7 @@ export class AuthorizationCodeCredential implements TokenCredential {
124127
options?.additionallyAllowedTenants,
125128
);
126129

127-
this.msalClient = createMsalClient(clientId, tenantId, {
130+
this.msalContext = createMsalClientContext(clientId, tenantId, {
128131
...options,
129132
logger,
130133
});
@@ -151,7 +154,8 @@ export class AuthorizationCodeCredential implements TokenCredential {
151154
newOptions.tenantId = tenantId;
152155

153156
const arrayScopes = ensureScopes(scopes);
154-
return this.msalClient.getTokenByAuthorizationCode(
157+
return getTokenByAuthorizationCode(
158+
this.msalContext,
155159
arrayScopes,
156160
this.redirectUri,
157161
this.authorizationCode,

sdk/identity/identity/src/credentials/brokerCredential.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ import {
1111
import { credentialLogger, formatError } from "../util/logging.js";
1212
import { ensureScopes } from "../util/scopeUtils.js";
1313
import { tracingClient } from "../util/tracing.js";
14-
import type { MsalClient, MsalClientOptions } from "../msal/nodeFlows/msalClient.js";
15-
import { createMsalClient } from "../msal/nodeFlows/msalClient.js";
14+
import {
15+
createMsalClientContext,
16+
getBrokeredToken,
17+
type MsalClientContext,
18+
type MsalClientOptions,
19+
} from "../msal/nodeFlows/msalClient.js";
1620
import { DeveloperSignOnClientId } from "../constants.js";
1721
import type { TokenCredentialOptions } from "../tokenCredentialOptions.js";
1822
import type { MultiTenantTokenCredentialOptions } from "./multiTenantTokenCredentialOptions.js";
@@ -25,7 +29,7 @@ const logger = credentialLogger("BrokerCredential");
2529
* This credential uses the default account logged into the OS via a broker.
2630
*/
2731
export class BrokerCredential implements TokenCredential {
28-
private brokerMsalClient: MsalClient;
32+
private brokerMsalContext: MsalClientContext;
2933
private brokerTenantId?: string;
3034
private brokerAdditionallyAllowedTenantIds: string[];
3135

@@ -54,7 +58,7 @@ export class BrokerCredential implements TokenCredential {
5458
},
5559
};
5660

57-
this.brokerMsalClient = createMsalClient(
61+
this.brokerMsalContext = createMsalClientContext(
5862
DeveloperSignOnClientId,
5963
this.brokerTenantId,
6064
msalClientOptions,
@@ -85,7 +89,7 @@ export class BrokerCredential implements TokenCredential {
8589

8690
const arrayScopes = ensureScopes(scopes);
8791
try {
88-
return this.brokerMsalClient.getBrokeredToken(arrayScopes, true, {
92+
return getBrokeredToken(this.brokerMsalContext, arrayScopes, true, {
8993
...newOptions,
9094
disableAutomaticAuthentication: true,
9195
});

sdk/identity/identity/src/credentials/clientAssertionCredential.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
// Licensed under the MIT License.
33

44
import type { AccessToken, GetTokenOptions, TokenCredential } from "@azure/core-auth";
5-
import type { MsalClient } from "../msal/nodeFlows/msalClient.js";
6-
import { createMsalClient } from "../msal/nodeFlows/msalClient.js";
5+
import {
6+
createMsalClientContext,
7+
getTokenByClientAssertion,
8+
type MsalClientContext,
9+
} from "../msal/nodeFlows/msalClient.js";
710
import {
811
processMultiTenantRequest,
912
resolveAdditionallyAllowedTenantIds,
@@ -20,7 +23,7 @@ const logger = credentialLogger("ClientAssertionCredential");
2023
* Authenticates a service principal with a JWT assertion.
2124
*/
2225
export class ClientAssertionCredential implements TokenCredential {
23-
private msalClient: MsalClient;
26+
private msalContext: MsalClientContext;
2427
private tenantId: string;
2528
private additionallyAllowedTenantIds: string[];
2629
private getAssertion: () => Promise<string>;
@@ -66,7 +69,7 @@ export class ClientAssertionCredential implements TokenCredential {
6669

6770
this.options = options;
6871
this.getAssertion = getAssertion;
69-
this.msalClient = createMsalClient(clientId, tenantId, {
72+
this.msalContext = createMsalClientContext(clientId, tenantId, {
7073
...this.options,
7174
logger,
7275
});
@@ -93,7 +96,8 @@ export class ClientAssertionCredential implements TokenCredential {
9396
);
9497

9598
const arrayScopes = Array.isArray(scopes) ? scopes : [scopes];
96-
return this.msalClient.getTokenByClientAssertion(
99+
return getTokenByClientAssertion(
100+
this.msalContext,
97101
arrayScopes,
98102
this.getAssertion,
99103
newOptions,

sdk/identity/identity/src/credentials/clientCertificateCredential.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
// Licensed under the MIT License.
33

44
import type { AccessToken, GetTokenOptions, TokenCredential } from "@azure/core-auth";
5-
import type { MsalClient } from "../msal/nodeFlows/msalClient.js";
6-
import { createMsalClient } from "../msal/nodeFlows/msalClient.js";
5+
import {
6+
createMsalClientContext,
7+
getTokenByClientCertificate,
8+
type MsalClientContext,
9+
} from "../msal/nodeFlows/msalClient.js";
710
import { createHash, createPrivateKey } from "node:crypto";
811
import {
912
processMultiTenantRequest,
@@ -38,7 +41,7 @@ export class ClientCertificateCredential implements TokenCredential {
3841
private additionallyAllowedTenantIds: string[];
3942
private certificateConfiguration: ClientCertificateCredentialPEMConfiguration;
4043
private sendCertificateChain?: boolean;
41-
private msalClient: MsalClient;
44+
private msalContext: MsalClientContext;
4245

4346
/**
4447
* Creates an instance of the ClientCertificateCredential with the details
@@ -126,7 +129,7 @@ export class ClientCertificateCredential implements TokenCredential {
126129
`${credentialName}: To avoid unexpected behaviors, providing both the contents of a PEM certificate and the path to a PEM certificate is forbidden. To troubleshoot, visit https://aka.ms/azsdk/js/identity/serviceprincipalauthentication/troubleshoot.`,
127130
);
128131
}
129-
this.msalClient = createMsalClient(clientId, tenantId, {
132+
this.msalContext = createMsalClientContext(clientId, tenantId, {
130133
...options,
131134
logger,
132135
});
@@ -151,7 +154,7 @@ export class ClientCertificateCredential implements TokenCredential {
151154

152155
const arrayScopes = Array.isArray(scopes) ? scopes : [scopes];
153156
const certificate = await this.buildClientCertificate();
154-
return this.msalClient.getTokenByClientCertificate(arrayScopes, certificate, newOptions);
157+
return getTokenByClientCertificate(this.msalContext, arrayScopes, certificate, newOptions);
155158
});
156159
}
157160

sdk/identity/identity/src/credentials/clientSecretCredential.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
// Licensed under the MIT License.
33

44
import type { AccessToken, GetTokenOptions, TokenCredential } from "@azure/core-auth";
5-
import type { MsalClient } from "../msal/nodeFlows/msalClient.js";
6-
import { createMsalClient } from "../msal/nodeFlows/msalClient.js";
5+
import {
6+
createMsalClientContext,
7+
getTokenByClientSecret,
8+
type MsalClientContext,
9+
} from "../msal/nodeFlows/msalClient.js";
710
import {
811
processMultiTenantRequest,
912
resolveAdditionallyAllowedTenantIds,
@@ -28,7 +31,7 @@ const logger = credentialLogger("ClientSecretCredential");
2831
export class ClientSecretCredential implements TokenCredential {
2932
private tenantId: string;
3033
private additionallyAllowedTenantIds: string[];
31-
private msalClient: MsalClient;
34+
private msalContext: MsalClientContext;
3235
private clientSecret: string;
3336

3437
/**
@@ -71,7 +74,7 @@ export class ClientSecretCredential implements TokenCredential {
7174
options?.additionallyAllowedTenants,
7275
);
7376

74-
this.msalClient = createMsalClient(clientId, tenantId, {
77+
this.msalContext = createMsalClientContext(clientId, tenantId, {
7578
...options,
7679
logger,
7780
});
@@ -98,7 +101,7 @@ export class ClientSecretCredential implements TokenCredential {
98101
);
99102

100103
const arrayScopes = ensureScopes(scopes);
101-
return this.msalClient.getTokenByClientSecret(arrayScopes, this.clientSecret, newOptions);
104+
return getTokenByClientSecret(this.msalContext, arrayScopes, this.clientSecret, newOptions);
102105
},
103106
);
104107
}

sdk/identity/identity/src/credentials/deviceCodeCredential.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// Licensed under the MIT License.
33

44
import type { AccessToken, GetTokenOptions, TokenCredential } from "@azure/core-auth";
5+
import {
6+
createMsalClientContext,
7+
getTokenByDeviceCode,
8+
type MsalClientContext,
9+
} from "../msal/nodeFlows/msalClient.js";
510
import {
611
processMultiTenantRequest,
712
resolveAdditionallyAllowedTenantIds,
@@ -16,8 +21,6 @@ import type { AuthenticationRecord } from "../msal/types.js";
1621
import { credentialLogger } from "../util/logging.js";
1722
import { ensureScopes } from "../util/scopeUtils.js";
1823
import { tracingClient } from "../util/tracing.js";
19-
import type { MsalClient } from "../msal/nodeFlows/msalClient.js";
20-
import { createMsalClient } from "../msal/nodeFlows/msalClient.js";
2124
import { DeveloperSignOnClientId } from "../constants.js";
2225

2326
const logger = credentialLogger("DeviceCodeCredential");
@@ -38,7 +41,7 @@ export class DeviceCodeCredential implements TokenCredential {
3841
private tenantId?: string;
3942
private additionallyAllowedTenantIds: string[];
4043
private disableAutomaticAuthentication?: boolean;
41-
private msalClient: MsalClient;
44+
private msalContext: MsalClientContext;
4245
private userPromptCallback: DeviceCodePromptCallback;
4346

4447
/**
@@ -71,7 +74,7 @@ export class DeviceCodeCredential implements TokenCredential {
7174
const clientId = options?.clientId ?? DeveloperSignOnClientId;
7275
const tenantId = resolveTenantId(logger, options?.tenantId, clientId);
7376
this.userPromptCallback = options?.userPromptCallback ?? defaultDeviceCodePromptCallback;
74-
this.msalClient = createMsalClient(clientId, tenantId, {
77+
this.msalContext = createMsalClientContext(clientId, tenantId, {
7578
...options,
7679
logger,
7780
});
@@ -103,7 +106,7 @@ export class DeviceCodeCredential implements TokenCredential {
103106
);
104107

105108
const arrayScopes = ensureScopes(scopes);
106-
return this.msalClient.getTokenByDeviceCode(arrayScopes, this.userPromptCallback, {
109+
return getTokenByDeviceCode(this.msalContext, arrayScopes, this.userPromptCallback, {
107110
...newOptions,
108111
disableAutomaticAuthentication: this.disableAutomaticAuthentication,
109112
});
@@ -130,11 +133,11 @@ export class DeviceCodeCredential implements TokenCredential {
130133
options,
131134
async (newOptions) => {
132135
const arrayScopes = Array.isArray(scopes) ? scopes : [scopes];
133-
await this.msalClient.getTokenByDeviceCode(arrayScopes, this.userPromptCallback, {
136+
await getTokenByDeviceCode(this.msalContext, arrayScopes, this.userPromptCallback, {
134137
...newOptions,
135138
disableAutomaticAuthentication: false, // this method should always allow user interaction
136139
});
137-
return this.msalClient.getActiveAccount();
140+
return this.msalContext.getActiveAccount();
138141
},
139142
);
140143
}

sdk/identity/identity/src/credentials/interactiveBrowserCredential.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ import type { AuthenticationRecord } from "../msal/types.js";
1616
import { credentialLogger } from "../util/logging.js";
1717
import { ensureScopes } from "../util/scopeUtils.js";
1818
import { tracingClient } from "../util/tracing.js";
19-
import type { MsalClient, MsalClientOptions } from "../msal/nodeFlows/msalClient.js";
20-
import { createMsalClient } from "../msal/nodeFlows/msalClient.js";
19+
import {
20+
createMsalClientContext,
21+
getTokenByInteractiveRequest,
22+
type MsalClientContext,
23+
type MsalClientOptions,
24+
} from "../msal/nodeFlows/msalClient.js";
2125
import { DeveloperSignOnClientId } from "../constants.js";
2226

2327
const logger = credentialLogger("InteractiveBrowserCredential");
@@ -29,7 +33,7 @@ const logger = credentialLogger("InteractiveBrowserCredential");
2933
export class InteractiveBrowserCredential implements TokenCredential {
3034
private tenantId?: string;
3135
private additionallyAllowedTenantIds: string[];
32-
private msalClient: MsalClient;
36+
private msalContext: MsalClientContext;
3337
private disableAutomaticAuthentication?: boolean;
3438
private browserCustomizationOptions: InteractiveBrowserCredentialNodeOptions["browserCustomizationOptions"];
3539
private loginHint?: string;
@@ -75,7 +79,7 @@ export class InteractiveBrowserCredential implements TokenCredential {
7579
};
7680
}
7781
}
78-
this.msalClient = createMsalClient(
82+
this.msalContext = createMsalClientContext(
7983
options.clientId ?? DeveloperSignOnClientId,
8084
this.tenantId,
8185
msalClientOptions,
@@ -108,7 +112,7 @@ export class InteractiveBrowserCredential implements TokenCredential {
108112
);
109113

110114
const arrayScopes = ensureScopes(scopes);
111-
return this.msalClient.getTokenByInteractiveRequest(arrayScopes, {
115+
return getTokenByInteractiveRequest(this.msalContext, arrayScopes, {
112116
...newOptions,
113117
disableAutomaticAuthentication: this.disableAutomaticAuthentication,
114118
browserCustomizationOptions: this.browserCustomizationOptions,
@@ -140,13 +144,13 @@ export class InteractiveBrowserCredential implements TokenCredential {
140144
options,
141145
async (newOptions) => {
142146
const arrayScopes = ensureScopes(scopes);
143-
await this.msalClient.getTokenByInteractiveRequest(arrayScopes, {
147+
await getTokenByInteractiveRequest(this.msalContext, arrayScopes, {
144148
...newOptions,
145149
disableAutomaticAuthentication: false, // this method should always allow user interaction
146150
browserCustomizationOptions: this.browserCustomizationOptions,
147151
loginHint: this.loginHint,
148152
});
149-
return this.msalClient.getActiveAccount();
153+
return this.msalContext.getActiveAccount();
150154
},
151155
);
152156
}

sdk/identity/identity/src/credentials/onBehalfOfCredential.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
// Licensed under the MIT License.
33

44
import type { AccessToken, GetTokenOptions, TokenCredential } from "@azure/core-auth";
5-
import type { MsalClient } from "../msal/nodeFlows/msalClient.js";
6-
import { createMsalClient } from "../msal/nodeFlows/msalClient.js";
5+
import {
6+
createMsalClientContext,
7+
getTokenOnBehalfOf,
8+
type MsalClientContext,
9+
} from "../msal/nodeFlows/msalClient.js";
710
import type {
811
OnBehalfOfCredentialAssertionOptions,
912
OnBehalfOfCredentialCertificateOptions,
@@ -35,7 +38,7 @@ const logger = credentialLogger(credentialName);
3538
export class OnBehalfOfCredential implements TokenCredential {
3639
private tenantId: string;
3740
private additionallyAllowedTenantIds: string[];
38-
private msalClient: MsalClient;
41+
private msalContext: MsalClientContext;
3942
private sendCertificateChain?: boolean;
4043
private certificatePath?: string;
4144
private clientSecret?: string;
@@ -178,7 +181,7 @@ export class OnBehalfOfCredential implements TokenCredential {
178181
additionallyAllowedTenantIds,
179182
);
180183

181-
this.msalClient = createMsalClient(clientId, this.tenantId, {
184+
this.msalContext = createMsalClientContext(clientId, this.tenantId, {
182185
...options,
183186
logger,
184187
});
@@ -204,21 +207,24 @@ export class OnBehalfOfCredential implements TokenCredential {
204207
if (this.certificatePath) {
205208
const clientCertificate = await this.buildClientCertificate(this.certificatePath);
206209

207-
return this.msalClient.getTokenOnBehalfOf(
210+
return getTokenOnBehalfOf(
211+
this.msalContext,
208212
arrayScopes,
209213
this.userAssertionToken,
210214
clientCertificate,
211215
newOptions,
212216
);
213217
} else if (this.clientSecret) {
214-
return this.msalClient.getTokenOnBehalfOf(
218+
return getTokenOnBehalfOf(
219+
this.msalContext,
215220
arrayScopes,
216221
this.userAssertionToken,
217222
this.clientSecret,
218223
options,
219224
);
220225
} else if (this.clientAssertion) {
221-
return this.msalClient.getTokenOnBehalfOf(
226+
return getTokenOnBehalfOf(
227+
this.msalContext,
222228
arrayScopes,
223229
this.userAssertionToken,
224230
this.clientAssertion,

0 commit comments

Comments
 (0)