@azure/msal-node 1.15.0 → 1.16.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.
@@ -112,6 +112,29 @@ class NetworkUtils {
112
112
  status: statusCode
113
113
  };
114
114
  }
115
+ /*
116
+ * Utility function that converts a URL object into an ordinary options object as expected by the
117
+ * http.request and https.request APIs.
118
+ * https://github.com/nodejs/node/blob/main/lib/internal/url.js#L1090
119
+ */
120
+ static urlToHttpOptions(url) {
121
+ const options = {
122
+ protocol: url.protocol,
123
+ hostname: url.hostname && url.hostname.startsWith("[") ? url.hostname.slice(1, -1) : url.hostname,
124
+ hash: url.hash,
125
+ search: url.search,
126
+ pathname: url.pathname,
127
+ path: `${url.pathname || ""}${url.search || ""}`,
128
+ href: url.href
129
+ };
130
+ if (url.port !== "") {
131
+ options.port = Number(url.port);
132
+ }
133
+ if (url.username || url.password) {
134
+ options.auth = `${decodeURIComponent(url.username)}:${decodeURIComponent(url.password)}`;
135
+ }
136
+ return options;
137
+ }
115
138
  }
116
139
 
117
140
  /*
@@ -151,11 +174,11 @@ class HttpClient {
151
174
  }
152
175
  }
153
176
  }
154
- const networkRequestViaProxy = (url, proxyUrlString, httpMethod, options, agentOptions, timeout) => {
155
- const headers = (options == null ? void 0 : options.headers) || {};
177
+ const networkRequestViaProxy = (destinationUrlString, proxyUrlString, httpMethod, options, agentOptions, timeout) => {
178
+ const destinationUrl = new URL(destinationUrlString);
156
179
  const proxyUrl = new URL(proxyUrlString);
157
- const destinationUrl = new URL(url);
158
180
  // "method: connect" must be used to establish a connection to the proxy
181
+ const headers = (options == null ? void 0 : options.headers) || {};
159
182
  const tunnelRequestOptions = {
160
183
  host: proxyUrl.hostname,
161
184
  port: proxyUrl.port,
@@ -266,13 +289,15 @@ const networkRequestViaProxy = (url, proxyUrlString, httpMethod, options, agentO
266
289
  });
267
290
  });
268
291
  };
269
- const networkRequestViaHttps = (url, httpMethod, options, agentOptions, timeout) => {
292
+ const networkRequestViaHttps = (urlString, httpMethod, options, agentOptions, timeout) => {
270
293
  const isPostRequest = httpMethod === HttpMethod.POST;
271
294
  const body = (options == null ? void 0 : options.body) || "";
272
- const emptyHeaders = {};
295
+ const url = new URL(urlString);
296
+ const headers = (options == null ? void 0 : options.headers) || {};
273
297
  const customOptions = {
274
298
  method: httpMethod,
275
- headers: (options == null ? void 0 : options.headers) || emptyHeaders
299
+ headers: headers,
300
+ ...NetworkUtils.urlToHttpOptions(url)
276
301
  };
277
302
  if (timeout) {
278
303
  customOptions.timeout = timeout;
@@ -288,7 +313,7 @@ const networkRequestViaHttps = (url, httpMethod, options, agentOptions, timeout)
288
313
  };
289
314
  }
290
315
  return new Promise((resolve, reject) => {
291
- const request = https.request(url, customOptions);
316
+ const request = https.request(customOptions);
292
317
  if (timeout) {
293
318
  request.on("timeout", () => {
294
319
  request.destroy();
@@ -424,6 +449,7 @@ const DEFAULT_TELEMETRY_OPTIONS = {
424
449
  */
425
450
  function buildAppConfiguration({
426
451
  auth,
452
+ broker,
427
453
  cache,
428
454
  system,
429
455
  telemetry
@@ -438,6 +464,9 @@ function buildAppConfiguration({
438
464
  ...DEFAULT_AUTH_OPTIONS,
439
465
  ...auth
440
466
  },
467
+ broker: {
468
+ ...broker
469
+ },
441
470
  cache: {
442
471
  ...DEFAULT_CACHE_OPTIONS,
443
472
  ...cache
@@ -1587,7 +1616,7 @@ class TokenCache {
1587
1616
 
1588
1617
  /* eslint-disable header/header */
1589
1618
  const name = "@azure/msal-node";
1590
- const version = "1.15.0";
1619
+ const version = "1.16.0";
1591
1620
 
1592
1621
  /*
1593
1622
  * Copyright (c) Microsoft Corporation. All rights reserved.
@@ -2101,6 +2130,14 @@ class PublicClientApplication extends ClientApplication {
2101
2130
  */
2102
2131
  constructor(configuration) {
2103
2132
  super(configuration);
2133
+ if (this.config.broker.nativeBrokerPlugin) {
2134
+ if (this.config.broker.nativeBrokerPlugin.isBrokerAvailable) {
2135
+ this.nativeBrokerPlugin = this.config.broker.nativeBrokerPlugin;
2136
+ this.nativeBrokerPlugin.setLogger(this.config.system.loggerOptions);
2137
+ } else {
2138
+ this.logger.warning("NativeBroker implementation was provided but the broker is unavailable.");
2139
+ }
2140
+ }
2104
2141
  }
2105
2142
  /**
2106
2143
  * Acquires a token from the authority using OAuth2.0 device code flow.
@@ -2129,24 +2166,46 @@ class PublicClientApplication extends ClientApplication {
2129
2166
  }
2130
2167
  }
2131
2168
  /**
2132
- * Acquires a token by requesting an Authorization code then exchanging it for a token.
2169
+ * Acquires a token interactively via the browser by requesting an authorization code then exchanging it for a token.
2133
2170
  */
2134
2171
  async acquireTokenInteractive(request) {
2135
- const {
2136
- verifier,
2137
- challenge
2138
- } = await this.cryptoProvider.generatePkceCodes();
2172
+ const correlationId = request.correlationId || this.cryptoProvider.createNewGuid();
2173
+ this.logger.trace("acquireTokenInteractive called", correlationId);
2139
2174
  const {
2140
2175
  openBrowser,
2141
2176
  successTemplate,
2142
2177
  errorTemplate,
2178
+ windowHandle,
2179
+ loopbackClient: customLoopbackClient,
2143
2180
  ...remainingProperties
2144
2181
  } = request;
2145
- const loopbackClient = new LoopbackClient();
2182
+ if (this.nativeBrokerPlugin) {
2183
+ var _remainingProperties$;
2184
+ const brokerRequest = {
2185
+ ...remainingProperties,
2186
+ clientId: this.config.auth.clientId,
2187
+ scopes: request.scopes || OIDC_DEFAULT_SCOPES,
2188
+ redirectUri: `${Constants.HTTP_PROTOCOL}${Constants.LOCALHOST}`,
2189
+ authority: request.authority || this.config.auth.authority,
2190
+ correlationId: correlationId,
2191
+ extraParameters: {
2192
+ ...remainingProperties.extraQueryParameters,
2193
+ ...remainingProperties.tokenQueryParameters
2194
+ },
2195
+ accountId: (_remainingProperties$ = remainingProperties.account) == null ? void 0 : _remainingProperties$.nativeAccountId
2196
+ };
2197
+ return this.nativeBrokerPlugin.acquireTokenInteractive(brokerRequest, windowHandle);
2198
+ }
2199
+ const {
2200
+ verifier,
2201
+ challenge
2202
+ } = await this.cryptoProvider.generatePkceCodes();
2203
+ const loopbackClient = customLoopbackClient || new LoopbackClient();
2146
2204
  const authCodeListener = loopbackClient.listenForAuthCode(successTemplate, errorTemplate);
2147
2205
  const redirectUri = loopbackClient.getRedirectUri();
2148
2206
  const validRequest = {
2149
2207
  ...remainingProperties,
2208
+ correlationId: correlationId,
2150
2209
  scopes: request.scopes || OIDC_DEFAULT_SCOPES,
2151
2210
  redirectUri: redirectUri,
2152
2211
  responseMode: ResponseMode.QUERY,
@@ -2172,6 +2231,57 @@ class PublicClientApplication extends ClientApplication {
2172
2231
  };
2173
2232
  return this.acquireTokenByCode(tokenRequest);
2174
2233
  }
2234
+ /**
2235
+ * Returns a token retrieved either from the cache or by exchanging the refresh token for a fresh access token. If brokering is enabled the token request will be serviced by the broker.
2236
+ * @param request
2237
+ * @returns
2238
+ */
2239
+ async acquireTokenSilent(request) {
2240
+ const correlationId = request.correlationId || this.cryptoProvider.createNewGuid();
2241
+ this.logger.trace("acquireTokenSilent called", correlationId);
2242
+ if (this.nativeBrokerPlugin) {
2243
+ const brokerRequest = {
2244
+ ...request,
2245
+ clientId: this.config.auth.clientId,
2246
+ scopes: request.scopes || OIDC_DEFAULT_SCOPES,
2247
+ redirectUri: `${Constants.HTTP_PROTOCOL}${Constants.LOCALHOST}`,
2248
+ authority: request.authority || this.config.auth.authority,
2249
+ correlationId: correlationId,
2250
+ extraParameters: request.tokenQueryParameters,
2251
+ accountId: request.account.nativeAccountId,
2252
+ forceRefresh: request.forceRefresh || false
2253
+ };
2254
+ return this.nativeBrokerPlugin.acquireTokenSilent(brokerRequest);
2255
+ }
2256
+ return super.acquireTokenSilent(request);
2257
+ }
2258
+ /**
2259
+ * Removes cache artifacts associated with the given account
2260
+ * @param request
2261
+ * @returns
2262
+ */
2263
+ async signOut(request) {
2264
+ if (this.nativeBrokerPlugin && request.account.nativeAccountId) {
2265
+ const signoutRequest = {
2266
+ clientId: this.config.auth.clientId,
2267
+ accountId: request.account.nativeAccountId,
2268
+ correlationId: request.correlationId || this.cryptoProvider.createNewGuid()
2269
+ };
2270
+ await this.nativeBrokerPlugin.signOut(signoutRequest);
2271
+ }
2272
+ await this.getTokenCache().removeAccount(request.account);
2273
+ }
2274
+ /**
2275
+ * Returns all cached accounts for this application. If brokering is enabled this request will be serviced by the broker.
2276
+ * @returns
2277
+ */
2278
+ async getAllAccounts() {
2279
+ if (this.nativeBrokerPlugin) {
2280
+ const correlationId = this.cryptoProvider.createNewGuid();
2281
+ return this.nativeBrokerPlugin.getAllAccounts(this.config.auth.clientId, correlationId);
2282
+ }
2283
+ return this.getTokenCache().getAllAccounts();
2284
+ }
2175
2285
  }
2176
2286
 
2177
2287
  /*