@azure/msal-node 3.8.0 → 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.d.ts.map +1 -1
- package/dist/cache/NodeStorage.mjs +2 -2
- package/dist/cache/NodeStorage.mjs.map +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.d.ts +54 -0
- package/dist/client/ManagedIdentitySources/AppService.d.ts.map +1 -1
- package/dist/client/ManagedIdentitySources/AppService.mjs +55 -1
- package/dist/client/ManagedIdentitySources/AppService.mjs.map +1 -1
- package/dist/client/ManagedIdentitySources/AzureArc.d.ts +81 -0
- package/dist/client/ManagedIdentitySources/AzureArc.d.ts.map +1 -1
- package/dist/client/ManagedIdentitySources/AzureArc.mjs +82 -1
- package/dist/client/ManagedIdentitySources/AzureArc.mjs.map +1 -1
- package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts +93 -2
- package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts.map +1 -1
- package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs +81 -1
- package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs.map +1 -1
- package/dist/client/ManagedIdentitySources/CloudShell.d.ts +54 -0
- package/dist/client/ManagedIdentitySources/CloudShell.d.ts.map +1 -1
- package/dist/client/ManagedIdentitySources/CloudShell.mjs +55 -1
- package/dist/client/ManagedIdentitySources/CloudShell.mjs.map +1 -1
- package/dist/client/ManagedIdentitySources/Imds.d.ts +44 -22
- package/dist/client/ManagedIdentitySources/Imds.d.ts.map +1 -1
- package/dist/client/ManagedIdentitySources/Imds.mjs +44 -22
- package/dist/client/ManagedIdentitySources/Imds.mjs.map +1 -1
- package/dist/client/ManagedIdentitySources/MachineLearning.d.ts +65 -0
- package/dist/client/ManagedIdentitySources/MachineLearning.d.ts.map +1 -1
- package/dist/client/ManagedIdentitySources/MachineLearning.mjs +66 -1
- package/dist/client/ManagedIdentitySources/MachineLearning.mjs.map +1 -1
- package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts +56 -22
- package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts.map +1 -1
- package/dist/client/ManagedIdentitySources/ServiceFabric.mjs +57 -23
- package/dist/client/ManagedIdentitySources/ServiceFabric.mjs.map +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 +1155 -699
- package/lib/msal-node.cjs.map +1 -1
- package/lib/types/cache/NodeStorage.d.ts.map +1 -1
- package/lib/types/client/ManagedIdentitySources/AppService.d.ts +54 -0
- package/lib/types/client/ManagedIdentitySources/AppService.d.ts.map +1 -1
- package/lib/types/client/ManagedIdentitySources/AzureArc.d.ts +81 -0
- package/lib/types/client/ManagedIdentitySources/AzureArc.d.ts.map +1 -1
- package/lib/types/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts +93 -2
- package/lib/types/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts.map +1 -1
- package/lib/types/client/ManagedIdentitySources/CloudShell.d.ts +54 -0
- package/lib/types/client/ManagedIdentitySources/CloudShell.d.ts.map +1 -1
- package/lib/types/client/ManagedIdentitySources/Imds.d.ts +44 -22
- package/lib/types/client/ManagedIdentitySources/Imds.d.ts.map +1 -1
- package/lib/types/client/ManagedIdentitySources/MachineLearning.d.ts +65 -0
- package/lib/types/client/ManagedIdentitySources/MachineLearning.d.ts.map +1 -1
- package/lib/types/client/ManagedIdentitySources/ServiceFabric.d.ts +56 -22
- package/lib/types/client/ManagedIdentitySources/ServiceFabric.d.ts.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 +2 -2
- package/src/cache/NodeStorage.ts +3 -1
- package/src/client/ManagedIdentitySources/AppService.ts +54 -0
- package/src/client/ManagedIdentitySources/AzureArc.ts +81 -0
- package/src/client/ManagedIdentitySources/BaseManagedIdentitySource.ts +92 -2
- package/src/client/ManagedIdentitySources/CloudShell.ts +54 -0
- package/src/client/ManagedIdentitySources/Imds.ts +43 -21
- package/src/client/ManagedIdentitySources/MachineLearning.ts +65 -0
- package/src/client/ManagedIdentitySources/ServiceFabric.ts +56 -22
- package/src/network/HttpClient.ts +399 -314
- package/src/packageMetadata.ts +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! @azure/msal-node v3.8.
|
|
1
|
+
/*! @azure/msal-node v3.8.2 2025-11-11 */
|
|
2
2
|
'use strict';
|
|
3
3
|
import { AuthError } from '@azure/msal-common/node';
|
|
4
4
|
import { wwwAuthenticateHeaderUnsupportedFormat, wwwAuthenticateHeaderMissing, userAssignedNotAvailableAtRuntime, unableToReadSecretFile, unableToCreateSource, unableToCreateCloudShell, unableToCreateAzureArc, networkUnavailable, MsiEnvironmentVariableUrlMalformedErrorCodes, missingId, platformNotSupported, invalidSecret, invalidManagedIdentityIdType, invalidFilePath, invalidFileExtension } from './ManagedIdentityErrorCodes.mjs';
|
package/dist/index.mjs
CHANGED
package/dist/internals.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
/// <reference types="node" resolution-mode="require"/>
|
|
3
|
-
import { INetworkModule, NetworkRequestOptions, NetworkResponse } from "@azure/msal-common/node";
|
|
3
|
+
import { INetworkModule, NetworkRequestOptions, NetworkResponse, LoggerOptions } from "@azure/msal-common/node";
|
|
4
4
|
import http from "http";
|
|
5
5
|
import https from "https";
|
|
6
6
|
/**
|
|
@@ -9,7 +9,9 @@ import https from "https";
|
|
|
9
9
|
export declare class HttpClient implements INetworkModule {
|
|
10
10
|
private proxyUrl;
|
|
11
11
|
private customAgentOptions;
|
|
12
|
-
|
|
12
|
+
private logger;
|
|
13
|
+
private isPiiEnabled;
|
|
14
|
+
constructor(proxyUrl?: string, customAgentOptions?: http.AgentOptions | https.AgentOptions, loggerOptions?: LoggerOptions);
|
|
13
15
|
/**
|
|
14
16
|
* Http Get request
|
|
15
17
|
* @param url
|
|
@@ -22,5 +24,30 @@ export declare class HttpClient implements INetworkModule {
|
|
|
22
24
|
* @param options
|
|
23
25
|
*/
|
|
24
26
|
sendPostRequestAsync<T>(url: string, options?: NetworkRequestOptions): Promise<NetworkResponse<T>>;
|
|
27
|
+
private networkRequestViaProxy;
|
|
28
|
+
private networkRequestViaHttps;
|
|
29
|
+
/**
|
|
30
|
+
* Check if extra parsing is needed on the repsonse from the server
|
|
31
|
+
* @param statusCode {number} the status code of the response from the server
|
|
32
|
+
* @param statusMessage {string | undefined} the status message of the response from the server
|
|
33
|
+
* @param headers {Record<string, string>} the headers of the response from the server
|
|
34
|
+
* @param body {string} the body from the response of the server
|
|
35
|
+
* @returns {Object} JSON parsed body or error object
|
|
36
|
+
*/
|
|
37
|
+
private parseBody;
|
|
38
|
+
/**
|
|
39
|
+
* Helper function to log a formatted message containing URLs, with PII-aware sanitization
|
|
40
|
+
* @param label {string} the label for the log message
|
|
41
|
+
* @param urlString {string} the URL to log
|
|
42
|
+
*/
|
|
43
|
+
private logUrlWithPiiAwareness;
|
|
44
|
+
/**
|
|
45
|
+
* Helper function to determine if a request should be destroyed based on status code and response body.
|
|
46
|
+
* Checks if the response is an error and not part of the device code flow (authorization_pending).
|
|
47
|
+
* @param statusCode {number} the status code of the response
|
|
48
|
+
* @param networkResponse {NetworkResponse<T>} the network response object
|
|
49
|
+
* @returns {boolean} true if the request should be destroyed, false otherwise
|
|
50
|
+
*/
|
|
51
|
+
private shouldDestroyRequest;
|
|
25
52
|
}
|
|
26
53
|
//# sourceMappingURL=HttpClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HttpClient.d.ts","sourceRoot":"","sources":["../../src/network/HttpClient.ts"],"names":[],"mappings":";;AAKA,OAAO,EACH,cAAc,EACd,qBAAqB,EACrB,eAAe,
|
|
1
|
+
{"version":3,"file":"HttpClient.d.ts","sourceRoot":"","sources":["../../src/network/HttpClient.ts"],"names":[],"mappings":";;AAKA,OAAO,EACH,cAAc,EACd,qBAAqB,EACrB,eAAe,EAGf,aAAa,EAChB,MAAM,yBAAyB,CAAC;AAIjC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B;;GAEG;AACH,qBAAa,UAAW,YAAW,cAAc;IAC7C,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,kBAAkB,CAAyC;IACnE,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,YAAY,CAAU;gBAG1B,QAAQ,CAAC,EAAE,MAAM,EACjB,kBAAkB,CAAC,EAAE,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC,YAAY,EAC3D,aAAa,CAAC,EAAE,aAAa;IASjC;;;;OAIG;IACU,mBAAmB,CAAC,CAAC,EAC9B,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,qBAAqB,EAC/B,OAAO,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;IAkB9B;;;;OAIG;IACU,oBAAoB,CAAC,CAAC,EAC/B,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,qBAAqB,GAChC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;IAQ9B,OAAO,CAAC,sBAAsB,CAwM5B;IAEF,OAAO,CAAC,sBAAsB,CAmH5B;IAEF;;;;;;;OAOG;IACH,OAAO,CAAC,SAAS,CA8Cf;IAEF;;;;OAIG;IACH,OAAO,CAAC,sBAAsB,CAoB5B;IAEF;;;;;;OAMG;IACH,OAAO,CAAC,oBAAoB,CAgB1B;CACL"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
/*! @azure/msal-node v3.8.
|
|
1
|
+
/*! @azure/msal-node v3.8.2 2025-11-11 */
|
|
2
2
|
'use strict';
|
|
3
|
-
import { HttpStatus } from '@azure/msal-common/node';
|
|
3
|
+
import { HttpStatus, Logger } from '@azure/msal-common/node';
|
|
4
4
|
import { ProxyStatus, Constants, HttpMethod } from '../utils/Constants.mjs';
|
|
5
5
|
import { NetworkUtils } from '../utils/NetworkUtils.mjs';
|
|
6
|
+
import { name, version } from '../packageMetadata.mjs';
|
|
6
7
|
import http from 'http';
|
|
7
8
|
import https from 'https';
|
|
8
9
|
|
|
@@ -14,9 +15,296 @@ import https from 'https';
|
|
|
14
15
|
* This class implements the API for network requests.
|
|
15
16
|
*/
|
|
16
17
|
class HttpClient {
|
|
17
|
-
constructor(proxyUrl, customAgentOptions) {
|
|
18
|
+
constructor(proxyUrl, customAgentOptions, loggerOptions) {
|
|
19
|
+
this.networkRequestViaProxy = (httpMethod, destinationUrlString, options, timeout) => {
|
|
20
|
+
const destinationUrl = new URL(destinationUrlString);
|
|
21
|
+
const proxyUrl = new URL(this.proxyUrl);
|
|
22
|
+
// "method: connect" must be used to establish a connection to the proxy
|
|
23
|
+
const headers = options?.headers || {};
|
|
24
|
+
const tunnelRequestOptions = {
|
|
25
|
+
host: proxyUrl.hostname,
|
|
26
|
+
port: proxyUrl.port,
|
|
27
|
+
method: "CONNECT",
|
|
28
|
+
path: destinationUrl.hostname,
|
|
29
|
+
headers: headers,
|
|
30
|
+
};
|
|
31
|
+
if (this.customAgentOptions &&
|
|
32
|
+
Object.keys(this.customAgentOptions).length) {
|
|
33
|
+
tunnelRequestOptions.agent = new http.Agent(this.customAgentOptions);
|
|
34
|
+
}
|
|
35
|
+
// compose a request string for the socket
|
|
36
|
+
let postRequestStringContent = "";
|
|
37
|
+
if (httpMethod === HttpMethod.POST) {
|
|
38
|
+
const body = options?.body || "";
|
|
39
|
+
postRequestStringContent =
|
|
40
|
+
"Content-Type: application/x-www-form-urlencoded\r\n" +
|
|
41
|
+
`Content-Length: ${body.length}\r\n` +
|
|
42
|
+
`\r\n${body}`;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
// optional timeout is only for get requests (regionDiscovery, for example)
|
|
46
|
+
if (timeout) {
|
|
47
|
+
tunnelRequestOptions.timeout = timeout;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const outgoingRequestString = `${httpMethod.toUpperCase()} ${destinationUrl.href} HTTP/1.1\r\n` +
|
|
51
|
+
`Host: ${destinationUrl.host}\r\n` +
|
|
52
|
+
"Connection: close\r\n" +
|
|
53
|
+
postRequestStringContent +
|
|
54
|
+
"\r\n";
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
const request = http.request(tunnelRequestOptions);
|
|
57
|
+
if (timeout) {
|
|
58
|
+
request.on("timeout", () => {
|
|
59
|
+
this.logUrlWithPiiAwareness(`Request timeout after ${timeout}ms for URL`, destinationUrlString);
|
|
60
|
+
request.destroy();
|
|
61
|
+
reject(new Error(`Request time out after ${timeout}ms`));
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
request.end();
|
|
65
|
+
// establish connection to the proxy
|
|
66
|
+
request.on("connect", (response, socket) => {
|
|
67
|
+
const proxyStatusCode = response?.statusCode || ProxyStatus.SERVER_ERROR;
|
|
68
|
+
if (proxyStatusCode < ProxyStatus.SUCCESS_RANGE_START ||
|
|
69
|
+
proxyStatusCode > ProxyStatus.SUCCESS_RANGE_END) {
|
|
70
|
+
request.destroy();
|
|
71
|
+
socket.destroy();
|
|
72
|
+
reject(new Error(`Error connecting to proxy. Http status code: ${response.statusCode}. Http status message: ${response?.statusMessage || "Unknown"}`));
|
|
73
|
+
}
|
|
74
|
+
// make a request over an HTTP tunnel
|
|
75
|
+
socket.write(outgoingRequestString);
|
|
76
|
+
const data = [];
|
|
77
|
+
socket.on("data", (chunk) => {
|
|
78
|
+
data.push(chunk);
|
|
79
|
+
});
|
|
80
|
+
socket.on("end", () => {
|
|
81
|
+
// combine all received buffer streams into one buffer, and then into a string
|
|
82
|
+
const dataString = Buffer.concat([...data]).toString();
|
|
83
|
+
// separate each line into it's own entry in an arry
|
|
84
|
+
const dataStringArray = dataString.split("\r\n");
|
|
85
|
+
// the first entry will contain the statusCode and statusMessage
|
|
86
|
+
const httpStatusCode = parseInt(dataStringArray[0].split(" ")[1]);
|
|
87
|
+
// remove "HTTP/1.1" and the status code to get the status message
|
|
88
|
+
const statusMessage = dataStringArray[0]
|
|
89
|
+
.split(" ")
|
|
90
|
+
.slice(2)
|
|
91
|
+
.join(" ");
|
|
92
|
+
// the last entry will contain the body
|
|
93
|
+
const body = dataStringArray[dataStringArray.length - 1];
|
|
94
|
+
// everything in between the first and last entries are the headers
|
|
95
|
+
const headersArray = dataStringArray.slice(1, dataStringArray.length - 2);
|
|
96
|
+
// build an object out of all the headers
|
|
97
|
+
const entries = new Map();
|
|
98
|
+
headersArray.forEach((header) => {
|
|
99
|
+
/**
|
|
100
|
+
* the header might look like "Content-Length: 1531", but that is just a string
|
|
101
|
+
* it needs to be converted to a key/value pair
|
|
102
|
+
* split the string at the first instance of ":"
|
|
103
|
+
* there may be more than one ":" if the value of the header is supposed to be a JSON object
|
|
104
|
+
*/
|
|
105
|
+
const headerKeyValue = header.split(new RegExp(/:\s(.*)/s));
|
|
106
|
+
const headerKey = headerKeyValue[0];
|
|
107
|
+
let headerValue = headerKeyValue[1];
|
|
108
|
+
// check if the value of the header is supposed to be a JSON object
|
|
109
|
+
try {
|
|
110
|
+
const object = JSON.parse(headerValue);
|
|
111
|
+
// if it is, then convert it from a string to a JSON object
|
|
112
|
+
if (object && typeof object === "object") {
|
|
113
|
+
headerValue = object;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch (e) {
|
|
117
|
+
// otherwise, leave it as a string
|
|
118
|
+
}
|
|
119
|
+
entries.set(headerKey, headerValue);
|
|
120
|
+
});
|
|
121
|
+
const headers = Object.fromEntries(entries);
|
|
122
|
+
const parsedHeaders = headers;
|
|
123
|
+
const networkResponse = NetworkUtils.getNetworkResponse(parsedHeaders, this.parseBody(httpStatusCode, statusMessage, parsedHeaders, body), httpStatusCode);
|
|
124
|
+
if (this.shouldDestroyRequest(httpStatusCode, networkResponse)) {
|
|
125
|
+
request.destroy();
|
|
126
|
+
}
|
|
127
|
+
resolve(networkResponse);
|
|
128
|
+
});
|
|
129
|
+
socket.on("error", (chunk) => {
|
|
130
|
+
request.destroy();
|
|
131
|
+
socket.destroy();
|
|
132
|
+
reject(new Error(chunk.toString()));
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
request.on("error", (chunk) => {
|
|
136
|
+
this.logger.error(`HttpClient - Proxy request error: ${chunk.toString()}`, "");
|
|
137
|
+
this.logUrlWithPiiAwareness("Destination URL", destinationUrlString);
|
|
138
|
+
this.logUrlWithPiiAwareness("Proxy URL", this.proxyUrl);
|
|
139
|
+
this.logger.error(`HttpClient - Method: ${httpMethod}`, "");
|
|
140
|
+
this.logger.errorPii(`HttpClient - Headers: ${JSON.stringify(headers)}`, "");
|
|
141
|
+
request.destroy();
|
|
142
|
+
reject(new Error(chunk.toString()));
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
this.networkRequestViaHttps = (httpMethod, urlString, options, timeout) => {
|
|
147
|
+
const isPostRequest = httpMethod === HttpMethod.POST;
|
|
148
|
+
const body = options?.body || "";
|
|
149
|
+
const url = new URL(urlString);
|
|
150
|
+
const headers = options?.headers || {};
|
|
151
|
+
const customOptions = {
|
|
152
|
+
method: httpMethod,
|
|
153
|
+
headers: headers,
|
|
154
|
+
...NetworkUtils.urlToHttpOptions(url),
|
|
155
|
+
};
|
|
156
|
+
if (this.customAgentOptions &&
|
|
157
|
+
Object.keys(this.customAgentOptions).length) {
|
|
158
|
+
customOptions.agent = new https.Agent(this.customAgentOptions);
|
|
159
|
+
}
|
|
160
|
+
if (isPostRequest) {
|
|
161
|
+
// needed for post request to work
|
|
162
|
+
customOptions.headers = {
|
|
163
|
+
...customOptions.headers,
|
|
164
|
+
"Content-Length": body.length,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
// optional timeout is only for get requests (regionDiscovery, for example)
|
|
169
|
+
if (timeout) {
|
|
170
|
+
customOptions.timeout = timeout;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return new Promise((resolve, reject) => {
|
|
174
|
+
let request;
|
|
175
|
+
// managed identity sources use http instead of https
|
|
176
|
+
if (customOptions.protocol === "http:") {
|
|
177
|
+
request = http.request(customOptions);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
request = https.request(customOptions);
|
|
181
|
+
}
|
|
182
|
+
if (isPostRequest) {
|
|
183
|
+
request.write(body);
|
|
184
|
+
}
|
|
185
|
+
if (timeout) {
|
|
186
|
+
request.on("timeout", () => {
|
|
187
|
+
this.logUrlWithPiiAwareness(`HTTPS request timeout after ${timeout}ms for URL`, urlString);
|
|
188
|
+
request.destroy();
|
|
189
|
+
reject(new Error(`Request time out after ${timeout}ms`));
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
request.end();
|
|
193
|
+
request.on("response", (response) => {
|
|
194
|
+
const headers = response.headers;
|
|
195
|
+
const statusCode = response.statusCode;
|
|
196
|
+
const statusMessage = response.statusMessage;
|
|
197
|
+
const data = [];
|
|
198
|
+
response.on("data", (chunk) => {
|
|
199
|
+
data.push(chunk);
|
|
200
|
+
});
|
|
201
|
+
response.on("end", () => {
|
|
202
|
+
// combine all received buffer streams into one buffer, and then into a string
|
|
203
|
+
const body = Buffer.concat([...data]).toString();
|
|
204
|
+
const parsedHeaders = headers;
|
|
205
|
+
const networkResponse = NetworkUtils.getNetworkResponse(parsedHeaders, this.parseBody(statusCode, statusMessage, parsedHeaders, body), statusCode);
|
|
206
|
+
if (this.shouldDestroyRequest(statusCode, networkResponse)) {
|
|
207
|
+
request.destroy();
|
|
208
|
+
}
|
|
209
|
+
resolve(networkResponse);
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
request.on("error", (chunk) => {
|
|
213
|
+
this.logger.error(`HttpClient - HTTPS request error: ${chunk.toString()}`, "");
|
|
214
|
+
this.logUrlWithPiiAwareness("URL", urlString);
|
|
215
|
+
this.logger.error(`HttpClient - Method: ${httpMethod}`, "");
|
|
216
|
+
this.logger.errorPii(`HttpClient - Headers: ${JSON.stringify(headers)}`, "");
|
|
217
|
+
request.destroy();
|
|
218
|
+
reject(new Error(chunk.toString()));
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
};
|
|
222
|
+
/**
|
|
223
|
+
* Check if extra parsing is needed on the repsonse from the server
|
|
224
|
+
* @param statusCode {number} the status code of the response from the server
|
|
225
|
+
* @param statusMessage {string | undefined} the status message of the response from the server
|
|
226
|
+
* @param headers {Record<string, string>} the headers of the response from the server
|
|
227
|
+
* @param body {string} the body from the response of the server
|
|
228
|
+
* @returns {Object} JSON parsed body or error object
|
|
229
|
+
*/
|
|
230
|
+
this.parseBody = (statusCode, statusMessage, headers, body) => {
|
|
231
|
+
/*
|
|
232
|
+
* Informational responses (100 – 199)
|
|
233
|
+
* Successful responses (200 – 299)
|
|
234
|
+
* Redirection messages (300 – 399)
|
|
235
|
+
* Client error responses (400 – 499)
|
|
236
|
+
* Server error responses (500 – 599)
|
|
237
|
+
*/
|
|
238
|
+
let parsedBody;
|
|
239
|
+
try {
|
|
240
|
+
parsedBody = JSON.parse(body);
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
let errorType;
|
|
244
|
+
let errorDescriptionHelper;
|
|
245
|
+
if (statusCode >= HttpStatus.CLIENT_ERROR_RANGE_START &&
|
|
246
|
+
statusCode <= HttpStatus.CLIENT_ERROR_RANGE_END) {
|
|
247
|
+
errorType = "client_error";
|
|
248
|
+
errorDescriptionHelper = "A client";
|
|
249
|
+
}
|
|
250
|
+
else if (statusCode >= HttpStatus.SERVER_ERROR_RANGE_START &&
|
|
251
|
+
statusCode <= HttpStatus.SERVER_ERROR_RANGE_END) {
|
|
252
|
+
errorType = "server_error";
|
|
253
|
+
errorDescriptionHelper = "A server";
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
errorType = "unknown_error";
|
|
257
|
+
errorDescriptionHelper = "An unknown";
|
|
258
|
+
}
|
|
259
|
+
parsedBody = {
|
|
260
|
+
error: errorType,
|
|
261
|
+
error_description: `${errorDescriptionHelper} error occured.\nHttp status code: ${statusCode}\nHttp status message: ${statusMessage || "Unknown"}\nHeaders: ${JSON.stringify(headers)}`,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
return parsedBody;
|
|
265
|
+
};
|
|
266
|
+
/**
|
|
267
|
+
* Helper function to log a formatted message containing URLs, with PII-aware sanitization
|
|
268
|
+
* @param label {string} the label for the log message
|
|
269
|
+
* @param urlString {string} the URL to log
|
|
270
|
+
*/
|
|
271
|
+
this.logUrlWithPiiAwareness = (label, urlString) => {
|
|
272
|
+
if (this.isPiiEnabled) {
|
|
273
|
+
this.logger.errorPii(`HttpClient - ${label}: ${urlString}`, "");
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
let urlHelper;
|
|
277
|
+
try {
|
|
278
|
+
const url = new URL(urlString);
|
|
279
|
+
urlHelper = `${url.protocol}//${url.host}${url.pathname}`;
|
|
280
|
+
}
|
|
281
|
+
catch {
|
|
282
|
+
urlHelper = urlString.split("?")[0] || "unknown";
|
|
283
|
+
}
|
|
284
|
+
this.logger.error(`HttpClient - ${label}: ${urlHelper} [Enable PII logging to see additional details]`, "");
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
/**
|
|
288
|
+
* Helper function to determine if a request should be destroyed based on status code and response body.
|
|
289
|
+
* Checks if the response is an error and not part of the device code flow (authorization_pending).
|
|
290
|
+
* @param statusCode {number} the status code of the response
|
|
291
|
+
* @param networkResponse {NetworkResponse<T>} the network response object
|
|
292
|
+
* @returns {boolean} true if the request should be destroyed, false otherwise
|
|
293
|
+
*/
|
|
294
|
+
this.shouldDestroyRequest = (statusCode, networkResponse) => {
|
|
295
|
+
return ((statusCode < HttpStatus.SUCCESS_RANGE_START ||
|
|
296
|
+
statusCode > HttpStatus.SUCCESS_RANGE_END) &&
|
|
297
|
+
// do not destroy the request for the device code flow
|
|
298
|
+
!(networkResponse.body &&
|
|
299
|
+
typeof networkResponse.body === "object" &&
|
|
300
|
+
"error" in networkResponse.body &&
|
|
301
|
+
networkResponse.body.error ===
|
|
302
|
+
Constants.AUTHORIZATION_PENDING));
|
|
303
|
+
};
|
|
18
304
|
this.proxyUrl = proxyUrl || "";
|
|
19
305
|
this.customAgentOptions = customAgentOptions || {};
|
|
306
|
+
this.logger = new Logger(loggerOptions || {}, name, version);
|
|
307
|
+
this.isPiiEnabled = this.logger.isPiiLoggingEnabled();
|
|
20
308
|
}
|
|
21
309
|
/**
|
|
22
310
|
* Http Get request
|
|
@@ -25,10 +313,10 @@ class HttpClient {
|
|
|
25
313
|
*/
|
|
26
314
|
async sendGetRequestAsync(url, options, timeout) {
|
|
27
315
|
if (this.proxyUrl) {
|
|
28
|
-
return networkRequestViaProxy(
|
|
316
|
+
return this.networkRequestViaProxy(HttpMethod.GET, url, options, timeout);
|
|
29
317
|
}
|
|
30
318
|
else {
|
|
31
|
-
return networkRequestViaHttps(
|
|
319
|
+
return this.networkRequestViaHttps(HttpMethod.GET, url, options, timeout);
|
|
32
320
|
}
|
|
33
321
|
}
|
|
34
322
|
/**
|
|
@@ -38,255 +326,13 @@ class HttpClient {
|
|
|
38
326
|
*/
|
|
39
327
|
async sendPostRequestAsync(url, options) {
|
|
40
328
|
if (this.proxyUrl) {
|
|
41
|
-
return networkRequestViaProxy(
|
|
329
|
+
return this.networkRequestViaProxy(HttpMethod.POST, url, options);
|
|
42
330
|
}
|
|
43
331
|
else {
|
|
44
|
-
return networkRequestViaHttps(
|
|
332
|
+
return this.networkRequestViaHttps(HttpMethod.POST, url, options);
|
|
45
333
|
}
|
|
46
334
|
}
|
|
47
|
-
}
|
|
48
|
-
const networkRequestViaProxy = (destinationUrlString, proxyUrlString, httpMethod, options, agentOptions, timeout) => {
|
|
49
|
-
const destinationUrl = new URL(destinationUrlString);
|
|
50
|
-
const proxyUrl = new URL(proxyUrlString);
|
|
51
|
-
// "method: connect" must be used to establish a connection to the proxy
|
|
52
|
-
const headers = options?.headers || {};
|
|
53
|
-
const tunnelRequestOptions = {
|
|
54
|
-
host: proxyUrl.hostname,
|
|
55
|
-
port: proxyUrl.port,
|
|
56
|
-
method: "CONNECT",
|
|
57
|
-
path: destinationUrl.hostname,
|
|
58
|
-
headers: headers,
|
|
59
|
-
};
|
|
60
|
-
if (agentOptions && Object.keys(agentOptions).length) {
|
|
61
|
-
tunnelRequestOptions.agent = new http.Agent(agentOptions);
|
|
62
|
-
}
|
|
63
|
-
// compose a request string for the socket
|
|
64
|
-
let postRequestStringContent = "";
|
|
65
|
-
if (httpMethod === HttpMethod.POST) {
|
|
66
|
-
const body = options?.body || "";
|
|
67
|
-
postRequestStringContent =
|
|
68
|
-
"Content-Type: application/x-www-form-urlencoded\r\n" +
|
|
69
|
-
`Content-Length: ${body.length}\r\n` +
|
|
70
|
-
`\r\n${body}`;
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
// optional timeout is only for get requests (regionDiscovery, for example)
|
|
74
|
-
if (timeout) {
|
|
75
|
-
tunnelRequestOptions.timeout = timeout;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
const outgoingRequestString = `${httpMethod.toUpperCase()} ${destinationUrl.href} HTTP/1.1\r\n` +
|
|
79
|
-
`Host: ${destinationUrl.host}\r\n` +
|
|
80
|
-
"Connection: close\r\n" +
|
|
81
|
-
postRequestStringContent +
|
|
82
|
-
"\r\n";
|
|
83
|
-
return new Promise((resolve, reject) => {
|
|
84
|
-
const request = http.request(tunnelRequestOptions);
|
|
85
|
-
if (timeout) {
|
|
86
|
-
request.on("timeout", () => {
|
|
87
|
-
request.destroy();
|
|
88
|
-
reject(new Error("Request time out"));
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
request.end();
|
|
92
|
-
// establish connection to the proxy
|
|
93
|
-
request.on("connect", (response, socket) => {
|
|
94
|
-
const proxyStatusCode = response?.statusCode || ProxyStatus.SERVER_ERROR;
|
|
95
|
-
if (proxyStatusCode < ProxyStatus.SUCCESS_RANGE_START ||
|
|
96
|
-
proxyStatusCode > ProxyStatus.SUCCESS_RANGE_END) {
|
|
97
|
-
request.destroy();
|
|
98
|
-
socket.destroy();
|
|
99
|
-
reject(new Error(`Error connecting to proxy. Http status code: ${response.statusCode}. Http status message: ${response?.statusMessage || "Unknown"}`));
|
|
100
|
-
}
|
|
101
|
-
// make a request over an HTTP tunnel
|
|
102
|
-
socket.write(outgoingRequestString);
|
|
103
|
-
const data = [];
|
|
104
|
-
socket.on("data", (chunk) => {
|
|
105
|
-
data.push(chunk);
|
|
106
|
-
});
|
|
107
|
-
socket.on("end", () => {
|
|
108
|
-
// combine all received buffer streams into one buffer, and then into a string
|
|
109
|
-
const dataString = Buffer.concat([...data]).toString();
|
|
110
|
-
// separate each line into it's own entry in an arry
|
|
111
|
-
const dataStringArray = dataString.split("\r\n");
|
|
112
|
-
// the first entry will contain the statusCode and statusMessage
|
|
113
|
-
const httpStatusCode = parseInt(dataStringArray[0].split(" ")[1]);
|
|
114
|
-
// remove "HTTP/1.1" and the status code to get the status message
|
|
115
|
-
const statusMessage = dataStringArray[0]
|
|
116
|
-
.split(" ")
|
|
117
|
-
.slice(2)
|
|
118
|
-
.join(" ");
|
|
119
|
-
// the last entry will contain the body
|
|
120
|
-
const body = dataStringArray[dataStringArray.length - 1];
|
|
121
|
-
// everything in between the first and last entries are the headers
|
|
122
|
-
const headersArray = dataStringArray.slice(1, dataStringArray.length - 2);
|
|
123
|
-
// build an object out of all the headers
|
|
124
|
-
const entries = new Map();
|
|
125
|
-
headersArray.forEach((header) => {
|
|
126
|
-
/**
|
|
127
|
-
* the header might look like "Content-Length: 1531", but that is just a string
|
|
128
|
-
* it needs to be converted to a key/value pair
|
|
129
|
-
* split the string at the first instance of ":"
|
|
130
|
-
* there may be more than one ":" if the value of the header is supposed to be a JSON object
|
|
131
|
-
*/
|
|
132
|
-
const headerKeyValue = header.split(new RegExp(/:\s(.*)/s));
|
|
133
|
-
const headerKey = headerKeyValue[0];
|
|
134
|
-
let headerValue = headerKeyValue[1];
|
|
135
|
-
// check if the value of the header is supposed to be a JSON object
|
|
136
|
-
try {
|
|
137
|
-
const object = JSON.parse(headerValue);
|
|
138
|
-
// if it is, then convert it from a string to a JSON object
|
|
139
|
-
if (object && typeof object === "object") {
|
|
140
|
-
headerValue = object;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
catch (e) {
|
|
144
|
-
// otherwise, leave it as a string
|
|
145
|
-
}
|
|
146
|
-
entries.set(headerKey, headerValue);
|
|
147
|
-
});
|
|
148
|
-
const headers = Object.fromEntries(entries);
|
|
149
|
-
const parsedHeaders = headers;
|
|
150
|
-
const networkResponse = NetworkUtils.getNetworkResponse(parsedHeaders, parseBody(httpStatusCode, statusMessage, parsedHeaders, body), httpStatusCode);
|
|
151
|
-
if ((httpStatusCode < HttpStatus.SUCCESS_RANGE_START ||
|
|
152
|
-
httpStatusCode > HttpStatus.SUCCESS_RANGE_END) &&
|
|
153
|
-
// do not destroy the request for the device code flow
|
|
154
|
-
networkResponse.body["error"] !==
|
|
155
|
-
Constants.AUTHORIZATION_PENDING) {
|
|
156
|
-
request.destroy();
|
|
157
|
-
}
|
|
158
|
-
resolve(networkResponse);
|
|
159
|
-
});
|
|
160
|
-
socket.on("error", (chunk) => {
|
|
161
|
-
request.destroy();
|
|
162
|
-
socket.destroy();
|
|
163
|
-
reject(new Error(chunk.toString()));
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
request.on("error", (chunk) => {
|
|
167
|
-
request.destroy();
|
|
168
|
-
reject(new Error(chunk.toString()));
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
};
|
|
172
|
-
const networkRequestViaHttps = (urlString, httpMethod, options, agentOptions, timeout) => {
|
|
173
|
-
const isPostRequest = httpMethod === HttpMethod.POST;
|
|
174
|
-
const body = options?.body || "";
|
|
175
|
-
const url = new URL(urlString);
|
|
176
|
-
const headers = options?.headers || {};
|
|
177
|
-
const customOptions = {
|
|
178
|
-
method: httpMethod,
|
|
179
|
-
headers: headers,
|
|
180
|
-
...NetworkUtils.urlToHttpOptions(url),
|
|
181
|
-
};
|
|
182
|
-
if (agentOptions && Object.keys(agentOptions).length) {
|
|
183
|
-
customOptions.agent = new https.Agent(agentOptions);
|
|
184
|
-
}
|
|
185
|
-
if (isPostRequest) {
|
|
186
|
-
// needed for post request to work
|
|
187
|
-
customOptions.headers = {
|
|
188
|
-
...customOptions.headers,
|
|
189
|
-
"Content-Length": body.length,
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
else {
|
|
193
|
-
// optional timeout is only for get requests (regionDiscovery, for example)
|
|
194
|
-
if (timeout) {
|
|
195
|
-
customOptions.timeout = timeout;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
return new Promise((resolve, reject) => {
|
|
199
|
-
let request;
|
|
200
|
-
// managed identity sources use http instead of https
|
|
201
|
-
if (customOptions.protocol === "http:") {
|
|
202
|
-
request = http.request(customOptions);
|
|
203
|
-
}
|
|
204
|
-
else {
|
|
205
|
-
request = https.request(customOptions);
|
|
206
|
-
}
|
|
207
|
-
if (isPostRequest) {
|
|
208
|
-
request.write(body);
|
|
209
|
-
}
|
|
210
|
-
if (timeout) {
|
|
211
|
-
request.on("timeout", () => {
|
|
212
|
-
request.destroy();
|
|
213
|
-
reject(new Error("Request time out"));
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
request.end();
|
|
217
|
-
request.on("response", (response) => {
|
|
218
|
-
const headers = response.headers;
|
|
219
|
-
const statusCode = response.statusCode;
|
|
220
|
-
const statusMessage = response.statusMessage;
|
|
221
|
-
const data = [];
|
|
222
|
-
response.on("data", (chunk) => {
|
|
223
|
-
data.push(chunk);
|
|
224
|
-
});
|
|
225
|
-
response.on("end", () => {
|
|
226
|
-
// combine all received buffer streams into one buffer, and then into a string
|
|
227
|
-
const body = Buffer.concat([...data]).toString();
|
|
228
|
-
const parsedHeaders = headers;
|
|
229
|
-
const networkResponse = NetworkUtils.getNetworkResponse(parsedHeaders, parseBody(statusCode, statusMessage, parsedHeaders, body), statusCode);
|
|
230
|
-
if ((statusCode < HttpStatus.SUCCESS_RANGE_START ||
|
|
231
|
-
statusCode > HttpStatus.SUCCESS_RANGE_END) &&
|
|
232
|
-
// do not destroy the request for the device code flow
|
|
233
|
-
networkResponse.body["error"] !==
|
|
234
|
-
Constants.AUTHORIZATION_PENDING) {
|
|
235
|
-
request.destroy();
|
|
236
|
-
}
|
|
237
|
-
resolve(networkResponse);
|
|
238
|
-
});
|
|
239
|
-
});
|
|
240
|
-
request.on("error", (chunk) => {
|
|
241
|
-
request.destroy();
|
|
242
|
-
reject(new Error(chunk.toString()));
|
|
243
|
-
});
|
|
244
|
-
});
|
|
245
|
-
};
|
|
246
|
-
/**
|
|
247
|
-
* Check if extra parsing is needed on the repsonse from the server
|
|
248
|
-
* @param statusCode {number} the status code of the response from the server
|
|
249
|
-
* @param statusMessage {string | undefined} the status message of the response from the server
|
|
250
|
-
* @param headers {Record<string, string>} the headers of the response from the server
|
|
251
|
-
* @param body {string} the body from the response of the server
|
|
252
|
-
* @returns {Object} JSON parsed body or error object
|
|
253
|
-
*/
|
|
254
|
-
const parseBody = (statusCode, statusMessage, headers, body) => {
|
|
255
|
-
/*
|
|
256
|
-
* Informational responses (100 – 199)
|
|
257
|
-
* Successful responses (200 – 299)
|
|
258
|
-
* Redirection messages (300 – 399)
|
|
259
|
-
* Client error responses (400 – 499)
|
|
260
|
-
* Server error responses (500 – 599)
|
|
261
|
-
*/
|
|
262
|
-
let parsedBody;
|
|
263
|
-
try {
|
|
264
|
-
parsedBody = JSON.parse(body);
|
|
265
|
-
}
|
|
266
|
-
catch (error) {
|
|
267
|
-
let errorType;
|
|
268
|
-
let errorDescriptionHelper;
|
|
269
|
-
if (statusCode >= HttpStatus.CLIENT_ERROR_RANGE_START &&
|
|
270
|
-
statusCode <= HttpStatus.CLIENT_ERROR_RANGE_END) {
|
|
271
|
-
errorType = "client_error";
|
|
272
|
-
errorDescriptionHelper = "A client";
|
|
273
|
-
}
|
|
274
|
-
else if (statusCode >= HttpStatus.SERVER_ERROR_RANGE_START &&
|
|
275
|
-
statusCode <= HttpStatus.SERVER_ERROR_RANGE_END) {
|
|
276
|
-
errorType = "server_error";
|
|
277
|
-
errorDescriptionHelper = "A server";
|
|
278
|
-
}
|
|
279
|
-
else {
|
|
280
|
-
errorType = "unknown_error";
|
|
281
|
-
errorDescriptionHelper = "An unknown";
|
|
282
|
-
}
|
|
283
|
-
parsedBody = {
|
|
284
|
-
error: errorType,
|
|
285
|
-
error_description: `${errorDescriptionHelper} error occured.\nHttp status code: ${statusCode}\nHttp status message: ${statusMessage || "Unknown"}\nHeaders: ${JSON.stringify(headers)}`,
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
return parsedBody;
|
|
289
|
-
};
|
|
335
|
+
}
|
|
290
336
|
|
|
291
337
|
export { HttpClient };
|
|
292
338
|
//# sourceMappingURL=HttpClient.mjs.map
|