@azure/msal-node 5.0.0-alpha.0 → 5.0.1

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 (269) hide show
  1. package/dist/cache/CacheHelpers.mjs +1 -1
  2. package/dist/cache/NodeStorage.mjs +1 -1
  3. package/dist/cache/NodeStorage.mjs.map +1 -1
  4. package/dist/cache/TokenCache.mjs +1 -1
  5. package/dist/cache/TokenCache.mjs.map +1 -1
  6. package/dist/cache/distributed/DistributedCachePlugin.mjs +1 -1
  7. package/dist/cache/distributed/DistributedCachePlugin.mjs.map +1 -1
  8. package/dist/cache/serializer/Deserializer.mjs +1 -1
  9. package/dist/cache/serializer/Deserializer.mjs.map +1 -1
  10. package/dist/cache/serializer/Serializer.mjs +1 -1
  11. package/dist/cache/serializer/Serializer.mjs.map +1 -1
  12. package/dist/client/BaseClient.d.ts +42 -0
  13. package/dist/client/BaseClient.d.ts.map +1 -0
  14. package/dist/client/BaseClient.mjs +68 -0
  15. package/dist/client/BaseClient.mjs.map +1 -0
  16. package/dist/client/ClientApplication.mjs +1 -1
  17. package/dist/client/ClientApplication.mjs.map +1 -1
  18. package/dist/client/ClientAssertion.mjs +1 -1
  19. package/dist/client/ClientAssertion.mjs.map +1 -1
  20. package/dist/client/ClientCredentialClient.d.ts +2 -1
  21. package/dist/client/ClientCredentialClient.d.ts.map +1 -1
  22. package/dist/client/ClientCredentialClient.mjs +4 -3
  23. package/dist/client/ClientCredentialClient.mjs.map +1 -1
  24. package/dist/client/ConfidentialClientApplication.mjs +1 -1
  25. package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
  26. package/dist/client/DeviceCodeClient.d.ts +2 -1
  27. package/dist/client/DeviceCodeClient.d.ts.map +1 -1
  28. package/dist/client/DeviceCodeClient.mjs +6 -5
  29. package/dist/client/DeviceCodeClient.mjs.map +1 -1
  30. package/dist/client/ManagedIdentityApplication.mjs +1 -1
  31. package/dist/client/ManagedIdentityApplication.mjs.map +1 -1
  32. package/dist/client/ManagedIdentityClient.mjs +1 -1
  33. package/dist/client/ManagedIdentityClient.mjs.map +1 -1
  34. package/dist/client/ManagedIdentitySources/AppService.d.ts +54 -0
  35. package/dist/client/ManagedIdentitySources/AppService.d.ts.map +1 -1
  36. package/dist/client/ManagedIdentitySources/AppService.mjs +55 -1
  37. package/dist/client/ManagedIdentitySources/AppService.mjs.map +1 -1
  38. package/dist/client/ManagedIdentitySources/AzureArc.d.ts +81 -0
  39. package/dist/client/ManagedIdentitySources/AzureArc.d.ts.map +1 -1
  40. package/dist/client/ManagedIdentitySources/AzureArc.mjs +82 -1
  41. package/dist/client/ManagedIdentitySources/AzureArc.mjs.map +1 -1
  42. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts +93 -2
  43. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts.map +1 -1
  44. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs +81 -1
  45. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs.map +1 -1
  46. package/dist/client/ManagedIdentitySources/CloudShell.d.ts +54 -0
  47. package/dist/client/ManagedIdentitySources/CloudShell.d.ts.map +1 -1
  48. package/dist/client/ManagedIdentitySources/CloudShell.mjs +55 -1
  49. package/dist/client/ManagedIdentitySources/CloudShell.mjs.map +1 -1
  50. package/dist/client/ManagedIdentitySources/Imds.d.ts +44 -22
  51. package/dist/client/ManagedIdentitySources/Imds.d.ts.map +1 -1
  52. package/dist/client/ManagedIdentitySources/Imds.mjs +44 -22
  53. package/dist/client/ManagedIdentitySources/Imds.mjs.map +1 -1
  54. package/dist/client/ManagedIdentitySources/MachineLearning.d.ts +65 -0
  55. package/dist/client/ManagedIdentitySources/MachineLearning.d.ts.map +1 -1
  56. package/dist/client/ManagedIdentitySources/MachineLearning.mjs +66 -1
  57. package/dist/client/ManagedIdentitySources/MachineLearning.mjs.map +1 -1
  58. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts +56 -22
  59. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts.map +1 -1
  60. package/dist/client/ManagedIdentitySources/ServiceFabric.mjs +57 -23
  61. package/dist/client/ManagedIdentitySources/ServiceFabric.mjs.map +1 -1
  62. package/dist/client/OnBehalfOfClient.d.ts +2 -1
  63. package/dist/client/OnBehalfOfClient.d.ts.map +1 -1
  64. package/dist/client/OnBehalfOfClient.mjs +4 -3
  65. package/dist/client/OnBehalfOfClient.mjs.map +1 -1
  66. package/dist/client/PublicClientApplication.d.ts.map +1 -1
  67. package/dist/client/PublicClientApplication.mjs +21 -5
  68. package/dist/client/PublicClientApplication.mjs.map +1 -1
  69. package/dist/client/UsernamePasswordClient.d.ts +2 -1
  70. package/dist/client/UsernamePasswordClient.d.ts.map +1 -1
  71. package/dist/client/UsernamePasswordClient.mjs +4 -3
  72. package/dist/client/UsernamePasswordClient.mjs.map +1 -1
  73. package/dist/config/Configuration.d.ts +0 -6
  74. package/dist/config/Configuration.d.ts.map +1 -1
  75. package/dist/config/Configuration.mjs +3 -5
  76. package/dist/config/Configuration.mjs.map +1 -1
  77. package/dist/config/ManagedIdentityId.mjs +1 -1
  78. package/dist/config/ManagedIdentityId.mjs.map +1 -1
  79. package/dist/config/ManagedIdentityRequestParameters.mjs +3 -3
  80. package/dist/config/ManagedIdentityRequestParameters.mjs.map +1 -1
  81. package/dist/crypto/CryptoProvider.mjs +1 -1
  82. package/dist/crypto/CryptoProvider.mjs.map +1 -1
  83. package/dist/crypto/GuidGenerator.mjs +1 -1
  84. package/dist/crypto/GuidGenerator.mjs.map +1 -1
  85. package/dist/crypto/HashUtils.mjs +1 -1
  86. package/dist/crypto/HashUtils.mjs.map +1 -1
  87. package/dist/crypto/PkceGenerator.mjs +1 -1
  88. package/dist/crypto/PkceGenerator.mjs.map +1 -1
  89. package/dist/error/ClientAuthErrorCodes.mjs +1 -1
  90. package/dist/error/ManagedIdentityError.mjs +1 -1
  91. package/dist/error/ManagedIdentityError.mjs.map +1 -1
  92. package/dist/error/ManagedIdentityErrorCodes.mjs +1 -1
  93. package/dist/error/ManagedIdentityErrorCodes.mjs.map +1 -1
  94. package/dist/error/NodeAuthError.d.ts +8 -0
  95. package/dist/error/NodeAuthError.d.ts.map +1 -1
  96. package/dist/error/NodeAuthError.mjs +11 -1
  97. package/dist/error/NodeAuthError.mjs.map +1 -1
  98. package/dist/index.d.ts +0 -6
  99. package/dist/index.d.ts.map +1 -1
  100. package/dist/index.mjs +1 -7
  101. package/dist/index.mjs.map +1 -1
  102. package/dist/internals.mjs +1 -1
  103. package/dist/network/HttpClient.d.ts +54 -14
  104. package/dist/network/HttpClient.d.ts.map +1 -1
  105. package/dist/network/HttpClient.mjs +140 -262
  106. package/dist/network/HttpClient.mjs.map +1 -1
  107. package/dist/network/HttpClientWithRetries.mjs +1 -1
  108. package/dist/network/HttpClientWithRetries.mjs.map +1 -1
  109. package/dist/network/LoopbackClient.mjs +1 -1
  110. package/dist/network/LoopbackClient.mjs.map +1 -1
  111. package/dist/packageMetadata.d.ts +1 -1
  112. package/dist/packageMetadata.d.ts.map +1 -1
  113. package/dist/packageMetadata.mjs +2 -2
  114. package/dist/protocol/Authorize.mjs +2 -2
  115. package/dist/protocol/Authorize.mjs.map +1 -1
  116. package/dist/request/AuthorizationCodeRequest.d.ts +12 -10
  117. package/dist/request/AuthorizationCodeRequest.d.ts.map +1 -1
  118. package/dist/request/AuthorizationUrlRequest.d.ts +6 -24
  119. package/dist/request/AuthorizationUrlRequest.d.ts.map +1 -1
  120. package/dist/request/ClientCredentialRequest.d.ts +3 -6
  121. package/dist/request/ClientCredentialRequest.d.ts.map +1 -1
  122. package/dist/request/CommonClientCredentialRequest.d.ts +10 -7
  123. package/dist/request/CommonClientCredentialRequest.d.ts.map +1 -1
  124. package/dist/request/CommonDeviceCodeRequest.d.ts +13 -10
  125. package/dist/request/CommonDeviceCodeRequest.d.ts.map +1 -1
  126. package/dist/request/CommonOnBehalfOfRequest.d.ts +8 -7
  127. package/dist/request/CommonOnBehalfOfRequest.d.ts.map +1 -1
  128. package/dist/request/CommonUsernamePasswordRequest.d.ts +6 -8
  129. package/dist/request/CommonUsernamePasswordRequest.d.ts.map +1 -1
  130. package/dist/request/DeviceCodeRequest.d.ts +6 -6
  131. package/dist/request/DeviceCodeRequest.d.ts.map +1 -1
  132. package/dist/request/InteractiveRequest.d.ts +19 -8
  133. package/dist/request/InteractiveRequest.d.ts.map +1 -1
  134. package/dist/request/ManagedIdentityRequest.d.ts +6 -2
  135. package/dist/request/ManagedIdentityRequest.d.ts.map +1 -1
  136. package/dist/request/ManagedIdentityRequestParams.d.ts +9 -3
  137. package/dist/request/ManagedIdentityRequestParams.d.ts.map +1 -1
  138. package/dist/request/OnBehalfOfRequest.d.ts +7 -6
  139. package/dist/request/OnBehalfOfRequest.d.ts.map +1 -1
  140. package/dist/request/RefreshTokenRequest.d.ts +9 -7
  141. package/dist/request/RefreshTokenRequest.d.ts.map +1 -1
  142. package/dist/request/SilentFlowRequest.d.ts +6 -7
  143. package/dist/request/SilentFlowRequest.d.ts.map +1 -1
  144. package/dist/request/UsernamePasswordRequest.d.ts +9 -8
  145. package/dist/request/UsernamePasswordRequest.d.ts.map +1 -1
  146. package/dist/retry/DefaultManagedIdentityRetryPolicy.mjs +1 -1
  147. package/dist/retry/DefaultManagedIdentityRetryPolicy.mjs.map +1 -1
  148. package/dist/retry/ExponentialRetryStrategy.mjs +1 -1
  149. package/dist/retry/ExponentialRetryStrategy.mjs.map +1 -1
  150. package/dist/retry/ImdsRetryPolicy.mjs +1 -1
  151. package/dist/retry/ImdsRetryPolicy.mjs.map +1 -1
  152. package/dist/retry/LinearRetryStrategy.mjs +1 -1
  153. package/dist/retry/LinearRetryStrategy.mjs.map +1 -1
  154. package/dist/utils/Constants.d.ts +2 -9
  155. package/dist/utils/Constants.d.ts.map +1 -1
  156. package/dist/utils/Constants.mjs +4 -12
  157. package/dist/utils/Constants.mjs.map +1 -1
  158. package/dist/utils/EncodingUtils.mjs +1 -1
  159. package/dist/utils/EncodingUtils.mjs.map +1 -1
  160. package/dist/utils/TimeUtils.mjs +1 -1
  161. package/dist/utils/TimeUtils.mjs.map +1 -1
  162. package/lib/msal-node.cjs +5788 -5371
  163. package/lib/msal-node.cjs.map +1 -1
  164. package/lib/types/client/BaseClient.d.ts +42 -0
  165. package/lib/types/client/BaseClient.d.ts.map +1 -0
  166. package/lib/types/client/ClientCredentialClient.d.ts +2 -1
  167. package/lib/types/client/ClientCredentialClient.d.ts.map +1 -1
  168. package/lib/types/client/DeviceCodeClient.d.ts +2 -1
  169. package/lib/types/client/DeviceCodeClient.d.ts.map +1 -1
  170. package/lib/types/client/ManagedIdentitySources/AppService.d.ts +54 -0
  171. package/lib/types/client/ManagedIdentitySources/AppService.d.ts.map +1 -1
  172. package/lib/types/client/ManagedIdentitySources/AzureArc.d.ts +81 -0
  173. package/lib/types/client/ManagedIdentitySources/AzureArc.d.ts.map +1 -1
  174. package/lib/types/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts +93 -2
  175. package/lib/types/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts.map +1 -1
  176. package/lib/types/client/ManagedIdentitySources/CloudShell.d.ts +54 -0
  177. package/lib/types/client/ManagedIdentitySources/CloudShell.d.ts.map +1 -1
  178. package/lib/types/client/ManagedIdentitySources/Imds.d.ts +44 -22
  179. package/lib/types/client/ManagedIdentitySources/Imds.d.ts.map +1 -1
  180. package/lib/types/client/ManagedIdentitySources/MachineLearning.d.ts +65 -0
  181. package/lib/types/client/ManagedIdentitySources/MachineLearning.d.ts.map +1 -1
  182. package/lib/types/client/ManagedIdentitySources/ServiceFabric.d.ts +56 -22
  183. package/lib/types/client/ManagedIdentitySources/ServiceFabric.d.ts.map +1 -1
  184. package/lib/types/client/OnBehalfOfClient.d.ts +2 -1
  185. package/lib/types/client/OnBehalfOfClient.d.ts.map +1 -1
  186. package/lib/types/client/PublicClientApplication.d.ts.map +1 -1
  187. package/lib/types/client/UsernamePasswordClient.d.ts +2 -1
  188. package/lib/types/client/UsernamePasswordClient.d.ts.map +1 -1
  189. package/lib/types/config/Configuration.d.ts +0 -6
  190. package/lib/types/config/Configuration.d.ts.map +1 -1
  191. package/lib/types/error/NodeAuthError.d.ts +8 -0
  192. package/lib/types/error/NodeAuthError.d.ts.map +1 -1
  193. package/lib/types/index.d.ts +0 -6
  194. package/lib/types/index.d.ts.map +1 -1
  195. package/lib/types/network/HttpClient.d.ts +54 -14
  196. package/lib/types/network/HttpClient.d.ts.map +1 -1
  197. package/lib/types/packageMetadata.d.ts +1 -1
  198. package/lib/types/packageMetadata.d.ts.map +1 -1
  199. package/lib/types/request/AuthorizationCodeRequest.d.ts +12 -10
  200. package/lib/types/request/AuthorizationCodeRequest.d.ts.map +1 -1
  201. package/lib/types/request/AuthorizationUrlRequest.d.ts +6 -24
  202. package/lib/types/request/AuthorizationUrlRequest.d.ts.map +1 -1
  203. package/lib/types/request/ClientCredentialRequest.d.ts +3 -6
  204. package/lib/types/request/ClientCredentialRequest.d.ts.map +1 -1
  205. package/lib/types/request/CommonClientCredentialRequest.d.ts +10 -7
  206. package/lib/types/request/CommonClientCredentialRequest.d.ts.map +1 -1
  207. package/lib/types/request/CommonDeviceCodeRequest.d.ts +13 -10
  208. package/lib/types/request/CommonDeviceCodeRequest.d.ts.map +1 -1
  209. package/lib/types/request/CommonOnBehalfOfRequest.d.ts +8 -7
  210. package/lib/types/request/CommonOnBehalfOfRequest.d.ts.map +1 -1
  211. package/lib/types/request/CommonUsernamePasswordRequest.d.ts +6 -8
  212. package/lib/types/request/CommonUsernamePasswordRequest.d.ts.map +1 -1
  213. package/lib/types/request/DeviceCodeRequest.d.ts +6 -6
  214. package/lib/types/request/DeviceCodeRequest.d.ts.map +1 -1
  215. package/lib/types/request/InteractiveRequest.d.ts +19 -8
  216. package/lib/types/request/InteractiveRequest.d.ts.map +1 -1
  217. package/lib/types/request/ManagedIdentityRequest.d.ts +6 -2
  218. package/lib/types/request/ManagedIdentityRequest.d.ts.map +1 -1
  219. package/lib/types/request/ManagedIdentityRequestParams.d.ts +9 -3
  220. package/lib/types/request/ManagedIdentityRequestParams.d.ts.map +1 -1
  221. package/lib/types/request/OnBehalfOfRequest.d.ts +7 -6
  222. package/lib/types/request/OnBehalfOfRequest.d.ts.map +1 -1
  223. package/lib/types/request/RefreshTokenRequest.d.ts +9 -7
  224. package/lib/types/request/RefreshTokenRequest.d.ts.map +1 -1
  225. package/lib/types/request/SilentFlowRequest.d.ts +6 -7
  226. package/lib/types/request/SilentFlowRequest.d.ts.map +1 -1
  227. package/lib/types/request/UsernamePasswordRequest.d.ts +9 -8
  228. package/lib/types/request/UsernamePasswordRequest.d.ts.map +1 -1
  229. package/lib/types/utils/Constants.d.ts +2 -9
  230. package/lib/types/utils/Constants.d.ts.map +1 -1
  231. package/package.json +88 -88
  232. package/src/client/BaseClient.ts +158 -0
  233. package/src/client/ClientCredentialClient.ts +2 -3
  234. package/src/client/DeviceCodeClient.ts +4 -5
  235. package/src/client/ManagedIdentitySources/AppService.ts +54 -0
  236. package/src/client/ManagedIdentitySources/AzureArc.ts +81 -0
  237. package/src/client/ManagedIdentitySources/BaseManagedIdentitySource.ts +92 -2
  238. package/src/client/ManagedIdentitySources/CloudShell.ts +54 -0
  239. package/src/client/ManagedIdentitySources/Imds.ts +43 -21
  240. package/src/client/ManagedIdentitySources/MachineLearning.ts +65 -0
  241. package/src/client/ManagedIdentitySources/ServiceFabric.ts +56 -22
  242. package/src/client/OnBehalfOfClient.ts +2 -3
  243. package/src/client/PublicClientApplication.ts +22 -4
  244. package/src/client/UsernamePasswordClient.ts +2 -3
  245. package/src/config/Configuration.ts +2 -14
  246. package/src/config/ManagedIdentityRequestParameters.ts +2 -2
  247. package/src/error/NodeAuthError.ts +14 -0
  248. package/src/index.ts +0 -8
  249. package/src/network/HttpClient.ts +166 -372
  250. package/src/packageMetadata.ts +1 -1
  251. package/src/protocol/Authorize.ts +1 -1
  252. package/src/request/AuthorizationCodeRequest.ts +12 -10
  253. package/src/request/AuthorizationUrlRequest.ts +6 -24
  254. package/src/request/ClientCredentialRequest.ts +3 -6
  255. package/src/request/CommonClientCredentialRequest.ts +13 -7
  256. package/src/request/CommonDeviceCodeRequest.ts +13 -14
  257. package/src/request/CommonOnBehalfOfRequest.ts +11 -7
  258. package/src/request/CommonUsernamePasswordRequest.ts +6 -8
  259. package/src/request/DeviceCodeRequest.ts +6 -6
  260. package/src/request/InteractiveRequest.ts +19 -11
  261. package/src/request/ManagedIdentityRequest.ts +6 -2
  262. package/src/request/ManagedIdentityRequestParams.ts +9 -3
  263. package/src/request/OnBehalfOfRequest.ts +7 -6
  264. package/src/request/RefreshTokenRequest.ts +9 -7
  265. package/src/request/SilentFlowRequest.ts +6 -7
  266. package/src/request/UsernamePasswordRequest.ts +9 -8
  267. package/src/utils/Constants.ts +2 -11
  268. package/dist/utils/NetworkUtils.mjs +0 -43
  269. package/dist/utils/NetworkUtils.mjs.map +0 -1
@@ -4,424 +4,218 @@
4
4
  */
5
5
 
6
6
  import {
7
- Constants as CommonConstants,
7
+ AuthError,
8
+ ClientAuthErrorCodes,
8
9
  INetworkModule,
9
10
  NetworkRequestOptions,
10
11
  NetworkResponse,
12
+ createAuthError,
13
+ createNetworkError,
11
14
  } from "@azure/msal-common/node";
12
- import { HttpMethod, Constants, ProxyStatus } from "../utils/Constants.js";
13
- import { NetworkUtils } from "../utils/NetworkUtils.js";
14
- import http from "http";
15
- import https from "https";
15
+ import { HttpMethod } from "../utils/Constants.js";
16
16
 
17
17
  /**
18
- * This class implements the API for network requests.
18
+ * HTTP client implementation using Node.js native fetch API.
19
+ *
20
+ * This class provides a clean interface for making HTTP requests using the modern
21
+ * fetch API available in Node.js 18+. It replaces the previous implementation that
22
+ * relied on custom proxy handling and the legacy http/https modules.
19
23
  */
20
24
  export class HttpClient implements INetworkModule {
21
- private proxyUrl: string;
22
- private customAgentOptions: http.AgentOptions | https.AgentOptions;
23
-
24
- constructor(
25
- proxyUrl?: string,
26
- customAgentOptions?: http.AgentOptions | https.AgentOptions
27
- ) {
28
- this.proxyUrl = proxyUrl || "";
29
- this.customAgentOptions = customAgentOptions || {};
30
- }
31
-
32
25
  /**
33
- * Http Get request
34
- * @param url
35
- * @param options
26
+ * Sends an HTTP GET request to the specified URL.
27
+ *
28
+ * This method handles GET requests with optional timeout support. The timeout
29
+ * is implemented using AbortController, which provides a clean way to cancel
30
+ * fetch requests that take too long to complete.
31
+ *
32
+ * @param url - The target URL for the GET request
33
+ * @param options - Optional request configuration including headers
34
+ * @param timeout - Optional timeout in milliseconds. If specified, the request
35
+ * will be aborted if it doesn't complete within this time
36
+ * @returns Promise that resolves to a NetworkResponse containing headers, body, and status
37
+ * @throws {AuthError} When the request times out or response parsing fails
38
+ * @throws {NetworkError} When the network request fails
36
39
  */
37
40
  async sendGetRequestAsync<T>(
38
41
  url: string,
39
42
  options?: NetworkRequestOptions,
40
43
  timeout?: number
41
44
  ): Promise<NetworkResponse<T>> {
42
- if (this.proxyUrl) {
43
- return networkRequestViaProxy(
44
- url,
45
- this.proxyUrl,
46
- HttpMethod.GET,
47
- options,
48
- this.customAgentOptions as http.AgentOptions,
49
- timeout
50
- );
51
- } else {
52
- return networkRequestViaHttps(
53
- url,
54
- HttpMethod.GET,
55
- options,
56
- this.customAgentOptions as https.AgentOptions,
57
- timeout
58
- );
59
- }
45
+ return this.sendRequest<T>(url, HttpMethod.GET, options, timeout);
60
46
  }
61
47
 
62
48
  /**
63
- * Http Post request
64
- * @param url
65
- * @param options
49
+ * Sends an HTTP POST request to the specified URL.
50
+ *
51
+ * This method handles POST requests with request body support. Currently,
52
+ * timeout functionality is not exposed for POST requests, but the underlying
53
+ * implementation supports it through the shared sendRequest method.
54
+ *
55
+ * @param url - The target URL for the POST request
56
+ * @param options - Optional request configuration including headers and body
57
+ * @returns Promise that resolves to a NetworkResponse containing headers, body, and status
58
+ * @throws {AuthError} When the request times out or response parsing fails
59
+ * @throws {NetworkError} When the network request fails
66
60
  */
67
61
  async sendPostRequestAsync<T>(
68
62
  url: string,
69
63
  options?: NetworkRequestOptions
70
64
  ): Promise<NetworkResponse<T>> {
71
- if (this.proxyUrl) {
72
- return networkRequestViaProxy(
73
- url,
74
- this.proxyUrl,
75
- HttpMethod.POST,
76
- options,
77
- this.customAgentOptions as http.AgentOptions
78
- );
79
- } else {
80
- return networkRequestViaHttps(
81
- url,
82
- HttpMethod.POST,
83
- options,
84
- this.customAgentOptions as https.AgentOptions
85
- );
86
- }
65
+ return this.sendRequest<T>(url, HttpMethod.POST, options);
87
66
  }
88
- }
89
67
 
90
- const networkRequestViaProxy = <T>(
91
- destinationUrlString: string,
92
- proxyUrlString: string,
93
- httpMethod: string,
94
- options?: NetworkRequestOptions,
95
- agentOptions?: http.AgentOptions,
96
- timeout?: number
97
- ): Promise<NetworkResponse<T>> => {
98
- const destinationUrl = new URL(destinationUrlString);
99
- const proxyUrl = new URL(proxyUrlString);
100
-
101
- // "method: connect" must be used to establish a connection to the proxy
102
- const headers = options?.headers || ({} as Record<string, string>);
103
- const tunnelRequestOptions: https.RequestOptions = {
104
- host: proxyUrl.hostname,
105
- port: proxyUrl.port,
106
- method: "CONNECT",
107
- path: destinationUrl.hostname,
108
- headers: headers,
109
- };
110
-
111
- if (agentOptions && Object.keys(agentOptions).length) {
112
- tunnelRequestOptions.agent = new http.Agent(agentOptions);
113
- }
114
-
115
- // compose a request string for the socket
116
- let postRequestStringContent: string = "";
117
- if (httpMethod === HttpMethod.POST) {
118
- const body = options?.body || "";
119
- postRequestStringContent =
120
- "Content-Type: application/x-www-form-urlencoded\r\n" +
121
- `Content-Length: ${body.length}\r\n` +
122
- `\r\n${body}`;
123
- } else {
124
- // optional timeout is only for get requests (regionDiscovery, for example)
68
+ /**
69
+ * Core HTTP request implementation using native fetch API.
70
+ *
71
+ * This method handles GET and POST HTTP requests with comprehensive
72
+ * timeout support and error handling. The timeout mechanism works as follows:
73
+ *
74
+ * 1. An AbortController is created for each request
75
+ * 2. If a timeout is specified, setTimeout is used to call abort() after the delay
76
+ * 3. The abort signal is passed to fetch, which will reject the promise if aborted
77
+ * 4. Cleanup occurs in both success and error cases to prevent timer leaks
78
+ *
79
+ * Error handling priority:
80
+ * 1. Timeout errors (AbortError) are converted to "Request timeout" messages
81
+ * 2. Network/connection errors are wrapped with "Network request failed" prefix
82
+ * 3. JSON parsing errors are wrapped with "Failed to parse response" prefix
83
+ *
84
+ * @param url - The target URL for the request
85
+ * @param method - HTTP method (GET or POST)
86
+ * @param options - Optional request configuration (headers, body)
87
+ * @param timeout - Optional timeout in milliseconds for request cancellation
88
+ * @returns Promise resolving to NetworkResponse with parsed JSON body
89
+ * @throws {AuthError} For timeouts or JSON parsing errors
90
+ * @throws {NetworkError} For network failures
91
+ */
92
+ private async sendRequest<T>(
93
+ url: string,
94
+ method: string,
95
+ options?: NetworkRequestOptions,
96
+ timeout?: number
97
+ ): Promise<NetworkResponse<T>> {
98
+ /*
99
+ * Setup timeout mechanism using AbortController
100
+ * This provides a standard way to cancel fetch requests
101
+ */
102
+ const controller = new AbortController();
103
+ let timeoutId: NodeJS.Timeout | undefined;
104
+
105
+ /*
106
+ * Configure timeout if specified
107
+ * The setTimeout will trigger abort() if the request takes too long
108
+ */
125
109
  if (timeout) {
126
- tunnelRequestOptions.timeout = timeout;
110
+ timeoutId = setTimeout(() => {
111
+ // Calling abort() will cause fetch to reject with AbortError
112
+ controller.abort();
113
+ }, timeout);
127
114
  }
128
- }
129
- const outgoingRequestString =
130
- `${httpMethod.toUpperCase()} ${destinationUrl.href} HTTP/1.1\r\n` +
131
- `Host: ${destinationUrl.host}\r\n` +
132
- "Connection: close\r\n" +
133
- postRequestStringContent +
134
- "\r\n";
135
115
 
136
- return new Promise<NetworkResponse<T>>((resolve, reject) => {
137
- const request = http.request(tunnelRequestOptions);
116
+ const fetchOptions: RequestInit = {
117
+ method: method,
118
+ headers: getFetchHeaders(options),
119
+ signal: controller.signal, // Enable cancellation via AbortController
120
+ };
138
121
 
139
- if (timeout) {
140
- request.on("timeout", () => {
141
- request.destroy();
142
- reject(new Error("Request time out"));
143
- });
122
+ if (method === HttpMethod.POST) {
123
+ fetchOptions.body = options?.body || "";
144
124
  }
145
125
 
146
- request.end();
147
-
148
- // establish connection to the proxy
149
- request.on("connect", (response, socket) => {
150
- const proxyStatusCode =
151
- response?.statusCode || ProxyStatus.SERVER_ERROR;
152
- if (
153
- proxyStatusCode < ProxyStatus.SUCCESS_RANGE_START ||
154
- proxyStatusCode > ProxyStatus.SUCCESS_RANGE_END
155
- ) {
156
- request.destroy();
157
- socket.destroy();
158
- reject(
159
- new Error(
160
- `Error connecting to proxy. Http status code: ${
161
- response.statusCode
162
- }. Http status message: ${
163
- response?.statusMessage || "Unknown"
164
- }`
165
- )
166
- );
126
+ let response: Response;
127
+ try {
128
+ response = await fetch(url, fetchOptions);
129
+ } catch (error) {
130
+ // Clean up timeout to prevent memory leaks
131
+ if (timeoutId) {
132
+ clearTimeout(timeoutId);
167
133
  }
168
134
 
169
- // make a request over an HTTP tunnel
170
- socket.write(outgoingRequestString);
171
-
172
- const data: Buffer[] = [];
173
- socket.on("data", (chunk) => {
174
- data.push(chunk);
175
- });
176
-
177
- socket.on("end", () => {
178
- // combine all received buffer streams into one buffer, and then into a string
179
- const dataString = Buffer.concat([...data]).toString();
180
-
181
- // separate each line into it's own entry in an arry
182
- const dataStringArray = dataString.split("\r\n");
183
- // the first entry will contain the statusCode and statusMessage
184
- const httpStatusCode = parseInt(
185
- dataStringArray[0].split(" ")[1]
135
+ if (error instanceof Error && error.name === "AbortError") {
136
+ throw createAuthError(
137
+ ClientAuthErrorCodes.networkError,
138
+ "Request timeout"
186
139
  );
187
- // remove "HTTP/1.1" and the status code to get the status message
188
- const statusMessage = dataStringArray[0]
189
- .split(" ")
190
- .slice(2)
191
- .join(" ");
192
- // the last entry will contain the body
193
- const body = dataStringArray[dataStringArray.length - 1];
194
-
195
- // everything in between the first and last entries are the headers
196
- const headersArray = dataStringArray.slice(
197
- 1,
198
- dataStringArray.length - 2
199
- );
200
-
201
- // build an object out of all the headers
202
- const entries = new Map();
203
- headersArray.forEach((header) => {
204
- /**
205
- * the header might look like "Content-Length: 1531", but that is just a string
206
- * it needs to be converted to a key/value pair
207
- * split the string at the first instance of ":"
208
- * there may be more than one ":" if the value of the header is supposed to be a JSON object
209
- */
210
- const headerKeyValue = header.split(new RegExp(/:\s(.*)/s));
211
- const headerKey = headerKeyValue[0];
212
- let headerValue = headerKeyValue[1];
213
-
214
- // check if the value of the header is supposed to be a JSON object
215
- try {
216
- const object = JSON.parse(headerValue);
217
-
218
- // if it is, then convert it from a string to a JSON object
219
- if (object && typeof object === "object") {
220
- headerValue = object;
221
- }
222
- } catch (e) {
223
- // otherwise, leave it as a string
224
- }
225
-
226
- entries.set(headerKey, headerValue);
227
- });
228
- const headers = Object.fromEntries(entries);
229
-
230
- const parsedHeaders = headers as Record<string, string>;
231
- const networkResponse = NetworkUtils.getNetworkResponse(
232
- parsedHeaders,
233
- parseBody(
234
- httpStatusCode,
235
- statusMessage,
236
- parsedHeaders,
237
- body
238
- ) as T,
239
- httpStatusCode
240
- );
241
-
242
- if (
243
- (httpStatusCode <
244
- CommonConstants.HTTP_SUCCESS_RANGE_START ||
245
- httpStatusCode >
246
- CommonConstants.HTTP_SUCCESS_RANGE_END) &&
247
- // do not destroy the request for the device code flow
248
- networkResponse.body["error"] !==
249
- Constants.AUTHORIZATION_PENDING
250
- ) {
251
- request.destroy();
252
- }
253
- resolve(networkResponse);
254
- });
255
-
256
- socket.on("error", (chunk) => {
257
- request.destroy();
258
- socket.destroy();
259
- reject(new Error(chunk.toString()));
260
- });
261
- });
262
-
263
- request.on("error", (chunk) => {
264
- request.destroy();
265
- reject(new Error(chunk.toString()));
266
- });
267
- });
268
- };
269
-
270
- const networkRequestViaHttps = <T>(
271
- urlString: string,
272
- httpMethod: string,
273
- options?: NetworkRequestOptions,
274
- agentOptions?: https.AgentOptions,
275
- timeout?: number
276
- ): Promise<NetworkResponse<T>> => {
277
- const isPostRequest = httpMethod === HttpMethod.POST;
278
- const body: string = options?.body || "";
279
-
280
- const url = new URL(urlString);
281
- const headers = options?.headers || ({} as Record<string, string>);
282
- const customOptions: https.RequestOptions = {
283
- method: httpMethod,
284
- headers: headers,
285
- ...NetworkUtils.urlToHttpOptions(url),
286
- };
287
-
288
- if (agentOptions && Object.keys(agentOptions).length) {
289
- customOptions.agent = new https.Agent(agentOptions);
290
- }
291
-
292
- if (isPostRequest) {
293
- // needed for post request to work
294
- customOptions.headers = {
295
- ...customOptions.headers,
296
- "Content-Length": body.length,
297
- };
298
- } else {
299
- // optional timeout is only for get requests (regionDiscovery, for example)
300
- if (timeout) {
301
- customOptions.timeout = timeout;
302
- }
303
- }
140
+ }
304
141
 
305
- return new Promise<NetworkResponse<T>>((resolve, reject) => {
306
- let request: http.ClientRequest;
307
- // managed identity sources use http instead of https
308
- if (customOptions.protocol === "http:") {
309
- request = http.request(customOptions);
310
- } else {
311
- request = https.request(customOptions);
142
+ const baseAuthError: AuthError = createAuthError(
143
+ ClientAuthErrorCodes.networkError,
144
+ `Network request failed: ${
145
+ error instanceof Error ? error.message : "unknown"
146
+ }`
147
+ );
148
+ throw createNetworkError(
149
+ baseAuthError,
150
+ undefined,
151
+ undefined,
152
+ error instanceof Error ? error : undefined
153
+ );
312
154
  }
313
155
 
314
- if (isPostRequest) {
315
- request.write(body);
156
+ // Clean up timeout to prevent memory leaks
157
+ if (timeoutId) {
158
+ clearTimeout(timeoutId);
316
159
  }
317
160
 
318
- if (timeout) {
319
- request.on("timeout", () => {
320
- request.destroy();
321
- reject(new Error("Request time out"));
322
- });
161
+ try {
162
+ return {
163
+ headers: getHeaderDict(response.headers),
164
+ body: (await response.json()) as T,
165
+ status: response.status,
166
+ };
167
+ } catch (error) {
168
+ throw createAuthError(
169
+ ClientAuthErrorCodes.tokenParsingError,
170
+ `Failed to parse response: ${
171
+ error instanceof Error ? error.message : "unknown"
172
+ }`
173
+ );
323
174
  }
175
+ }
176
+ }
324
177
 
325
- request.end();
326
-
327
- request.on("response", (response) => {
328
- const headers = response.headers;
329
- const statusCode = response.statusCode as number;
330
- const statusMessage = response.statusMessage;
331
-
332
- const data: Buffer[] = [];
333
- response.on("data", (chunk) => {
334
- data.push(chunk);
335
- });
336
-
337
- response.on("end", () => {
338
- // combine all received buffer streams into one buffer, and then into a string
339
- const body = Buffer.concat([...data]).toString();
340
-
341
- const parsedHeaders = headers as Record<string, string>;
342
- const networkResponse = NetworkUtils.getNetworkResponse(
343
- parsedHeaders,
344
- parseBody(
345
- statusCode,
346
- statusMessage,
347
- parsedHeaders,
348
- body
349
- ) as T,
350
- statusCode
351
- );
352
-
353
- if (
354
- (statusCode < CommonConstants.HTTP_SUCCESS_RANGE_START ||
355
- statusCode > CommonConstants.HTTP_SUCCESS_RANGE_END) &&
356
- // do not destroy the request for the device code flow
357
- networkResponse.body["error"] !==
358
- Constants.AUTHORIZATION_PENDING
359
- ) {
360
- request.destroy();
361
- }
362
- resolve(networkResponse);
363
- });
364
- });
178
+ /**
179
+ * Converts a fetch Headers object to a plain JavaScript object.
180
+ *
181
+ * The fetch API returns headers as a Headers object with methods like get(), has(),
182
+ * etc. However, the rest of the MSAL codebase expects headers as a simple key-value
183
+ * object. This function performs that conversion.
184
+ *
185
+ * @param headers - The Headers object returned by fetch response
186
+ * @returns A plain object with header names as keys and values as strings
187
+ */
188
+ function getHeaderDict(headers: Headers): Record<string, string> {
189
+ const headerDict: Record<string, string> = {};
365
190
 
366
- request.on("error", (chunk) => {
367
- request.destroy();
368
- reject(new Error(chunk.toString()));
369
- });
191
+ headers.forEach((value: string, key: string) => {
192
+ headerDict[key] = value;
370
193
  });
371
- };
194
+
195
+ return headerDict;
196
+ }
372
197
 
373
198
  /**
374
- * Check if extra parsing is needed on the repsonse from the server
375
- * @param statusCode {number} the status code of the response from the server
376
- * @param statusMessage {string | undefined} the status message of the response from the server
377
- * @param headers {Record<string, string>} the headers of the response from the server
378
- * @param body {string} the body from the response of the server
379
- * @returns {Object} JSON parsed body or error object
199
+ * Converts NetworkRequestOptions headers to a fetch-compatible Headers object.
200
+ *
201
+ * The MSAL library uses plain objects for headers in NetworkRequestOptions,
202
+ * but the fetch API expects either a Headers object, plain object, or array
203
+ * of arrays. Using the Headers constructor provides better compatibility
204
+ * and validation.
205
+ *
206
+ * @param options - Optional NetworkRequestOptions containing headers
207
+ * @returns A Headers object ready for use with fetch API
380
208
  */
381
- const parseBody = (
382
- statusCode: number,
383
- statusMessage: string | undefined,
384
- headers: Record<string, string>,
385
- body: string
386
- ) => {
387
- /*
388
- * Informational responses (100 – 199)
389
- * Successful responses (200 – 299)
390
- * Redirection messages (300 – 399)
391
- * Client error responses (400 – 499)
392
- * Server error responses (500 – 599)
393
- */
394
-
395
- let parsedBody;
396
- try {
397
- parsedBody = JSON.parse(body);
398
- } catch (error) {
399
- let errorType;
400
- let errorDescriptionHelper;
401
- if (
402
- statusCode >= CommonConstants.HTTP_CLIENT_ERROR_RANGE_START &&
403
- statusCode <= CommonConstants.HTTP_CLIENT_ERROR_RANGE_END
404
- ) {
405
- errorType = "client_error";
406
- errorDescriptionHelper = "A client";
407
- } else if (
408
- statusCode >= CommonConstants.HTTP_SERVER_ERROR_RANGE_START &&
409
- statusCode <= CommonConstants.HTTP_SERVER_ERROR_RANGE_END
410
- ) {
411
- errorType = "server_error";
412
- errorDescriptionHelper = "A server";
413
- } else {
414
- errorType = "unknown_error";
415
- errorDescriptionHelper = "An unknown";
416
- }
209
+ function getFetchHeaders(options?: NetworkRequestOptions): Headers {
210
+ const headers = new Headers();
417
211
 
418
- parsedBody = {
419
- error: errorType,
420
- error_description: `${errorDescriptionHelper} error occured.\nHttp status code: ${statusCode}\nHttp status message: ${
421
- statusMessage || "Unknown"
422
- }\nHeaders: ${JSON.stringify(headers)}`,
423
- };
212
+ if (!(options && options.headers)) {
213
+ return headers;
424
214
  }
425
215
 
426
- return parsedBody;
427
- };
216
+ Object.entries(options.headers).forEach(([key, value]) => {
217
+ headers.append(key, value);
218
+ });
219
+
220
+ return headers;
221
+ }
@@ -1,3 +1,3 @@
1
1
  /* eslint-disable header/header */
2
2
  export const name = "@azure/msal-node";
3
- export const version = "5.0.0-alpha.0";
3
+ export const version = "5.0.1";
@@ -63,7 +63,7 @@ export function getAuthCodeRequestUrl(
63
63
  );
64
64
  }
65
65
 
66
- RequestParameterBuilder.addExtraQueryParameters(
66
+ RequestParameterBuilder.addExtraParameters(
67
67
  parameters,
68
68
  request.extraQueryParameters || {}
69
69
  );
@@ -7,16 +7,6 @@ import { CommonAuthorizationCodeRequest } from "@azure/msal-common/node";
7
7
 
8
8
  /**
9
9
  * Request object passed by user to acquire a token from the server exchanging a valid authorization code (second leg of OAuth2.0 Authorization Code flow)
10
- *
11
- * - scopes - Array of scopes the application is requesting access to.
12
- * - claims - A stringified claims request which will be added to all /authorize and /token calls
13
- * - authority: - URL of the authority, the security token service (STS) from which MSAL will acquire tokens. If authority is set on client application object, this will override that value. Overriding the value will cause for authority validation to happen each time. If the same authority will be used for all request, set on the application object instead of the requests.
14
- * - correlationId - Unique GUID set per request to trace a request end-to-end for telemetry purposes.
15
- * - redirectUri - The redirect URI of your app, where the authority will redirect to after the user inputs credentials and consents. It must exactly match one of the redirect URIs you registered in the portal.
16
- * - tokenQueryParameters - String to string map of custom query parameters added to the /token call
17
- * - code - The authorization_code that the user acquired in the first leg of the flow.
18
- * - codeVerifier - The same code_verifier that was used to obtain the authorization_code. Required if PKCE was used in the authorization code grant request.For more information, see the PKCE RFC: https://tools.ietf.org/html/rfc7636
19
- * - state - Unique GUID generated by the user that is cached by the user and sent to the server during the first leg of the flow. This string is sent back by the server with the authorization code. The user cached state is then compared with the state received from the server to mitigate the risk of CSRF attacks. See https://datatracker.ietf.org/doc/html/rfc6819#section-3.6.
20
10
  * @public
21
11
  */
22
12
  export type AuthorizationCodeRequest = Partial<
@@ -31,8 +21,20 @@ export type AuthorizationCodeRequest = Partial<
31
21
  | "storeInCache"
32
22
  >
33
23
  > & {
24
+ /**
25
+ * Array of scopes the application is requesting access to.
26
+ */
34
27
  scopes: Array<string>;
28
+ /**
29
+ * The redirect URI of your app, where the authority will redirect to after the user inputs credentials and consents. It must exactly match one of the redirect URIs you registered in the portal.
30
+ */
35
31
  redirectUri: string;
32
+ /**
33
+ * The authorization_code that the user acquired in the first leg of the flow.
34
+ */
36
35
  code: string;
36
+ /**
37
+ * Unique GUID generated by the user that is cached by the user and sent to the server during the first leg of the flow. This string is sent back by the server with the authorization code. The user cached state is then compared with the state received from the server to mitigate the risk of CSRF attacks. See https://datatracker.ietf.org/doc/html/rfc6819#section-3.6.
38
+ */
37
39
  state?: string;
38
40
  };