@azure/msal-node 1.12.1 → 1.13.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.
@@ -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, AuthError, RefreshTokenClient, SilentFlowClient, UsernamePasswordClient, OIDC_DEFAULT_SCOPES, ServerTelemetryManager, Authority, AuthorityFactory, DeviceCodeClient, ClientAuthError, TimeUtils, ClientCredentialClient, OnBehalfOfClient } from '@azure/msal-common';
1
+ import { Constants as Constants$1, ProtocolMode, AzureCloudInstance, LogLevel, StringUtils, AccountEntity, CacheManager, IdTokenEntity, AccessTokenEntity, RefreshTokenEntity, AppMetadataEntity, ServerTelemetryEntity, AuthorityMetadataEntity, ThrottlingEntity, TokenCacheContext, Logger, ResponseMode, AuthenticationScheme, AuthorizationCodeClient, AuthError, RefreshTokenClient, SilentFlowClient, UsernamePasswordClient, OIDC_DEFAULT_SCOPES, ServerTelemetryManager, Authority, AuthorityFactory, UrlString, DeviceCodeClient, ServerError, CodeChallengeMethodValues, ClientAuthError, 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,7 @@ class TokenCache {
1753
1766
 
1754
1767
  /* eslint-disable header/header */
1755
1768
  const name = "@azure/msal-node";
1756
- const version = "1.12.1";
1769
+ const version = "1.13.0";
1757
1770
 
1758
1771
  /*
1759
1772
  * Copyright (c) Microsoft Corporation. All rights reserved.
@@ -2082,6 +2095,192 @@ class ClientApplication {
2082
2095
 
2083
2096
  }
2084
2097
 
2098
+ /*
2099
+ * Copyright (c) Microsoft Corporation. All rights reserved.
2100
+ * Licensed under the MIT License.
2101
+ */
2102
+ /**
2103
+ * NodeAuthErrorMessage class containing string constants used by error codes and messages.
2104
+ */
2105
+
2106
+ const NodeAuthErrorMessage = {
2107
+ invalidLoopbackAddressType: {
2108
+ code: "invalid_loopback_server_address_type",
2109
+ desc: "Loopback server address is not type string. This is unexpected."
2110
+ },
2111
+ unableToLoadRedirectUri: {
2112
+ code: "unable_to_load_redirectUrl",
2113
+ desc: "Loopback server callback was invoked without a url. This is unexpected."
2114
+ },
2115
+ noAuthCodeInResponse: {
2116
+ code: "no_auth_code_in_response",
2117
+ desc: "No auth code found in the server response. Please check your network trace to determine what happened."
2118
+ },
2119
+ noLoopbackServerExists: {
2120
+ code: "no_loopback_server_exists",
2121
+ desc: "No loopback server exists yet."
2122
+ },
2123
+ loopbackServerAlreadyExists: {
2124
+ code: "loopback_server_already_exists",
2125
+ desc: "Loopback server already exists. Cannot create another."
2126
+ },
2127
+ loopbackServerTimeout: {
2128
+ code: "loopback_server_timeout",
2129
+ desc: "Timed out waiting for auth code listener to be registered."
2130
+ }
2131
+ };
2132
+ class NodeAuthError extends AuthError {
2133
+ constructor(errorCode, errorMessage) {
2134
+ super(errorCode, errorMessage);
2135
+ this.name = "NodeAuthError";
2136
+ }
2137
+ /**
2138
+ * Creates an error thrown if loopback server address is of type string.
2139
+ */
2140
+
2141
+
2142
+ static createInvalidLoopbackAddressTypeError() {
2143
+ return new NodeAuthError(NodeAuthErrorMessage.invalidLoopbackAddressType.code, `${NodeAuthErrorMessage.invalidLoopbackAddressType.desc}`);
2144
+ }
2145
+ /**
2146
+ * Creates an error thrown if the loopback server is unable to get a url.
2147
+ */
2148
+
2149
+
2150
+ static createUnableToLoadRedirectUrlError() {
2151
+ return new NodeAuthError(NodeAuthErrorMessage.unableToLoadRedirectUri.code, `${NodeAuthErrorMessage.unableToLoadRedirectUri.desc}`);
2152
+ }
2153
+ /**
2154
+ * Creates an error thrown if the server response does not contain an auth code.
2155
+ */
2156
+
2157
+
2158
+ static createNoAuthCodeInResponseError() {
2159
+ return new NodeAuthError(NodeAuthErrorMessage.noAuthCodeInResponse.code, `${NodeAuthErrorMessage.noAuthCodeInResponse.desc}`);
2160
+ }
2161
+ /**
2162
+ * Creates an error thrown if the loopback server has not been spun up yet.
2163
+ */
2164
+
2165
+
2166
+ static createNoLoopbackServerExistsError() {
2167
+ return new NodeAuthError(NodeAuthErrorMessage.noLoopbackServerExists.code, `${NodeAuthErrorMessage.noLoopbackServerExists.desc}`);
2168
+ }
2169
+ /**
2170
+ * Creates an error thrown if a loopback server already exists when attempting to create another one.
2171
+ */
2172
+
2173
+
2174
+ static createLoopbackServerAlreadyExistsError() {
2175
+ return new NodeAuthError(NodeAuthErrorMessage.loopbackServerAlreadyExists.code, `${NodeAuthErrorMessage.loopbackServerAlreadyExists.desc}`);
2176
+ }
2177
+ /**
2178
+ * Creates an error thrown if the loopback server times out registering the auth code listener.
2179
+ */
2180
+
2181
+
2182
+ static createLoopbackServerTimeoutError() {
2183
+ return new NodeAuthError(NodeAuthErrorMessage.loopbackServerTimeout.code, `${NodeAuthErrorMessage.loopbackServerTimeout.desc}`);
2184
+ }
2185
+
2186
+ }
2187
+
2188
+ /*
2189
+ * Copyright (c) Microsoft Corporation. All rights reserved.
2190
+ * Licensed under the MIT License.
2191
+ */
2192
+ class LoopbackClient {
2193
+ /**
2194
+ * Spins up a loopback server which returns the server response when the localhost redirectUri is hit
2195
+ * @param successTemplate
2196
+ * @param errorTemplate
2197
+ * @returns
2198
+ */
2199
+ async listenForAuthCode(successTemplate, errorTemplate) {
2200
+ if (!!this.server) {
2201
+ throw NodeAuthError.createLoopbackServerAlreadyExistsError();
2202
+ }
2203
+
2204
+ const authCodeListener = new Promise((resolve, reject) => {
2205
+ this.server = createServer(async (req, res) => {
2206
+ const url = req.url;
2207
+
2208
+ if (!url) {
2209
+ res.end(errorTemplate || "Error occurred loading redirectUrl");
2210
+ reject(NodeAuthError.createUnableToLoadRedirectUrlError());
2211
+ return;
2212
+ } else if (url === Constants$1.FORWARD_SLASH) {
2213
+ res.end(successTemplate || "Auth code was successfully acquired. You can close this window now.");
2214
+ return;
2215
+ }
2216
+
2217
+ const authCodeResponse = UrlString.getDeserializedQueryString(url);
2218
+
2219
+ if (authCodeResponse.code) {
2220
+ const redirectUri = await this.getRedirectUri();
2221
+ res.writeHead(HttpStatus.REDIRECT, {
2222
+ location: redirectUri
2223
+ }); // Prevent auth code from being saved in the browser history
2224
+
2225
+ res.end();
2226
+ }
2227
+
2228
+ resolve(authCodeResponse);
2229
+ });
2230
+ this.server.listen(0); // Listen on any available port
2231
+ }); // Wait for server to be listening
2232
+
2233
+ await new Promise(resolve => {
2234
+ let ticks = 0;
2235
+ const id = setInterval(() => {
2236
+ if (LOOPBACK_SERVER_CONSTANTS.TIMEOUT_MS / LOOPBACK_SERVER_CONSTANTS.INTERVAL_MS < ticks) {
2237
+ throw NodeAuthError.createLoopbackServerTimeoutError();
2238
+ }
2239
+
2240
+ if (this.server.listening) {
2241
+ clearInterval(id);
2242
+ resolve();
2243
+ }
2244
+
2245
+ ticks++;
2246
+ }, LOOPBACK_SERVER_CONSTANTS.INTERVAL_MS);
2247
+ });
2248
+ return authCodeListener;
2249
+ }
2250
+ /**
2251
+ * Get the port that the loopback server is running on
2252
+ * @returns
2253
+ */
2254
+
2255
+
2256
+ getRedirectUri() {
2257
+ if (!this.server) {
2258
+ throw NodeAuthError.createNoLoopbackServerExistsError();
2259
+ }
2260
+
2261
+ const address = this.server.address();
2262
+
2263
+ if (!address || typeof address === "string" || !address.port) {
2264
+ this.closeServer();
2265
+ throw NodeAuthError.createInvalidLoopbackAddressTypeError();
2266
+ }
2267
+
2268
+ const port = address && address.port;
2269
+ return `${Constants.HTTP_PROTOCOL}${Constants.LOCALHOST}:${port}`;
2270
+ }
2271
+ /**
2272
+ * Close the loopback server
2273
+ */
2274
+
2275
+
2276
+ closeServer() {
2277
+ if (!!this.server) {
2278
+ this.server.close();
2279
+ }
2280
+ }
2281
+
2282
+ }
2283
+
2085
2284
  /*
2086
2285
  * Copyright (c) Microsoft Corporation. All rights reserved.
2087
2286
  * Licensed under the MIT License.
@@ -2143,6 +2342,54 @@ class PublicClientApplication extends ClientApplication {
2143
2342
  throw e;
2144
2343
  }
2145
2344
  }
2345
+ /**
2346
+ * Acquires a token by requesting an Authorization code then exchanging it for a token.
2347
+ */
2348
+
2349
+
2350
+ async acquireTokenInteractive(request) {
2351
+ const {
2352
+ verifier,
2353
+ challenge
2354
+ } = await this.cryptoProvider.generatePkceCodes();
2355
+ const {
2356
+ openBrowser,
2357
+ successTemplate,
2358
+ errorTemplate,
2359
+ ...remainingProperties
2360
+ } = request;
2361
+ const loopbackClient = new LoopbackClient();
2362
+ const authCodeListener = loopbackClient.listenForAuthCode(successTemplate, errorTemplate);
2363
+ const redirectUri = loopbackClient.getRedirectUri();
2364
+ const validRequest = { ...remainingProperties,
2365
+ scopes: request.scopes || [],
2366
+ redirectUri: redirectUri,
2367
+ responseMode: ResponseMode.QUERY,
2368
+ codeChallenge: challenge,
2369
+ codeChallengeMethod: CodeChallengeMethodValues.S256
2370
+ };
2371
+ const authCodeUrl = await this.getAuthCodeUrl(validRequest);
2372
+ await openBrowser(authCodeUrl);
2373
+ const authCodeResponse = await authCodeListener.finally(() => {
2374
+ loopbackClient.closeServer();
2375
+ });
2376
+
2377
+ if (authCodeResponse.error) {
2378
+ throw new ServerError(authCodeResponse.error, authCodeResponse.error_description, authCodeResponse.suberror);
2379
+ } else if (!authCodeResponse.code) {
2380
+ throw NodeAuthError.createNoAuthCodeInResponseError();
2381
+ }
2382
+
2383
+ const clientInfo = authCodeResponse.client_info;
2384
+ const tokenRequest = {
2385
+ code: authCodeResponse.code,
2386
+ scopes: OIDC_DEFAULT_SCOPES,
2387
+ redirectUri: validRequest.redirectUri,
2388
+ codeVerifier: verifier,
2389
+ clientInfo: clientInfo || Constants$1.EMPTY_STRING
2390
+ };
2391
+ return this.acquireTokenByCode(tokenRequest);
2392
+ }
2146
2393
 
2147
2394
  }
2148
2395