@azure/msal-node 2.0.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/cache/NodeStorage.cjs +1 -1
- package/dist/cache/NodeStorage.cjs.map +1 -1
- package/dist/cache/NodeStorage.d.ts +1 -1
- package/dist/cache/NodeStorage.d.ts.map +1 -1
- package/dist/cache/NodeStorage.mjs +1 -1
- package/dist/cache/NodeStorage.mjs.map +1 -1
- package/dist/cache/TokenCache.cjs +5 -9
- package/dist/cache/TokenCache.cjs.map +1 -1
- package/dist/cache/TokenCache.d.ts +3 -3
- package/dist/cache/TokenCache.d.ts.map +1 -1
- package/dist/cache/TokenCache.mjs +6 -10
- package/dist/cache/TokenCache.mjs.map +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.cjs +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.cjs.map +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.d.ts +2 -2
- package/dist/cache/distributed/DistributedCachePlugin.d.ts.map +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.mjs +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.mjs.map +1 -1
- package/dist/cache/serializer/Deserializer.cjs +2 -4
- package/dist/cache/serializer/Deserializer.cjs.map +1 -1
- package/dist/cache/serializer/Deserializer.d.ts +1 -1
- package/dist/cache/serializer/Deserializer.d.ts.map +1 -1
- package/dist/cache/serializer/Deserializer.mjs +3 -5
- package/dist/cache/serializer/Deserializer.mjs.map +1 -1
- package/dist/cache/serializer/Serializer.cjs +1 -1
- package/dist/cache/serializer/Serializer.cjs.map +1 -1
- package/dist/cache/serializer/Serializer.d.ts +1 -1
- package/dist/cache/serializer/Serializer.d.ts.map +1 -1
- package/dist/cache/serializer/Serializer.mjs +1 -1
- package/dist/cache/serializer/Serializer.mjs.map +1 -1
- package/dist/client/ClientApplication.cjs +2 -2
- package/dist/client/ClientApplication.cjs.map +1 -1
- package/dist/client/ClientApplication.d.ts +10 -10
- package/dist/client/ClientApplication.d.ts.map +1 -1
- package/dist/client/ClientApplication.mjs +2 -2
- package/dist/client/ClientApplication.mjs.map +1 -1
- package/dist/client/ClientAssertion.cjs +1 -1
- package/dist/client/ClientAssertion.cjs.map +1 -1
- package/dist/client/ClientAssertion.d.ts +1 -1
- package/dist/client/ClientAssertion.d.ts.map +1 -1
- package/dist/client/ClientAssertion.mjs +1 -1
- package/dist/client/ClientAssertion.mjs.map +1 -1
- package/dist/client/ClientCredentialClient.cjs +26 -4
- package/dist/client/ClientCredentialClient.cjs.map +1 -1
- package/dist/client/ClientCredentialClient.d.ts +1 -0
- package/dist/client/ClientCredentialClient.d.ts.map +1 -1
- package/dist/client/ClientCredentialClient.mjs +27 -5
- package/dist/client/ClientCredentialClient.mjs.map +1 -1
- package/dist/client/ConfidentialClientApplication.cjs +4 -5
- package/dist/client/ConfidentialClientApplication.cjs.map +1 -1
- package/dist/client/ConfidentialClientApplication.d.ts +5 -5
- package/dist/client/ConfidentialClientApplication.d.ts.map +1 -1
- package/dist/client/ConfidentialClientApplication.mjs +5 -6
- package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
- package/dist/client/DeviceCodeClient.cjs +3 -3
- package/dist/client/DeviceCodeClient.cjs.map +1 -1
- package/dist/client/DeviceCodeClient.d.ts.map +1 -1
- package/dist/client/DeviceCodeClient.mjs +4 -4
- package/dist/client/DeviceCodeClient.mjs.map +1 -1
- package/dist/client/IConfidentialClientApplication.d.ts +8 -8
- package/dist/client/IConfidentialClientApplication.d.ts.map +1 -1
- package/dist/client/IPublicClientApplication.d.ts +9 -9
- package/dist/client/IPublicClientApplication.d.ts.map +1 -1
- package/dist/client/OnBehalfOfClient.cjs +7 -8
- package/dist/client/OnBehalfOfClient.cjs.map +1 -1
- package/dist/client/OnBehalfOfClient.d.ts.map +1 -1
- package/dist/client/OnBehalfOfClient.mjs +7 -8
- package/dist/client/OnBehalfOfClient.mjs.map +1 -1
- package/dist/client/PublicClientApplication.cjs +36 -29
- package/dist/client/PublicClientApplication.cjs.map +1 -1
- package/dist/client/PublicClientApplication.d.ts +7 -7
- package/dist/client/PublicClientApplication.d.ts.map +1 -1
- package/dist/client/PublicClientApplication.mjs +40 -33
- package/dist/client/PublicClientApplication.mjs.map +1 -1
- package/dist/client/UsernamePasswordClient.cjs +2 -2
- package/dist/client/UsernamePasswordClient.cjs.map +1 -1
- package/dist/client/UsernamePasswordClient.d.ts.map +1 -1
- package/dist/client/UsernamePasswordClient.mjs +2 -2
- package/dist/client/UsernamePasswordClient.mjs.map +1 -1
- package/dist/config/Configuration.cjs +1 -1
- package/dist/config/Configuration.cjs.map +1 -1
- package/dist/config/Configuration.mjs +1 -1
- package/dist/config/Configuration.mjs.map +1 -1
- package/dist/crypto/CryptoProvider.cjs +1 -1
- package/dist/crypto/CryptoProvider.cjs.map +1 -1
- package/dist/crypto/CryptoProvider.mjs +1 -1
- package/dist/crypto/CryptoProvider.mjs.map +1 -1
- package/dist/crypto/GuidGenerator.cjs +1 -1
- package/dist/crypto/GuidGenerator.cjs.map +1 -1
- package/dist/crypto/GuidGenerator.mjs +1 -1
- package/dist/crypto/GuidGenerator.mjs.map +1 -1
- package/dist/crypto/HashUtils.cjs +1 -1
- package/dist/crypto/HashUtils.cjs.map +1 -1
- package/dist/crypto/HashUtils.mjs +1 -1
- package/dist/crypto/HashUtils.mjs.map +1 -1
- package/dist/crypto/PkceGenerator.cjs +1 -1
- package/dist/crypto/PkceGenerator.cjs.map +1 -1
- package/dist/crypto/PkceGenerator.mjs +1 -1
- package/dist/crypto/PkceGenerator.mjs.map +1 -1
- package/dist/error/NodeAuthError.cjs +1 -1
- package/dist/error/NodeAuthError.cjs.map +1 -1
- package/dist/error/NodeAuthError.mjs +1 -1
- package/dist/error/NodeAuthError.mjs.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +32 -32
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/internals.cjs +1 -1
- package/dist/internals.cjs.map +1 -1
- package/dist/internals.d.ts +2 -2
- package/dist/internals.d.ts.map +1 -1
- package/dist/internals.mjs +1 -1
- package/dist/internals.mjs.map +1 -1
- package/dist/network/HttpClient.cjs +10 -9
- package/dist/network/HttpClient.cjs.map +1 -1
- package/dist/network/HttpClient.d.ts.map +1 -1
- package/dist/network/HttpClient.mjs +3 -2
- package/dist/network/HttpClient.mjs.map +1 -1
- package/dist/network/LoopbackClient.cjs +2 -2
- package/dist/network/LoopbackClient.cjs.map +1 -1
- package/dist/network/LoopbackClient.d.ts +1 -1
- package/dist/network/LoopbackClient.d.ts.map +1 -1
- package/dist/network/LoopbackClient.mjs +3 -3
- package/dist/network/LoopbackClient.mjs.map +1 -1
- package/dist/packageMetadata.cjs +2 -2
- package/dist/packageMetadata.cjs.map +1 -1
- package/dist/packageMetadata.d.ts +1 -1
- package/dist/packageMetadata.mjs +2 -2
- package/dist/packageMetadata.mjs.map +1 -1
- package/dist/request/InteractiveRequest.d.ts +2 -2
- package/dist/request/InteractiveRequest.d.ts.map +1 -1
- package/dist/utils/Constants.cjs +1 -11
- package/dist/utils/Constants.cjs.map +1 -1
- package/dist/utils/Constants.d.ts +0 -10
- package/dist/utils/Constants.d.ts.map +1 -1
- package/dist/utils/Constants.mjs +2 -11
- package/dist/utils/Constants.mjs.map +1 -1
- package/dist/utils/EncodingUtils.cjs +1 -1
- package/dist/utils/EncodingUtils.cjs.map +1 -1
- package/dist/utils/EncodingUtils.mjs +1 -1
- package/dist/utils/EncodingUtils.mjs.map +1 -1
- package/dist/utils/NetworkUtils.cjs +1 -1
- package/dist/utils/NetworkUtils.cjs.map +1 -1
- package/dist/utils/NetworkUtils.mjs +1 -1
- package/dist/utils/NetworkUtils.mjs.map +1 -1
- package/package.json +8 -5
- package/src/cache/ITokenCache.ts +24 -0
- package/src/cache/NodeStorage.ts +533 -0
- package/src/cache/TokenCache.ts +358 -0
- package/src/cache/distributed/DistributedCachePlugin.ts +56 -0
- package/src/cache/distributed/ICacheClient.ts +23 -0
- package/src/cache/distributed/IPartitionManager.ts +35 -0
- package/src/cache/serializer/Deserializer.ts +218 -0
- package/src/cache/serializer/Serializer.ts +173 -0
- package/src/cache/serializer/SerializerTypes.ts +121 -0
- package/src/client/ClientApplication.ts +595 -0
- package/src/client/ClientAssertion.ts +160 -0
- package/src/client/ClientCredentialClient.ts +348 -0
- package/src/client/ConfidentialClientApplication.ts +261 -0
- package/src/client/DeviceCodeClient.ts +360 -0
- package/src/client/IConfidentialClientApplication.ts +72 -0
- package/src/client/IPublicClientApplication.ts +72 -0
- package/src/client/OnBehalfOfClient.ts +361 -0
- package/src/client/PublicClientApplication.ts +283 -0
- package/src/client/UsernamePasswordClient.ts +180 -0
- package/src/config/Configuration.ts +201 -0
- package/src/crypto/CryptoProvider.ts +98 -0
- package/src/crypto/GuidGenerator.ts +28 -0
- package/src/crypto/HashUtils.ts +17 -0
- package/src/crypto/PkceGenerator.ts +63 -0
- package/src/error/NodeAuthError.ts +117 -0
- package/src/index.ts +121 -0
- package/src/internals.ts +12 -0
- package/src/network/HttpClient.ts +425 -0
- package/src/network/ILoopbackClient.ts +19 -0
- package/src/network/LoopbackClient.ts +126 -0
- package/src/packageMetadata.ts +3 -0
- package/src/request/AuthorizationCodeRequest.ts +39 -0
- package/src/request/AuthorizationUrlRequest.ts +50 -0
- package/src/request/ClientCredentialRequest.ts +29 -0
- package/src/request/DeviceCodeRequest.ts +34 -0
- package/src/request/InteractiveRequest.ts +39 -0
- package/src/request/OnBehalfOfRequest.ts +30 -0
- package/src/request/RefreshTokenRequest.ts +34 -0
- package/src/request/SignOutRequest.ts +11 -0
- package/src/request/SilentFlowRequest.ts +32 -0
- package/src/request/UsernamePasswordRequest.ts +36 -0
- package/src/utils/Constants.ts +103 -0
- package/src/utils/EncodingUtils.ts +50 -0
- package/src/utils/NetworkUtils.ts +49 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
INetworkModule,
|
|
8
|
+
NetworkRequestOptions,
|
|
9
|
+
NetworkResponse,
|
|
10
|
+
HttpStatus,
|
|
11
|
+
} from "@azure/msal-common";
|
|
12
|
+
import { HttpMethod, Constants, ProxyStatus } from "../utils/Constants.js";
|
|
13
|
+
import { NetworkUtils } from "../utils/NetworkUtils.js";
|
|
14
|
+
import http from "http";
|
|
15
|
+
import https from "https";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* This class implements the API for network requests.
|
|
19
|
+
*/
|
|
20
|
+
export class HttpClient implements INetworkModule {
|
|
21
|
+
private proxyUrl: string;
|
|
22
|
+
private customAgentOptions: http.AgentOptions | https.AgentOptions;
|
|
23
|
+
|
|
24
|
+
constructor(
|
|
25
|
+
proxyUrl?: string,
|
|
26
|
+
customAgentOptions?: http.AgentOptions | https.AgentOptions
|
|
27
|
+
) {
|
|
28
|
+
this.proxyUrl = proxyUrl || "";
|
|
29
|
+
this.customAgentOptions = customAgentOptions || {};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Http Get request
|
|
34
|
+
* @param url
|
|
35
|
+
* @param options
|
|
36
|
+
*/
|
|
37
|
+
async sendGetRequestAsync<T>(
|
|
38
|
+
url: string,
|
|
39
|
+
options?: NetworkRequestOptions
|
|
40
|
+
): Promise<NetworkResponse<T>> {
|
|
41
|
+
if (this.proxyUrl) {
|
|
42
|
+
return networkRequestViaProxy(
|
|
43
|
+
url,
|
|
44
|
+
this.proxyUrl,
|
|
45
|
+
HttpMethod.GET,
|
|
46
|
+
options,
|
|
47
|
+
this.customAgentOptions as http.AgentOptions
|
|
48
|
+
);
|
|
49
|
+
} else {
|
|
50
|
+
return networkRequestViaHttps(
|
|
51
|
+
url,
|
|
52
|
+
HttpMethod.GET,
|
|
53
|
+
options,
|
|
54
|
+
this.customAgentOptions as https.AgentOptions
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Http Post request
|
|
61
|
+
* @param url
|
|
62
|
+
* @param options
|
|
63
|
+
*/
|
|
64
|
+
async sendPostRequestAsync<T>(
|
|
65
|
+
url: string,
|
|
66
|
+
options?: NetworkRequestOptions,
|
|
67
|
+
cancellationToken?: number
|
|
68
|
+
): Promise<NetworkResponse<T>> {
|
|
69
|
+
if (this.proxyUrl) {
|
|
70
|
+
return networkRequestViaProxy(
|
|
71
|
+
url,
|
|
72
|
+
this.proxyUrl,
|
|
73
|
+
HttpMethod.POST,
|
|
74
|
+
options,
|
|
75
|
+
this.customAgentOptions as http.AgentOptions,
|
|
76
|
+
cancellationToken
|
|
77
|
+
);
|
|
78
|
+
} else {
|
|
79
|
+
return networkRequestViaHttps(
|
|
80
|
+
url,
|
|
81
|
+
HttpMethod.POST,
|
|
82
|
+
options,
|
|
83
|
+
this.customAgentOptions as https.AgentOptions,
|
|
84
|
+
cancellationToken
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const networkRequestViaProxy = <T>(
|
|
91
|
+
destinationUrlString: string,
|
|
92
|
+
proxyUrlString: string,
|
|
93
|
+
httpMethod: string,
|
|
94
|
+
options?: NetworkRequestOptions,
|
|
95
|
+
agentOptions?: http.AgentOptions,
|
|
96
|
+
timeout?: number
|
|
97
|
+
): Promise<NetworkResponse<T>> => {
|
|
98
|
+
const destinationUrl = new URL(destinationUrlString);
|
|
99
|
+
const proxyUrl = new URL(proxyUrlString);
|
|
100
|
+
|
|
101
|
+
// "method: connect" must be used to establish a connection to the proxy
|
|
102
|
+
const headers = options?.headers || ({} as Record<string, string>);
|
|
103
|
+
const tunnelRequestOptions: https.RequestOptions = {
|
|
104
|
+
host: proxyUrl.hostname,
|
|
105
|
+
port: proxyUrl.port,
|
|
106
|
+
method: "CONNECT",
|
|
107
|
+
path: destinationUrl.hostname,
|
|
108
|
+
headers: headers,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
if (timeout) {
|
|
112
|
+
tunnelRequestOptions.timeout = timeout;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (agentOptions && Object.keys(agentOptions).length) {
|
|
116
|
+
tunnelRequestOptions.agent = new http.Agent(agentOptions);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// compose a request string for the socket
|
|
120
|
+
let postRequestStringContent: string = "";
|
|
121
|
+
if (httpMethod === HttpMethod.POST) {
|
|
122
|
+
const body = options?.body || "";
|
|
123
|
+
postRequestStringContent =
|
|
124
|
+
"Content-Type: application/x-www-form-urlencoded\r\n" +
|
|
125
|
+
`Content-Length: ${body.length}\r\n` +
|
|
126
|
+
`\r\n${body}`;
|
|
127
|
+
}
|
|
128
|
+
const outgoingRequestString =
|
|
129
|
+
`${httpMethod.toUpperCase()} ${destinationUrl.href} HTTP/1.1\r\n` +
|
|
130
|
+
`Host: ${destinationUrl.host}\r\n` +
|
|
131
|
+
"Connection: close\r\n" +
|
|
132
|
+
postRequestStringContent +
|
|
133
|
+
"\r\n";
|
|
134
|
+
|
|
135
|
+
return new Promise<NetworkResponse<T>>((resolve, reject) => {
|
|
136
|
+
const request = http.request(tunnelRequestOptions);
|
|
137
|
+
|
|
138
|
+
if (tunnelRequestOptions.timeout) {
|
|
139
|
+
request.on("timeout", () => {
|
|
140
|
+
request.destroy();
|
|
141
|
+
reject(new Error("Request time out"));
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
request.end();
|
|
146
|
+
|
|
147
|
+
// establish connection to the proxy
|
|
148
|
+
request.on("connect", (response, socket) => {
|
|
149
|
+
const proxyStatusCode =
|
|
150
|
+
response?.statusCode || ProxyStatus.SERVER_ERROR;
|
|
151
|
+
if (
|
|
152
|
+
proxyStatusCode < ProxyStatus.SUCCESS_RANGE_START ||
|
|
153
|
+
proxyStatusCode > ProxyStatus.SUCCESS_RANGE_END
|
|
154
|
+
) {
|
|
155
|
+
request.destroy();
|
|
156
|
+
socket.destroy();
|
|
157
|
+
reject(
|
|
158
|
+
new Error(
|
|
159
|
+
`Error connecting to proxy. Http status code: ${
|
|
160
|
+
response.statusCode
|
|
161
|
+
}. Http status message: ${
|
|
162
|
+
response?.statusMessage || "Unknown"
|
|
163
|
+
}`
|
|
164
|
+
)
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
if (tunnelRequestOptions.timeout) {
|
|
168
|
+
socket.setTimeout(tunnelRequestOptions.timeout);
|
|
169
|
+
socket.on("timeout", () => {
|
|
170
|
+
request.destroy();
|
|
171
|
+
socket.destroy();
|
|
172
|
+
reject(new Error("Request time out"));
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// make a request over an HTTP tunnel
|
|
177
|
+
socket.write(outgoingRequestString);
|
|
178
|
+
|
|
179
|
+
const data: Buffer[] = [];
|
|
180
|
+
socket.on("data", (chunk) => {
|
|
181
|
+
data.push(chunk);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
socket.on("end", () => {
|
|
185
|
+
// combine all received buffer streams into one buffer, and then into a string
|
|
186
|
+
const dataString = Buffer.concat([...data]).toString();
|
|
187
|
+
|
|
188
|
+
// separate each line into it's own entry in an arry
|
|
189
|
+
const dataStringArray = dataString.split("\r\n");
|
|
190
|
+
// the first entry will contain the statusCode and statusMessage
|
|
191
|
+
const httpStatusCode = parseInt(
|
|
192
|
+
dataStringArray[0].split(" ")[1]
|
|
193
|
+
);
|
|
194
|
+
// remove "HTTP/1.1" and the status code to get the status message
|
|
195
|
+
const statusMessage = dataStringArray[0]
|
|
196
|
+
.split(" ")
|
|
197
|
+
.slice(2)
|
|
198
|
+
.join(" ");
|
|
199
|
+
// the last entry will contain the body
|
|
200
|
+
const body = dataStringArray[dataStringArray.length - 1];
|
|
201
|
+
|
|
202
|
+
// everything in between the first and last entries are the headers
|
|
203
|
+
const headersArray = dataStringArray.slice(
|
|
204
|
+
1,
|
|
205
|
+
dataStringArray.length - 2
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
// build an object out of all the headers
|
|
209
|
+
const entries = new Map();
|
|
210
|
+
headersArray.forEach((header) => {
|
|
211
|
+
/**
|
|
212
|
+
* the header might look like "Content-Length: 1531", but that is just a string
|
|
213
|
+
* it needs to be converted to a key/value pair
|
|
214
|
+
* split the string at the first instance of ":"
|
|
215
|
+
* there may be more than one ":" if the value of the header is supposed to be a JSON object
|
|
216
|
+
*/
|
|
217
|
+
const headerKeyValue = header.split(new RegExp(/:\s(.*)/s));
|
|
218
|
+
const headerKey = headerKeyValue[0];
|
|
219
|
+
let headerValue = headerKeyValue[1];
|
|
220
|
+
|
|
221
|
+
// check if the value of the header is supposed to be a JSON object
|
|
222
|
+
try {
|
|
223
|
+
const object = JSON.parse(headerValue);
|
|
224
|
+
|
|
225
|
+
// if it is, then convert it from a string to a JSON object
|
|
226
|
+
if (object && typeof object === "object") {
|
|
227
|
+
headerValue = object;
|
|
228
|
+
}
|
|
229
|
+
} catch (e) {
|
|
230
|
+
// otherwise, leave it as a string
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
entries.set(headerKey, headerValue);
|
|
234
|
+
});
|
|
235
|
+
const headers = Object.fromEntries(entries);
|
|
236
|
+
|
|
237
|
+
const parsedHeaders = headers as Record<string, string>;
|
|
238
|
+
const networkResponse = NetworkUtils.getNetworkResponse(
|
|
239
|
+
parsedHeaders,
|
|
240
|
+
parseBody(
|
|
241
|
+
httpStatusCode,
|
|
242
|
+
statusMessage,
|
|
243
|
+
parsedHeaders,
|
|
244
|
+
body
|
|
245
|
+
) as T,
|
|
246
|
+
httpStatusCode
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
if (
|
|
250
|
+
(httpStatusCode < HttpStatus.SUCCESS_RANGE_START ||
|
|
251
|
+
httpStatusCode > HttpStatus.SUCCESS_RANGE_END) &&
|
|
252
|
+
// do not destroy the request for the device code flow
|
|
253
|
+
networkResponse.body["error"] !==
|
|
254
|
+
Constants.AUTHORIZATION_PENDING
|
|
255
|
+
) {
|
|
256
|
+
request.destroy();
|
|
257
|
+
}
|
|
258
|
+
resolve(networkResponse);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
socket.on("error", (chunk) => {
|
|
262
|
+
request.destroy();
|
|
263
|
+
socket.destroy();
|
|
264
|
+
reject(new Error(chunk.toString()));
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
request.on("error", (chunk) => {
|
|
269
|
+
request.destroy();
|
|
270
|
+
reject(new Error(chunk.toString()));
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const networkRequestViaHttps = <T>(
|
|
276
|
+
urlString: string,
|
|
277
|
+
httpMethod: string,
|
|
278
|
+
options?: NetworkRequestOptions,
|
|
279
|
+
agentOptions?: https.AgentOptions,
|
|
280
|
+
timeout?: number
|
|
281
|
+
): Promise<NetworkResponse<T>> => {
|
|
282
|
+
const isPostRequest = httpMethod === HttpMethod.POST;
|
|
283
|
+
const body: string = options?.body || "";
|
|
284
|
+
|
|
285
|
+
const url = new URL(urlString);
|
|
286
|
+
const headers = options?.headers || ({} as Record<string, string>);
|
|
287
|
+
const customOptions: https.RequestOptions = {
|
|
288
|
+
method: httpMethod,
|
|
289
|
+
headers: headers,
|
|
290
|
+
...NetworkUtils.urlToHttpOptions(url),
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
if (timeout) {
|
|
294
|
+
customOptions.timeout = timeout;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (agentOptions && Object.keys(agentOptions).length) {
|
|
298
|
+
customOptions.agent = new https.Agent(agentOptions);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (isPostRequest) {
|
|
302
|
+
// needed for post request to work
|
|
303
|
+
customOptions.headers = {
|
|
304
|
+
...customOptions.headers,
|
|
305
|
+
"Content-Length": body.length,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return new Promise<NetworkResponse<T>>((resolve, reject) => {
|
|
310
|
+
const request = https.request(customOptions);
|
|
311
|
+
|
|
312
|
+
if (timeout) {
|
|
313
|
+
request.on("timeout", () => {
|
|
314
|
+
request.destroy();
|
|
315
|
+
reject(new Error("Request time out"));
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (isPostRequest) {
|
|
320
|
+
request.write(body);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
request.end();
|
|
324
|
+
|
|
325
|
+
request.on("response", (response) => {
|
|
326
|
+
const headers = response.headers;
|
|
327
|
+
const statusCode = response.statusCode as number;
|
|
328
|
+
const statusMessage = response.statusMessage;
|
|
329
|
+
|
|
330
|
+
const data: Buffer[] = [];
|
|
331
|
+
response.on("data", (chunk) => {
|
|
332
|
+
data.push(chunk);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
response.on("end", () => {
|
|
336
|
+
// combine all received buffer streams into one buffer, and then into a string
|
|
337
|
+
const body = Buffer.concat([...data]).toString();
|
|
338
|
+
|
|
339
|
+
const parsedHeaders = headers as Record<string, string>;
|
|
340
|
+
const networkResponse = NetworkUtils.getNetworkResponse(
|
|
341
|
+
parsedHeaders,
|
|
342
|
+
parseBody(
|
|
343
|
+
statusCode,
|
|
344
|
+
statusMessage,
|
|
345
|
+
parsedHeaders,
|
|
346
|
+
body
|
|
347
|
+
) as T,
|
|
348
|
+
statusCode
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
if (
|
|
352
|
+
(statusCode < HttpStatus.SUCCESS_RANGE_START ||
|
|
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);
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
request.on("error", (chunk) => {
|
|
365
|
+
request.destroy();
|
|
366
|
+
reject(new Error(chunk.toString()));
|
|
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)
|
|
391
|
+
*/
|
|
392
|
+
|
|
393
|
+
let parsedBody;
|
|
394
|
+
try {
|
|
395
|
+
parsedBody = JSON.parse(body);
|
|
396
|
+
} catch (error) {
|
|
397
|
+
let errorType;
|
|
398
|
+
let errorDescriptionHelper;
|
|
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";
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
parsedBody = {
|
|
417
|
+
error: errorType,
|
|
418
|
+
error_description: `${errorDescriptionHelper} error occured.\nHttp status code: ${statusCode}\nHttp status message: ${
|
|
419
|
+
statusMessage || "Unknown"
|
|
420
|
+
}\nHeaders: ${JSON.stringify(headers)}`,
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
return parsedBody;
|
|
425
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { ServerAuthorizationCodeResponse } from "@azure/msal-common";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Interface for LoopbackClient allowing to replace the default loopback server with a custom implementation.
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
export interface ILoopbackClient {
|
|
13
|
+
listenForAuthCode(
|
|
14
|
+
successTemplate?: string,
|
|
15
|
+
errorTemplate?: string
|
|
16
|
+
): Promise<ServerAuthorizationCodeResponse>;
|
|
17
|
+
getRedirectUri(): string;
|
|
18
|
+
closeServer(): void;
|
|
19
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
Constants as CommonConstants,
|
|
8
|
+
ServerAuthorizationCodeResponse,
|
|
9
|
+
UrlString,
|
|
10
|
+
HttpStatus,
|
|
11
|
+
} from "@azure/msal-common";
|
|
12
|
+
import http from "http";
|
|
13
|
+
import { NodeAuthError } from "../error/NodeAuthError.js";
|
|
14
|
+
import { Constants, LOOPBACK_SERVER_CONSTANTS } from "../utils/Constants.js";
|
|
15
|
+
import { ILoopbackClient } from "./ILoopbackClient.js";
|
|
16
|
+
|
|
17
|
+
export class LoopbackClient implements ILoopbackClient {
|
|
18
|
+
private server: http.Server;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Spins up a loopback server which returns the server response when the localhost redirectUri is hit
|
|
22
|
+
* @param successTemplate
|
|
23
|
+
* @param errorTemplate
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
async listenForAuthCode(
|
|
27
|
+
successTemplate?: string,
|
|
28
|
+
errorTemplate?: string
|
|
29
|
+
): Promise<ServerAuthorizationCodeResponse> {
|
|
30
|
+
if (!!this.server) {
|
|
31
|
+
throw NodeAuthError.createLoopbackServerAlreadyExistsError();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const authCodeListener = new Promise<ServerAuthorizationCodeResponse>(
|
|
35
|
+
(resolve, reject) => {
|
|
36
|
+
this.server = http.createServer(
|
|
37
|
+
async (
|
|
38
|
+
req: http.IncomingMessage,
|
|
39
|
+
res: http.ServerResponse
|
|
40
|
+
) => {
|
|
41
|
+
const url = req.url;
|
|
42
|
+
if (!url) {
|
|
43
|
+
res.end(
|
|
44
|
+
errorTemplate ||
|
|
45
|
+
"Error occurred loading redirectUrl"
|
|
46
|
+
);
|
|
47
|
+
reject(
|
|
48
|
+
NodeAuthError.createUnableToLoadRedirectUrlError()
|
|
49
|
+
);
|
|
50
|
+
return;
|
|
51
|
+
} else if (url === CommonConstants.FORWARD_SLASH) {
|
|
52
|
+
res.end(
|
|
53
|
+
successTemplate ||
|
|
54
|
+
"Auth code was successfully acquired. You can close this window now."
|
|
55
|
+
);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const authCodeResponse =
|
|
60
|
+
UrlString.getDeserializedQueryString(url);
|
|
61
|
+
if (authCodeResponse.code) {
|
|
62
|
+
const redirectUri = await this.getRedirectUri();
|
|
63
|
+
res.writeHead(HttpStatus.REDIRECT, {
|
|
64
|
+
location: redirectUri,
|
|
65
|
+
}); // Prevent auth code from being saved in the browser history
|
|
66
|
+
res.end();
|
|
67
|
+
}
|
|
68
|
+
resolve(authCodeResponse);
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
this.server.listen(0); // Listen on any available port
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
// Wait for server to be listening
|
|
76
|
+
await new Promise<void>((resolve) => {
|
|
77
|
+
let ticks = 0;
|
|
78
|
+
const id = setInterval(() => {
|
|
79
|
+
if (
|
|
80
|
+
LOOPBACK_SERVER_CONSTANTS.TIMEOUT_MS /
|
|
81
|
+
LOOPBACK_SERVER_CONSTANTS.INTERVAL_MS <
|
|
82
|
+
ticks
|
|
83
|
+
) {
|
|
84
|
+
throw NodeAuthError.createLoopbackServerTimeoutError();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (this.server.listening) {
|
|
88
|
+
clearInterval(id);
|
|
89
|
+
resolve();
|
|
90
|
+
}
|
|
91
|
+
ticks++;
|
|
92
|
+
}, LOOPBACK_SERVER_CONSTANTS.INTERVAL_MS);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
return authCodeListener;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Get the port that the loopback server is running on
|
|
100
|
+
* @returns
|
|
101
|
+
*/
|
|
102
|
+
getRedirectUri(): string {
|
|
103
|
+
if (!this.server) {
|
|
104
|
+
throw NodeAuthError.createNoLoopbackServerExistsError();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const address = this.server.address();
|
|
108
|
+
if (!address || typeof address === "string" || !address.port) {
|
|
109
|
+
this.closeServer();
|
|
110
|
+
throw NodeAuthError.createInvalidLoopbackAddressTypeError();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const port = address && address.port;
|
|
114
|
+
|
|
115
|
+
return `${Constants.HTTP_PROTOCOL}${Constants.LOCALHOST}:${port}`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Close the loopback server
|
|
120
|
+
*/
|
|
121
|
+
closeServer(): void {
|
|
122
|
+
if (!!this.server) {
|
|
123
|
+
this.server.close();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { CommonAuthorizationCodeRequest } from "@azure/msal-common";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Request object passed by user to acquire a token from the server exchanging a valid authorization code (second leg of OAuth2.0 Authorization Code flow)
|
|
10
|
+
*
|
|
11
|
+
* - scopes - Array of scopes the application is requesting access to.
|
|
12
|
+
* - claims - A stringified claims request which will be added to all /authorize and /token calls
|
|
13
|
+
* - authority: - URL of the authority, the security token service (STS) from which MSAL will acquire tokens. If authority is set on client application object, this will override that value. Overriding the value will cause for authority validation to happen each time. If the same authority will be used for all request, set on the application object instead of the requests.
|
|
14
|
+
* - correlationId - Unique GUID set per request to trace a request end-to-end for telemetry purposes.
|
|
15
|
+
* - redirectUri - The redirect URI of your app, where the authority will redirect to after the user inputs credentials and consents. It must exactly match one of the redirect URIs you registered in the portal.
|
|
16
|
+
* - tokenQueryParameters - String to string map of custom query parameters added to the /token call
|
|
17
|
+
* - code - The authorization_code that the user acquired in the first leg of the flow.
|
|
18
|
+
* - codeVerifier - The same code_verifier that was used to obtain the authorization_code. Required if PKCE was used in the authorization code grant request.For more information, see the PKCE RFC: https://tools.ietf.org/html/rfc7636
|
|
19
|
+
* - state - Unique GUID generated by the user that is cached by the user and sent to the server during the first leg of the flow. This string is sent back by the server with the authorization code. The user cached state is then compared with the state received from the server to mitigate the risk of CSRF attacks. See https://datatracker.ietf.org/doc/html/rfc6819#section-3.6.
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export type AuthorizationCodeRequest = Partial<
|
|
23
|
+
Omit<
|
|
24
|
+
CommonAuthorizationCodeRequest,
|
|
25
|
+
| "scopes"
|
|
26
|
+
| "redirectUri"
|
|
27
|
+
| "code"
|
|
28
|
+
| "authenticationScheme"
|
|
29
|
+
| "resourceRequestMethod"
|
|
30
|
+
| "resourceRequestUri"
|
|
31
|
+
| "requestedClaimsHash"
|
|
32
|
+
| "storeInCache"
|
|
33
|
+
>
|
|
34
|
+
> & {
|
|
35
|
+
scopes: Array<string>;
|
|
36
|
+
redirectUri: string;
|
|
37
|
+
code: string;
|
|
38
|
+
state?: string;
|
|
39
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { CommonAuthorizationUrlRequest } from "@azure/msal-common";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Request object passed by user to retrieve a Code from the server (first leg of authorization code grant flow)
|
|
10
|
+
*
|
|
11
|
+
* - scopes - Array of scopes the application is requesting access to.
|
|
12
|
+
* - claims - A stringified claims request which will be added to all /authorize and /token calls
|
|
13
|
+
* - authority - Url of the authority which the application acquires tokens from.
|
|
14
|
+
* - correlationId - Unique GUID set per request to trace a request end-to-end for telemetry purposes.
|
|
15
|
+
* - redirectUri - The redirect URI where authentication responses can be received by your application. It must exactly match one of the redirect URIs registered in the Azure portal.
|
|
16
|
+
* - extraScopesToConsent - Scopes for a different resource when the user needs consent upfront.
|
|
17
|
+
* - responseMode - Specifies the method that should be used to send the authentication result to your app. Can be query, form_post, or fragment. If no value is passed in, it defaults to query.
|
|
18
|
+
* - codeChallenge - Used to secure authorization code grant via Proof of Key for Code Exchange (PKCE). For more information, see the PKCE RCF:https://tools.ietf.org/html/rfc7636
|
|
19
|
+
* - codeChallengeMethod - The method used to encode the code verifier for the code challenge parameter. Can be "plain" or "S256". If excluded, code challenge is assumed to be plaintext. For more information, see the PKCE RCF: https://tools.ietf.org/html/rfc7636
|
|
20
|
+
* - state - A value included in the request that is also returned in the token response. A randomly generated unique value is typically used for preventing cross site request forgery attacks. The state is also used to encode information about the user's state in the app before the authentication request occurred.
|
|
21
|
+
* - prompt - Indicates the type of user interaction that is required.
|
|
22
|
+
* login: will force the user to enter their credentials on that request, negating single-sign on
|
|
23
|
+
* none: will ensure that the user isn't presented with any interactive prompt. if request can't be completed via single-sign on, the endpoint will return an interaction_required error
|
|
24
|
+
* consent: will the trigger the OAuth consent dialog after the user signs in, asking the user to grant permissions to the app
|
|
25
|
+
* select_account: will interrupt single sign-=on providing account selection experience listing all the accounts in session or any remembered accounts or an option to choose to use a different account
|
|
26
|
+
* create: will direct the user to the account creation experience instead of the log in experience
|
|
27
|
+
* - account - AccountInfo obtained from a getAccount API. Will be used in certain scenarios to generate login_hint if both loginHint and sid params are not provided.
|
|
28
|
+
* - loginHint - Can be used to pre-fill the username/email address field of the sign-in page for the user, if you know the username/email address ahead of time. Often apps use this parameter during re-authentication, having already extracted the username from a previous sign-in using the preferred_username claim.
|
|
29
|
+
* - sid - Session ID, unique identifier for the session. Available as an optional claim on ID tokens.
|
|
30
|
+
* - domainHint - Provides a hint about the tenant or domain that the user should use to sign in. The value of the domain hint is a registered domain for the tenant.
|
|
31
|
+
* - extraQueryParameters - String to string map of custom query parameters added to the /authorize call
|
|
32
|
+
* - tokenQueryParameters - String to string map of custom query parameters added to the /token call
|
|
33
|
+
* - nonce - A value included in the request that is returned in the id token. A randomly generated unique value is typically used to mitigate replay attacks.
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export type AuthorizationUrlRequest = Partial<
|
|
37
|
+
Omit<
|
|
38
|
+
CommonAuthorizationUrlRequest,
|
|
39
|
+
| "scopes"
|
|
40
|
+
| "redirectUri"
|
|
41
|
+
| "resourceRequestMethod"
|
|
42
|
+
| "resourceRequestUri"
|
|
43
|
+
| "authenticationScheme"
|
|
44
|
+
| "requestedClaimsHash"
|
|
45
|
+
| "storeInCache"
|
|
46
|
+
>
|
|
47
|
+
> & {
|
|
48
|
+
scopes: Array<string>;
|
|
49
|
+
redirectUri: string;
|
|
50
|
+
};
|