@azure/msal-node 1.2.0 → 1.3.3

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,5 +1,5 @@
1
- import { Constants as Constants$1, ProtocolMode, LogLevel, StringUtils, AccountEntity, CacheManager, IdTokenEntity, AccessTokenEntity, RefreshTokenEntity, AppMetadataEntity, ServerTelemetryEntity, AuthorityMetadataEntity, ThrottlingEntity, TokenCacheContext, Logger, ResponseMode, AuthenticationScheme, AuthorizationCodeClient, RefreshTokenClient, SilentFlowClient, OIDC_DEFAULT_SCOPES, ServerTelemetryManager, AuthorityFactory, DeviceCodeClient, UsernamePasswordClient, ClientAuthError, TimeUtils, ClientCredentialClient, OnBehalfOfClient } from '@azure/msal-common';
2
- export { AuthError, AuthErrorMessage, ClientAuthError, ClientAuthErrorMessage, ClientConfigurationError, ClientConfigurationErrorMessage, InteractionRequiredAuthError, LogLevel, Logger, PromptValue, ProtocolMode, ResponseMode, ServerError, TokenCacheContext } from '@azure/msal-common';
1
+ import { Constants as Constants$1, ProtocolMode, LogLevel, StringUtils, AccountEntity, CacheManager, IdTokenEntity, AccessTokenEntity, RefreshTokenEntity, AppMetadataEntity, ServerTelemetryEntity, AuthorityMetadataEntity, ThrottlingEntity, TokenCacheContext, Logger, ResponseMode, AuthenticationScheme, AuthorizationCodeClient, AuthError, RefreshTokenClient, SilentFlowClient, UsernamePasswordClient, OIDC_DEFAULT_SCOPES, ServerTelemetryManager, AuthorityFactory, DeviceCodeClient, ClientAuthError, TimeUtils, ClientCredentialClient, OnBehalfOfClient } from '@azure/msal-common';
2
+ export { AuthError, AuthErrorMessage, ClientAuthError, ClientAuthErrorMessage, ClientConfigurationError, ClientConfigurationErrorMessage, InteractionRequiredAuthError, InteractionRequiredAuthErrorMessage, LogLevel, Logger, PromptValue, ProtocolMode, ResponseMode, ServerError, TokenCacheContext } from '@azure/msal-common';
3
3
  import axios from 'axios';
4
4
  import { v4 } from 'uuid';
5
5
  import crypto from 'crypto';
@@ -106,7 +106,11 @@ class HttpClient {
106
106
  const request = {
107
107
  method: HttpMethod.GET,
108
108
  url: url,
109
+
110
+ /* istanbul ignore next */
109
111
  headers: options && options.headers,
112
+
113
+ /* istanbul ignore next */
110
114
  validateStatus: () => true
111
115
  };
112
116
  const response = await axios(request);
@@ -127,9 +131,15 @@ class HttpClient {
127
131
  const request = {
128
132
  method: HttpMethod.POST,
129
133
  url: url,
134
+
135
+ /* istanbul ignore next */
130
136
  data: options && options.body || "",
131
137
  timeout: cancellationToken,
138
+
139
+ /* istanbul ignore next */
132
140
  headers: options && options.headers,
141
+
142
+ /* istanbul ignore next */
133
143
  validateStatus: () => true
134
144
  };
135
145
  const response = await axios(request);
@@ -320,8 +330,25 @@ class PkceGenerator {
320
330
 
321
331
 
322
332
  generateCodeVerifier() {
323
- const buffer = crypto.randomBytes(RANDOM_OCTET_SIZE);
324
- const verifier = this.bufferToCVString(buffer);
333
+ const charArr = [];
334
+ const maxNumber = 256 - 256 % CharSet.CV_CHARSET.length;
335
+
336
+ while (charArr.length <= RANDOM_OCTET_SIZE) {
337
+ const byte = crypto.randomBytes(1)[0];
338
+
339
+ if (byte >= maxNumber) {
340
+ /*
341
+ * Ignore this number to maintain randomness.
342
+ * Including it would result in an unequal distribution of characters after doing the modulo
343
+ */
344
+ continue;
345
+ }
346
+
347
+ const index = byte % CharSet.CV_CHARSET.length;
348
+ charArr.push(CharSet.CV_CHARSET[index]);
349
+ }
350
+
351
+ const verifier = charArr.join("");
325
352
  return EncodingUtils.base64EncodeUrl(verifier);
326
353
  }
327
354
  /**
@@ -342,22 +369,6 @@ class PkceGenerator {
342
369
  sha256(buffer) {
343
370
  return crypto.createHash(Hash.SHA256).update(buffer).digest();
344
371
  }
345
- /**
346
- * Accepted characters; reference: https://tools.ietf.org/html/rfc7636#section-4.1
347
- * @param buffer
348
- */
349
-
350
-
351
- bufferToCVString(buffer) {
352
- const charArr = [];
353
-
354
- for (let i = 0; i < buffer.byteLength; i += 1) {
355
- const index = buffer[i] % CharSet.CV_CHARSET.length;
356
- charArr.push(CharSet.CV_CHARSET[index]);
357
- }
358
-
359
- return charArr.join("");
360
- }
361
372
 
362
373
  }
363
374
 
@@ -419,6 +430,23 @@ class CryptoProvider {
419
430
  getPublicKeyThumbprint() {
420
431
  throw new Error("Method not implemented.");
421
432
  }
433
+ /**
434
+ * Removes cryptographic keypair from key store matching the keyId passed in
435
+ * @param kid
436
+ */
437
+
438
+
439
+ removeTokenBindingKey() {
440
+ throw new Error("Method not implemented.");
441
+ }
442
+ /**
443
+ * Removes all cryptographic keys from Keystore
444
+ */
445
+
446
+
447
+ clearKeystore() {
448
+ throw new Error("Method not implemented.");
449
+ }
422
450
  /**
423
451
  * Signs the given object as a jwt payload with private key retrieved by given kid - currently not implemented for node
424
452
  */
@@ -1176,7 +1204,7 @@ class NodeStorage extends CacheManager {
1176
1204
  */
1177
1205
 
1178
1206
 
1179
- clear() {
1207
+ async clear() {
1180
1208
  this.logger.trace("Clearing cache entries created by MSAL"); // read inMemoryCache
1181
1209
 
1182
1210
  const cacheKeys = this.getKeys(); // delete each element
@@ -1360,7 +1388,7 @@ class TokenCache {
1360
1388
  await this.persistence.beforeCacheAccess(cacheContext);
1361
1389
  }
1362
1390
 
1363
- this.storage.removeAccount(AccountEntity.generateAccountCacheKey(account));
1391
+ await this.storage.removeAccount(AccountEntity.generateAccountCacheKey(account));
1364
1392
  } finally {
1365
1393
  if (this.persistence && cacheContext) {
1366
1394
  await this.persistence.afterCacheAccess(cacheContext);
@@ -1489,7 +1517,7 @@ class TokenCache {
1489
1517
 
1490
1518
  /* eslint-disable header/header */
1491
1519
  const name = "@azure/msal-node";
1492
- const version = "1.2.0";
1520
+ const version = "1.3.3";
1493
1521
 
1494
1522
  /*
1495
1523
  * Copyright (c) Microsoft Corporation. All rights reserved.
@@ -1558,6 +1586,10 @@ class ClientApplication {
1558
1586
  this.logger.verbose("Auth code client created", validRequest.correlationId);
1559
1587
  return authorizationCodeClient.acquireToken(validRequest);
1560
1588
  } catch (e) {
1589
+ if (e instanceof AuthError) {
1590
+ e.setCorrelationId(validRequest.correlationId);
1591
+ }
1592
+
1561
1593
  serverTelemetryManager.cacheFailedRequest(e);
1562
1594
  throw e;
1563
1595
  }
@@ -1585,6 +1617,10 @@ class ClientApplication {
1585
1617
  this.logger.verbose("Refresh token client created", validRequest.correlationId);
1586
1618
  return refreshTokenClient.acquireToken(validRequest);
1587
1619
  } catch (e) {
1620
+ if (e instanceof AuthError) {
1621
+ e.setCorrelationId(validRequest.correlationId);
1622
+ }
1623
+
1588
1624
  serverTelemetryManager.cacheFailedRequest(e);
1589
1625
  throw e;
1590
1626
  }
@@ -1612,6 +1648,43 @@ class ClientApplication {
1612
1648
  this.logger.verbose("Silent flow client created", validRequest.correlationId);
1613
1649
  return silentFlowClient.acquireToken(validRequest);
1614
1650
  } catch (e) {
1651
+ if (e instanceof AuthError) {
1652
+ e.setCorrelationId(validRequest.correlationId);
1653
+ }
1654
+
1655
+ serverTelemetryManager.cacheFailedRequest(e);
1656
+ throw e;
1657
+ }
1658
+ }
1659
+ /**
1660
+ * Acquires tokens with password grant by exchanging client applications username and password for credentials
1661
+ *
1662
+ * The latest OAuth 2.0 Security Best Current Practice disallows the password grant entirely.
1663
+ * More details on this recommendation at https://tools.ietf.org/html/draft-ietf-oauth-security-topics-13#section-3.4
1664
+ * Microsoft's documentation and recommendations are at:
1665
+ * https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows#usernamepassword
1666
+ *
1667
+ * @param request - UsenamePasswordRequest
1668
+ */
1669
+
1670
+
1671
+ async acquireTokenByUsernamePassword(request) {
1672
+ this.logger.info("acquireTokenByUsernamePassword called", request.correlationId);
1673
+ const validRequest = { ...request,
1674
+ ...this.initializeBaseRequest(request)
1675
+ };
1676
+ const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.acquireTokenByUsernamePassword, validRequest.correlationId);
1677
+
1678
+ try {
1679
+ const usernamePasswordClientConfig = await this.buildOauthClientConfiguration(validRequest.authority, validRequest.correlationId, serverTelemetryManager);
1680
+ const usernamePasswordClient = new UsernamePasswordClient(usernamePasswordClientConfig);
1681
+ this.logger.verbose("Username password client created", validRequest.correlationId);
1682
+ return usernamePasswordClient.acquireToken(validRequest);
1683
+ } catch (e) {
1684
+ if (e instanceof AuthError) {
1685
+ e.setCorrelationId(validRequest.correlationId);
1686
+ }
1687
+
1615
1688
  serverTelemetryManager.cacheFailedRequest(e);
1616
1689
  throw e;
1617
1690
  }
@@ -1654,6 +1727,7 @@ class ClientApplication {
1654
1727
 
1655
1728
  this.logger.verbose(`building oauth client configuration with the authority: ${authority}`, requestCorrelationId);
1656
1729
  const discoveredAuthority = await this.createAuthority(authority, azureRegionConfiguration, requestCorrelationId);
1730
+ serverTelemetryManager == null ? void 0 : serverTelemetryManager.updateRegionDiscoveryMetadata(discoveredAuthority.regionDiscoveryMetadata);
1657
1731
  return {
1658
1732
  authOptions: {
1659
1733
  clientId: this.config.auth.clientId,
@@ -1802,35 +1876,10 @@ class PublicClientApplication extends ClientApplication {
1802
1876
  this.logger.verbose("Device code client created", validRequest.correlationId);
1803
1877
  return deviceCodeClient.acquireToken(validRequest);
1804
1878
  } catch (e) {
1805
- serverTelemetryManager.cacheFailedRequest(e);
1806
- throw e;
1807
- }
1808
- }
1809
- /**
1810
- * Acquires tokens with password grant by exchanging client applications username and password for credentials
1811
- *
1812
- * The latest OAuth 2.0 Security Best Current Practice disallows the password grant entirely.
1813
- * More details on this recommendation at https://tools.ietf.org/html/draft-ietf-oauth-security-topics-13#section-3.4
1814
- * Microsoft's documentation and recommendations are at:
1815
- * https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows#usernamepassword
1816
- *
1817
- * @param request - UsenamePasswordRequest
1818
- */
1819
-
1820
-
1821
- async acquireTokenByUsernamePassword(request) {
1822
- this.logger.info("acquireTokenByUsernamePassword called", request.correlationId);
1823
- const validRequest = { ...request,
1824
- ...this.initializeBaseRequest(request)
1825
- };
1826
- const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.acquireTokenByUsernamePassword, validRequest.correlationId);
1879
+ if (e instanceof AuthError) {
1880
+ e.setCorrelationId(validRequest.correlationId);
1881
+ }
1827
1882
 
1828
- try {
1829
- const usernamePasswordClientConfig = await this.buildOauthClientConfiguration(validRequest.authority, validRequest.correlationId, serverTelemetryManager);
1830
- const usernamePasswordClient = new UsernamePasswordClient(usernamePasswordClientConfig);
1831
- this.logger.verbose("Username password client created", validRequest.correlationId);
1832
- return usernamePasswordClient.acquireToken(validRequest);
1833
- } catch (e) {
1834
1883
  serverTelemetryManager.cacheFailedRequest(e);
1835
1884
  throw e;
1836
1885
  }
@@ -2031,6 +2080,10 @@ class ConfidentialClientApplication extends ClientApplication {
2031
2080
  this.logger.verbose("Client credential client created", validRequest.correlationId);
2032
2081
  return clientCredentialClient.acquireToken(validRequest);
2033
2082
  } catch (e) {
2083
+ if (e instanceof AuthError) {
2084
+ e.setCorrelationId(validRequest.correlationId);
2085
+ }
2086
+
2034
2087
  serverTelemetryManager.cacheFailedRequest(e);
2035
2088
  throw e;
2036
2089
  }
@@ -2053,28 +2106,40 @@ class ConfidentialClientApplication extends ClientApplication {
2053
2106
  const validRequest = { ...request,
2054
2107
  ...this.initializeBaseRequest(request)
2055
2108
  };
2056
- const clientCredentialConfig = await this.buildOauthClientConfiguration(validRequest.authority, validRequest.correlationId);
2057
- const oboClient = new OnBehalfOfClient(clientCredentialConfig);
2058
- this.logger.verbose("On behalf of client created", validRequest.correlationId);
2059
- return oboClient.acquireToken(validRequest);
2109
+
2110
+ try {
2111
+ const clientCredentialConfig = await this.buildOauthClientConfiguration(validRequest.authority, validRequest.correlationId);
2112
+ const oboClient = new OnBehalfOfClient(clientCredentialConfig);
2113
+ this.logger.verbose("On behalf of client created", validRequest.correlationId);
2114
+ return oboClient.acquireToken(validRequest);
2115
+ } catch (e) {
2116
+ if (e instanceof AuthError) {
2117
+ e.setCorrelationId(validRequest.correlationId);
2118
+ }
2119
+
2120
+ throw e;
2121
+ }
2060
2122
  }
2061
2123
 
2062
2124
  setClientCredential(configuration) {
2063
2125
  const clientSecretNotEmpty = !StringUtils.isEmpty(configuration.auth.clientSecret);
2064
2126
  const clientAssertionNotEmpty = !StringUtils.isEmpty(configuration.auth.clientAssertion);
2065
- const certificate = configuration.auth.clientCertificate;
2127
+ const certificate = configuration.auth.clientCertificate || {
2128
+ thumbprint: "",
2129
+ privateKey: ""
2130
+ };
2066
2131
  const certificateNotEmpty = !StringUtils.isEmpty(certificate.thumbprint) || !StringUtils.isEmpty(certificate.privateKey); // Check that at most one credential is set on the application
2067
2132
 
2068
2133
  if (clientSecretNotEmpty && clientAssertionNotEmpty || clientAssertionNotEmpty && certificateNotEmpty || clientSecretNotEmpty && certificateNotEmpty) {
2069
2134
  throw ClientAuthError.createInvalidCredentialError();
2070
2135
  }
2071
2136
 
2072
- if (clientSecretNotEmpty) {
2137
+ if (configuration.auth.clientSecret) {
2073
2138
  this.clientSecret = configuration.auth.clientSecret;
2074
2139
  return;
2075
2140
  }
2076
2141
 
2077
- if (clientAssertionNotEmpty) {
2142
+ if (configuration.auth.clientAssertion) {
2078
2143
  this.clientAssertion = ClientAssertion.fromAssertion(configuration.auth.clientAssertion);
2079
2144
  return;
2080
2145
  }
@@ -2090,5 +2155,5 @@ class ConfidentialClientApplication extends ClientApplication {
2090
2155
 
2091
2156
  }
2092
2157
 
2093
- export { ClientApplication, ClientAssertion, ConfidentialClientApplication, CryptoProvider, NodeStorage, PublicClientApplication, TokenCache, buildAppConfiguration };
2158
+ export { ClientApplication, ClientAssertion, ConfidentialClientApplication, CryptoProvider, NodeStorage, PublicClientApplication, TokenCache, buildAppConfiguration, version };
2094
2159
  //# sourceMappingURL=msal-node.esm.js.map