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