@azure/msal-node 1.12.0 → 1.14.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/dist/client/ClientApplication.d.ts +13 -2
- package/dist/client/ClientApplication.d.ts.map +1 -1
- package/dist/client/IConfidentialClientApplication.d.ts +1 -1
- package/dist/client/IConfidentialClientApplication.d.ts.map +1 -1
- package/dist/client/IPublicClientApplication.d.ts +4 -1
- package/dist/client/IPublicClientApplication.d.ts.map +1 -1
- package/dist/client/PublicClientApplication.d.ts +5 -0
- package/dist/client/PublicClientApplication.d.ts.map +1 -1
- package/dist/error/NodeAuthError.d.ts +66 -0
- package/dist/error/NodeAuthError.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/msal-node.cjs.development.js +295 -5
- package/dist/msal-node.cjs.development.js.map +1 -1
- package/dist/msal-node.cjs.production.min.js +1 -1
- package/dist/msal-node.cjs.production.min.js.map +1 -1
- package/dist/msal-node.esm.js +294 -5
- package/dist/msal-node.esm.js.map +1 -1
- package/dist/network/LoopbackClient.d.ts +21 -0
- package/dist/network/LoopbackClient.d.ts.map +1 -0
- package/dist/packageMetadata.d.ts +1 -1
- package/dist/request/AuthorizationCodeRequest.d.ts +2 -0
- package/dist/request/AuthorizationCodeRequest.d.ts.map +1 -1
- package/dist/request/InteractiveRequest.d.ts +8 -0
- package/dist/request/InteractiveRequest.d.ts.map +1 -0
- package/dist/utils/Constants.d.ts +10 -0
- package/dist/utils/Constants.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/msal-node.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Constants as Constants$1, ProtocolMode, AzureCloudInstance, LogLevel, StringUtils, AccountEntity, CacheManager, IdTokenEntity, AccessTokenEntity, RefreshTokenEntity, AppMetadataEntity, ServerTelemetryEntity, AuthorityMetadataEntity, ThrottlingEntity, TokenCacheContext, Logger, ResponseMode, AuthenticationScheme, AuthorizationCodeClient,
|
|
1
|
+
import { Constants as Constants$1, ProtocolMode, AzureCloudInstance, LogLevel, StringUtils, AccountEntity, CacheManager, IdTokenEntity, AccessTokenEntity, RefreshTokenEntity, AppMetadataEntity, ServerTelemetryEntity, AuthorityMetadataEntity, ThrottlingEntity, TokenCacheContext, AuthError, Logger, ResponseMode, AuthenticationScheme, AuthorizationCodeClient, RefreshTokenClient, SilentFlowClient, UsernamePasswordClient, ClientAuthError, OIDC_DEFAULT_SCOPES, ServerTelemetryManager, Authority, AuthorityFactory, UrlString, DeviceCodeClient, ServerError, CodeChallengeMethodValues, TimeUtils, ClientCredentialClient, OnBehalfOfClient } from '@azure/msal-common';
|
|
2
2
|
export { AuthError, AuthErrorMessage, AzureCloudInstance, ClientAuthError, ClientAuthErrorMessage, ClientConfigurationError, ClientConfigurationErrorMessage, InteractionRequiredAuthError, InteractionRequiredAuthErrorMessage, LogLevel, Logger, PromptValue, ProtocolMode, ResponseMode, ServerError, TokenCacheContext } from '@azure/msal-common';
|
|
3
|
-
import http from 'http';
|
|
3
|
+
import http, { createServer } from 'http';
|
|
4
4
|
import https from 'https';
|
|
5
5
|
import { v4 } from 'uuid';
|
|
6
6
|
import crypto from 'crypto';
|
|
@@ -20,6 +20,13 @@ var HttpMethod;
|
|
|
20
20
|
HttpMethod["GET"] = "get";
|
|
21
21
|
HttpMethod["POST"] = "post";
|
|
22
22
|
})(HttpMethod || (HttpMethod = {}));
|
|
23
|
+
|
|
24
|
+
var HttpStatus;
|
|
25
|
+
|
|
26
|
+
(function (HttpStatus) {
|
|
27
|
+
HttpStatus[HttpStatus["OK"] = 200] = "OK";
|
|
28
|
+
HttpStatus[HttpStatus["REDIRECT"] = 302] = "REDIRECT";
|
|
29
|
+
})(HttpStatus || (HttpStatus = {}));
|
|
23
30
|
/**
|
|
24
31
|
* Constants used for region discovery
|
|
25
32
|
*/
|
|
@@ -52,7 +59,9 @@ const CharSet = {
|
|
|
52
59
|
const Constants = {
|
|
53
60
|
MSAL_SKU: "msal.js.node",
|
|
54
61
|
JWT_BEARER_ASSERTION_TYPE: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
|
|
55
|
-
AUTHORIZATION_PENDING: "authorization_pending"
|
|
62
|
+
AUTHORIZATION_PENDING: "authorization_pending",
|
|
63
|
+
HTTP_PROTOCOL: "http://",
|
|
64
|
+
LOCALHOST: "localhost"
|
|
56
65
|
};
|
|
57
66
|
/**
|
|
58
67
|
* API Codes for Telemetry purposes.
|
|
@@ -89,6 +98,10 @@ const JwtConstants = {
|
|
|
89
98
|
NOT_BEFORE: "nbf",
|
|
90
99
|
JWT_ID: "jti"
|
|
91
100
|
};
|
|
101
|
+
const LOOPBACK_SERVER_CONSTANTS = {
|
|
102
|
+
INTERVAL_MS: 100,
|
|
103
|
+
TIMEOUT_MS: 5000
|
|
104
|
+
};
|
|
92
105
|
|
|
93
106
|
/*
|
|
94
107
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
@@ -1753,7 +1766,109 @@ class TokenCache {
|
|
|
1753
1766
|
|
|
1754
1767
|
/* eslint-disable header/header */
|
|
1755
1768
|
const name = "@azure/msal-node";
|
|
1756
|
-
const version = "1.
|
|
1769
|
+
const version = "1.14.0";
|
|
1770
|
+
|
|
1771
|
+
/*
|
|
1772
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
1773
|
+
* Licensed under the MIT License.
|
|
1774
|
+
*/
|
|
1775
|
+
/**
|
|
1776
|
+
* NodeAuthErrorMessage class containing string constants used by error codes and messages.
|
|
1777
|
+
*/
|
|
1778
|
+
|
|
1779
|
+
const NodeAuthErrorMessage = {
|
|
1780
|
+
invalidLoopbackAddressType: {
|
|
1781
|
+
code: "invalid_loopback_server_address_type",
|
|
1782
|
+
desc: "Loopback server address is not type string. This is unexpected."
|
|
1783
|
+
},
|
|
1784
|
+
unableToLoadRedirectUri: {
|
|
1785
|
+
code: "unable_to_load_redirectUrl",
|
|
1786
|
+
desc: "Loopback server callback was invoked without a url. This is unexpected."
|
|
1787
|
+
},
|
|
1788
|
+
noAuthCodeInResponse: {
|
|
1789
|
+
code: "no_auth_code_in_response",
|
|
1790
|
+
desc: "No auth code found in the server response. Please check your network trace to determine what happened."
|
|
1791
|
+
},
|
|
1792
|
+
noLoopbackServerExists: {
|
|
1793
|
+
code: "no_loopback_server_exists",
|
|
1794
|
+
desc: "No loopback server exists yet."
|
|
1795
|
+
},
|
|
1796
|
+
loopbackServerAlreadyExists: {
|
|
1797
|
+
code: "loopback_server_already_exists",
|
|
1798
|
+
desc: "Loopback server already exists. Cannot create another."
|
|
1799
|
+
},
|
|
1800
|
+
loopbackServerTimeout: {
|
|
1801
|
+
code: "loopback_server_timeout",
|
|
1802
|
+
desc: "Timed out waiting for auth code listener to be registered."
|
|
1803
|
+
},
|
|
1804
|
+
stateNotFoundError: {
|
|
1805
|
+
code: "state_not_found",
|
|
1806
|
+
desc: "State not found. Please verify that the request originated from msal."
|
|
1807
|
+
}
|
|
1808
|
+
};
|
|
1809
|
+
class NodeAuthError extends AuthError {
|
|
1810
|
+
constructor(errorCode, errorMessage) {
|
|
1811
|
+
super(errorCode, errorMessage);
|
|
1812
|
+
this.name = "NodeAuthError";
|
|
1813
|
+
}
|
|
1814
|
+
/**
|
|
1815
|
+
* Creates an error thrown if loopback server address is of type string.
|
|
1816
|
+
*/
|
|
1817
|
+
|
|
1818
|
+
|
|
1819
|
+
static createInvalidLoopbackAddressTypeError() {
|
|
1820
|
+
return new NodeAuthError(NodeAuthErrorMessage.invalidLoopbackAddressType.code, `${NodeAuthErrorMessage.invalidLoopbackAddressType.desc}`);
|
|
1821
|
+
}
|
|
1822
|
+
/**
|
|
1823
|
+
* Creates an error thrown if the loopback server is unable to get a url.
|
|
1824
|
+
*/
|
|
1825
|
+
|
|
1826
|
+
|
|
1827
|
+
static createUnableToLoadRedirectUrlError() {
|
|
1828
|
+
return new NodeAuthError(NodeAuthErrorMessage.unableToLoadRedirectUri.code, `${NodeAuthErrorMessage.unableToLoadRedirectUri.desc}`);
|
|
1829
|
+
}
|
|
1830
|
+
/**
|
|
1831
|
+
* Creates an error thrown if the server response does not contain an auth code.
|
|
1832
|
+
*/
|
|
1833
|
+
|
|
1834
|
+
|
|
1835
|
+
static createNoAuthCodeInResponseError() {
|
|
1836
|
+
return new NodeAuthError(NodeAuthErrorMessage.noAuthCodeInResponse.code, `${NodeAuthErrorMessage.noAuthCodeInResponse.desc}`);
|
|
1837
|
+
}
|
|
1838
|
+
/**
|
|
1839
|
+
* Creates an error thrown if the loopback server has not been spun up yet.
|
|
1840
|
+
*/
|
|
1841
|
+
|
|
1842
|
+
|
|
1843
|
+
static createNoLoopbackServerExistsError() {
|
|
1844
|
+
return new NodeAuthError(NodeAuthErrorMessage.noLoopbackServerExists.code, `${NodeAuthErrorMessage.noLoopbackServerExists.desc}`);
|
|
1845
|
+
}
|
|
1846
|
+
/**
|
|
1847
|
+
* Creates an error thrown if a loopback server already exists when attempting to create another one.
|
|
1848
|
+
*/
|
|
1849
|
+
|
|
1850
|
+
|
|
1851
|
+
static createLoopbackServerAlreadyExistsError() {
|
|
1852
|
+
return new NodeAuthError(NodeAuthErrorMessage.loopbackServerAlreadyExists.code, `${NodeAuthErrorMessage.loopbackServerAlreadyExists.desc}`);
|
|
1853
|
+
}
|
|
1854
|
+
/**
|
|
1855
|
+
* Creates an error thrown if the loopback server times out registering the auth code listener.
|
|
1856
|
+
*/
|
|
1857
|
+
|
|
1858
|
+
|
|
1859
|
+
static createLoopbackServerTimeoutError() {
|
|
1860
|
+
return new NodeAuthError(NodeAuthErrorMessage.loopbackServerTimeout.code, `${NodeAuthErrorMessage.loopbackServerTimeout.desc}`);
|
|
1861
|
+
}
|
|
1862
|
+
/**
|
|
1863
|
+
* Creates an error thrown when the state is not present.
|
|
1864
|
+
*/
|
|
1865
|
+
|
|
1866
|
+
|
|
1867
|
+
static createStateNotFoundError() {
|
|
1868
|
+
return new NodeAuthError(NodeAuthErrorMessage.stateNotFoundError.code, NodeAuthErrorMessage.stateNotFoundError.desc);
|
|
1869
|
+
}
|
|
1870
|
+
|
|
1871
|
+
}
|
|
1757
1872
|
|
|
1758
1873
|
/*
|
|
1759
1874
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
@@ -1809,7 +1924,17 @@ class ClientApplication {
|
|
|
1809
1924
|
|
|
1810
1925
|
|
|
1811
1926
|
async acquireTokenByCode(request, authCodePayLoad) {
|
|
1812
|
-
this.logger.info("acquireTokenByCode called"
|
|
1927
|
+
this.logger.info("acquireTokenByCode called");
|
|
1928
|
+
|
|
1929
|
+
if (request.state && authCodePayLoad) {
|
|
1930
|
+
this.logger.info("acquireTokenByCode - validating state");
|
|
1931
|
+
this.validateState(request.state, authCodePayLoad.state || ""); // eslint-disable-next-line no-param-reassign
|
|
1932
|
+
|
|
1933
|
+
authCodePayLoad = { ...authCodePayLoad,
|
|
1934
|
+
state: ""
|
|
1935
|
+
};
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1813
1938
|
const validRequest = { ...request,
|
|
1814
1939
|
...(await this.initializeBaseRequest(request)),
|
|
1815
1940
|
authenticationScheme: AuthenticationScheme.BEARER
|
|
@@ -1934,6 +2059,26 @@ class ClientApplication {
|
|
|
1934
2059
|
this.logger.info("getTokenCache called");
|
|
1935
2060
|
return this.tokenCache;
|
|
1936
2061
|
}
|
|
2062
|
+
/**
|
|
2063
|
+
* Validates OIDC state by comparing the user cached state with the state received from the server.
|
|
2064
|
+
*
|
|
2065
|
+
* This API is provided for scenarios where you would use OAuth2.0 state parameter to mitigate against
|
|
2066
|
+
* CSRF attacks.
|
|
2067
|
+
* For more information about state, visit https://datatracker.ietf.org/doc/html/rfc6819#section-3.6.
|
|
2068
|
+
* @param state
|
|
2069
|
+
* @param cachedState
|
|
2070
|
+
*/
|
|
2071
|
+
|
|
2072
|
+
|
|
2073
|
+
validateState(state, cachedState) {
|
|
2074
|
+
if (!state) {
|
|
2075
|
+
throw NodeAuthError.createStateNotFoundError();
|
|
2076
|
+
}
|
|
2077
|
+
|
|
2078
|
+
if (state !== cachedState) {
|
|
2079
|
+
throw ClientAuthError.createStateMismatchError();
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
1937
2082
|
/**
|
|
1938
2083
|
* Returns the logger instance
|
|
1939
2084
|
*/
|
|
@@ -2082,6 +2227,102 @@ class ClientApplication {
|
|
|
2082
2227
|
|
|
2083
2228
|
}
|
|
2084
2229
|
|
|
2230
|
+
/*
|
|
2231
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
2232
|
+
* Licensed under the MIT License.
|
|
2233
|
+
*/
|
|
2234
|
+
class LoopbackClient {
|
|
2235
|
+
/**
|
|
2236
|
+
* Spins up a loopback server which returns the server response when the localhost redirectUri is hit
|
|
2237
|
+
* @param successTemplate
|
|
2238
|
+
* @param errorTemplate
|
|
2239
|
+
* @returns
|
|
2240
|
+
*/
|
|
2241
|
+
async listenForAuthCode(successTemplate, errorTemplate) {
|
|
2242
|
+
if (!!this.server) {
|
|
2243
|
+
throw NodeAuthError.createLoopbackServerAlreadyExistsError();
|
|
2244
|
+
}
|
|
2245
|
+
|
|
2246
|
+
const authCodeListener = new Promise((resolve, reject) => {
|
|
2247
|
+
this.server = createServer(async (req, res) => {
|
|
2248
|
+
const url = req.url;
|
|
2249
|
+
|
|
2250
|
+
if (!url) {
|
|
2251
|
+
res.end(errorTemplate || "Error occurred loading redirectUrl");
|
|
2252
|
+
reject(NodeAuthError.createUnableToLoadRedirectUrlError());
|
|
2253
|
+
return;
|
|
2254
|
+
} else if (url === Constants$1.FORWARD_SLASH) {
|
|
2255
|
+
res.end(successTemplate || "Auth code was successfully acquired. You can close this window now.");
|
|
2256
|
+
return;
|
|
2257
|
+
}
|
|
2258
|
+
|
|
2259
|
+
const authCodeResponse = UrlString.getDeserializedQueryString(url);
|
|
2260
|
+
|
|
2261
|
+
if (authCodeResponse.code) {
|
|
2262
|
+
const redirectUri = await this.getRedirectUri();
|
|
2263
|
+
res.writeHead(HttpStatus.REDIRECT, {
|
|
2264
|
+
location: redirectUri
|
|
2265
|
+
}); // Prevent auth code from being saved in the browser history
|
|
2266
|
+
|
|
2267
|
+
res.end();
|
|
2268
|
+
}
|
|
2269
|
+
|
|
2270
|
+
resolve(authCodeResponse);
|
|
2271
|
+
});
|
|
2272
|
+
this.server.listen(0); // Listen on any available port
|
|
2273
|
+
}); // Wait for server to be listening
|
|
2274
|
+
|
|
2275
|
+
await new Promise(resolve => {
|
|
2276
|
+
let ticks = 0;
|
|
2277
|
+
const id = setInterval(() => {
|
|
2278
|
+
if (LOOPBACK_SERVER_CONSTANTS.TIMEOUT_MS / LOOPBACK_SERVER_CONSTANTS.INTERVAL_MS < ticks) {
|
|
2279
|
+
throw NodeAuthError.createLoopbackServerTimeoutError();
|
|
2280
|
+
}
|
|
2281
|
+
|
|
2282
|
+
if (this.server.listening) {
|
|
2283
|
+
clearInterval(id);
|
|
2284
|
+
resolve();
|
|
2285
|
+
}
|
|
2286
|
+
|
|
2287
|
+
ticks++;
|
|
2288
|
+
}, LOOPBACK_SERVER_CONSTANTS.INTERVAL_MS);
|
|
2289
|
+
});
|
|
2290
|
+
return authCodeListener;
|
|
2291
|
+
}
|
|
2292
|
+
/**
|
|
2293
|
+
* Get the port that the loopback server is running on
|
|
2294
|
+
* @returns
|
|
2295
|
+
*/
|
|
2296
|
+
|
|
2297
|
+
|
|
2298
|
+
getRedirectUri() {
|
|
2299
|
+
if (!this.server) {
|
|
2300
|
+
throw NodeAuthError.createNoLoopbackServerExistsError();
|
|
2301
|
+
}
|
|
2302
|
+
|
|
2303
|
+
const address = this.server.address();
|
|
2304
|
+
|
|
2305
|
+
if (!address || typeof address === "string" || !address.port) {
|
|
2306
|
+
this.closeServer();
|
|
2307
|
+
throw NodeAuthError.createInvalidLoopbackAddressTypeError();
|
|
2308
|
+
}
|
|
2309
|
+
|
|
2310
|
+
const port = address && address.port;
|
|
2311
|
+
return `${Constants.HTTP_PROTOCOL}${Constants.LOCALHOST}:${port}`;
|
|
2312
|
+
}
|
|
2313
|
+
/**
|
|
2314
|
+
* Close the loopback server
|
|
2315
|
+
*/
|
|
2316
|
+
|
|
2317
|
+
|
|
2318
|
+
closeServer() {
|
|
2319
|
+
if (!!this.server) {
|
|
2320
|
+
this.server.close();
|
|
2321
|
+
}
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
}
|
|
2325
|
+
|
|
2085
2326
|
/*
|
|
2086
2327
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
2087
2328
|
* Licensed under the MIT License.
|
|
@@ -2143,6 +2384,54 @@ class PublicClientApplication extends ClientApplication {
|
|
|
2143
2384
|
throw e;
|
|
2144
2385
|
}
|
|
2145
2386
|
}
|
|
2387
|
+
/**
|
|
2388
|
+
* Acquires a token by requesting an Authorization code then exchanging it for a token.
|
|
2389
|
+
*/
|
|
2390
|
+
|
|
2391
|
+
|
|
2392
|
+
async acquireTokenInteractive(request) {
|
|
2393
|
+
const {
|
|
2394
|
+
verifier,
|
|
2395
|
+
challenge
|
|
2396
|
+
} = await this.cryptoProvider.generatePkceCodes();
|
|
2397
|
+
const {
|
|
2398
|
+
openBrowser,
|
|
2399
|
+
successTemplate,
|
|
2400
|
+
errorTemplate,
|
|
2401
|
+
...remainingProperties
|
|
2402
|
+
} = request;
|
|
2403
|
+
const loopbackClient = new LoopbackClient();
|
|
2404
|
+
const authCodeListener = loopbackClient.listenForAuthCode(successTemplate, errorTemplate);
|
|
2405
|
+
const redirectUri = loopbackClient.getRedirectUri();
|
|
2406
|
+
const validRequest = { ...remainingProperties,
|
|
2407
|
+
scopes: request.scopes || [],
|
|
2408
|
+
redirectUri: redirectUri,
|
|
2409
|
+
responseMode: ResponseMode.QUERY,
|
|
2410
|
+
codeChallenge: challenge,
|
|
2411
|
+
codeChallengeMethod: CodeChallengeMethodValues.S256
|
|
2412
|
+
};
|
|
2413
|
+
const authCodeUrl = await this.getAuthCodeUrl(validRequest);
|
|
2414
|
+
await openBrowser(authCodeUrl);
|
|
2415
|
+
const authCodeResponse = await authCodeListener.finally(() => {
|
|
2416
|
+
loopbackClient.closeServer();
|
|
2417
|
+
});
|
|
2418
|
+
|
|
2419
|
+
if (authCodeResponse.error) {
|
|
2420
|
+
throw new ServerError(authCodeResponse.error, authCodeResponse.error_description, authCodeResponse.suberror);
|
|
2421
|
+
} else if (!authCodeResponse.code) {
|
|
2422
|
+
throw NodeAuthError.createNoAuthCodeInResponseError();
|
|
2423
|
+
}
|
|
2424
|
+
|
|
2425
|
+
const clientInfo = authCodeResponse.client_info;
|
|
2426
|
+
const tokenRequest = {
|
|
2427
|
+
code: authCodeResponse.code,
|
|
2428
|
+
scopes: OIDC_DEFAULT_SCOPES,
|
|
2429
|
+
redirectUri: validRequest.redirectUri,
|
|
2430
|
+
codeVerifier: verifier,
|
|
2431
|
+
clientInfo: clientInfo || Constants$1.EMPTY_STRING
|
|
2432
|
+
};
|
|
2433
|
+
return this.acquireTokenByCode(tokenRequest);
|
|
2434
|
+
}
|
|
2146
2435
|
|
|
2147
2436
|
}
|
|
2148
2437
|
|