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