@azure/msal-node 2.12.0 → 2.13.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 (159) hide show
  1. package/dist/cache/ITokenCache.d.ts +15 -15
  2. package/dist/cache/NodeStorage.cjs +423 -423
  3. package/dist/cache/NodeStorage.d.ts +193 -193
  4. package/dist/cache/NodeStorage.mjs +423 -423
  5. package/dist/cache/TokenCache.cjs +265 -265
  6. package/dist/cache/TokenCache.d.ts +90 -90
  7. package/dist/cache/TokenCache.mjs +265 -265
  8. package/dist/cache/distributed/DistributedCachePlugin.cjs +26 -26
  9. package/dist/cache/distributed/DistributedCachePlugin.d.ts +10 -10
  10. package/dist/cache/distributed/DistributedCachePlugin.mjs +26 -26
  11. package/dist/cache/distributed/ICacheClient.d.ts +17 -17
  12. package/dist/cache/distributed/IPartitionManager.d.ts +28 -28
  13. package/dist/cache/serializer/Deserializer.cjs +167 -167
  14. package/dist/cache/serializer/Deserializer.d.ts +42 -42
  15. package/dist/cache/serializer/Deserializer.mjs +167 -167
  16. package/dist/cache/serializer/Serializer.cjs +137 -137
  17. package/dist/cache/serializer/Serializer.d.ts +39 -39
  18. package/dist/cache/serializer/Serializer.mjs +137 -137
  19. package/dist/cache/serializer/SerializerTypes.d.ts +102 -102
  20. package/dist/client/ClientApplication.cjs +334 -334
  21. package/dist/client/ClientApplication.d.ts +143 -143
  22. package/dist/client/ClientApplication.mjs +334 -334
  23. package/dist/client/ClientAssertion.cjs +143 -143
  24. package/dist/client/ClientAssertion.d.ts +55 -55
  25. package/dist/client/ClientAssertion.mjs +143 -143
  26. package/dist/client/ClientCredentialClient.cjs +194 -194
  27. package/dist/client/ClientCredentialClient.d.ts +33 -33
  28. package/dist/client/ClientCredentialClient.mjs +194 -194
  29. package/dist/client/ConfidentialClientApplication.cjs +169 -169
  30. package/dist/client/ConfidentialClientApplication.cjs.map +1 -1
  31. package/dist/client/ConfidentialClientApplication.d.ts +58 -58
  32. package/dist/client/ConfidentialClientApplication.mjs +169 -169
  33. package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
  34. package/dist/client/DeviceCodeClient.cjs +207 -207
  35. package/dist/client/DeviceCodeClient.d.ts +53 -53
  36. package/dist/client/DeviceCodeClient.mjs +207 -207
  37. package/dist/client/IConfidentialClientApplication.d.ts +39 -39
  38. package/dist/client/IPublicClientApplication.d.ts +42 -42
  39. package/dist/client/ManagedIdentityApplication.cjs +84 -84
  40. package/dist/client/ManagedIdentityApplication.d.ts +30 -30
  41. package/dist/client/ManagedIdentityApplication.mjs +84 -84
  42. package/dist/client/ManagedIdentityClient.cjs +60 -60
  43. package/dist/client/ManagedIdentityClient.d.ts +27 -27
  44. package/dist/client/ManagedIdentityClient.mjs +60 -60
  45. package/dist/client/ManagedIdentitySources/AppService.cjs +45 -45
  46. package/dist/client/ManagedIdentitySources/AppService.d.ts +17 -17
  47. package/dist/client/ManagedIdentitySources/AppService.mjs +45 -45
  48. package/dist/client/ManagedIdentitySources/AzureArc.cjs +155 -120
  49. package/dist/client/ManagedIdentitySources/AzureArc.cjs.map +1 -1
  50. package/dist/client/ManagedIdentitySources/AzureArc.d.ts +27 -23
  51. package/dist/client/ManagedIdentitySources/AzureArc.d.ts.map +1 -1
  52. package/dist/client/ManagedIdentitySources/AzureArc.mjs +155 -122
  53. package/dist/client/ManagedIdentitySources/AzureArc.mjs.map +1 -1
  54. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs +122 -122
  55. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs.map +1 -1
  56. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts +30 -30
  57. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs +122 -122
  58. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs.map +1 -1
  59. package/dist/client/ManagedIdentitySources/CloudShell.cjs +38 -38
  60. package/dist/client/ManagedIdentitySources/CloudShell.d.ts +16 -16
  61. package/dist/client/ManagedIdentitySources/CloudShell.mjs +38 -38
  62. package/dist/client/ManagedIdentitySources/Imds.cjs +43 -43
  63. package/dist/client/ManagedIdentitySources/Imds.d.ts +12 -12
  64. package/dist/client/ManagedIdentitySources/Imds.mjs +43 -43
  65. package/dist/client/ManagedIdentitySources/ServiceFabric.cjs +54 -54
  66. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts +17 -17
  67. package/dist/client/ManagedIdentitySources/ServiceFabric.mjs +54 -54
  68. package/dist/client/OnBehalfOfClient.cjs +202 -202
  69. package/dist/client/OnBehalfOfClient.d.ts +47 -47
  70. package/dist/client/OnBehalfOfClient.mjs +202 -202
  71. package/dist/client/PublicClientApplication.cjs +237 -237
  72. package/dist/client/PublicClientApplication.d.ts +72 -72
  73. package/dist/client/PublicClientApplication.mjs +237 -237
  74. package/dist/client/UsernamePasswordClient.cjs +95 -95
  75. package/dist/client/UsernamePasswordClient.d.ts +25 -25
  76. package/dist/client/UsernamePasswordClient.mjs +95 -95
  77. package/dist/config/Configuration.cjs +106 -106
  78. package/dist/config/Configuration.d.ts +131 -131
  79. package/dist/config/Configuration.mjs +106 -106
  80. package/dist/config/ManagedIdentityId.cjs +48 -48
  81. package/dist/config/ManagedIdentityId.d.ts +11 -11
  82. package/dist/config/ManagedIdentityId.mjs +48 -48
  83. package/dist/config/ManagedIdentityRequestParameters.cjs +28 -28
  84. package/dist/config/ManagedIdentityRequestParameters.d.ts +11 -11
  85. package/dist/config/ManagedIdentityRequestParameters.mjs +28 -28
  86. package/dist/crypto/CryptoProvider.cjs +89 -89
  87. package/dist/crypto/CryptoProvider.d.ts +62 -62
  88. package/dist/crypto/CryptoProvider.mjs +89 -89
  89. package/dist/crypto/GuidGenerator.cjs +22 -22
  90. package/dist/crypto/GuidGenerator.d.ts +14 -14
  91. package/dist/crypto/GuidGenerator.mjs +22 -22
  92. package/dist/crypto/HashUtils.cjs +13 -13
  93. package/dist/crypto/HashUtils.d.ts +8 -8
  94. package/dist/crypto/HashUtils.mjs +13 -13
  95. package/dist/crypto/PkceGenerator.cjs +49 -49
  96. package/dist/crypto/PkceGenerator.d.ts +22 -22
  97. package/dist/crypto/PkceGenerator.mjs +49 -49
  98. package/dist/error/ManagedIdentityError.cjs +41 -41
  99. package/dist/error/ManagedIdentityError.d.ts +30 -30
  100. package/dist/error/ManagedIdentityError.mjs +41 -41
  101. package/dist/error/ManagedIdentityErrorCodes.cjs +24 -24
  102. package/dist/error/ManagedIdentityErrorCodes.d.ts +22 -22
  103. package/dist/error/ManagedIdentityErrorCodes.mjs +24 -24
  104. package/dist/error/NodeAuthError.cjs +95 -95
  105. package/dist/error/NodeAuthError.d.ts +73 -73
  106. package/dist/error/NodeAuthError.mjs +95 -95
  107. package/dist/index.cjs +1 -1
  108. package/dist/index.d.ts +45 -45
  109. package/dist/index.mjs +1 -1
  110. package/dist/internals.cjs +8 -8
  111. package/dist/internals.d.ts +6 -6
  112. package/dist/internals.mjs +8 -8
  113. package/dist/network/HttpClient.cjs +281 -281
  114. package/dist/network/HttpClient.d.ts +25 -25
  115. package/dist/network/HttpClient.mjs +281 -281
  116. package/dist/network/HttpClientWithRetries.cjs +34 -34
  117. package/dist/network/HttpClientWithRetries.d.ts +11 -11
  118. package/dist/network/HttpClientWithRetries.mjs +34 -34
  119. package/dist/network/ILoopbackClient.d.ts +10 -10
  120. package/dist/network/LoopbackClient.cjs +81 -77
  121. package/dist/network/LoopbackClient.cjs.map +1 -1
  122. package/dist/network/LoopbackClient.d.ts +21 -21
  123. package/dist/network/LoopbackClient.d.ts.map +1 -1
  124. package/dist/network/LoopbackClient.mjs +81 -77
  125. package/dist/network/LoopbackClient.mjs.map +1 -1
  126. package/dist/packageMetadata.cjs +4 -4
  127. package/dist/packageMetadata.d.ts +2 -2
  128. package/dist/packageMetadata.mjs +4 -4
  129. package/dist/request/AuthorizationCodeRequest.d.ts +21 -21
  130. package/dist/request/AuthorizationUrlRequest.d.ts +33 -33
  131. package/dist/request/ClientCredentialRequest.d.ts +14 -14
  132. package/dist/request/DeviceCodeRequest.d.ts +15 -15
  133. package/dist/request/InteractiveRequest.d.ts +22 -22
  134. package/dist/request/ManagedIdentityRequest.d.ts +8 -8
  135. package/dist/request/ManagedIdentityRequestParams.d.ts +9 -9
  136. package/dist/request/OnBehalfOfRequest.d.ts +14 -14
  137. package/dist/request/RefreshTokenRequest.d.ts +17 -17
  138. package/dist/request/SignOutRequest.d.ts +5 -5
  139. package/dist/request/SilentFlowRequest.d.ts +16 -16
  140. package/dist/request/UsernamePasswordRequest.d.ts +19 -19
  141. package/dist/response/ManagedIdentityTokenResponse.d.ts +34 -34
  142. package/dist/retry/IHttpRetryPolicy.d.ts +5 -5
  143. package/dist/retry/LinearRetryPolicy.cjs +40 -40
  144. package/dist/retry/LinearRetryPolicy.d.ts +11 -11
  145. package/dist/retry/LinearRetryPolicy.mjs +40 -40
  146. package/dist/utils/Constants.cjs +136 -136
  147. package/dist/utils/Constants.d.ts +141 -141
  148. package/dist/utils/Constants.mjs +136 -136
  149. package/dist/utils/EncodingUtils.cjs +44 -44
  150. package/dist/utils/EncodingUtils.d.ts +26 -26
  151. package/dist/utils/EncodingUtils.mjs +44 -44
  152. package/dist/utils/NetworkUtils.cjs +38 -38
  153. package/dist/utils/NetworkUtils.d.ts +5 -5
  154. package/dist/utils/NetworkUtils.mjs +38 -38
  155. package/package.json +2 -2
  156. package/src/client/ConfidentialClientApplication.ts +3 -3
  157. package/src/client/ManagedIdentitySources/AzureArc.ts +80 -25
  158. package/src/network/LoopbackClient.ts +7 -1
  159. package/src/packageMetadata.ts +1 -1
@@ -1,4 +1,4 @@
1
- /*! @azure/msal-node v2.12.0 2024-07-23 */
1
+ /*! @azure/msal-node v2.13.1 2024-08-29 */
2
2
  'use strict';
3
3
  import { HttpStatus } from '@azure/msal-common';
4
4
  import { ProxyStatus, Constants, HttpMethod } from '../utils/Constants.mjs';
@@ -6,286 +6,286 @@ import { NetworkUtils } from '../utils/NetworkUtils.mjs';
6
6
  import http from 'http';
7
7
  import https from 'https';
8
8
 
9
- /*
10
- * Copyright (c) Microsoft Corporation. All rights reserved.
11
- * Licensed under the MIT License.
12
- */
13
- /**
14
- * This class implements the API for network requests.
15
- */
16
- class HttpClient {
17
- constructor(proxyUrl, customAgentOptions) {
18
- this.proxyUrl = proxyUrl || "";
19
- this.customAgentOptions = customAgentOptions || {};
20
- }
21
- /**
22
- * Http Get request
23
- * @param url
24
- * @param options
25
- */
26
- async sendGetRequestAsync(url, options, timeout) {
27
- if (this.proxyUrl) {
28
- return networkRequestViaProxy(url, this.proxyUrl, HttpMethod.GET, options, this.customAgentOptions, timeout);
29
- }
30
- else {
31
- return networkRequestViaHttps(url, HttpMethod.GET, options, this.customAgentOptions, timeout);
32
- }
33
- }
34
- /**
35
- * Http Post request
36
- * @param url
37
- * @param options
38
- */
39
- async sendPostRequestAsync(url, options) {
40
- if (this.proxyUrl) {
41
- return networkRequestViaProxy(url, this.proxyUrl, HttpMethod.POST, options, this.customAgentOptions);
42
- }
43
- else {
44
- return networkRequestViaHttps(url, HttpMethod.POST, options, this.customAgentOptions);
45
- }
46
- }
47
- }
48
- const networkRequestViaProxy = (destinationUrlString, proxyUrlString, httpMethod, options, agentOptions, timeout) => {
49
- const destinationUrl = new URL(destinationUrlString);
50
- const proxyUrl = new URL(proxyUrlString);
51
- // "method: connect" must be used to establish a connection to the proxy
52
- const headers = options?.headers || {};
53
- const tunnelRequestOptions = {
54
- host: proxyUrl.hostname,
55
- port: proxyUrl.port,
56
- method: "CONNECT",
57
- path: destinationUrl.hostname,
58
- headers: headers,
59
- };
60
- if (agentOptions && Object.keys(agentOptions).length) {
61
- tunnelRequestOptions.agent = new http.Agent(agentOptions);
62
- }
63
- // compose a request string for the socket
64
- let postRequestStringContent = "";
65
- if (httpMethod === HttpMethod.POST) {
66
- const body = options?.body || "";
67
- postRequestStringContent =
68
- "Content-Type: application/x-www-form-urlencoded\r\n" +
69
- `Content-Length: ${body.length}\r\n` +
70
- `\r\n${body}`;
71
- }
72
- else {
73
- // optional timeout is only for get requests (regionDiscovery, for example)
74
- if (timeout) {
75
- tunnelRequestOptions.timeout = timeout;
76
- }
77
- }
78
- const outgoingRequestString = `${httpMethod.toUpperCase()} ${destinationUrl.href} HTTP/1.1\r\n` +
79
- `Host: ${destinationUrl.host}\r\n` +
80
- "Connection: close\r\n" +
81
- postRequestStringContent +
82
- "\r\n";
83
- return new Promise((resolve, reject) => {
84
- const request = http.request(tunnelRequestOptions);
85
- if (timeout) {
86
- request.on("timeout", () => {
87
- request.destroy();
88
- reject(new Error("Request time out"));
89
- });
90
- }
91
- request.end();
92
- // establish connection to the proxy
93
- request.on("connect", (response, socket) => {
94
- const proxyStatusCode = response?.statusCode || ProxyStatus.SERVER_ERROR;
95
- if (proxyStatusCode < ProxyStatus.SUCCESS_RANGE_START ||
96
- proxyStatusCode > ProxyStatus.SUCCESS_RANGE_END) {
97
- request.destroy();
98
- socket.destroy();
99
- reject(new Error(`Error connecting to proxy. Http status code: ${response.statusCode}. Http status message: ${response?.statusMessage || "Unknown"}`));
100
- }
101
- // make a request over an HTTP tunnel
102
- socket.write(outgoingRequestString);
103
- const data = [];
104
- socket.on("data", (chunk) => {
105
- data.push(chunk);
106
- });
107
- socket.on("end", () => {
108
- // combine all received buffer streams into one buffer, and then into a string
109
- const dataString = Buffer.concat([...data]).toString();
110
- // separate each line into it's own entry in an arry
111
- const dataStringArray = dataString.split("\r\n");
112
- // the first entry will contain the statusCode and statusMessage
113
- const httpStatusCode = parseInt(dataStringArray[0].split(" ")[1]);
114
- // remove "HTTP/1.1" and the status code to get the status message
115
- const statusMessage = dataStringArray[0]
116
- .split(" ")
117
- .slice(2)
118
- .join(" ");
119
- // the last entry will contain the body
120
- const body = dataStringArray[dataStringArray.length - 1];
121
- // everything in between the first and last entries are the headers
122
- const headersArray = dataStringArray.slice(1, dataStringArray.length - 2);
123
- // build an object out of all the headers
124
- const entries = new Map();
125
- headersArray.forEach((header) => {
126
- /**
127
- * the header might look like "Content-Length: 1531", but that is just a string
128
- * it needs to be converted to a key/value pair
129
- * split the string at the first instance of ":"
130
- * there may be more than one ":" if the value of the header is supposed to be a JSON object
131
- */
132
- const headerKeyValue = header.split(new RegExp(/:\s(.*)/s));
133
- const headerKey = headerKeyValue[0];
134
- let headerValue = headerKeyValue[1];
135
- // check if the value of the header is supposed to be a JSON object
136
- try {
137
- const object = JSON.parse(headerValue);
138
- // if it is, then convert it from a string to a JSON object
139
- if (object && typeof object === "object") {
140
- headerValue = object;
141
- }
142
- }
143
- catch (e) {
144
- // otherwise, leave it as a string
145
- }
146
- entries.set(headerKey, headerValue);
147
- });
148
- const headers = Object.fromEntries(entries);
149
- const parsedHeaders = headers;
150
- const networkResponse = NetworkUtils.getNetworkResponse(parsedHeaders, parseBody(httpStatusCode, statusMessage, parsedHeaders, body), httpStatusCode);
151
- if ((httpStatusCode < HttpStatus.SUCCESS_RANGE_START ||
152
- httpStatusCode > HttpStatus.SUCCESS_RANGE_END) &&
153
- // do not destroy the request for the device code flow
154
- networkResponse.body["error"] !==
155
- Constants.AUTHORIZATION_PENDING) {
156
- request.destroy();
157
- }
158
- resolve(networkResponse);
159
- });
160
- socket.on("error", (chunk) => {
161
- request.destroy();
162
- socket.destroy();
163
- reject(new Error(chunk.toString()));
164
- });
165
- });
166
- request.on("error", (chunk) => {
167
- request.destroy();
168
- reject(new Error(chunk.toString()));
169
- });
170
- });
171
- };
172
- const networkRequestViaHttps = (urlString, httpMethod, options, agentOptions, timeout) => {
173
- const isPostRequest = httpMethod === HttpMethod.POST;
174
- const body = options?.body || "";
175
- const url = new URL(urlString);
176
- const headers = options?.headers || {};
177
- const customOptions = {
178
- method: httpMethod,
179
- headers: headers,
180
- ...NetworkUtils.urlToHttpOptions(url),
181
- };
182
- if (agentOptions && Object.keys(agentOptions).length) {
183
- customOptions.agent = new https.Agent(agentOptions);
184
- }
185
- if (isPostRequest) {
186
- // needed for post request to work
187
- customOptions.headers = {
188
- ...customOptions.headers,
189
- "Content-Length": body.length,
190
- };
191
- }
192
- else {
193
- // optional timeout is only for get requests (regionDiscovery, for example)
194
- if (timeout) {
195
- customOptions.timeout = timeout;
196
- }
197
- }
198
- return new Promise((resolve, reject) => {
199
- let request;
200
- // managed identity sources use http instead of https
201
- if (customOptions.protocol === "http:") {
202
- request = http.request(customOptions);
203
- }
204
- else {
205
- request = https.request(customOptions);
206
- }
207
- if (isPostRequest) {
208
- request.write(body);
209
- }
210
- if (timeout) {
211
- request.on("timeout", () => {
212
- request.destroy();
213
- reject(new Error("Request time out"));
214
- });
215
- }
216
- request.end();
217
- request.on("response", (response) => {
218
- const headers = response.headers;
219
- const statusCode = response.statusCode;
220
- const statusMessage = response.statusMessage;
221
- const data = [];
222
- response.on("data", (chunk) => {
223
- data.push(chunk);
224
- });
225
- response.on("end", () => {
226
- // combine all received buffer streams into one buffer, and then into a string
227
- const body = Buffer.concat([...data]).toString();
228
- const parsedHeaders = headers;
229
- const networkResponse = NetworkUtils.getNetworkResponse(parsedHeaders, parseBody(statusCode, statusMessage, parsedHeaders, body), statusCode);
230
- if ((statusCode < HttpStatus.SUCCESS_RANGE_START ||
231
- statusCode > HttpStatus.SUCCESS_RANGE_END) &&
232
- // do not destroy the request for the device code flow
233
- networkResponse.body["error"] !==
234
- Constants.AUTHORIZATION_PENDING) {
235
- request.destroy();
236
- }
237
- resolve(networkResponse);
238
- });
239
- });
240
- request.on("error", (chunk) => {
241
- request.destroy();
242
- reject(new Error(chunk.toString()));
243
- });
244
- });
245
- };
246
- /**
247
- * Check if extra parsing is needed on the repsonse from the server
248
- * @param statusCode {number} the status code of the response from the server
249
- * @param statusMessage {string | undefined} the status message of the response from the server
250
- * @param headers {Record<string, string>} the headers of the response from the server
251
- * @param body {string} the body from the response of the server
252
- * @returns {Object} JSON parsed body or error object
253
- */
254
- const parseBody = (statusCode, statusMessage, headers, body) => {
255
- /*
256
- * Informational responses (100 – 199)
257
- * Successful responses (200 – 299)
258
- * Redirection messages (300 – 399)
259
- * Client error responses (400 – 499)
260
- * Server error responses (500 – 599)
261
- */
262
- let parsedBody;
263
- try {
264
- parsedBody = JSON.parse(body);
265
- }
266
- catch (error) {
267
- let errorType;
268
- let errorDescriptionHelper;
269
- if (statusCode >= HttpStatus.CLIENT_ERROR_RANGE_START &&
270
- statusCode <= HttpStatus.CLIENT_ERROR_RANGE_END) {
271
- errorType = "client_error";
272
- errorDescriptionHelper = "A client";
273
- }
274
- else if (statusCode >= HttpStatus.SERVER_ERROR_RANGE_START &&
275
- statusCode <= HttpStatus.SERVER_ERROR_RANGE_END) {
276
- errorType = "server_error";
277
- errorDescriptionHelper = "A server";
278
- }
279
- else {
280
- errorType = "unknown_error";
281
- errorDescriptionHelper = "An unknown";
282
- }
283
- parsedBody = {
284
- error: errorType,
285
- error_description: `${errorDescriptionHelper} error occured.\nHttp status code: ${statusCode}\nHttp status message: ${statusMessage || "Unknown"}\nHeaders: ${JSON.stringify(headers)}`,
286
- };
287
- }
288
- return parsedBody;
9
+ /*
10
+ * Copyright (c) Microsoft Corporation. All rights reserved.
11
+ * Licensed under the MIT License.
12
+ */
13
+ /**
14
+ * This class implements the API for network requests.
15
+ */
16
+ class HttpClient {
17
+ constructor(proxyUrl, customAgentOptions) {
18
+ this.proxyUrl = proxyUrl || "";
19
+ this.customAgentOptions = customAgentOptions || {};
20
+ }
21
+ /**
22
+ * Http Get request
23
+ * @param url
24
+ * @param options
25
+ */
26
+ async sendGetRequestAsync(url, options, timeout) {
27
+ if (this.proxyUrl) {
28
+ return networkRequestViaProxy(url, this.proxyUrl, HttpMethod.GET, options, this.customAgentOptions, timeout);
29
+ }
30
+ else {
31
+ return networkRequestViaHttps(url, HttpMethod.GET, options, this.customAgentOptions, timeout);
32
+ }
33
+ }
34
+ /**
35
+ * Http Post request
36
+ * @param url
37
+ * @param options
38
+ */
39
+ async sendPostRequestAsync(url, options) {
40
+ if (this.proxyUrl) {
41
+ return networkRequestViaProxy(url, this.proxyUrl, HttpMethod.POST, options, this.customAgentOptions);
42
+ }
43
+ else {
44
+ return networkRequestViaHttps(url, HttpMethod.POST, options, this.customAgentOptions);
45
+ }
46
+ }
47
+ }
48
+ const networkRequestViaProxy = (destinationUrlString, proxyUrlString, httpMethod, options, agentOptions, timeout) => {
49
+ const destinationUrl = new URL(destinationUrlString);
50
+ const proxyUrl = new URL(proxyUrlString);
51
+ // "method: connect" must be used to establish a connection to the proxy
52
+ const headers = options?.headers || {};
53
+ const tunnelRequestOptions = {
54
+ host: proxyUrl.hostname,
55
+ port: proxyUrl.port,
56
+ method: "CONNECT",
57
+ path: destinationUrl.hostname,
58
+ headers: headers,
59
+ };
60
+ if (agentOptions && Object.keys(agentOptions).length) {
61
+ tunnelRequestOptions.agent = new http.Agent(agentOptions);
62
+ }
63
+ // compose a request string for the socket
64
+ let postRequestStringContent = "";
65
+ if (httpMethod === HttpMethod.POST) {
66
+ const body = options?.body || "";
67
+ postRequestStringContent =
68
+ "Content-Type: application/x-www-form-urlencoded\r\n" +
69
+ `Content-Length: ${body.length}\r\n` +
70
+ `\r\n${body}`;
71
+ }
72
+ else {
73
+ // optional timeout is only for get requests (regionDiscovery, for example)
74
+ if (timeout) {
75
+ tunnelRequestOptions.timeout = timeout;
76
+ }
77
+ }
78
+ const outgoingRequestString = `${httpMethod.toUpperCase()} ${destinationUrl.href} HTTP/1.1\r\n` +
79
+ `Host: ${destinationUrl.host}\r\n` +
80
+ "Connection: close\r\n" +
81
+ postRequestStringContent +
82
+ "\r\n";
83
+ return new Promise((resolve, reject) => {
84
+ const request = http.request(tunnelRequestOptions);
85
+ if (timeout) {
86
+ request.on("timeout", () => {
87
+ request.destroy();
88
+ reject(new Error("Request time out"));
89
+ });
90
+ }
91
+ request.end();
92
+ // establish connection to the proxy
93
+ request.on("connect", (response, socket) => {
94
+ const proxyStatusCode = response?.statusCode || ProxyStatus.SERVER_ERROR;
95
+ if (proxyStatusCode < ProxyStatus.SUCCESS_RANGE_START ||
96
+ proxyStatusCode > ProxyStatus.SUCCESS_RANGE_END) {
97
+ request.destroy();
98
+ socket.destroy();
99
+ reject(new Error(`Error connecting to proxy. Http status code: ${response.statusCode}. Http status message: ${response?.statusMessage || "Unknown"}`));
100
+ }
101
+ // make a request over an HTTP tunnel
102
+ socket.write(outgoingRequestString);
103
+ const data = [];
104
+ socket.on("data", (chunk) => {
105
+ data.push(chunk);
106
+ });
107
+ socket.on("end", () => {
108
+ // combine all received buffer streams into one buffer, and then into a string
109
+ const dataString = Buffer.concat([...data]).toString();
110
+ // separate each line into it's own entry in an arry
111
+ const dataStringArray = dataString.split("\r\n");
112
+ // the first entry will contain the statusCode and statusMessage
113
+ const httpStatusCode = parseInt(dataStringArray[0].split(" ")[1]);
114
+ // remove "HTTP/1.1" and the status code to get the status message
115
+ const statusMessage = dataStringArray[0]
116
+ .split(" ")
117
+ .slice(2)
118
+ .join(" ");
119
+ // the last entry will contain the body
120
+ const body = dataStringArray[dataStringArray.length - 1];
121
+ // everything in between the first and last entries are the headers
122
+ const headersArray = dataStringArray.slice(1, dataStringArray.length - 2);
123
+ // build an object out of all the headers
124
+ const entries = new Map();
125
+ headersArray.forEach((header) => {
126
+ /**
127
+ * the header might look like "Content-Length: 1531", but that is just a string
128
+ * it needs to be converted to a key/value pair
129
+ * split the string at the first instance of ":"
130
+ * there may be more than one ":" if the value of the header is supposed to be a JSON object
131
+ */
132
+ const headerKeyValue = header.split(new RegExp(/:\s(.*)/s));
133
+ const headerKey = headerKeyValue[0];
134
+ let headerValue = headerKeyValue[1];
135
+ // check if the value of the header is supposed to be a JSON object
136
+ try {
137
+ const object = JSON.parse(headerValue);
138
+ // if it is, then convert it from a string to a JSON object
139
+ if (object && typeof object === "object") {
140
+ headerValue = object;
141
+ }
142
+ }
143
+ catch (e) {
144
+ // otherwise, leave it as a string
145
+ }
146
+ entries.set(headerKey, headerValue);
147
+ });
148
+ const headers = Object.fromEntries(entries);
149
+ const parsedHeaders = headers;
150
+ const networkResponse = NetworkUtils.getNetworkResponse(parsedHeaders, parseBody(httpStatusCode, statusMessage, parsedHeaders, body), httpStatusCode);
151
+ if ((httpStatusCode < HttpStatus.SUCCESS_RANGE_START ||
152
+ httpStatusCode > HttpStatus.SUCCESS_RANGE_END) &&
153
+ // do not destroy the request for the device code flow
154
+ networkResponse.body["error"] !==
155
+ Constants.AUTHORIZATION_PENDING) {
156
+ request.destroy();
157
+ }
158
+ resolve(networkResponse);
159
+ });
160
+ socket.on("error", (chunk) => {
161
+ request.destroy();
162
+ socket.destroy();
163
+ reject(new Error(chunk.toString()));
164
+ });
165
+ });
166
+ request.on("error", (chunk) => {
167
+ request.destroy();
168
+ reject(new Error(chunk.toString()));
169
+ });
170
+ });
171
+ };
172
+ const networkRequestViaHttps = (urlString, httpMethod, options, agentOptions, timeout) => {
173
+ const isPostRequest = httpMethod === HttpMethod.POST;
174
+ const body = options?.body || "";
175
+ const url = new URL(urlString);
176
+ const headers = options?.headers || {};
177
+ const customOptions = {
178
+ method: httpMethod,
179
+ headers: headers,
180
+ ...NetworkUtils.urlToHttpOptions(url),
181
+ };
182
+ if (agentOptions && Object.keys(agentOptions).length) {
183
+ customOptions.agent = new https.Agent(agentOptions);
184
+ }
185
+ if (isPostRequest) {
186
+ // needed for post request to work
187
+ customOptions.headers = {
188
+ ...customOptions.headers,
189
+ "Content-Length": body.length,
190
+ };
191
+ }
192
+ else {
193
+ // optional timeout is only for get requests (regionDiscovery, for example)
194
+ if (timeout) {
195
+ customOptions.timeout = timeout;
196
+ }
197
+ }
198
+ return new Promise((resolve, reject) => {
199
+ let request;
200
+ // managed identity sources use http instead of https
201
+ if (customOptions.protocol === "http:") {
202
+ request = http.request(customOptions);
203
+ }
204
+ else {
205
+ request = https.request(customOptions);
206
+ }
207
+ if (isPostRequest) {
208
+ request.write(body);
209
+ }
210
+ if (timeout) {
211
+ request.on("timeout", () => {
212
+ request.destroy();
213
+ reject(new Error("Request time out"));
214
+ });
215
+ }
216
+ request.end();
217
+ request.on("response", (response) => {
218
+ const headers = response.headers;
219
+ const statusCode = response.statusCode;
220
+ const statusMessage = response.statusMessage;
221
+ const data = [];
222
+ response.on("data", (chunk) => {
223
+ data.push(chunk);
224
+ });
225
+ response.on("end", () => {
226
+ // combine all received buffer streams into one buffer, and then into a string
227
+ const body = Buffer.concat([...data]).toString();
228
+ const parsedHeaders = headers;
229
+ const networkResponse = NetworkUtils.getNetworkResponse(parsedHeaders, parseBody(statusCode, statusMessage, parsedHeaders, body), statusCode);
230
+ if ((statusCode < HttpStatus.SUCCESS_RANGE_START ||
231
+ statusCode > HttpStatus.SUCCESS_RANGE_END) &&
232
+ // do not destroy the request for the device code flow
233
+ networkResponse.body["error"] !==
234
+ Constants.AUTHORIZATION_PENDING) {
235
+ request.destroy();
236
+ }
237
+ resolve(networkResponse);
238
+ });
239
+ });
240
+ request.on("error", (chunk) => {
241
+ request.destroy();
242
+ reject(new Error(chunk.toString()));
243
+ });
244
+ });
245
+ };
246
+ /**
247
+ * Check if extra parsing is needed on the repsonse from the server
248
+ * @param statusCode {number} the status code of the response from the server
249
+ * @param statusMessage {string | undefined} the status message of the response from the server
250
+ * @param headers {Record<string, string>} the headers of the response from the server
251
+ * @param body {string} the body from the response of the server
252
+ * @returns {Object} JSON parsed body or error object
253
+ */
254
+ const parseBody = (statusCode, statusMessage, headers, body) => {
255
+ /*
256
+ * Informational responses (100 – 199)
257
+ * Successful responses (200 – 299)
258
+ * Redirection messages (300 – 399)
259
+ * Client error responses (400 – 499)
260
+ * Server error responses (500 – 599)
261
+ */
262
+ let parsedBody;
263
+ try {
264
+ parsedBody = JSON.parse(body);
265
+ }
266
+ catch (error) {
267
+ let errorType;
268
+ let errorDescriptionHelper;
269
+ if (statusCode >= HttpStatus.CLIENT_ERROR_RANGE_START &&
270
+ statusCode <= HttpStatus.CLIENT_ERROR_RANGE_END) {
271
+ errorType = "client_error";
272
+ errorDescriptionHelper = "A client";
273
+ }
274
+ else if (statusCode >= HttpStatus.SERVER_ERROR_RANGE_START &&
275
+ statusCode <= HttpStatus.SERVER_ERROR_RANGE_END) {
276
+ errorType = "server_error";
277
+ errorDescriptionHelper = "A server";
278
+ }
279
+ else {
280
+ errorType = "unknown_error";
281
+ errorDescriptionHelper = "An unknown";
282
+ }
283
+ parsedBody = {
284
+ error: errorType,
285
+ error_description: `${errorDescriptionHelper} error occured.\nHttp status code: ${statusCode}\nHttp status message: ${statusMessage || "Unknown"}\nHeaders: ${JSON.stringify(headers)}`,
286
+ };
287
+ }
288
+ return parsedBody;
289
289
  };
290
290
 
291
291
  export { HttpClient };
@@ -1,43 +1,43 @@
1
- /*! @azure/msal-node v2.12.0 2024-07-23 */
1
+ /*! @azure/msal-node v2.13.1 2024-08-29 */
2
2
  'use strict';
3
3
  'use strict';
4
4
 
5
5
  var msalCommon = require('@azure/msal-common');
6
6
  var Constants = require('../utils/Constants.cjs');
7
7
 
8
- /*
9
- * Copyright (c) Microsoft Corporation. All rights reserved.
10
- * Licensed under the MIT License.
11
- */
12
- class HttpClientWithRetries {
13
- constructor(httpClientNoRetries, retryPolicy) {
14
- this.httpClientNoRetries = httpClientNoRetries;
15
- this.retryPolicy = retryPolicy;
16
- }
17
- async sendNetworkRequestAsyncHelper(httpMethod, url, options) {
18
- if (httpMethod === Constants.HttpMethod.GET) {
19
- return this.httpClientNoRetries.sendGetRequestAsync(url, options);
20
- }
21
- else {
22
- return this.httpClientNoRetries.sendPostRequestAsync(url, options);
23
- }
24
- }
25
- async sendNetworkRequestAsync(httpMethod, url, options) {
26
- // the underlying network module (custom or HttpClient) will make the call
27
- let response = await this.sendNetworkRequestAsyncHelper(httpMethod, url, options);
28
- let currentRetry = 0;
29
- while (await this.retryPolicy.pauseForRetry(response.status, currentRetry, response.headers[msalCommon.HeaderNames.RETRY_AFTER])) {
30
- response = await this.sendNetworkRequestAsyncHelper(httpMethod, url, options);
31
- currentRetry++;
32
- }
33
- return response;
34
- }
35
- async sendGetRequestAsync(url, options) {
36
- return this.sendNetworkRequestAsync(Constants.HttpMethod.GET, url, options);
37
- }
38
- async sendPostRequestAsync(url, options) {
39
- return this.sendNetworkRequestAsync(Constants.HttpMethod.POST, url, options);
40
- }
8
+ /*
9
+ * Copyright (c) Microsoft Corporation. All rights reserved.
10
+ * Licensed under the MIT License.
11
+ */
12
+ class HttpClientWithRetries {
13
+ constructor(httpClientNoRetries, retryPolicy) {
14
+ this.httpClientNoRetries = httpClientNoRetries;
15
+ this.retryPolicy = retryPolicy;
16
+ }
17
+ async sendNetworkRequestAsyncHelper(httpMethod, url, options) {
18
+ if (httpMethod === Constants.HttpMethod.GET) {
19
+ return this.httpClientNoRetries.sendGetRequestAsync(url, options);
20
+ }
21
+ else {
22
+ return this.httpClientNoRetries.sendPostRequestAsync(url, options);
23
+ }
24
+ }
25
+ async sendNetworkRequestAsync(httpMethod, url, options) {
26
+ // the underlying network module (custom or HttpClient) will make the call
27
+ let response = await this.sendNetworkRequestAsyncHelper(httpMethod, url, options);
28
+ let currentRetry = 0;
29
+ while (await this.retryPolicy.pauseForRetry(response.status, currentRetry, response.headers[msalCommon.HeaderNames.RETRY_AFTER])) {
30
+ response = await this.sendNetworkRequestAsyncHelper(httpMethod, url, options);
31
+ currentRetry++;
32
+ }
33
+ return response;
34
+ }
35
+ async sendGetRequestAsync(url, options) {
36
+ return this.sendNetworkRequestAsync(Constants.HttpMethod.GET, url, options);
37
+ }
38
+ async sendPostRequestAsync(url, options) {
39
+ return this.sendNetworkRequestAsync(Constants.HttpMethod.POST, url, options);
40
+ }
41
41
  }
42
42
 
43
43
  exports.HttpClientWithRetries = HttpClientWithRetries;