@azure/msal-node 2.7.0 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. package/README.md +1 -1
  2. package/dist/cache/NodeStorage.cjs +1 -1
  3. package/dist/cache/NodeStorage.mjs +1 -1
  4. package/dist/cache/TokenCache.cjs +1 -1
  5. package/dist/cache/TokenCache.mjs +1 -1
  6. package/dist/cache/distributed/DistributedCachePlugin.cjs +1 -1
  7. package/dist/cache/distributed/DistributedCachePlugin.mjs +1 -1
  8. package/dist/cache/serializer/Deserializer.cjs +1 -1
  9. package/dist/cache/serializer/Deserializer.mjs +1 -1
  10. package/dist/cache/serializer/Serializer.cjs +1 -1
  11. package/dist/cache/serializer/Serializer.mjs +1 -1
  12. package/dist/client/ClientApplication.cjs +6 -4
  13. package/dist/client/ClientApplication.cjs.map +1 -1
  14. package/dist/client/ClientApplication.d.ts +2 -1
  15. package/dist/client/ClientApplication.d.ts.map +1 -1
  16. package/dist/client/ClientApplication.mjs +7 -5
  17. package/dist/client/ClientApplication.mjs.map +1 -1
  18. package/dist/client/ClientAssertion.cjs +1 -1
  19. package/dist/client/ClientAssertion.mjs +1 -1
  20. package/dist/client/ClientCredentialClient.cjs +5 -5
  21. package/dist/client/ClientCredentialClient.cjs.map +1 -1
  22. package/dist/client/ClientCredentialClient.d.ts.map +1 -1
  23. package/dist/client/ClientCredentialClient.mjs +6 -6
  24. package/dist/client/ClientCredentialClient.mjs.map +1 -1
  25. package/dist/client/ConfidentialClientApplication.cjs +6 -3
  26. package/dist/client/ConfidentialClientApplication.cjs.map +1 -1
  27. package/dist/client/ConfidentialClientApplication.d.ts.map +1 -1
  28. package/dist/client/ConfidentialClientApplication.mjs +11 -8
  29. package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
  30. package/dist/client/DeviceCodeClient.cjs +1 -1
  31. package/dist/client/DeviceCodeClient.mjs +1 -1
  32. package/dist/client/ManagedIdentityApplication.cjs +3 -9
  33. package/dist/client/ManagedIdentityApplication.cjs.map +1 -1
  34. package/dist/client/ManagedIdentityApplication.d.ts.map +1 -1
  35. package/dist/client/ManagedIdentityApplication.mjs +4 -10
  36. package/dist/client/ManagedIdentityApplication.mjs.map +1 -1
  37. package/dist/client/ManagedIdentityClient.cjs +1 -1
  38. package/dist/client/ManagedIdentityClient.mjs +1 -1
  39. package/dist/client/ManagedIdentitySources/AppService.cjs +1 -1
  40. package/dist/client/ManagedIdentitySources/AppService.mjs +1 -1
  41. package/dist/client/ManagedIdentitySources/AzureArc.cjs +1 -1
  42. package/dist/client/ManagedIdentitySources/AzureArc.mjs +1 -1
  43. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs +11 -7
  44. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs.map +1 -1
  45. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts.map +1 -1
  46. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs +12 -8
  47. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs.map +1 -1
  48. package/dist/client/ManagedIdentitySources/CloudShell.cjs +1 -1
  49. package/dist/client/ManagedIdentitySources/CloudShell.mjs +1 -1
  50. package/dist/client/ManagedIdentitySources/Imds.cjs +1 -1
  51. package/dist/client/ManagedIdentitySources/Imds.mjs +1 -1
  52. package/dist/client/ManagedIdentitySources/ServiceFabric.cjs +1 -1
  53. package/dist/client/ManagedIdentitySources/ServiceFabric.mjs +1 -1
  54. package/dist/client/OnBehalfOfClient.cjs +7 -7
  55. package/dist/client/OnBehalfOfClient.cjs.map +1 -1
  56. package/dist/client/OnBehalfOfClient.d.ts.map +1 -1
  57. package/dist/client/OnBehalfOfClient.mjs +8 -8
  58. package/dist/client/OnBehalfOfClient.mjs.map +1 -1
  59. package/dist/client/PublicClientApplication.cjs +1 -1
  60. package/dist/client/PublicClientApplication.mjs +1 -1
  61. package/dist/client/UsernamePasswordClient.cjs +6 -6
  62. package/dist/client/UsernamePasswordClient.cjs.map +1 -1
  63. package/dist/client/UsernamePasswordClient.d.ts.map +1 -1
  64. package/dist/client/UsernamePasswordClient.mjs +7 -7
  65. package/dist/client/UsernamePasswordClient.mjs.map +1 -1
  66. package/dist/config/Configuration.cjs +1 -1
  67. package/dist/config/Configuration.cjs.map +1 -1
  68. package/dist/config/Configuration.d.ts +6 -3
  69. package/dist/config/Configuration.d.ts.map +1 -1
  70. package/dist/config/Configuration.mjs +1 -1
  71. package/dist/config/Configuration.mjs.map +1 -1
  72. package/dist/config/ManagedIdentityId.cjs +1 -1
  73. package/dist/config/ManagedIdentityId.mjs +1 -1
  74. package/dist/config/ManagedIdentityRequestParameters.cjs +1 -1
  75. package/dist/config/ManagedIdentityRequestParameters.mjs +1 -1
  76. package/dist/crypto/CryptoProvider.cjs +1 -1
  77. package/dist/crypto/CryptoProvider.mjs +1 -1
  78. package/dist/crypto/GuidGenerator.cjs +1 -1
  79. package/dist/crypto/GuidGenerator.mjs +1 -1
  80. package/dist/crypto/HashUtils.cjs +1 -1
  81. package/dist/crypto/HashUtils.mjs +1 -1
  82. package/dist/crypto/PkceGenerator.cjs +1 -1
  83. package/dist/crypto/PkceGenerator.mjs +1 -1
  84. package/dist/error/ManagedIdentityError.cjs +1 -2
  85. package/dist/error/ManagedIdentityError.cjs.map +1 -1
  86. package/dist/error/ManagedIdentityError.d.ts +0 -1
  87. package/dist/error/ManagedIdentityError.d.ts.map +1 -1
  88. package/dist/error/ManagedIdentityError.mjs +2 -3
  89. package/dist/error/ManagedIdentityError.mjs.map +1 -1
  90. package/dist/error/ManagedIdentityErrorCodes.cjs +1 -3
  91. package/dist/error/ManagedIdentityErrorCodes.cjs.map +1 -1
  92. package/dist/error/ManagedIdentityErrorCodes.d.ts +0 -1
  93. package/dist/error/ManagedIdentityErrorCodes.d.ts.map +1 -1
  94. package/dist/error/ManagedIdentityErrorCodes.mjs +2 -3
  95. package/dist/error/ManagedIdentityErrorCodes.mjs.map +1 -1
  96. package/dist/error/NodeAuthError.cjs +1 -1
  97. package/dist/error/NodeAuthError.mjs +1 -1
  98. package/dist/index.cjs +1 -1
  99. package/dist/index.d.ts +1 -1
  100. package/dist/index.d.ts.map +1 -1
  101. package/dist/index.mjs +1 -1
  102. package/dist/internals.cjs +1 -1
  103. package/dist/internals.mjs +1 -1
  104. package/dist/network/HttpClient.cjs +23 -25
  105. package/dist/network/HttpClient.cjs.map +1 -1
  106. package/dist/network/HttpClient.d.ts +2 -2
  107. package/dist/network/HttpClient.d.ts.map +1 -1
  108. package/dist/network/HttpClient.mjs +23 -25
  109. package/dist/network/HttpClient.mjs.map +1 -1
  110. package/dist/network/HttpClientWithRetries.cjs +1 -1
  111. package/dist/network/HttpClientWithRetries.mjs +1 -1
  112. package/dist/network/LoopbackClient.cjs +1 -1
  113. package/dist/network/LoopbackClient.mjs +1 -1
  114. package/dist/packageMetadata.cjs +2 -2
  115. package/dist/packageMetadata.d.ts +1 -1
  116. package/dist/packageMetadata.mjs +2 -2
  117. package/dist/request/ClientCredentialRequest.d.ts +3 -3
  118. package/dist/request/ClientCredentialRequest.d.ts.map +1 -1
  119. package/dist/retry/LinearRetryPolicy.cjs +1 -1
  120. package/dist/retry/LinearRetryPolicy.mjs +1 -1
  121. package/dist/utils/Constants.cjs +6 -5
  122. package/dist/utils/Constants.cjs.map +1 -1
  123. package/dist/utils/Constants.d.ts +1 -0
  124. package/dist/utils/Constants.d.ts.map +1 -1
  125. package/dist/utils/Constants.mjs +6 -5
  126. package/dist/utils/Constants.mjs.map +1 -1
  127. package/dist/utils/EncodingUtils.cjs +1 -1
  128. package/dist/utils/EncodingUtils.mjs +1 -1
  129. package/dist/utils/NetworkUtils.cjs +1 -1
  130. package/dist/utils/NetworkUtils.mjs +1 -1
  131. package/package.json +2 -2
  132. package/src/client/ClientApplication.ts +19 -6
  133. package/src/client/ClientCredentialClient.ts +14 -6
  134. package/src/client/ConfidentialClientApplication.ts +10 -5
  135. package/src/client/ManagedIdentityApplication.ts +5 -13
  136. package/src/client/ManagedIdentitySources/BaseManagedIdentitySource.ts +12 -9
  137. package/src/client/OnBehalfOfClient.ts +18 -7
  138. package/src/client/UsernamePasswordClient.ts +16 -7
  139. package/src/config/Configuration.ts +6 -2
  140. package/src/error/ManagedIdentityError.ts +0 -2
  141. package/src/error/ManagedIdentityErrorCodes.ts +0 -1
  142. package/src/index.ts +1 -0
  143. package/src/network/HttpClient.ts +23 -30
  144. package/src/packageMetadata.ts +1 -1
  145. package/src/request/ClientCredentialRequest.ts +6 -3
  146. package/src/utils/Constants.ts +5 -4
@@ -26,6 +26,8 @@ import {
26
26
  AADAuthorityConstants,
27
27
  createClientAuthError,
28
28
  ClientAuthErrorCodes,
29
+ ClientAssertion as ClientAssertionType,
30
+ getClientAssertion,
29
31
  } from "@azure/msal-common";
30
32
  import { IConfidentialClientApplication } from "./IConfidentialClientApplication.js";
31
33
  import { OnBehalfOfRequest } from "../request/OnBehalfOfRequest.js";
@@ -91,10 +93,14 @@ export class ConfidentialClientApplication
91
93
  );
92
94
 
93
95
  // If there is a client assertion present in the request, it overrides the one present in the client configuration
94
- let clientAssertion;
96
+ let clientAssertion: ClientAssertionType | undefined;
95
97
  if (request.clientAssertion) {
96
98
  clientAssertion = {
97
- assertion: request.clientAssertion,
99
+ assertion: await getClientAssertion(
100
+ request.clientAssertion,
101
+ this.config.auth.clientId
102
+ // tokenEndpoint will be undefined. resourceRequestUri is omitted in ClientCredentialRequest
103
+ ),
98
104
  assertionType: NodeConstants.JWT_BEARER_ASSERTION_TYPE,
99
105
  };
100
106
  }
@@ -247,9 +253,8 @@ export class ConfidentialClientApplication
247
253
  }
248
254
 
249
255
  if (configuration.auth.clientAssertion) {
250
- this.clientAssertion = ClientAssertion.fromAssertion(
251
- configuration.auth.clientAssertion
252
- );
256
+ this.developerProvidedClientAssertion =
257
+ configuration.auth.clientAssertion;
253
258
  return;
254
259
  }
255
260
 
@@ -16,7 +16,8 @@ import {
16
16
  ProtocolMode,
17
17
  StaticAuthorityOptions,
18
18
  AuthenticationResult,
19
- UrlString,
19
+ createClientConfigurationError,
20
+ ClientConfigurationErrorCodes,
20
21
  } from "@azure/msal-common";
21
22
  import {
22
23
  ManagedIdentityConfiguration,
@@ -31,10 +32,6 @@ import { ManagedIdentityClient } from "./ManagedIdentityClient";
31
32
  import { ManagedIdentityRequestParams } from "../request/ManagedIdentityRequestParams";
32
33
  import { NodeStorage } from "../cache/NodeStorage";
33
34
  import { DEFAULT_AUTHORITY_FOR_MANAGED_IDENTITY } from "../utils/Constants";
34
- import {
35
- ManagedIdentityErrorCodes,
36
- createManagedIdentityError,
37
- } from "../error/ManagedIdentityError";
38
35
 
39
36
  /**
40
37
  * Class to initialize a managed identity and identify the service
@@ -122,14 +119,9 @@ export class ManagedIdentityApplication {
122
119
  public async acquireToken(
123
120
  managedIdentityRequestParams: ManagedIdentityRequestParams
124
121
  ): Promise<AuthenticationResult> {
125
- const resourceUrlString = new UrlString(
126
- managedIdentityRequestParams.resource.replace("/.default", "")
127
- );
128
- try {
129
- resourceUrlString.validateAsUri();
130
- } catch (e) {
131
- throw createManagedIdentityError(
132
- ManagedIdentityErrorCodes.invalidResource
122
+ if (!managedIdentityRequestParams.resource) {
123
+ throw createClientConfigurationError(
124
+ ClientConfigurationErrorCodes.urlEmptyError
133
125
  );
134
126
  }
135
127
 
@@ -81,28 +81,31 @@ export abstract class BaseManagedIdentitySource {
81
81
  public getServerTokenResponse(
82
82
  response: NetworkResponse<ManagedIdentityTokenResponse>
83
83
  ): ServerAuthorizationTokenResponse {
84
+ let refreshIn, expiresIn: number | undefined;
85
+ if (response.body.expires_on) {
86
+ expiresIn = response.body.expires_on - TimeUtils.nowSeconds();
87
+
88
+ // compute refresh_in as 1/2 of expires_in, but only if expires_in > 2h
89
+ if (expiresIn > 2 * 3600) {
90
+ refreshIn = expiresIn / 2;
91
+ }
92
+ }
93
+
84
94
  const serverTokenResponse: ServerAuthorizationTokenResponse = {
85
95
  status: response.status,
86
96
 
87
97
  // success
88
98
  access_token: response.body.access_token,
89
- expires_in: response.body.expires_on,
99
+ expires_in: expiresIn,
90
100
  scope: response.body.resource,
91
101
  token_type: response.body.token_type,
102
+ refresh_in: refreshIn,
92
103
 
93
104
  // error
94
105
  error: response.body.message,
95
106
  correlation_id: response.body.correlationId,
96
107
  };
97
108
 
98
- // compute refresh_in as 1/2 of expires_in, but only if expires_in > 2h
99
- if (
100
- serverTokenResponse.expires_in &&
101
- serverTokenResponse.expires_in > 2 * 3600
102
- ) {
103
- serverTokenResponse.refresh_in = serverTokenResponse.expires_in / 2;
104
- }
105
-
106
109
  return serverTokenResponse;
107
110
  }
108
111
 
@@ -30,6 +30,8 @@ import {
30
30
  TimeUtils,
31
31
  TokenClaims,
32
32
  UrlString,
33
+ ClientAssertion,
34
+ getClientAssertion,
33
35
  } from "@azure/msal-common";
34
36
  import { EncodingUtils } from "../utils/EncodingUtils";
35
37
 
@@ -58,7 +60,7 @@ export class OnBehalfOfClient extends BaseClient {
58
60
  request.oboAssertion
59
61
  );
60
62
 
61
- if (request.skipCache) {
63
+ if (request.skipCache || request.claims) {
62
64
  return this.executeTokenRequest(
63
65
  request,
64
66
  this.authority,
@@ -257,7 +259,7 @@ export class OnBehalfOfClient extends BaseClient {
257
259
  authority.tokenEndpoint,
258
260
  queryParametersString
259
261
  );
260
- const requestBody = this.createTokenRequestBody(request);
262
+ const requestBody = await this.createTokenRequestBody(request);
261
263
  const headers: Record<string, string> =
262
264
  this.createTokenRequestHeaders();
263
265
  const thumbprint: RequestThumbprint = {
@@ -307,7 +309,9 @@ export class OnBehalfOfClient extends BaseClient {
307
309
  * generate a server request in accepable format
308
310
  * @param request
309
311
  */
310
- private createTokenRequestBody(request: CommonOnBehalfOfRequest): string {
312
+ private async createTokenRequestBody(
313
+ request: CommonOnBehalfOfRequest
314
+ ): Promise<string> {
311
315
  const parameterBuilder = new RequestParameterBuilder();
312
316
 
313
317
  parameterBuilder.addClientId(this.config.authOptions.clientId);
@@ -343,10 +347,17 @@ export class OnBehalfOfClient extends BaseClient {
343
347
  );
344
348
  }
345
349
 
346
- if (this.config.clientCredentials.clientAssertion) {
347
- const clientAssertion =
348
- this.config.clientCredentials.clientAssertion;
349
- parameterBuilder.addClientAssertion(clientAssertion.assertion);
350
+ const clientAssertion: ClientAssertion | undefined =
351
+ this.config.clientCredentials.clientAssertion;
352
+
353
+ if (clientAssertion) {
354
+ parameterBuilder.addClientAssertion(
355
+ await getClientAssertion(
356
+ clientAssertion.assertion,
357
+ this.config.authOptions.clientId,
358
+ request.resourceRequestUri
359
+ )
360
+ );
350
361
  parameterBuilder.addClientAssertionType(
351
362
  clientAssertion.assertionType
352
363
  );
@@ -8,6 +8,7 @@ import {
8
8
  Authority,
9
9
  BaseClient,
10
10
  CcsCredentialType,
11
+ ClientAssertion,
11
12
  ClientConfiguration,
12
13
  CommonUsernamePasswordRequest,
13
14
  GrantType,
@@ -19,6 +20,7 @@ import {
19
20
  StringUtils,
20
21
  TimeUtils,
21
22
  UrlString,
23
+ getClientAssertion,
22
24
  } from "@azure/msal-common";
23
25
 
24
26
  /**
@@ -81,7 +83,7 @@ export class UsernamePasswordClient extends BaseClient {
81
83
  authority.tokenEndpoint,
82
84
  queryParametersString
83
85
  );
84
- const requestBody = this.createTokenRequestBody(request);
86
+ const requestBody = await this.createTokenRequestBody(request);
85
87
  const headers: Record<string, string> = this.createTokenRequestHeaders({
86
88
  credential: request.username,
87
89
  type: CcsCredentialType.UPN,
@@ -111,9 +113,9 @@ export class UsernamePasswordClient extends BaseClient {
111
113
  * Generates a map for all the params to be sent to the service
112
114
  * @param request
113
115
  */
114
- private createTokenRequestBody(
116
+ private async createTokenRequestBody(
115
117
  request: CommonUsernamePasswordRequest
116
- ): string {
118
+ ): Promise<string> {
117
119
  const parameterBuilder = new RequestParameterBuilder();
118
120
 
119
121
  parameterBuilder.addClientId(this.config.authOptions.clientId);
@@ -148,10 +150,17 @@ export class UsernamePasswordClient extends BaseClient {
148
150
  );
149
151
  }
150
152
 
151
- if (this.config.clientCredentials.clientAssertion) {
152
- const clientAssertion =
153
- this.config.clientCredentials.clientAssertion;
154
- parameterBuilder.addClientAssertion(clientAssertion.assertion);
153
+ const clientAssertion: ClientAssertion | undefined =
154
+ this.config.clientCredentials.clientAssertion;
155
+
156
+ if (clientAssertion) {
157
+ parameterBuilder.addClientAssertion(
158
+ await getClientAssertion(
159
+ clientAssertion.assertion,
160
+ this.config.authOptions.clientId,
161
+ request.resourceRequestUri
162
+ )
163
+ );
155
164
  parameterBuilder.addClientAssertionType(
156
165
  clientAssertion.assertionType
157
166
  );
@@ -14,6 +14,7 @@ import {
14
14
  AzureCloudOptions,
15
15
  ApplicationTelemetry,
16
16
  INativeBrokerPlugin,
17
+ ClientAssertionCallback,
17
18
  } from "@azure/msal-common";
18
19
  import { HttpClient } from "../network/HttpClient.js";
19
20
  import http from "http";
@@ -32,7 +33,7 @@ import { HttpClientWithRetries } from "../network/HttpClientWithRetries.js";
32
33
  * - authority - Url of the authority. If no value is set, defaults to https://login.microsoftonline.com/common.
33
34
  * - knownAuthorities - Needed for Azure B2C and ADFS. All authorities that will be used in the client application. Only the host of the authority should be passed in.
34
35
  * - clientSecret - Secret string that the application uses when requesting a token. Only used in confidential client applications. Can be created in the Azure app registration portal.
35
- * - clientAssertion - Assertion string that the application uses when requesting a token. Only used in confidential client applications. Assertion should be of type urn:ietf:params:oauth:client-assertion-type:jwt-bearer.
36
+ * - clientAssertion - A ClientAssertion object containing an assertion string or a callback function that returns an assertion string that the application uses when requesting a token, as well as the assertion's type (urn:ietf:params:oauth:client-assertion-type:jwt-bearer). Only used in confidential client applications.
36
37
  * - clientCertificate - Certificate that the application uses when requesting a token. Only used in confidential client applications. Requires hex encoded X.509 SHA-1 thumbprint of the certificiate, and the PEM encoded private key (string should contain -----BEGIN PRIVATE KEY----- ... -----END PRIVATE KEY----- )
37
38
  * - protocolMode - Enum that represents the protocol that msal follows. Used for configuring proper endpoints.
38
39
  * - skipAuthorityMetadataCache - A flag to choose whether to use or not use the local metadata cache during authority initialization. Defaults to false.
@@ -42,7 +43,7 @@ export type NodeAuthOptions = {
42
43
  clientId: string;
43
44
  authority?: string;
44
45
  clientSecret?: string;
45
- clientAssertion?: string;
46
+ clientAssertion?: string | ClientAssertionCallback;
46
47
  clientCertificate?: {
47
48
  thumbprint: string;
48
49
  privateKey: string;
@@ -65,6 +66,9 @@ export type NodeAuthOptions = {
65
66
  */
66
67
  export type CacheOptions = {
67
68
  cachePlugin?: ICachePlugin;
69
+ /**
70
+ * @deprecated claims-based-caching functionality will be removed in the next version of MSALJS
71
+ */
68
72
  claimsBasedCachingEnabled?: boolean;
69
73
  };
70
74
 
@@ -14,8 +14,6 @@ export { ManagedIdentityErrorCodes };
14
14
  export const ManagedIdentityErrorMessages = {
15
15
  [ManagedIdentityErrorCodes.invalidManagedIdentityIdType]:
16
16
  "More than one ManagedIdentityIdType was provided.",
17
- [ManagedIdentityErrorCodes.invalidResource]:
18
- "The supplied resource is an invalid URL.",
19
17
  [ManagedIdentityErrorCodes.missingId]:
20
18
  "A ManagedIdentityId id was not provided.",
21
19
  [ManagedIdentityErrorCodes.MsiEnvironmentVariableUrlMalformedErrorCodes
@@ -6,7 +6,6 @@
6
6
  import { ManagedIdentityEnvironmentVariableNames } from "../utils/Constants";
7
7
 
8
8
  export const invalidManagedIdentityIdType = "invalid_managed_identity_id_type";
9
- export const invalidResource = "invalid_resource";
10
9
  export const missingId = "missing_client_id";
11
10
  export const networkUnavailable = "network_unavailable";
12
11
  export const unableToCreateAzureArc = "unable_to_create_azure_arc";
package/src/index.ts CHANGED
@@ -125,6 +125,7 @@ export {
125
125
  AppTokenProviderParameters,
126
126
  AppTokenProviderResult,
127
127
  INativeBrokerPlugin,
128
+ ClientAssertionCallback,
128
129
  } from "@azure/msal-common";
129
130
 
130
131
  export { version } from "./packageMetadata.js";
@@ -36,7 +36,8 @@ export class HttpClient implements INetworkModule {
36
36
  */
37
37
  async sendGetRequestAsync<T>(
38
38
  url: string,
39
- options?: NetworkRequestOptions
39
+ options?: NetworkRequestOptions,
40
+ timeout?: number
40
41
  ): Promise<NetworkResponse<T>> {
41
42
  if (this.proxyUrl) {
42
43
  return networkRequestViaProxy(
@@ -44,7 +45,8 @@ export class HttpClient implements INetworkModule {
44
45
  this.proxyUrl,
45
46
  HttpMethod.GET,
46
47
  options,
47
- this.customAgentOptions as http.AgentOptions
48
+ this.customAgentOptions as http.AgentOptions,
49
+ timeout
48
50
  );
49
51
  } else {
50
52
  return networkRequestViaHttps(
@@ -52,7 +54,7 @@ export class HttpClient implements INetworkModule {
52
54
  HttpMethod.GET,
53
55
  options,
54
56
  this.customAgentOptions as https.AgentOptions,
55
- undefined
57
+ timeout
56
58
  );
57
59
  }
58
60
  }
@@ -64,8 +66,7 @@ export class HttpClient implements INetworkModule {
64
66
  */
65
67
  async sendPostRequestAsync<T>(
66
68
  url: string,
67
- options?: NetworkRequestOptions,
68
- cancellationToken?: number
69
+ options?: NetworkRequestOptions
69
70
  ): Promise<NetworkResponse<T>> {
70
71
  if (this.proxyUrl) {
71
72
  return networkRequestViaProxy(
@@ -73,16 +74,14 @@ export class HttpClient implements INetworkModule {
73
74
  this.proxyUrl,
74
75
  HttpMethod.POST,
75
76
  options,
76
- this.customAgentOptions as http.AgentOptions,
77
- cancellationToken
77
+ this.customAgentOptions as http.AgentOptions
78
78
  );
79
79
  } else {
80
80
  return networkRequestViaHttps(
81
81
  url,
82
82
  HttpMethod.POST,
83
83
  options,
84
- this.customAgentOptions as https.AgentOptions,
85
- cancellationToken
84
+ this.customAgentOptions as https.AgentOptions
86
85
  );
87
86
  }
88
87
  }
@@ -109,10 +108,6 @@ const networkRequestViaProxy = <T>(
109
108
  headers: headers,
110
109
  };
111
110
 
112
- if (timeout) {
113
- tunnelRequestOptions.timeout = timeout;
114
- }
115
-
116
111
  if (agentOptions && Object.keys(agentOptions).length) {
117
112
  tunnelRequestOptions.agent = new http.Agent(agentOptions);
118
113
  }
@@ -125,6 +120,11 @@ const networkRequestViaProxy = <T>(
125
120
  "Content-Type: application/x-www-form-urlencoded\r\n" +
126
121
  `Content-Length: ${body.length}\r\n` +
127
122
  `\r\n${body}`;
123
+ } else {
124
+ // optional timeout is only for get requests (regionDiscovery, for example)
125
+ if (timeout) {
126
+ tunnelRequestOptions.timeout = timeout;
127
+ }
128
128
  }
129
129
  const outgoingRequestString =
130
130
  `${httpMethod.toUpperCase()} ${destinationUrl.href} HTTP/1.1\r\n` +
@@ -136,7 +136,7 @@ const networkRequestViaProxy = <T>(
136
136
  return new Promise<NetworkResponse<T>>((resolve, reject) => {
137
137
  const request = http.request(tunnelRequestOptions);
138
138
 
139
- if (tunnelRequestOptions.timeout) {
139
+ if (timeout) {
140
140
  request.on("timeout", () => {
141
141
  request.destroy();
142
142
  reject(new Error("Request time out"));
@@ -165,14 +165,6 @@ const networkRequestViaProxy = <T>(
165
165
  )
166
166
  );
167
167
  }
168
- if (tunnelRequestOptions.timeout) {
169
- socket.setTimeout(tunnelRequestOptions.timeout);
170
- socket.on("timeout", () => {
171
- request.destroy();
172
- socket.destroy();
173
- reject(new Error("Request time out"));
174
- });
175
- }
176
168
 
177
169
  // make a request over an HTTP tunnel
178
170
  socket.write(outgoingRequestString);
@@ -291,10 +283,6 @@ const networkRequestViaHttps = <T>(
291
283
  ...NetworkUtils.urlToHttpOptions(url),
292
284
  };
293
285
 
294
- if (timeout) {
295
- customOptions.timeout = timeout;
296
- }
297
-
298
286
  if (agentOptions && Object.keys(agentOptions).length) {
299
287
  customOptions.agent = new https.Agent(agentOptions);
300
288
  }
@@ -305,6 +293,11 @@ const networkRequestViaHttps = <T>(
305
293
  ...customOptions.headers,
306
294
  "Content-Length": body.length,
307
295
  };
296
+ } else {
297
+ // optional timeout is only for get requests (regionDiscovery, for example)
298
+ if (timeout) {
299
+ customOptions.timeout = timeout;
300
+ }
308
301
  }
309
302
 
310
303
  return new Promise<NetworkResponse<T>>((resolve, reject) => {
@@ -316,6 +309,10 @@ const networkRequestViaHttps = <T>(
316
309
  request = https.request(customOptions);
317
310
  }
318
311
 
312
+ if (isPostRequest) {
313
+ request.write(body);
314
+ }
315
+
319
316
  if (timeout) {
320
317
  request.on("timeout", () => {
321
318
  request.destroy();
@@ -323,10 +320,6 @@ const networkRequestViaHttps = <T>(
323
320
  });
324
321
  }
325
322
 
326
- if (isPostRequest) {
327
- request.write(body);
328
- }
329
-
330
323
  request.end();
331
324
 
332
325
  request.on("response", (response) => {
@@ -1,3 +1,3 @@
1
1
  /* eslint-disable header/header */
2
2
  export const name = "@azure/msal-node";
3
- export const version = "2.7.0";
3
+ export const version = "2.8.0";
@@ -3,7 +3,10 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
- import { CommonClientCredentialRequest } from "@azure/msal-common";
6
+ import {
7
+ ClientAssertionCallback,
8
+ CommonClientCredentialRequest,
9
+ } from "@azure/msal-common";
7
10
 
8
11
  /**
9
12
  * CommonClientCredentialRequest
@@ -11,7 +14,7 @@ import { CommonClientCredentialRequest } from "@azure/msal-common";
11
14
  * - authority - URL of the authority, the security token service (STS) from which MSAL will acquire tokens.
12
15
  * - correlationId - Unique GUID set per request to trace a request end-to-end for telemetry purposes.
13
16
  * - skipCache - Skip token cache lookup and force request to authority to get a a new token. Defaults to false.
14
- * - clientAssertion - A Base64Url-encoded signed JWT assertion string used in the Client Credential flow
17
+ * - clientAssertion - An assertion string or a callback function that returns an assertion string (both are Base64Url-encoded signed JWTs) used in the Client Credential flow
15
18
  * - tokenQueryParameters - String to string map of custom query parameters added to the /token call
16
19
  * @public
17
20
  */
@@ -25,5 +28,5 @@ export type ClientCredentialRequest = Partial<
25
28
  | "storeInCache"
26
29
  >
27
30
  > & {
28
- clientAssertion?: string;
31
+ clientAssertion?: string | ClientAssertionCallback;
29
32
  };
@@ -65,9 +65,10 @@ export const HttpMethod = {
65
65
  export type HttpMethod = (typeof HttpMethod)[keyof typeof HttpMethod];
66
66
 
67
67
  export const ProxyStatus = {
68
- SUCCESS_RANGE_START: 200,
69
- SUCCESS_RANGE_END: 299,
70
- SERVER_ERROR: 500,
68
+ SUCCESS: HttpStatus.SUCCESS,
69
+ SUCCESS_RANGE_START: HttpStatus.SUCCESS_RANGE_START,
70
+ SUCCESS_RANGE_END: HttpStatus.SUCCESS_RANGE_END,
71
+ SERVER_ERROR: HttpStatus.SERVER_ERROR,
71
72
  } as const;
72
73
  export type ProxyStatus = (typeof ProxyStatus)[keyof typeof ProxyStatus];
73
74
 
@@ -160,7 +161,7 @@ export const MANAGED_IDENTITY_HTTP_STATUS_CODES_TO_RETRY_ON = [
160
161
  HttpStatus.NOT_FOUND,
161
162
  HttpStatus.REQUEST_TIMEOUT,
162
163
  HttpStatus.TOO_MANY_REQUESTS,
163
- HttpStatus.INTERNAL_SERVER_ERROR,
164
+ HttpStatus.SERVER_ERROR,
164
165
  HttpStatus.SERVICE_UNAVAILABLE,
165
166
  HttpStatus.GATEWAY_TIMEOUT,
166
167
  ];