@azure/msal-node 2.12.0 → 2.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.
Files changed (197) hide show
  1. package/dist/cache/ITokenCache.d.ts +15 -15
  2. package/dist/cache/NodeStorage.cjs +425 -423
  3. package/dist/cache/NodeStorage.cjs.map +1 -1
  4. package/dist/cache/NodeStorage.d.ts +193 -193
  5. package/dist/cache/NodeStorage.mjs +423 -423
  6. package/dist/cache/TokenCache.cjs +267 -265
  7. package/dist/cache/TokenCache.cjs.map +1 -1
  8. package/dist/cache/TokenCache.d.ts +90 -90
  9. package/dist/cache/TokenCache.mjs +265 -265
  10. package/dist/cache/distributed/DistributedCachePlugin.cjs +28 -26
  11. package/dist/cache/distributed/DistributedCachePlugin.cjs.map +1 -1
  12. package/dist/cache/distributed/DistributedCachePlugin.d.ts +10 -10
  13. package/dist/cache/distributed/DistributedCachePlugin.mjs +26 -26
  14. package/dist/cache/distributed/ICacheClient.d.ts +17 -17
  15. package/dist/cache/distributed/IPartitionManager.d.ts +28 -28
  16. package/dist/cache/serializer/Deserializer.cjs +169 -167
  17. package/dist/cache/serializer/Deserializer.cjs.map +1 -1
  18. package/dist/cache/serializer/Deserializer.d.ts +42 -42
  19. package/dist/cache/serializer/Deserializer.mjs +167 -167
  20. package/dist/cache/serializer/Serializer.cjs +139 -137
  21. package/dist/cache/serializer/Serializer.cjs.map +1 -1
  22. package/dist/cache/serializer/Serializer.d.ts +39 -39
  23. package/dist/cache/serializer/Serializer.mjs +137 -137
  24. package/dist/cache/serializer/SerializerTypes.d.ts +102 -102
  25. package/dist/client/ClientApplication.cjs +336 -334
  26. package/dist/client/ClientApplication.cjs.map +1 -1
  27. package/dist/client/ClientApplication.d.ts +143 -143
  28. package/dist/client/ClientApplication.mjs +334 -334
  29. package/dist/client/ClientAssertion.cjs +149 -143
  30. package/dist/client/ClientAssertion.cjs.map +1 -1
  31. package/dist/client/ClientAssertion.d.ts +55 -55
  32. package/dist/client/ClientAssertion.mjs +143 -143
  33. package/dist/client/ClientCredentialClient.cjs +196 -194
  34. package/dist/client/ClientCredentialClient.cjs.map +1 -1
  35. package/dist/client/ClientCredentialClient.d.ts +33 -33
  36. package/dist/client/ClientCredentialClient.mjs +194 -194
  37. package/dist/client/ConfidentialClientApplication.cjs +170 -169
  38. package/dist/client/ConfidentialClientApplication.cjs.map +1 -1
  39. package/dist/client/ConfidentialClientApplication.d.ts +58 -58
  40. package/dist/client/ConfidentialClientApplication.mjs +168 -169
  41. package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
  42. package/dist/client/DeviceCodeClient.cjs +209 -207
  43. package/dist/client/DeviceCodeClient.cjs.map +1 -1
  44. package/dist/client/DeviceCodeClient.d.ts +53 -53
  45. package/dist/client/DeviceCodeClient.mjs +207 -207
  46. package/dist/client/IConfidentialClientApplication.d.ts +39 -39
  47. package/dist/client/IPublicClientApplication.d.ts +42 -42
  48. package/dist/client/ManagedIdentityApplication.cjs +86 -84
  49. package/dist/client/ManagedIdentityApplication.cjs.map +1 -1
  50. package/dist/client/ManagedIdentityApplication.d.ts +30 -30
  51. package/dist/client/ManagedIdentityApplication.mjs +84 -84
  52. package/dist/client/ManagedIdentityClient.cjs +62 -60
  53. package/dist/client/ManagedIdentityClient.cjs.map +1 -1
  54. package/dist/client/ManagedIdentityClient.d.ts +27 -27
  55. package/dist/client/ManagedIdentityClient.mjs +60 -60
  56. package/dist/client/ManagedIdentitySources/AppService.cjs +47 -45
  57. package/dist/client/ManagedIdentitySources/AppService.cjs.map +1 -1
  58. package/dist/client/ManagedIdentitySources/AppService.d.ts +17 -17
  59. package/dist/client/ManagedIdentitySources/AppService.mjs +46 -46
  60. package/dist/client/ManagedIdentitySources/AzureArc.cjs +161 -120
  61. package/dist/client/ManagedIdentitySources/AzureArc.cjs.map +1 -1
  62. package/dist/client/ManagedIdentitySources/AzureArc.d.ts +27 -23
  63. package/dist/client/ManagedIdentitySources/AzureArc.d.ts.map +1 -1
  64. package/dist/client/ManagedIdentitySources/AzureArc.mjs +155 -122
  65. package/dist/client/ManagedIdentitySources/AzureArc.mjs.map +1 -1
  66. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs +124 -122
  67. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs.map +1 -1
  68. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts +30 -30
  69. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs +122 -122
  70. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs.map +1 -1
  71. package/dist/client/ManagedIdentitySources/CloudShell.cjs +40 -38
  72. package/dist/client/ManagedIdentitySources/CloudShell.cjs.map +1 -1
  73. package/dist/client/ManagedIdentitySources/CloudShell.d.ts +16 -16
  74. package/dist/client/ManagedIdentitySources/CloudShell.mjs +39 -39
  75. package/dist/client/ManagedIdentitySources/Imds.cjs +45 -43
  76. package/dist/client/ManagedIdentitySources/Imds.cjs.map +1 -1
  77. package/dist/client/ManagedIdentitySources/Imds.d.ts +12 -12
  78. package/dist/client/ManagedIdentitySources/Imds.mjs +44 -44
  79. package/dist/client/ManagedIdentitySources/ServiceFabric.cjs +56 -54
  80. package/dist/client/ManagedIdentitySources/ServiceFabric.cjs.map +1 -1
  81. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts +17 -17
  82. package/dist/client/ManagedIdentitySources/ServiceFabric.mjs +55 -55
  83. package/dist/client/OnBehalfOfClient.cjs +204 -202
  84. package/dist/client/OnBehalfOfClient.cjs.map +1 -1
  85. package/dist/client/OnBehalfOfClient.d.ts +47 -47
  86. package/dist/client/OnBehalfOfClient.mjs +202 -202
  87. package/dist/client/PublicClientApplication.cjs +239 -237
  88. package/dist/client/PublicClientApplication.cjs.map +1 -1
  89. package/dist/client/PublicClientApplication.d.ts +72 -72
  90. package/dist/client/PublicClientApplication.mjs +237 -237
  91. package/dist/client/UsernamePasswordClient.cjs +97 -95
  92. package/dist/client/UsernamePasswordClient.cjs.map +1 -1
  93. package/dist/client/UsernamePasswordClient.d.ts +25 -25
  94. package/dist/client/UsernamePasswordClient.mjs +95 -95
  95. package/dist/config/Configuration.cjs +108 -106
  96. package/dist/config/Configuration.cjs.map +1 -1
  97. package/dist/config/Configuration.d.ts +131 -131
  98. package/dist/config/Configuration.mjs +106 -106
  99. package/dist/config/ManagedIdentityId.cjs +50 -48
  100. package/dist/config/ManagedIdentityId.cjs.map +1 -1
  101. package/dist/config/ManagedIdentityId.d.ts +11 -11
  102. package/dist/config/ManagedIdentityId.mjs +48 -48
  103. package/dist/config/ManagedIdentityRequestParameters.cjs +30 -28
  104. package/dist/config/ManagedIdentityRequestParameters.cjs.map +1 -1
  105. package/dist/config/ManagedIdentityRequestParameters.d.ts +11 -11
  106. package/dist/config/ManagedIdentityRequestParameters.mjs +28 -28
  107. package/dist/crypto/CryptoProvider.cjs +91 -89
  108. package/dist/crypto/CryptoProvider.cjs.map +1 -1
  109. package/dist/crypto/CryptoProvider.d.ts +62 -62
  110. package/dist/crypto/CryptoProvider.mjs +89 -89
  111. package/dist/crypto/GuidGenerator.cjs +24 -22
  112. package/dist/crypto/GuidGenerator.cjs.map +1 -1
  113. package/dist/crypto/GuidGenerator.d.ts +14 -14
  114. package/dist/crypto/GuidGenerator.mjs +22 -22
  115. package/dist/crypto/HashUtils.cjs +19 -13
  116. package/dist/crypto/HashUtils.cjs.map +1 -1
  117. package/dist/crypto/HashUtils.d.ts +8 -8
  118. package/dist/crypto/HashUtils.mjs +13 -13
  119. package/dist/crypto/PkceGenerator.cjs +55 -49
  120. package/dist/crypto/PkceGenerator.cjs.map +1 -1
  121. package/dist/crypto/PkceGenerator.d.ts +22 -22
  122. package/dist/crypto/PkceGenerator.mjs +49 -49
  123. package/dist/error/ManagedIdentityError.cjs +43 -41
  124. package/dist/error/ManagedIdentityError.cjs.map +1 -1
  125. package/dist/error/ManagedIdentityError.d.ts +30 -30
  126. package/dist/error/ManagedIdentityError.mjs +41 -41
  127. package/dist/error/ManagedIdentityErrorCodes.cjs +26 -24
  128. package/dist/error/ManagedIdentityErrorCodes.cjs.map +1 -1
  129. package/dist/error/ManagedIdentityErrorCodes.d.ts +22 -22
  130. package/dist/error/ManagedIdentityErrorCodes.mjs +24 -24
  131. package/dist/error/NodeAuthError.cjs +97 -95
  132. package/dist/error/NodeAuthError.cjs.map +1 -1
  133. package/dist/error/NodeAuthError.d.ts +73 -73
  134. package/dist/error/NodeAuthError.mjs +95 -95
  135. package/dist/index.cjs +3 -1
  136. package/dist/index.cjs.map +1 -1
  137. package/dist/index.d.ts +45 -45
  138. package/dist/index.mjs +1 -1
  139. package/dist/internals.cjs +6 -8
  140. package/dist/internals.cjs.map +1 -1
  141. package/dist/internals.d.ts +6 -6
  142. package/dist/internals.mjs +4 -8
  143. package/dist/internals.mjs.map +1 -1
  144. package/dist/network/HttpClient.cjs +288 -281
  145. package/dist/network/HttpClient.cjs.map +1 -1
  146. package/dist/network/HttpClient.d.ts +25 -25
  147. package/dist/network/HttpClient.mjs +281 -281
  148. package/dist/network/HttpClientWithRetries.cjs +36 -34
  149. package/dist/network/HttpClientWithRetries.cjs.map +1 -1
  150. package/dist/network/HttpClientWithRetries.d.ts +11 -11
  151. package/dist/network/HttpClientWithRetries.mjs +34 -34
  152. package/dist/network/ILoopbackClient.d.ts +10 -10
  153. package/dist/network/LoopbackClient.cjs +87 -77
  154. package/dist/network/LoopbackClient.cjs.map +1 -1
  155. package/dist/network/LoopbackClient.d.ts +21 -21
  156. package/dist/network/LoopbackClient.d.ts.map +1 -1
  157. package/dist/network/LoopbackClient.mjs +81 -77
  158. package/dist/network/LoopbackClient.mjs.map +1 -1
  159. package/dist/packageMetadata.cjs +6 -4
  160. package/dist/packageMetadata.cjs.map +1 -1
  161. package/dist/packageMetadata.d.ts +2 -2
  162. package/dist/packageMetadata.mjs +4 -4
  163. package/dist/request/AuthorizationCodeRequest.d.ts +21 -21
  164. package/dist/request/AuthorizationUrlRequest.d.ts +33 -33
  165. package/dist/request/ClientCredentialRequest.d.ts +14 -14
  166. package/dist/request/DeviceCodeRequest.d.ts +15 -15
  167. package/dist/request/InteractiveRequest.d.ts +22 -22
  168. package/dist/request/ManagedIdentityRequest.d.ts +8 -8
  169. package/dist/request/ManagedIdentityRequestParams.d.ts +9 -9
  170. package/dist/request/OnBehalfOfRequest.d.ts +14 -14
  171. package/dist/request/RefreshTokenRequest.d.ts +17 -17
  172. package/dist/request/SignOutRequest.d.ts +5 -5
  173. package/dist/request/SilentFlowRequest.d.ts +16 -16
  174. package/dist/request/UsernamePasswordRequest.d.ts +19 -19
  175. package/dist/response/ManagedIdentityTokenResponse.d.ts +34 -34
  176. package/dist/retry/IHttpRetryPolicy.d.ts +5 -5
  177. package/dist/retry/LinearRetryPolicy.cjs +42 -40
  178. package/dist/retry/LinearRetryPolicy.cjs.map +1 -1
  179. package/dist/retry/LinearRetryPolicy.d.ts +11 -11
  180. package/dist/retry/LinearRetryPolicy.mjs +40 -40
  181. package/dist/utils/Constants.cjs +138 -136
  182. package/dist/utils/Constants.cjs.map +1 -1
  183. package/dist/utils/Constants.d.ts +141 -141
  184. package/dist/utils/Constants.mjs +136 -136
  185. package/dist/utils/EncodingUtils.cjs +46 -44
  186. package/dist/utils/EncodingUtils.cjs.map +1 -1
  187. package/dist/utils/EncodingUtils.d.ts +26 -26
  188. package/dist/utils/EncodingUtils.mjs +44 -44
  189. package/dist/utils/NetworkUtils.cjs +40 -38
  190. package/dist/utils/NetworkUtils.cjs.map +1 -1
  191. package/dist/utils/NetworkUtils.d.ts +5 -5
  192. package/dist/utils/NetworkUtils.mjs +38 -38
  193. package/package.json +2 -2
  194. package/src/client/ConfidentialClientApplication.ts +3 -3
  195. package/src/client/ManagedIdentitySources/AzureArc.ts +80 -25
  196. package/src/network/LoopbackClient.ts +6 -0
  197. package/src/packageMetadata.ts +1 -1
@@ -1,4 +1,4 @@
1
- /*! @azure/msal-node v2.12.0 2024-07-23 */
1
+ /*! @azure/msal-node v2.13.0 2024-08-13 */
2
2
  'use strict';
3
3
  import { Logger, buildStaticAuthorityOptions, ResponseMode, AuthenticationScheme, AuthorizationCodeClient, AuthError, RefreshTokenClient, SilentFlowClient, createClientAuthError, ClientAuthErrorCodes, Constants as Constants$1, getClientAssertion, StringUtils, OIDC_DEFAULT_SCOPES, ServerTelemetryManager, Authority, AuthorityFactory } from '@azure/msal-common';
4
4
  import { buildAppConfiguration } from '../config/Configuration.mjs';
@@ -11,339 +11,339 @@ import { name, version } from '../packageMetadata.mjs';
11
11
  import { NodeAuthError } from '../error/NodeAuthError.mjs';
12
12
  import { UsernamePasswordClient } from './UsernamePasswordClient.mjs';
13
13
 
14
- /*
15
- * Copyright (c) Microsoft Corporation. All rights reserved.
16
- * Licensed under the MIT License.
17
- */
18
- /**
19
- * Base abstract class for all ClientApplications - public and confidential
20
- * @public
21
- */
22
- class ClientApplication {
23
- /**
24
- * Constructor for the ClientApplication
25
- */
26
- constructor(configuration) {
27
- this.config = buildAppConfiguration(configuration);
28
- this.cryptoProvider = new CryptoProvider();
29
- this.logger = new Logger(this.config.system.loggerOptions, name, version);
30
- this.storage = new NodeStorage(this.logger, this.config.auth.clientId, this.cryptoProvider, buildStaticAuthorityOptions(this.config.auth));
31
- this.tokenCache = new TokenCache(this.storage, this.logger, this.config.cache.cachePlugin);
32
- }
33
- /**
34
- * Creates the URL of the authorization request, letting the user input credentials and consent to the
35
- * application. The URL targets the /authorize endpoint of the authority configured in the
36
- * application object.
37
- *
38
- * Once the user inputs their credentials and consents, the authority will send a response to the redirect URI
39
- * sent in the request and should contain an authorization code, which can then be used to acquire tokens via
40
- * `acquireTokenByCode(AuthorizationCodeRequest)`.
41
- */
42
- async getAuthCodeUrl(request) {
43
- this.logger.info("getAuthCodeUrl called", request.correlationId);
44
- const validRequest = {
45
- ...request,
46
- ...(await this.initializeBaseRequest(request)),
47
- responseMode: request.responseMode || ResponseMode.QUERY,
48
- authenticationScheme: AuthenticationScheme.BEARER,
49
- };
50
- const authClientConfig = await this.buildOauthClientConfiguration(validRequest.authority, validRequest.correlationId, undefined, undefined, request.azureCloudOptions);
51
- const authorizationCodeClient = new AuthorizationCodeClient(authClientConfig);
52
- this.logger.verbose("Auth code client created", validRequest.correlationId);
53
- return authorizationCodeClient.getAuthCodeUrl(validRequest);
54
- }
55
- /**
56
- * Acquires a token by exchanging the Authorization Code received from the first step of OAuth2.0
57
- * Authorization Code flow.
58
- *
59
- * `getAuthCodeUrl(AuthorizationCodeUrlRequest)` can be used to create the URL for the first step of OAuth2.0
60
- * Authorization Code flow. Ensure that values for redirectUri and scopes in AuthorizationCodeUrlRequest and
61
- * AuthorizationCodeRequest are the same.
62
- */
63
- async acquireTokenByCode(request, authCodePayLoad) {
64
- this.logger.info("acquireTokenByCode called");
65
- if (request.state && authCodePayLoad) {
66
- this.logger.info("acquireTokenByCode - validating state");
67
- this.validateState(request.state, authCodePayLoad.state || "");
68
- // eslint-disable-next-line no-param-reassign
69
- authCodePayLoad = { ...authCodePayLoad, state: "" };
70
- }
71
- const validRequest = {
72
- ...request,
73
- ...(await this.initializeBaseRequest(request)),
74
- authenticationScheme: AuthenticationScheme.BEARER,
75
- };
76
- const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.acquireTokenByCode, validRequest.correlationId);
77
- try {
78
- const authClientConfig = await this.buildOauthClientConfiguration(validRequest.authority, validRequest.correlationId, serverTelemetryManager, undefined, request.azureCloudOptions);
79
- const authorizationCodeClient = new AuthorizationCodeClient(authClientConfig);
80
- this.logger.verbose("Auth code client created", validRequest.correlationId);
81
- return await authorizationCodeClient.acquireToken(validRequest, authCodePayLoad);
82
- }
83
- catch (e) {
84
- if (e instanceof AuthError) {
85
- e.setCorrelationId(validRequest.correlationId);
86
- }
87
- serverTelemetryManager.cacheFailedRequest(e);
88
- throw e;
89
- }
90
- }
91
- /**
92
- * Acquires a token by exchanging the refresh token provided for a new set of tokens.
93
- *
94
- * This API is provided only for scenarios where you would like to migrate from ADAL to MSAL. Otherwise, it is
95
- * recommended that you use `acquireTokenSilent()` for silent scenarios. When using `acquireTokenSilent()`, MSAL will
96
- * handle the caching and refreshing of tokens automatically.
97
- */
98
- async acquireTokenByRefreshToken(request) {
99
- this.logger.info("acquireTokenByRefreshToken called", request.correlationId);
100
- const validRequest = {
101
- ...request,
102
- ...(await this.initializeBaseRequest(request)),
103
- authenticationScheme: AuthenticationScheme.BEARER,
104
- };
105
- const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.acquireTokenByRefreshToken, validRequest.correlationId);
106
- try {
107
- const refreshTokenClientConfig = await this.buildOauthClientConfiguration(validRequest.authority, validRequest.correlationId, serverTelemetryManager, undefined, request.azureCloudOptions);
108
- const refreshTokenClient = new RefreshTokenClient(refreshTokenClientConfig);
109
- this.logger.verbose("Refresh token client created", validRequest.correlationId);
110
- return await refreshTokenClient.acquireToken(validRequest);
111
- }
112
- catch (e) {
113
- if (e instanceof AuthError) {
114
- e.setCorrelationId(validRequest.correlationId);
115
- }
116
- serverTelemetryManager.cacheFailedRequest(e);
117
- throw e;
118
- }
119
- }
120
- /**
121
- * Acquires a token silently when a user specifies the account the token is requested for.
122
- *
123
- * This API expects the user to provide an account object and looks into the cache to retrieve the token if present.
124
- * There is also an optional "forceRefresh" boolean the user can send to bypass the cache for access_token and id_token.
125
- * In case the refresh_token is expired or not found, an error is thrown
126
- * and the guidance is for the user to call any interactive token acquisition API (eg: `acquireTokenByCode()`).
127
- */
128
- async acquireTokenSilent(request) {
129
- const validRequest = {
130
- ...request,
131
- ...(await this.initializeBaseRequest(request)),
132
- forceRefresh: request.forceRefresh || false,
133
- };
134
- const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.acquireTokenSilent, validRequest.correlationId, validRequest.forceRefresh);
135
- try {
136
- const silentFlowClientConfig = await this.buildOauthClientConfiguration(validRequest.authority, validRequest.correlationId, serverTelemetryManager, undefined, request.azureCloudOptions);
137
- const silentFlowClient = new SilentFlowClient(silentFlowClientConfig);
138
- this.logger.verbose("Silent flow client created", validRequest.correlationId);
139
- return await silentFlowClient.acquireToken(validRequest);
140
- }
141
- catch (e) {
142
- if (e instanceof AuthError) {
143
- e.setCorrelationId(validRequest.correlationId);
144
- }
145
- serverTelemetryManager.cacheFailedRequest(e);
146
- throw e;
147
- }
148
- }
149
- /**
150
- * Acquires tokens with password grant by exchanging client applications username and password for credentials
151
- *
152
- * The latest OAuth 2.0 Security Best Current Practice disallows the password grant entirely.
153
- * More details on this recommendation at https://tools.ietf.org/html/draft-ietf-oauth-security-topics-13#section-3.4
154
- * Microsoft's documentation and recommendations are at:
155
- * https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows#usernamepassword
156
- *
157
- * @param request - UsenamePasswordRequest
158
- */
159
- async acquireTokenByUsernamePassword(request) {
160
- this.logger.info("acquireTokenByUsernamePassword called", request.correlationId);
161
- const validRequest = {
162
- ...request,
163
- ...(await this.initializeBaseRequest(request)),
164
- };
165
- const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.acquireTokenByUsernamePassword, validRequest.correlationId);
166
- try {
167
- const usernamePasswordClientConfig = await this.buildOauthClientConfiguration(validRequest.authority, validRequest.correlationId, serverTelemetryManager, undefined, request.azureCloudOptions);
168
- const usernamePasswordClient = new UsernamePasswordClient(usernamePasswordClientConfig);
169
- this.logger.verbose("Username password client created", validRequest.correlationId);
170
- return await usernamePasswordClient.acquireToken(validRequest);
171
- }
172
- catch (e) {
173
- if (e instanceof AuthError) {
174
- e.setCorrelationId(validRequest.correlationId);
175
- }
176
- serverTelemetryManager.cacheFailedRequest(e);
177
- throw e;
178
- }
179
- }
180
- /**
181
- * Gets the token cache for the application.
182
- */
183
- getTokenCache() {
184
- this.logger.info("getTokenCache called");
185
- return this.tokenCache;
186
- }
187
- /**
188
- * Validates OIDC state by comparing the user cached state with the state received from the server.
189
- *
190
- * This API is provided for scenarios where you would use OAuth2.0 state parameter to mitigate against
191
- * CSRF attacks.
192
- * For more information about state, visit https://datatracker.ietf.org/doc/html/rfc6819#section-3.6.
193
- * @param state
194
- * @param cachedState
195
- */
196
- validateState(state, cachedState) {
197
- if (!state) {
198
- throw NodeAuthError.createStateNotFoundError();
199
- }
200
- if (state !== cachedState) {
201
- throw createClientAuthError(ClientAuthErrorCodes.stateMismatch);
202
- }
203
- }
204
- /**
205
- * Returns the logger instance
206
- */
207
- getLogger() {
208
- return this.logger;
209
- }
210
- /**
211
- * Replaces the default logger set in configurations with new Logger with new configurations
212
- * @param logger - Logger instance
213
- */
214
- setLogger(logger) {
215
- this.logger = logger;
216
- }
217
- /**
218
- * Builds the common configuration to be passed to the common component based on the platform configurarion
219
- * @param authority - user passed authority in configuration
220
- * @param serverTelemetryManager - initializes servertelemetry if passed
221
- */
222
- async buildOauthClientConfiguration(authority, requestCorrelationId, serverTelemetryManager, azureRegionConfiguration, azureCloudOptions) {
223
- this.logger.verbose("buildOauthClientConfiguration called", requestCorrelationId);
224
- // precedence - azureCloudInstance + tenant >> authority and request >> config
225
- const userAzureCloudOptions = azureCloudOptions
226
- ? azureCloudOptions
227
- : this.config.auth.azureCloudOptions;
228
- // using null assertion operator as we ensure that all config values have default values in buildConfiguration()
229
- const discoveredAuthority = await this.createAuthority(authority, requestCorrelationId, azureRegionConfiguration, userAzureCloudOptions);
230
- this.logger.info(`Building oauth client configuration with the following authority: ${discoveredAuthority.tokenEndpoint}.`, requestCorrelationId);
231
- serverTelemetryManager?.updateRegionDiscoveryMetadata(discoveredAuthority.regionDiscoveryMetadata);
232
- const clientConfiguration = {
233
- authOptions: {
234
- clientId: this.config.auth.clientId,
235
- authority: discoveredAuthority,
236
- clientCapabilities: this.config.auth.clientCapabilities,
237
- },
238
- loggerOptions: {
239
- logLevel: this.config.system.loggerOptions.logLevel,
240
- loggerCallback: this.config.system.loggerOptions.loggerCallback,
241
- piiLoggingEnabled: this.config.system.loggerOptions.piiLoggingEnabled,
242
- correlationId: requestCorrelationId,
243
- },
244
- cacheOptions: {
245
- claimsBasedCachingEnabled: this.config.cache.claimsBasedCachingEnabled,
246
- },
247
- cryptoInterface: this.cryptoProvider,
248
- networkInterface: this.config.system.networkClient,
249
- storageInterface: this.storage,
250
- serverTelemetryManager: serverTelemetryManager,
251
- clientCredentials: {
252
- clientSecret: this.clientSecret,
253
- clientAssertion: await this.getClientAssertion(discoveredAuthority),
254
- },
255
- libraryInfo: {
256
- sku: Constants.MSAL_SKU,
257
- version: version,
258
- cpu: process.arch || Constants$1.EMPTY_STRING,
259
- os: process.platform || Constants$1.EMPTY_STRING,
260
- },
261
- telemetry: this.config.telemetry,
262
- persistencePlugin: this.config.cache.cachePlugin,
263
- serializableCache: this.tokenCache,
264
- };
265
- return clientConfiguration;
266
- }
267
- async getClientAssertion(authority) {
268
- if (this.developerProvidedClientAssertion) {
269
- this.clientAssertion = ClientAssertion.fromAssertion(await getClientAssertion(this.developerProvidedClientAssertion, this.config.auth.clientId, authority.tokenEndpoint));
270
- }
271
- return (this.clientAssertion && {
272
- assertion: this.clientAssertion.getJwt(this.cryptoProvider, this.config.auth.clientId, authority.tokenEndpoint),
273
- assertionType: Constants.JWT_BEARER_ASSERTION_TYPE,
274
- });
275
- }
276
- /**
277
- * Generates a request with the default scopes & generates a correlationId.
278
- * @param authRequest - BaseAuthRequest for initialization
279
- */
280
- async initializeBaseRequest(authRequest) {
281
- this.logger.verbose("initializeRequestScopes called", authRequest.correlationId);
282
- // Default authenticationScheme to Bearer, log that POP isn't supported yet
283
- if (authRequest.authenticationScheme &&
284
- authRequest.authenticationScheme === AuthenticationScheme.POP) {
285
- this.logger.verbose("Authentication Scheme 'pop' is not supported yet, setting Authentication Scheme to 'Bearer' for request", authRequest.correlationId);
286
- }
287
- authRequest.authenticationScheme = AuthenticationScheme.BEARER;
288
- // Set requested claims hash if claims-based caching is enabled and claims were requested
289
- if (this.config.cache.claimsBasedCachingEnabled &&
290
- authRequest.claims &&
291
- // Checks for empty stringified object "{}" which doesn't qualify as requested claims
292
- !StringUtils.isEmptyObj(authRequest.claims)) {
293
- authRequest.requestedClaimsHash =
294
- await this.cryptoProvider.hashString(authRequest.claims);
295
- }
296
- return {
297
- ...authRequest,
298
- scopes: [
299
- ...((authRequest && authRequest.scopes) || []),
300
- ...OIDC_DEFAULT_SCOPES,
301
- ],
302
- correlationId: (authRequest && authRequest.correlationId) ||
303
- this.cryptoProvider.createNewGuid(),
304
- authority: authRequest.authority || this.config.auth.authority,
305
- };
306
- }
307
- /**
308
- * Initializes the server telemetry payload
309
- * @param apiId - Id for a specific request
310
- * @param correlationId - GUID
311
- * @param forceRefresh - boolean to indicate network call
312
- */
313
- initializeServerTelemetryManager(apiId, correlationId, forceRefresh) {
314
- const telemetryPayload = {
315
- clientId: this.config.auth.clientId,
316
- correlationId: correlationId,
317
- apiId: apiId,
318
- forceRefresh: forceRefresh || false,
319
- };
320
- return new ServerTelemetryManager(telemetryPayload, this.storage);
321
- }
322
- /**
323
- * Create authority instance. If authority not passed in request, default to authority set on the application
324
- * object. If no authority set in application object, then default to common authority.
325
- * @param authorityString - authority from user configuration
326
- */
327
- async createAuthority(authorityString, requestCorrelationId, azureRegionConfiguration, azureCloudOptions) {
328
- this.logger.verbose("createAuthority called", requestCorrelationId);
329
- // build authority string based on auth params - azureCloudInstance is prioritized if provided
330
- const authorityUrl = Authority.generateAuthority(authorityString, azureCloudOptions);
331
- const authorityOptions = {
332
- protocolMode: this.config.auth.protocolMode,
333
- knownAuthorities: this.config.auth.knownAuthorities,
334
- cloudDiscoveryMetadata: this.config.auth.cloudDiscoveryMetadata,
335
- authorityMetadata: this.config.auth.authorityMetadata,
336
- azureRegionConfiguration,
337
- skipAuthorityMetadataCache: this.config.auth.skipAuthorityMetadataCache,
338
- };
339
- return AuthorityFactory.createDiscoveredInstance(authorityUrl, this.config.system.networkClient, this.storage, authorityOptions, this.logger, requestCorrelationId);
340
- }
341
- /**
342
- * Clear the cache
343
- */
344
- clearCache() {
345
- void this.storage.clear();
346
- }
14
+ /*
15
+ * Copyright (c) Microsoft Corporation. All rights reserved.
16
+ * Licensed under the MIT License.
17
+ */
18
+ /**
19
+ * Base abstract class for all ClientApplications - public and confidential
20
+ * @public
21
+ */
22
+ class ClientApplication {
23
+ /**
24
+ * Constructor for the ClientApplication
25
+ */
26
+ constructor(configuration) {
27
+ this.config = buildAppConfiguration(configuration);
28
+ this.cryptoProvider = new CryptoProvider();
29
+ this.logger = new Logger(this.config.system.loggerOptions, name, version);
30
+ this.storage = new NodeStorage(this.logger, this.config.auth.clientId, this.cryptoProvider, buildStaticAuthorityOptions(this.config.auth));
31
+ this.tokenCache = new TokenCache(this.storage, this.logger, this.config.cache.cachePlugin);
32
+ }
33
+ /**
34
+ * Creates the URL of the authorization request, letting the user input credentials and consent to the
35
+ * application. The URL targets the /authorize endpoint of the authority configured in the
36
+ * application object.
37
+ *
38
+ * Once the user inputs their credentials and consents, the authority will send a response to the redirect URI
39
+ * sent in the request and should contain an authorization code, which can then be used to acquire tokens via
40
+ * `acquireTokenByCode(AuthorizationCodeRequest)`.
41
+ */
42
+ async getAuthCodeUrl(request) {
43
+ this.logger.info("getAuthCodeUrl called", request.correlationId);
44
+ const validRequest = {
45
+ ...request,
46
+ ...(await this.initializeBaseRequest(request)),
47
+ responseMode: request.responseMode || ResponseMode.QUERY,
48
+ authenticationScheme: AuthenticationScheme.BEARER,
49
+ };
50
+ const authClientConfig = await this.buildOauthClientConfiguration(validRequest.authority, validRequest.correlationId, undefined, undefined, request.azureCloudOptions);
51
+ const authorizationCodeClient = new AuthorizationCodeClient(authClientConfig);
52
+ this.logger.verbose("Auth code client created", validRequest.correlationId);
53
+ return authorizationCodeClient.getAuthCodeUrl(validRequest);
54
+ }
55
+ /**
56
+ * Acquires a token by exchanging the Authorization Code received from the first step of OAuth2.0
57
+ * Authorization Code flow.
58
+ *
59
+ * `getAuthCodeUrl(AuthorizationCodeUrlRequest)` can be used to create the URL for the first step of OAuth2.0
60
+ * Authorization Code flow. Ensure that values for redirectUri and scopes in AuthorizationCodeUrlRequest and
61
+ * AuthorizationCodeRequest are the same.
62
+ */
63
+ async acquireTokenByCode(request, authCodePayLoad) {
64
+ this.logger.info("acquireTokenByCode called");
65
+ if (request.state && authCodePayLoad) {
66
+ this.logger.info("acquireTokenByCode - validating state");
67
+ this.validateState(request.state, authCodePayLoad.state || "");
68
+ // eslint-disable-next-line no-param-reassign
69
+ authCodePayLoad = { ...authCodePayLoad, state: "" };
70
+ }
71
+ const validRequest = {
72
+ ...request,
73
+ ...(await this.initializeBaseRequest(request)),
74
+ authenticationScheme: AuthenticationScheme.BEARER,
75
+ };
76
+ const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.acquireTokenByCode, validRequest.correlationId);
77
+ try {
78
+ const authClientConfig = await this.buildOauthClientConfiguration(validRequest.authority, validRequest.correlationId, serverTelemetryManager, undefined, request.azureCloudOptions);
79
+ const authorizationCodeClient = new AuthorizationCodeClient(authClientConfig);
80
+ this.logger.verbose("Auth code client created", validRequest.correlationId);
81
+ return await authorizationCodeClient.acquireToken(validRequest, authCodePayLoad);
82
+ }
83
+ catch (e) {
84
+ if (e instanceof AuthError) {
85
+ e.setCorrelationId(validRequest.correlationId);
86
+ }
87
+ serverTelemetryManager.cacheFailedRequest(e);
88
+ throw e;
89
+ }
90
+ }
91
+ /**
92
+ * Acquires a token by exchanging the refresh token provided for a new set of tokens.
93
+ *
94
+ * This API is provided only for scenarios where you would like to migrate from ADAL to MSAL. Otherwise, it is
95
+ * recommended that you use `acquireTokenSilent()` for silent scenarios. When using `acquireTokenSilent()`, MSAL will
96
+ * handle the caching and refreshing of tokens automatically.
97
+ */
98
+ async acquireTokenByRefreshToken(request) {
99
+ this.logger.info("acquireTokenByRefreshToken called", request.correlationId);
100
+ const validRequest = {
101
+ ...request,
102
+ ...(await this.initializeBaseRequest(request)),
103
+ authenticationScheme: AuthenticationScheme.BEARER,
104
+ };
105
+ const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.acquireTokenByRefreshToken, validRequest.correlationId);
106
+ try {
107
+ const refreshTokenClientConfig = await this.buildOauthClientConfiguration(validRequest.authority, validRequest.correlationId, serverTelemetryManager, undefined, request.azureCloudOptions);
108
+ const refreshTokenClient = new RefreshTokenClient(refreshTokenClientConfig);
109
+ this.logger.verbose("Refresh token client created", validRequest.correlationId);
110
+ return await refreshTokenClient.acquireToken(validRequest);
111
+ }
112
+ catch (e) {
113
+ if (e instanceof AuthError) {
114
+ e.setCorrelationId(validRequest.correlationId);
115
+ }
116
+ serverTelemetryManager.cacheFailedRequest(e);
117
+ throw e;
118
+ }
119
+ }
120
+ /**
121
+ * Acquires a token silently when a user specifies the account the token is requested for.
122
+ *
123
+ * This API expects the user to provide an account object and looks into the cache to retrieve the token if present.
124
+ * There is also an optional "forceRefresh" boolean the user can send to bypass the cache for access_token and id_token.
125
+ * In case the refresh_token is expired or not found, an error is thrown
126
+ * and the guidance is for the user to call any interactive token acquisition API (eg: `acquireTokenByCode()`).
127
+ */
128
+ async acquireTokenSilent(request) {
129
+ const validRequest = {
130
+ ...request,
131
+ ...(await this.initializeBaseRequest(request)),
132
+ forceRefresh: request.forceRefresh || false,
133
+ };
134
+ const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.acquireTokenSilent, validRequest.correlationId, validRequest.forceRefresh);
135
+ try {
136
+ const silentFlowClientConfig = await this.buildOauthClientConfiguration(validRequest.authority, validRequest.correlationId, serverTelemetryManager, undefined, request.azureCloudOptions);
137
+ const silentFlowClient = new SilentFlowClient(silentFlowClientConfig);
138
+ this.logger.verbose("Silent flow client created", validRequest.correlationId);
139
+ return await silentFlowClient.acquireToken(validRequest);
140
+ }
141
+ catch (e) {
142
+ if (e instanceof AuthError) {
143
+ e.setCorrelationId(validRequest.correlationId);
144
+ }
145
+ serverTelemetryManager.cacheFailedRequest(e);
146
+ throw e;
147
+ }
148
+ }
149
+ /**
150
+ * Acquires tokens with password grant by exchanging client applications username and password for credentials
151
+ *
152
+ * The latest OAuth 2.0 Security Best Current Practice disallows the password grant entirely.
153
+ * More details on this recommendation at https://tools.ietf.org/html/draft-ietf-oauth-security-topics-13#section-3.4
154
+ * Microsoft's documentation and recommendations are at:
155
+ * https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows#usernamepassword
156
+ *
157
+ * @param request - UsenamePasswordRequest
158
+ */
159
+ async acquireTokenByUsernamePassword(request) {
160
+ this.logger.info("acquireTokenByUsernamePassword called", request.correlationId);
161
+ const validRequest = {
162
+ ...request,
163
+ ...(await this.initializeBaseRequest(request)),
164
+ };
165
+ const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.acquireTokenByUsernamePassword, validRequest.correlationId);
166
+ try {
167
+ const usernamePasswordClientConfig = await this.buildOauthClientConfiguration(validRequest.authority, validRequest.correlationId, serverTelemetryManager, undefined, request.azureCloudOptions);
168
+ const usernamePasswordClient = new UsernamePasswordClient(usernamePasswordClientConfig);
169
+ this.logger.verbose("Username password client created", validRequest.correlationId);
170
+ return await usernamePasswordClient.acquireToken(validRequest);
171
+ }
172
+ catch (e) {
173
+ if (e instanceof AuthError) {
174
+ e.setCorrelationId(validRequest.correlationId);
175
+ }
176
+ serverTelemetryManager.cacheFailedRequest(e);
177
+ throw e;
178
+ }
179
+ }
180
+ /**
181
+ * Gets the token cache for the application.
182
+ */
183
+ getTokenCache() {
184
+ this.logger.info("getTokenCache called");
185
+ return this.tokenCache;
186
+ }
187
+ /**
188
+ * Validates OIDC state by comparing the user cached state with the state received from the server.
189
+ *
190
+ * This API is provided for scenarios where you would use OAuth2.0 state parameter to mitigate against
191
+ * CSRF attacks.
192
+ * For more information about state, visit https://datatracker.ietf.org/doc/html/rfc6819#section-3.6.
193
+ * @param state
194
+ * @param cachedState
195
+ */
196
+ validateState(state, cachedState) {
197
+ if (!state) {
198
+ throw NodeAuthError.createStateNotFoundError();
199
+ }
200
+ if (state !== cachedState) {
201
+ throw createClientAuthError(ClientAuthErrorCodes.stateMismatch);
202
+ }
203
+ }
204
+ /**
205
+ * Returns the logger instance
206
+ */
207
+ getLogger() {
208
+ return this.logger;
209
+ }
210
+ /**
211
+ * Replaces the default logger set in configurations with new Logger with new configurations
212
+ * @param logger - Logger instance
213
+ */
214
+ setLogger(logger) {
215
+ this.logger = logger;
216
+ }
217
+ /**
218
+ * Builds the common configuration to be passed to the common component based on the platform configurarion
219
+ * @param authority - user passed authority in configuration
220
+ * @param serverTelemetryManager - initializes servertelemetry if passed
221
+ */
222
+ async buildOauthClientConfiguration(authority, requestCorrelationId, serverTelemetryManager, azureRegionConfiguration, azureCloudOptions) {
223
+ this.logger.verbose("buildOauthClientConfiguration called", requestCorrelationId);
224
+ // precedence - azureCloudInstance + tenant >> authority and request >> config
225
+ const userAzureCloudOptions = azureCloudOptions
226
+ ? azureCloudOptions
227
+ : this.config.auth.azureCloudOptions;
228
+ // using null assertion operator as we ensure that all config values have default values in buildConfiguration()
229
+ const discoveredAuthority = await this.createAuthority(authority, requestCorrelationId, azureRegionConfiguration, userAzureCloudOptions);
230
+ this.logger.info(`Building oauth client configuration with the following authority: ${discoveredAuthority.tokenEndpoint}.`, requestCorrelationId);
231
+ serverTelemetryManager?.updateRegionDiscoveryMetadata(discoveredAuthority.regionDiscoveryMetadata);
232
+ const clientConfiguration = {
233
+ authOptions: {
234
+ clientId: this.config.auth.clientId,
235
+ authority: discoveredAuthority,
236
+ clientCapabilities: this.config.auth.clientCapabilities,
237
+ },
238
+ loggerOptions: {
239
+ logLevel: this.config.system.loggerOptions.logLevel,
240
+ loggerCallback: this.config.system.loggerOptions.loggerCallback,
241
+ piiLoggingEnabled: this.config.system.loggerOptions.piiLoggingEnabled,
242
+ correlationId: requestCorrelationId,
243
+ },
244
+ cacheOptions: {
245
+ claimsBasedCachingEnabled: this.config.cache.claimsBasedCachingEnabled,
246
+ },
247
+ cryptoInterface: this.cryptoProvider,
248
+ networkInterface: this.config.system.networkClient,
249
+ storageInterface: this.storage,
250
+ serverTelemetryManager: serverTelemetryManager,
251
+ clientCredentials: {
252
+ clientSecret: this.clientSecret,
253
+ clientAssertion: await this.getClientAssertion(discoveredAuthority),
254
+ },
255
+ libraryInfo: {
256
+ sku: Constants.MSAL_SKU,
257
+ version: version,
258
+ cpu: process.arch || Constants$1.EMPTY_STRING,
259
+ os: process.platform || Constants$1.EMPTY_STRING,
260
+ },
261
+ telemetry: this.config.telemetry,
262
+ persistencePlugin: this.config.cache.cachePlugin,
263
+ serializableCache: this.tokenCache,
264
+ };
265
+ return clientConfiguration;
266
+ }
267
+ async getClientAssertion(authority) {
268
+ if (this.developerProvidedClientAssertion) {
269
+ this.clientAssertion = ClientAssertion.fromAssertion(await getClientAssertion(this.developerProvidedClientAssertion, this.config.auth.clientId, authority.tokenEndpoint));
270
+ }
271
+ return (this.clientAssertion && {
272
+ assertion: this.clientAssertion.getJwt(this.cryptoProvider, this.config.auth.clientId, authority.tokenEndpoint),
273
+ assertionType: Constants.JWT_BEARER_ASSERTION_TYPE,
274
+ });
275
+ }
276
+ /**
277
+ * Generates a request with the default scopes & generates a correlationId.
278
+ * @param authRequest - BaseAuthRequest for initialization
279
+ */
280
+ async initializeBaseRequest(authRequest) {
281
+ this.logger.verbose("initializeRequestScopes called", authRequest.correlationId);
282
+ // Default authenticationScheme to Bearer, log that POP isn't supported yet
283
+ if (authRequest.authenticationScheme &&
284
+ authRequest.authenticationScheme === AuthenticationScheme.POP) {
285
+ this.logger.verbose("Authentication Scheme 'pop' is not supported yet, setting Authentication Scheme to 'Bearer' for request", authRequest.correlationId);
286
+ }
287
+ authRequest.authenticationScheme = AuthenticationScheme.BEARER;
288
+ // Set requested claims hash if claims-based caching is enabled and claims were requested
289
+ if (this.config.cache.claimsBasedCachingEnabled &&
290
+ authRequest.claims &&
291
+ // Checks for empty stringified object "{}" which doesn't qualify as requested claims
292
+ !StringUtils.isEmptyObj(authRequest.claims)) {
293
+ authRequest.requestedClaimsHash =
294
+ await this.cryptoProvider.hashString(authRequest.claims);
295
+ }
296
+ return {
297
+ ...authRequest,
298
+ scopes: [
299
+ ...((authRequest && authRequest.scopes) || []),
300
+ ...OIDC_DEFAULT_SCOPES,
301
+ ],
302
+ correlationId: (authRequest && authRequest.correlationId) ||
303
+ this.cryptoProvider.createNewGuid(),
304
+ authority: authRequest.authority || this.config.auth.authority,
305
+ };
306
+ }
307
+ /**
308
+ * Initializes the server telemetry payload
309
+ * @param apiId - Id for a specific request
310
+ * @param correlationId - GUID
311
+ * @param forceRefresh - boolean to indicate network call
312
+ */
313
+ initializeServerTelemetryManager(apiId, correlationId, forceRefresh) {
314
+ const telemetryPayload = {
315
+ clientId: this.config.auth.clientId,
316
+ correlationId: correlationId,
317
+ apiId: apiId,
318
+ forceRefresh: forceRefresh || false,
319
+ };
320
+ return new ServerTelemetryManager(telemetryPayload, this.storage);
321
+ }
322
+ /**
323
+ * Create authority instance. If authority not passed in request, default to authority set on the application
324
+ * object. If no authority set in application object, then default to common authority.
325
+ * @param authorityString - authority from user configuration
326
+ */
327
+ async createAuthority(authorityString, requestCorrelationId, azureRegionConfiguration, azureCloudOptions) {
328
+ this.logger.verbose("createAuthority called", requestCorrelationId);
329
+ // build authority string based on auth params - azureCloudInstance is prioritized if provided
330
+ const authorityUrl = Authority.generateAuthority(authorityString, azureCloudOptions);
331
+ const authorityOptions = {
332
+ protocolMode: this.config.auth.protocolMode,
333
+ knownAuthorities: this.config.auth.knownAuthorities,
334
+ cloudDiscoveryMetadata: this.config.auth.cloudDiscoveryMetadata,
335
+ authorityMetadata: this.config.auth.authorityMetadata,
336
+ azureRegionConfiguration,
337
+ skipAuthorityMetadataCache: this.config.auth.skipAuthorityMetadataCache,
338
+ };
339
+ return AuthorityFactory.createDiscoveredInstance(authorityUrl, this.config.system.networkClient, this.storage, authorityOptions, this.logger, requestCorrelationId);
340
+ }
341
+ /**
342
+ * Clear the cache
343
+ */
344
+ clearCache() {
345
+ void this.storage.clear();
346
+ }
347
347
  }
348
348
 
349
349
  export { ClientApplication };