@azure/msal-node 3.8.1 → 3.8.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/dist/cache/CacheHelpers.mjs +1 -1
- package/dist/cache/NodeStorage.mjs +1 -1
- package/dist/cache/TokenCache.mjs +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.mjs +1 -1
- package/dist/cache/serializer/Deserializer.mjs +1 -1
- package/dist/cache/serializer/Serializer.mjs +1 -1
- package/dist/client/ClientApplication.mjs +1 -1
- package/dist/client/ClientAssertion.mjs +1 -1
- package/dist/client/ClientCredentialClient.mjs +1 -1
- package/dist/client/ConfidentialClientApplication.mjs +1 -1
- package/dist/client/DeviceCodeClient.mjs +1 -1
- package/dist/client/ManagedIdentityApplication.mjs +1 -1
- package/dist/client/ManagedIdentityClient.mjs +1 -1
- package/dist/client/ManagedIdentitySources/AppService.mjs +1 -1
- package/dist/client/ManagedIdentitySources/AzureArc.mjs +1 -1
- package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs +1 -1
- package/dist/client/ManagedIdentitySources/CloudShell.mjs +1 -1
- package/dist/client/ManagedIdentitySources/Imds.mjs +1 -1
- package/dist/client/ManagedIdentitySources/MachineLearning.mjs +1 -1
- package/dist/client/ManagedIdentitySources/ServiceFabric.mjs +1 -1
- package/dist/client/OnBehalfOfClient.mjs +1 -1
- package/dist/client/PublicClientApplication.mjs +1 -1
- package/dist/client/UsernamePasswordClient.mjs +1 -1
- package/dist/config/Configuration.mjs +1 -1
- package/dist/config/ManagedIdentityId.mjs +1 -1
- package/dist/config/ManagedIdentityRequestParameters.mjs +1 -1
- package/dist/crypto/CryptoProvider.mjs +1 -1
- package/dist/crypto/GuidGenerator.mjs +1 -1
- package/dist/crypto/HashUtils.mjs +1 -1
- package/dist/crypto/PkceGenerator.mjs +1 -1
- package/dist/error/ManagedIdentityError.mjs +1 -1
- package/dist/error/ManagedIdentityErrorCodes.mjs +1 -1
- package/dist/error/NodeAuthError.mjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/internals.mjs +1 -1
- package/dist/network/HttpClient.d.ts +29 -2
- package/dist/network/HttpClient.d.ts.map +1 -1
- package/dist/network/HttpClient.mjs +296 -250
- package/dist/network/HttpClient.mjs.map +1 -1
- package/dist/network/HttpClientWithRetries.mjs +1 -1
- package/dist/network/LoopbackClient.mjs +1 -1
- package/dist/packageMetadata.d.ts +1 -1
- package/dist/packageMetadata.mjs +2 -2
- package/dist/protocol/Authorize.mjs +1 -1
- package/dist/retry/DefaultManagedIdentityRetryPolicy.mjs +1 -1
- package/dist/retry/ExponentialRetryStrategy.mjs +1 -1
- package/dist/retry/ImdsRetryPolicy.mjs +1 -1
- package/dist/retry/LinearRetryStrategy.mjs +1 -1
- package/dist/utils/Constants.mjs +1 -1
- package/dist/utils/EncodingUtils.mjs +1 -1
- package/dist/utils/NetworkUtils.mjs +1 -1
- package/dist/utils/TimeUtils.mjs +1 -1
- package/lib/msal-node.cjs +356 -311
- package/lib/msal-node.cjs.map +1 -1
- package/lib/types/network/HttpClient.d.ts +29 -2
- package/lib/types/network/HttpClient.d.ts.map +1 -1
- package/lib/types/packageMetadata.d.ts +1 -1
- package/package.json +1 -1
- package/src/network/HttpClient.ts +399 -314
- package/src/packageMetadata.ts +1 -1
|
@@ -8,9 +8,12 @@ import {
|
|
|
8
8
|
NetworkRequestOptions,
|
|
9
9
|
NetworkResponse,
|
|
10
10
|
HttpStatus,
|
|
11
|
+
Logger,
|
|
12
|
+
LoggerOptions,
|
|
11
13
|
} from "@azure/msal-common/node";
|
|
12
14
|
import { HttpMethod, Constants, ProxyStatus } from "../utils/Constants.js";
|
|
13
15
|
import { NetworkUtils } from "../utils/NetworkUtils.js";
|
|
16
|
+
import { name, version } from "../packageMetadata.js";
|
|
14
17
|
import http from "http";
|
|
15
18
|
import https from "https";
|
|
16
19
|
|
|
@@ -20,13 +23,19 @@ import https from "https";
|
|
|
20
23
|
export class HttpClient implements INetworkModule {
|
|
21
24
|
private proxyUrl: string;
|
|
22
25
|
private customAgentOptions: http.AgentOptions | https.AgentOptions;
|
|
26
|
+
private logger: Logger;
|
|
27
|
+
private isPiiEnabled: boolean;
|
|
23
28
|
|
|
24
29
|
constructor(
|
|
25
30
|
proxyUrl?: string,
|
|
26
|
-
customAgentOptions?: http.AgentOptions | https.AgentOptions
|
|
31
|
+
customAgentOptions?: http.AgentOptions | https.AgentOptions,
|
|
32
|
+
loggerOptions?: LoggerOptions
|
|
27
33
|
) {
|
|
28
34
|
this.proxyUrl = proxyUrl || "";
|
|
29
35
|
this.customAgentOptions = customAgentOptions || {};
|
|
36
|
+
|
|
37
|
+
this.logger = new Logger(loggerOptions || {}, name, version);
|
|
38
|
+
this.isPiiEnabled = this.logger.isPiiLoggingEnabled();
|
|
30
39
|
}
|
|
31
40
|
|
|
32
41
|
/**
|
|
@@ -34,26 +43,23 @@ export class HttpClient implements INetworkModule {
|
|
|
34
43
|
* @param url
|
|
35
44
|
* @param options
|
|
36
45
|
*/
|
|
37
|
-
async sendGetRequestAsync<T>(
|
|
46
|
+
public async sendGetRequestAsync<T>(
|
|
38
47
|
url: string,
|
|
39
48
|
options?: NetworkRequestOptions,
|
|
40
49
|
timeout?: number
|
|
41
50
|
): Promise<NetworkResponse<T>> {
|
|
42
51
|
if (this.proxyUrl) {
|
|
43
|
-
return networkRequestViaProxy(
|
|
44
|
-
url,
|
|
45
|
-
this.proxyUrl,
|
|
52
|
+
return this.networkRequestViaProxy(
|
|
46
53
|
HttpMethod.GET,
|
|
54
|
+
url,
|
|
47
55
|
options,
|
|
48
|
-
this.customAgentOptions as http.AgentOptions,
|
|
49
56
|
timeout
|
|
50
57
|
);
|
|
51
58
|
} else {
|
|
52
|
-
return networkRequestViaHttps(
|
|
53
|
-
url,
|
|
59
|
+
return this.networkRequestViaHttps(
|
|
54
60
|
HttpMethod.GET,
|
|
61
|
+
url,
|
|
55
62
|
options,
|
|
56
|
-
this.customAgentOptions as https.AgentOptions,
|
|
57
63
|
timeout
|
|
58
64
|
);
|
|
59
65
|
}
|
|
@@ -64,362 +70,441 @@ export class HttpClient implements INetworkModule {
|
|
|
64
70
|
* @param url
|
|
65
71
|
* @param options
|
|
66
72
|
*/
|
|
67
|
-
async sendPostRequestAsync<T>(
|
|
73
|
+
public async sendPostRequestAsync<T>(
|
|
68
74
|
url: string,
|
|
69
75
|
options?: NetworkRequestOptions
|
|
70
76
|
): Promise<NetworkResponse<T>> {
|
|
71
77
|
if (this.proxyUrl) {
|
|
72
|
-
return networkRequestViaProxy(
|
|
73
|
-
url,
|
|
74
|
-
this.proxyUrl,
|
|
75
|
-
HttpMethod.POST,
|
|
76
|
-
options,
|
|
77
|
-
this.customAgentOptions as http.AgentOptions
|
|
78
|
-
);
|
|
78
|
+
return this.networkRequestViaProxy(HttpMethod.POST, url, options);
|
|
79
79
|
} else {
|
|
80
|
-
return networkRequestViaHttps(
|
|
81
|
-
url,
|
|
82
|
-
HttpMethod.POST,
|
|
83
|
-
options,
|
|
84
|
-
this.customAgentOptions as https.AgentOptions
|
|
85
|
-
);
|
|
80
|
+
return this.networkRequestViaHttps(HttpMethod.POST, url, options);
|
|
86
81
|
}
|
|
87
82
|
}
|
|
88
|
-
}
|
|
89
83
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
headers: headers,
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
if (agentOptions && Object.keys(agentOptions).length) {
|
|
112
|
-
tunnelRequestOptions.agent = new http.Agent(agentOptions);
|
|
113
|
-
}
|
|
84
|
+
private networkRequestViaProxy = <T>(
|
|
85
|
+
httpMethod: string,
|
|
86
|
+
destinationUrlString: string,
|
|
87
|
+
options?: NetworkRequestOptions,
|
|
88
|
+
timeout?: number
|
|
89
|
+
): Promise<NetworkResponse<T>> => {
|
|
90
|
+
const destinationUrl = new URL(destinationUrlString);
|
|
91
|
+
const proxyUrl = new URL(this.proxyUrl);
|
|
92
|
+
|
|
93
|
+
// "method: connect" must be used to establish a connection to the proxy
|
|
94
|
+
const headers = options?.headers || ({} as Record<string, string>);
|
|
95
|
+
const tunnelRequestOptions: https.RequestOptions = {
|
|
96
|
+
host: proxyUrl.hostname,
|
|
97
|
+
port: proxyUrl.port,
|
|
98
|
+
method: "CONNECT",
|
|
99
|
+
path: destinationUrl.hostname,
|
|
100
|
+
headers: headers,
|
|
101
|
+
};
|
|
114
102
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
`\r\n${body}`;
|
|
123
|
-
} else {
|
|
124
|
-
// optional timeout is only for get requests (regionDiscovery, for example)
|
|
125
|
-
if (timeout) {
|
|
126
|
-
tunnelRequestOptions.timeout = timeout;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
const outgoingRequestString =
|
|
130
|
-
`${httpMethod.toUpperCase()} ${destinationUrl.href} HTTP/1.1\r\n` +
|
|
131
|
-
`Host: ${destinationUrl.host}\r\n` +
|
|
132
|
-
"Connection: close\r\n" +
|
|
133
|
-
postRequestStringContent +
|
|
134
|
-
"\r\n";
|
|
135
|
-
|
|
136
|
-
return new Promise<NetworkResponse<T>>((resolve, reject) => {
|
|
137
|
-
const request = http.request(tunnelRequestOptions);
|
|
138
|
-
|
|
139
|
-
if (timeout) {
|
|
140
|
-
request.on("timeout", () => {
|
|
141
|
-
request.destroy();
|
|
142
|
-
reject(new Error("Request time out"));
|
|
143
|
-
});
|
|
103
|
+
if (
|
|
104
|
+
this.customAgentOptions &&
|
|
105
|
+
Object.keys(this.customAgentOptions).length
|
|
106
|
+
) {
|
|
107
|
+
tunnelRequestOptions.agent = new http.Agent(
|
|
108
|
+
this.customAgentOptions
|
|
109
|
+
);
|
|
144
110
|
}
|
|
145
111
|
|
|
146
|
-
request
|
|
112
|
+
// compose a request string for the socket
|
|
113
|
+
let postRequestStringContent: string = "";
|
|
114
|
+
if (httpMethod === HttpMethod.POST) {
|
|
115
|
+
const body = options?.body || "";
|
|
116
|
+
postRequestStringContent =
|
|
117
|
+
"Content-Type: application/x-www-form-urlencoded\r\n" +
|
|
118
|
+
`Content-Length: ${body.length}\r\n` +
|
|
119
|
+
`\r\n${body}`;
|
|
120
|
+
} else {
|
|
121
|
+
// optional timeout is only for get requests (regionDiscovery, for example)
|
|
122
|
+
if (timeout) {
|
|
123
|
+
tunnelRequestOptions.timeout = timeout;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const outgoingRequestString =
|
|
127
|
+
`${httpMethod.toUpperCase()} ${destinationUrl.href} HTTP/1.1\r\n` +
|
|
128
|
+
`Host: ${destinationUrl.host}\r\n` +
|
|
129
|
+
"Connection: close\r\n" +
|
|
130
|
+
postRequestStringContent +
|
|
131
|
+
"\r\n";
|
|
132
|
+
|
|
133
|
+
return new Promise<NetworkResponse<T>>((resolve, reject) => {
|
|
134
|
+
const request = http.request(tunnelRequestOptions);
|
|
135
|
+
|
|
136
|
+
if (timeout) {
|
|
137
|
+
request.on("timeout", () => {
|
|
138
|
+
this.logUrlWithPiiAwareness(
|
|
139
|
+
`Request timeout after ${timeout}ms for URL`,
|
|
140
|
+
destinationUrlString
|
|
141
|
+
);
|
|
147
142
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
response?.statusCode || ProxyStatus.SERVER_ERROR;
|
|
152
|
-
if (
|
|
153
|
-
proxyStatusCode < ProxyStatus.SUCCESS_RANGE_START ||
|
|
154
|
-
proxyStatusCode > ProxyStatus.SUCCESS_RANGE_END
|
|
155
|
-
) {
|
|
156
|
-
request.destroy();
|
|
157
|
-
socket.destroy();
|
|
158
|
-
reject(
|
|
159
|
-
new Error(
|
|
160
|
-
`Error connecting to proxy. Http status code: ${
|
|
161
|
-
response.statusCode
|
|
162
|
-
}. Http status message: ${
|
|
163
|
-
response?.statusMessage || "Unknown"
|
|
164
|
-
}`
|
|
165
|
-
)
|
|
166
|
-
);
|
|
143
|
+
request.destroy();
|
|
144
|
+
reject(new Error(`Request time out after ${timeout}ms`));
|
|
145
|
+
});
|
|
167
146
|
}
|
|
168
147
|
|
|
169
|
-
|
|
170
|
-
socket.write(outgoingRequestString);
|
|
148
|
+
request.end();
|
|
171
149
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
150
|
+
// establish connection to the proxy
|
|
151
|
+
request.on("connect", (response, socket) => {
|
|
152
|
+
const proxyStatusCode =
|
|
153
|
+
response?.statusCode || ProxyStatus.SERVER_ERROR;
|
|
154
|
+
if (
|
|
155
|
+
proxyStatusCode < ProxyStatus.SUCCESS_RANGE_START ||
|
|
156
|
+
proxyStatusCode > ProxyStatus.SUCCESS_RANGE_END
|
|
157
|
+
) {
|
|
158
|
+
request.destroy();
|
|
159
|
+
socket.destroy();
|
|
160
|
+
reject(
|
|
161
|
+
new Error(
|
|
162
|
+
`Error connecting to proxy. Http status code: ${
|
|
163
|
+
response.statusCode
|
|
164
|
+
}. Http status message: ${
|
|
165
|
+
response?.statusMessage || "Unknown"
|
|
166
|
+
}`
|
|
167
|
+
)
|
|
168
|
+
);
|
|
169
|
+
}
|
|
176
170
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
const dataString = Buffer.concat([...data]).toString();
|
|
171
|
+
// make a request over an HTTP tunnel
|
|
172
|
+
socket.write(outgoingRequestString);
|
|
180
173
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
dataStringArray[0].split(" ")[1]
|
|
186
|
-
);
|
|
187
|
-
// remove "HTTP/1.1" and the status code to get the status message
|
|
188
|
-
const statusMessage = dataStringArray[0]
|
|
189
|
-
.split(" ")
|
|
190
|
-
.slice(2)
|
|
191
|
-
.join(" ");
|
|
192
|
-
// the last entry will contain the body
|
|
193
|
-
const body = dataStringArray[dataStringArray.length - 1];
|
|
194
|
-
|
|
195
|
-
// everything in between the first and last entries are the headers
|
|
196
|
-
const headersArray = dataStringArray.slice(
|
|
197
|
-
1,
|
|
198
|
-
dataStringArray.length - 2
|
|
199
|
-
);
|
|
174
|
+
const data: Buffer[] = [];
|
|
175
|
+
socket.on("data", (chunk) => {
|
|
176
|
+
data.push(chunk);
|
|
177
|
+
});
|
|
200
178
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
179
|
+
socket.on("end", () => {
|
|
180
|
+
// combine all received buffer streams into one buffer, and then into a string
|
|
181
|
+
const dataString = Buffer.concat([...data]).toString();
|
|
182
|
+
|
|
183
|
+
// separate each line into it's own entry in an arry
|
|
184
|
+
const dataStringArray = dataString.split("\r\n");
|
|
185
|
+
// the first entry will contain the statusCode and statusMessage
|
|
186
|
+
const httpStatusCode = parseInt(
|
|
187
|
+
dataStringArray[0].split(" ")[1]
|
|
188
|
+
);
|
|
189
|
+
// remove "HTTP/1.1" and the status code to get the status message
|
|
190
|
+
const statusMessage = dataStringArray[0]
|
|
191
|
+
.split(" ")
|
|
192
|
+
.slice(2)
|
|
193
|
+
.join(" ");
|
|
194
|
+
// the last entry will contain the body
|
|
195
|
+
const body = dataStringArray[dataStringArray.length - 1];
|
|
196
|
+
|
|
197
|
+
// everything in between the first and last entries are the headers
|
|
198
|
+
const headersArray = dataStringArray.slice(
|
|
199
|
+
1,
|
|
200
|
+
dataStringArray.length - 2
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
// build an object out of all the headers
|
|
204
|
+
const entries = new Map();
|
|
205
|
+
headersArray.forEach((header) => {
|
|
206
|
+
/**
|
|
207
|
+
* the header might look like "Content-Length: 1531", but that is just a string
|
|
208
|
+
* it needs to be converted to a key/value pair
|
|
209
|
+
* split the string at the first instance of ":"
|
|
210
|
+
* there may be more than one ":" if the value of the header is supposed to be a JSON object
|
|
211
|
+
*/
|
|
212
|
+
const headerKeyValue = header.split(
|
|
213
|
+
new RegExp(/:\s(.*)/s)
|
|
214
|
+
);
|
|
215
|
+
const headerKey = headerKeyValue[0];
|
|
216
|
+
let headerValue = headerKeyValue[1];
|
|
217
|
+
|
|
218
|
+
// check if the value of the header is supposed to be a JSON object
|
|
219
|
+
try {
|
|
220
|
+
const object = JSON.parse(headerValue);
|
|
221
|
+
|
|
222
|
+
// if it is, then convert it from a string to a JSON object
|
|
223
|
+
if (object && typeof object === "object") {
|
|
224
|
+
headerValue = object;
|
|
225
|
+
}
|
|
226
|
+
} catch (e) {
|
|
227
|
+
// otherwise, leave it as a string
|
|
221
228
|
}
|
|
222
|
-
} catch (e) {
|
|
223
|
-
// otherwise, leave it as a string
|
|
224
|
-
}
|
|
225
229
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
parsedHeaders,
|
|
233
|
-
parseBody(
|
|
234
|
-
httpStatusCode,
|
|
235
|
-
statusMessage,
|
|
230
|
+
entries.set(headerKey, headerValue);
|
|
231
|
+
});
|
|
232
|
+
const headers = Object.fromEntries(entries);
|
|
233
|
+
|
|
234
|
+
const parsedHeaders = headers as Record<string, string>;
|
|
235
|
+
const networkResponse = NetworkUtils.getNetworkResponse(
|
|
236
236
|
parsedHeaders,
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
237
|
+
this.parseBody(
|
|
238
|
+
httpStatusCode,
|
|
239
|
+
statusMessage,
|
|
240
|
+
parsedHeaders,
|
|
241
|
+
body
|
|
242
|
+
) as T,
|
|
243
|
+
httpStatusCode
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
if (
|
|
247
|
+
this.shouldDestroyRequest(
|
|
248
|
+
httpStatusCode,
|
|
249
|
+
networkResponse
|
|
250
|
+
)
|
|
251
|
+
) {
|
|
252
|
+
request.destroy();
|
|
253
|
+
}
|
|
254
|
+
resolve(networkResponse);
|
|
255
|
+
});
|
|
241
256
|
|
|
242
|
-
|
|
243
|
-
(httpStatusCode < HttpStatus.SUCCESS_RANGE_START ||
|
|
244
|
-
httpStatusCode > HttpStatus.SUCCESS_RANGE_END) &&
|
|
245
|
-
// do not destroy the request for the device code flow
|
|
246
|
-
networkResponse.body["error"] !==
|
|
247
|
-
Constants.AUTHORIZATION_PENDING
|
|
248
|
-
) {
|
|
257
|
+
socket.on("error", (chunk) => {
|
|
249
258
|
request.destroy();
|
|
250
|
-
|
|
251
|
-
|
|
259
|
+
socket.destroy();
|
|
260
|
+
reject(new Error(chunk.toString()));
|
|
261
|
+
});
|
|
252
262
|
});
|
|
253
263
|
|
|
254
|
-
|
|
264
|
+
request.on("error", (chunk) => {
|
|
265
|
+
this.logger.error(
|
|
266
|
+
`HttpClient - Proxy request error: ${chunk.toString()}`,
|
|
267
|
+
""
|
|
268
|
+
);
|
|
269
|
+
this.logUrlWithPiiAwareness(
|
|
270
|
+
"Destination URL",
|
|
271
|
+
destinationUrlString
|
|
272
|
+
);
|
|
273
|
+
this.logUrlWithPiiAwareness("Proxy URL", this.proxyUrl);
|
|
274
|
+
this.logger.error(`HttpClient - Method: ${httpMethod}`, "");
|
|
275
|
+
this.logger.errorPii(
|
|
276
|
+
`HttpClient - Headers: ${JSON.stringify(headers)}`,
|
|
277
|
+
""
|
|
278
|
+
);
|
|
279
|
+
|
|
255
280
|
request.destroy();
|
|
256
|
-
socket.destroy();
|
|
257
281
|
reject(new Error(chunk.toString()));
|
|
258
282
|
});
|
|
259
283
|
});
|
|
260
|
-
|
|
261
|
-
request.on("error", (chunk) => {
|
|
262
|
-
request.destroy();
|
|
263
|
-
reject(new Error(chunk.toString()));
|
|
264
|
-
});
|
|
265
|
-
});
|
|
266
|
-
};
|
|
267
|
-
|
|
268
|
-
const networkRequestViaHttps = <T>(
|
|
269
|
-
urlString: string,
|
|
270
|
-
httpMethod: string,
|
|
271
|
-
options?: NetworkRequestOptions,
|
|
272
|
-
agentOptions?: https.AgentOptions,
|
|
273
|
-
timeout?: number
|
|
274
|
-
): Promise<NetworkResponse<T>> => {
|
|
275
|
-
const isPostRequest = httpMethod === HttpMethod.POST;
|
|
276
|
-
const body: string = options?.body || "";
|
|
277
|
-
|
|
278
|
-
const url = new URL(urlString);
|
|
279
|
-
const headers = options?.headers || ({} as Record<string, string>);
|
|
280
|
-
const customOptions: https.RequestOptions = {
|
|
281
|
-
method: httpMethod,
|
|
282
|
-
headers: headers,
|
|
283
|
-
...NetworkUtils.urlToHttpOptions(url),
|
|
284
284
|
};
|
|
285
285
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
286
|
+
private networkRequestViaHttps = <T>(
|
|
287
|
+
httpMethod: string,
|
|
288
|
+
urlString: string,
|
|
289
|
+
options?: NetworkRequestOptions,
|
|
290
|
+
timeout?: number
|
|
291
|
+
): Promise<NetworkResponse<T>> => {
|
|
292
|
+
const isPostRequest = httpMethod === HttpMethod.POST;
|
|
293
|
+
const body: string = options?.body || "";
|
|
294
|
+
|
|
295
|
+
const url = new URL(urlString);
|
|
296
|
+
const headers = options?.headers || ({} as Record<string, string>);
|
|
297
|
+
const customOptions: https.RequestOptions = {
|
|
298
|
+
method: httpMethod,
|
|
299
|
+
headers: headers,
|
|
300
|
+
...NetworkUtils.urlToHttpOptions(url),
|
|
295
301
|
};
|
|
296
|
-
} else {
|
|
297
|
-
// optional timeout is only for get requests (regionDiscovery, for example)
|
|
298
|
-
if (timeout) {
|
|
299
|
-
customOptions.timeout = timeout;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
302
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
} else {
|
|
309
|
-
request = https.request(customOptions);
|
|
303
|
+
if (
|
|
304
|
+
this.customAgentOptions &&
|
|
305
|
+
Object.keys(this.customAgentOptions).length
|
|
306
|
+
) {
|
|
307
|
+
customOptions.agent = new https.Agent(this.customAgentOptions);
|
|
310
308
|
}
|
|
311
309
|
|
|
312
310
|
if (isPostRequest) {
|
|
313
|
-
request
|
|
311
|
+
// needed for post request to work
|
|
312
|
+
customOptions.headers = {
|
|
313
|
+
...customOptions.headers,
|
|
314
|
+
"Content-Length": body.length,
|
|
315
|
+
};
|
|
316
|
+
} else {
|
|
317
|
+
// optional timeout is only for get requests (regionDiscovery, for example)
|
|
318
|
+
if (timeout) {
|
|
319
|
+
customOptions.timeout = timeout;
|
|
320
|
+
}
|
|
314
321
|
}
|
|
315
322
|
|
|
316
|
-
|
|
317
|
-
request.
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
323
|
+
return new Promise<NetworkResponse<T>>((resolve, reject) => {
|
|
324
|
+
let request: http.ClientRequest;
|
|
325
|
+
// managed identity sources use http instead of https
|
|
326
|
+
if (customOptions.protocol === "http:") {
|
|
327
|
+
request = http.request(customOptions);
|
|
328
|
+
} else {
|
|
329
|
+
request = https.request(customOptions);
|
|
330
|
+
}
|
|
322
331
|
|
|
323
|
-
|
|
332
|
+
if (isPostRequest) {
|
|
333
|
+
request.write(body);
|
|
334
|
+
}
|
|
324
335
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
336
|
+
if (timeout) {
|
|
337
|
+
request.on("timeout", () => {
|
|
338
|
+
this.logUrlWithPiiAwareness(
|
|
339
|
+
`HTTPS request timeout after ${timeout}ms for URL`,
|
|
340
|
+
urlString
|
|
341
|
+
);
|
|
329
342
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
}
|
|
343
|
+
request.destroy();
|
|
344
|
+
reject(new Error(`Request time out after ${timeout}ms`));
|
|
345
|
+
});
|
|
346
|
+
}
|
|
334
347
|
|
|
335
|
-
|
|
336
|
-
// combine all received buffer streams into one buffer, and then into a string
|
|
337
|
-
const body = Buffer.concat([...data]).toString();
|
|
348
|
+
request.end();
|
|
338
349
|
|
|
339
|
-
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
350
|
+
request.on("response", (response) => {
|
|
351
|
+
const headers = response.headers;
|
|
352
|
+
const statusCode = response.statusCode as number;
|
|
353
|
+
const statusMessage = response.statusMessage;
|
|
354
|
+
|
|
355
|
+
const data: Buffer[] = [];
|
|
356
|
+
response.on("data", (chunk) => {
|
|
357
|
+
data.push(chunk);
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
response.on("end", () => {
|
|
361
|
+
// combine all received buffer streams into one buffer, and then into a string
|
|
362
|
+
const body = Buffer.concat([...data]).toString();
|
|
363
|
+
|
|
364
|
+
const parsedHeaders = headers as Record<string, string>;
|
|
365
|
+
const networkResponse = NetworkUtils.getNetworkResponse(
|
|
345
366
|
parsedHeaders,
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
367
|
+
this.parseBody(
|
|
368
|
+
statusCode,
|
|
369
|
+
statusMessage,
|
|
370
|
+
parsedHeaders,
|
|
371
|
+
body
|
|
372
|
+
) as T,
|
|
373
|
+
statusCode
|
|
374
|
+
);
|
|
375
|
+
|
|
376
|
+
if (
|
|
377
|
+
this.shouldDestroyRequest(statusCode, networkResponse)
|
|
378
|
+
) {
|
|
379
|
+
request.destroy();
|
|
380
|
+
}
|
|
381
|
+
resolve(networkResponse);
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
request.on("error", (chunk) => {
|
|
386
|
+
this.logger.error(
|
|
387
|
+
`HttpClient - HTTPS request error: ${chunk.toString()}`,
|
|
388
|
+
""
|
|
389
|
+
);
|
|
390
|
+
this.logUrlWithPiiAwareness("URL", urlString);
|
|
391
|
+
this.logger.error(`HttpClient - Method: ${httpMethod}`, "");
|
|
392
|
+
this.logger.errorPii(
|
|
393
|
+
`HttpClient - Headers: ${JSON.stringify(headers)}`,
|
|
394
|
+
""
|
|
349
395
|
);
|
|
350
396
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
statusCode > HttpStatus.SUCCESS_RANGE_END) &&
|
|
354
|
-
// do not destroy the request for the device code flow
|
|
355
|
-
networkResponse.body["error"] !==
|
|
356
|
-
Constants.AUTHORIZATION_PENDING
|
|
357
|
-
) {
|
|
358
|
-
request.destroy();
|
|
359
|
-
}
|
|
360
|
-
resolve(networkResponse);
|
|
397
|
+
request.destroy();
|
|
398
|
+
reject(new Error(chunk.toString()));
|
|
361
399
|
});
|
|
362
400
|
});
|
|
401
|
+
};
|
|
363
402
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* Check if extra parsing is needed on the repsonse from the server
|
|
373
|
-
* @param statusCode {number} the status code of the response from the server
|
|
374
|
-
* @param statusMessage {string | undefined} the status message of the response from the server
|
|
375
|
-
* @param headers {Record<string, string>} the headers of the response from the server
|
|
376
|
-
* @param body {string} the body from the response of the server
|
|
377
|
-
* @returns {Object} JSON parsed body or error object
|
|
378
|
-
*/
|
|
379
|
-
const parseBody = (
|
|
380
|
-
statusCode: number,
|
|
381
|
-
statusMessage: string | undefined,
|
|
382
|
-
headers: Record<string, string>,
|
|
383
|
-
body: string
|
|
384
|
-
) => {
|
|
385
|
-
/*
|
|
386
|
-
* Informational responses (100 – 199)
|
|
387
|
-
* Successful responses (200 – 299)
|
|
388
|
-
* Redirection messages (300 – 399)
|
|
389
|
-
* Client error responses (400 – 499)
|
|
390
|
-
* Server error responses (500 – 599)
|
|
403
|
+
/**
|
|
404
|
+
* Check if extra parsing is needed on the repsonse from the server
|
|
405
|
+
* @param statusCode {number} the status code of the response from the server
|
|
406
|
+
* @param statusMessage {string | undefined} the status message of the response from the server
|
|
407
|
+
* @param headers {Record<string, string>} the headers of the response from the server
|
|
408
|
+
* @param body {string} the body from the response of the server
|
|
409
|
+
* @returns {Object} JSON parsed body or error object
|
|
391
410
|
*/
|
|
411
|
+
private parseBody = (
|
|
412
|
+
statusCode: number,
|
|
413
|
+
statusMessage: string | undefined,
|
|
414
|
+
headers: Record<string, string>,
|
|
415
|
+
body: string
|
|
416
|
+
) => {
|
|
417
|
+
/*
|
|
418
|
+
* Informational responses (100 – 199)
|
|
419
|
+
* Successful responses (200 – 299)
|
|
420
|
+
* Redirection messages (300 – 399)
|
|
421
|
+
* Client error responses (400 – 499)
|
|
422
|
+
* Server error responses (500 – 599)
|
|
423
|
+
*/
|
|
424
|
+
|
|
425
|
+
let parsedBody;
|
|
426
|
+
try {
|
|
427
|
+
parsedBody = JSON.parse(body);
|
|
428
|
+
} catch (error) {
|
|
429
|
+
let errorType;
|
|
430
|
+
let errorDescriptionHelper;
|
|
431
|
+
if (
|
|
432
|
+
statusCode >= HttpStatus.CLIENT_ERROR_RANGE_START &&
|
|
433
|
+
statusCode <= HttpStatus.CLIENT_ERROR_RANGE_END
|
|
434
|
+
) {
|
|
435
|
+
errorType = "client_error";
|
|
436
|
+
errorDescriptionHelper = "A client";
|
|
437
|
+
} else if (
|
|
438
|
+
statusCode >= HttpStatus.SERVER_ERROR_RANGE_START &&
|
|
439
|
+
statusCode <= HttpStatus.SERVER_ERROR_RANGE_END
|
|
440
|
+
) {
|
|
441
|
+
errorType = "server_error";
|
|
442
|
+
errorDescriptionHelper = "A server";
|
|
443
|
+
} else {
|
|
444
|
+
errorType = "unknown_error";
|
|
445
|
+
errorDescriptionHelper = "An unknown";
|
|
446
|
+
}
|
|
392
447
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
if (
|
|
400
|
-
statusCode >= HttpStatus.CLIENT_ERROR_RANGE_START &&
|
|
401
|
-
statusCode <= HttpStatus.CLIENT_ERROR_RANGE_END
|
|
402
|
-
) {
|
|
403
|
-
errorType = "client_error";
|
|
404
|
-
errorDescriptionHelper = "A client";
|
|
405
|
-
} else if (
|
|
406
|
-
statusCode >= HttpStatus.SERVER_ERROR_RANGE_START &&
|
|
407
|
-
statusCode <= HttpStatus.SERVER_ERROR_RANGE_END
|
|
408
|
-
) {
|
|
409
|
-
errorType = "server_error";
|
|
410
|
-
errorDescriptionHelper = "A server";
|
|
411
|
-
} else {
|
|
412
|
-
errorType = "unknown_error";
|
|
413
|
-
errorDescriptionHelper = "An unknown";
|
|
448
|
+
parsedBody = {
|
|
449
|
+
error: errorType,
|
|
450
|
+
error_description: `${errorDescriptionHelper} error occured.\nHttp status code: ${statusCode}\nHttp status message: ${
|
|
451
|
+
statusMessage || "Unknown"
|
|
452
|
+
}\nHeaders: ${JSON.stringify(headers)}`,
|
|
453
|
+
};
|
|
414
454
|
}
|
|
415
455
|
|
|
416
|
-
parsedBody
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
456
|
+
return parsedBody;
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Helper function to log a formatted message containing URLs, with PII-aware sanitization
|
|
461
|
+
* @param label {string} the label for the log message
|
|
462
|
+
* @param urlString {string} the URL to log
|
|
463
|
+
*/
|
|
464
|
+
private logUrlWithPiiAwareness = (
|
|
465
|
+
label: string,
|
|
466
|
+
urlString: string
|
|
467
|
+
): void => {
|
|
468
|
+
if (this.isPiiEnabled) {
|
|
469
|
+
this.logger.errorPii(`HttpClient - ${label}: ${urlString}`, "");
|
|
470
|
+
} else {
|
|
471
|
+
let urlHelper: string;
|
|
472
|
+
try {
|
|
473
|
+
const url = new URL(urlString);
|
|
474
|
+
urlHelper = `${url.protocol}//${url.host}${url.pathname}`;
|
|
475
|
+
} catch {
|
|
476
|
+
urlHelper = urlString.split("?")[0] || "unknown";
|
|
477
|
+
}
|
|
423
478
|
|
|
424
|
-
|
|
425
|
-
}
|
|
479
|
+
this.logger.error(
|
|
480
|
+
`HttpClient - ${label}: ${urlHelper} [Enable PII logging to see additional details]`,
|
|
481
|
+
""
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* Helper function to determine if a request should be destroyed based on status code and response body.
|
|
488
|
+
* Checks if the response is an error and not part of the device code flow (authorization_pending).
|
|
489
|
+
* @param statusCode {number} the status code of the response
|
|
490
|
+
* @param networkResponse {NetworkResponse<T>} the network response object
|
|
491
|
+
* @returns {boolean} true if the request should be destroyed, false otherwise
|
|
492
|
+
*/
|
|
493
|
+
private shouldDestroyRequest = <T>(
|
|
494
|
+
statusCode: number,
|
|
495
|
+
networkResponse: NetworkResponse<T>
|
|
496
|
+
): boolean => {
|
|
497
|
+
return (
|
|
498
|
+
(statusCode < HttpStatus.SUCCESS_RANGE_START ||
|
|
499
|
+
statusCode > HttpStatus.SUCCESS_RANGE_END) &&
|
|
500
|
+
// do not destroy the request for the device code flow
|
|
501
|
+
!(
|
|
502
|
+
networkResponse.body &&
|
|
503
|
+
typeof networkResponse.body === "object" &&
|
|
504
|
+
"error" in networkResponse.body &&
|
|
505
|
+
(networkResponse.body as { error: string }).error ===
|
|
506
|
+
Constants.AUTHORIZATION_PENDING
|
|
507
|
+
)
|
|
508
|
+
);
|
|
509
|
+
};
|
|
510
|
+
}
|