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