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