@azure/msal-node 2.11.1 → 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 (200) 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 +240 -229
  88. package/dist/client/PublicClientApplication.cjs.map +1 -1
  89. package/dist/client/PublicClientApplication.d.ts +72 -71
  90. package/dist/client/PublicClientApplication.d.ts.map +1 -1
  91. package/dist/client/PublicClientApplication.mjs +240 -231
  92. package/dist/client/PublicClientApplication.mjs.map +1 -1
  93. package/dist/client/UsernamePasswordClient.cjs +97 -95
  94. package/dist/client/UsernamePasswordClient.cjs.map +1 -1
  95. package/dist/client/UsernamePasswordClient.d.ts +25 -25
  96. package/dist/client/UsernamePasswordClient.mjs +95 -95
  97. package/dist/config/Configuration.cjs +108 -106
  98. package/dist/config/Configuration.cjs.map +1 -1
  99. package/dist/config/Configuration.d.ts +131 -131
  100. package/dist/config/Configuration.mjs +106 -106
  101. package/dist/config/ManagedIdentityId.cjs +50 -48
  102. package/dist/config/ManagedIdentityId.cjs.map +1 -1
  103. package/dist/config/ManagedIdentityId.d.ts +11 -11
  104. package/dist/config/ManagedIdentityId.mjs +48 -48
  105. package/dist/config/ManagedIdentityRequestParameters.cjs +30 -28
  106. package/dist/config/ManagedIdentityRequestParameters.cjs.map +1 -1
  107. package/dist/config/ManagedIdentityRequestParameters.d.ts +11 -11
  108. package/dist/config/ManagedIdentityRequestParameters.mjs +28 -28
  109. package/dist/crypto/CryptoProvider.cjs +91 -89
  110. package/dist/crypto/CryptoProvider.cjs.map +1 -1
  111. package/dist/crypto/CryptoProvider.d.ts +62 -62
  112. package/dist/crypto/CryptoProvider.mjs +89 -89
  113. package/dist/crypto/GuidGenerator.cjs +24 -22
  114. package/dist/crypto/GuidGenerator.cjs.map +1 -1
  115. package/dist/crypto/GuidGenerator.d.ts +14 -14
  116. package/dist/crypto/GuidGenerator.mjs +22 -22
  117. package/dist/crypto/HashUtils.cjs +19 -13
  118. package/dist/crypto/HashUtils.cjs.map +1 -1
  119. package/dist/crypto/HashUtils.d.ts +8 -8
  120. package/dist/crypto/HashUtils.mjs +13 -13
  121. package/dist/crypto/PkceGenerator.cjs +55 -49
  122. package/dist/crypto/PkceGenerator.cjs.map +1 -1
  123. package/dist/crypto/PkceGenerator.d.ts +22 -22
  124. package/dist/crypto/PkceGenerator.mjs +49 -49
  125. package/dist/error/ManagedIdentityError.cjs +43 -41
  126. package/dist/error/ManagedIdentityError.cjs.map +1 -1
  127. package/dist/error/ManagedIdentityError.d.ts +30 -30
  128. package/dist/error/ManagedIdentityError.mjs +41 -41
  129. package/dist/error/ManagedIdentityErrorCodes.cjs +26 -24
  130. package/dist/error/ManagedIdentityErrorCodes.cjs.map +1 -1
  131. package/dist/error/ManagedIdentityErrorCodes.d.ts +22 -22
  132. package/dist/error/ManagedIdentityErrorCodes.mjs +24 -24
  133. package/dist/error/NodeAuthError.cjs +97 -95
  134. package/dist/error/NodeAuthError.cjs.map +1 -1
  135. package/dist/error/NodeAuthError.d.ts +73 -73
  136. package/dist/error/NodeAuthError.mjs +95 -95
  137. package/dist/index.cjs +3 -1
  138. package/dist/index.cjs.map +1 -1
  139. package/dist/index.d.ts +45 -45
  140. package/dist/index.mjs +1 -1
  141. package/dist/internals.cjs +6 -8
  142. package/dist/internals.cjs.map +1 -1
  143. package/dist/internals.d.ts +6 -6
  144. package/dist/internals.mjs +4 -8
  145. package/dist/internals.mjs.map +1 -1
  146. package/dist/network/HttpClient.cjs +288 -281
  147. package/dist/network/HttpClient.cjs.map +1 -1
  148. package/dist/network/HttpClient.d.ts +25 -25
  149. package/dist/network/HttpClient.mjs +281 -281
  150. package/dist/network/HttpClientWithRetries.cjs +36 -34
  151. package/dist/network/HttpClientWithRetries.cjs.map +1 -1
  152. package/dist/network/HttpClientWithRetries.d.ts +11 -11
  153. package/dist/network/HttpClientWithRetries.mjs +34 -34
  154. package/dist/network/ILoopbackClient.d.ts +10 -10
  155. package/dist/network/LoopbackClient.cjs +87 -77
  156. package/dist/network/LoopbackClient.cjs.map +1 -1
  157. package/dist/network/LoopbackClient.d.ts +21 -21
  158. package/dist/network/LoopbackClient.d.ts.map +1 -1
  159. package/dist/network/LoopbackClient.mjs +81 -77
  160. package/dist/network/LoopbackClient.mjs.map +1 -1
  161. package/dist/packageMetadata.cjs +6 -4
  162. package/dist/packageMetadata.cjs.map +1 -1
  163. package/dist/packageMetadata.d.ts +2 -2
  164. package/dist/packageMetadata.mjs +4 -4
  165. package/dist/request/AuthorizationCodeRequest.d.ts +21 -21
  166. package/dist/request/AuthorizationUrlRequest.d.ts +33 -33
  167. package/dist/request/ClientCredentialRequest.d.ts +14 -14
  168. package/dist/request/DeviceCodeRequest.d.ts +15 -15
  169. package/dist/request/InteractiveRequest.d.ts +22 -22
  170. package/dist/request/ManagedIdentityRequest.d.ts +8 -8
  171. package/dist/request/ManagedIdentityRequestParams.d.ts +9 -9
  172. package/dist/request/OnBehalfOfRequest.d.ts +14 -14
  173. package/dist/request/RefreshTokenRequest.d.ts +17 -17
  174. package/dist/request/SignOutRequest.d.ts +5 -5
  175. package/dist/request/SilentFlowRequest.d.ts +16 -16
  176. package/dist/request/UsernamePasswordRequest.d.ts +19 -19
  177. package/dist/response/ManagedIdentityTokenResponse.d.ts +34 -34
  178. package/dist/retry/IHttpRetryPolicy.d.ts +5 -5
  179. package/dist/retry/LinearRetryPolicy.cjs +42 -40
  180. package/dist/retry/LinearRetryPolicy.cjs.map +1 -1
  181. package/dist/retry/LinearRetryPolicy.d.ts +11 -11
  182. package/dist/retry/LinearRetryPolicy.mjs +40 -40
  183. package/dist/utils/Constants.cjs +138 -136
  184. package/dist/utils/Constants.cjs.map +1 -1
  185. package/dist/utils/Constants.d.ts +141 -141
  186. package/dist/utils/Constants.mjs +136 -136
  187. package/dist/utils/EncodingUtils.cjs +46 -44
  188. package/dist/utils/EncodingUtils.cjs.map +1 -1
  189. package/dist/utils/EncodingUtils.d.ts +26 -26
  190. package/dist/utils/EncodingUtils.mjs +44 -44
  191. package/dist/utils/NetworkUtils.cjs +40 -38
  192. package/dist/utils/NetworkUtils.cjs.map +1 -1
  193. package/dist/utils/NetworkUtils.d.ts +5 -5
  194. package/dist/utils/NetworkUtils.mjs +38 -38
  195. package/package.json +2 -2
  196. package/src/client/ConfidentialClientApplication.ts +3 -3
  197. package/src/client/ManagedIdentitySources/AzureArc.ts +80 -25
  198. package/src/client/PublicClientApplication.ts +13 -1
  199. package/src/network/LoopbackClient.ts +6 -0
  200. package/src/packageMetadata.ts +1 -1
@@ -1,200 +1,200 @@
1
- /*! @azure/msal-node v2.11.1 2024-07-16 */
1
+ /*! @azure/msal-node v2.13.0 2024-08-13 */
2
2
  'use strict';
3
3
  import { BaseClient, CacheOutcome, TokenCacheContext, ScopeSet, TimeUtils, DEFAULT_TOKEN_RENEWAL_OFFSET_SEC, ResponseHandler, Constants, CredentialType, createClientAuthError, ClientAuthErrorCodes, UrlString, RequestParameterBuilder, GrantType, getClientAssertion, StringUtils, AuthenticationScheme } from '@azure/msal-common';
4
4
 
5
- /*
6
- * Copyright (c) Microsoft Corporation. All rights reserved.
7
- * Licensed under the MIT License.
8
- */
9
- /**
10
- * OAuth2.0 client credential grant
11
- */
12
- class ClientCredentialClient extends BaseClient {
13
- constructor(configuration, appTokenProvider) {
14
- super(configuration);
15
- this.appTokenProvider = appTokenProvider;
16
- }
17
- /**
18
- * Public API to acquire a token with ClientCredential Flow for Confidential clients
19
- * @param request
20
- */
21
- async acquireToken(request) {
22
- if (request.skipCache || request.claims) {
23
- return this.executeTokenRequest(request, this.authority);
24
- }
25
- const [cachedAuthenticationResult, lastCacheOutcome] = await this.getCachedAuthenticationResult(request, this.config, this.cryptoUtils, this.authority, this.cacheManager, this.serverTelemetryManager);
26
- if (cachedAuthenticationResult) {
27
- // if the token is not expired but must be refreshed; get a new one in the background
28
- if (lastCacheOutcome === CacheOutcome.PROACTIVELY_REFRESHED) {
29
- this.logger.info("ClientCredentialClient:getCachedAuthenticationResult - Cached access token's refreshOn property has been exceeded'. It's not expired, but must be refreshed.");
30
- // refresh the access token in the background
31
- const refreshAccessToken = true;
32
- await this.executeTokenRequest(request, this.authority, refreshAccessToken);
33
- }
34
- // return the cached token
35
- return cachedAuthenticationResult;
36
- }
37
- else {
38
- return this.executeTokenRequest(request, this.authority);
39
- }
40
- }
41
- /**
42
- * looks up cache if the tokens are cached already
43
- */
44
- async getCachedAuthenticationResult(request, config, cryptoUtils, authority, cacheManager, serverTelemetryManager) {
45
- const clientConfiguration = config;
46
- const managedIdentityConfiguration = config;
47
- let lastCacheOutcome = CacheOutcome.NOT_APPLICABLE;
48
- // read the user-supplied cache into memory, if applicable
49
- let cacheContext;
50
- if (clientConfiguration.serializableCache &&
51
- clientConfiguration.persistencePlugin) {
52
- cacheContext = new TokenCacheContext(clientConfiguration.serializableCache, false);
53
- await clientConfiguration.persistencePlugin.beforeCacheAccess(cacheContext);
54
- }
55
- const cachedAccessToken = this.readAccessTokenFromCache(authority, managedIdentityConfiguration.managedIdentityId?.id ||
56
- clientConfiguration.authOptions.clientId, new ScopeSet(request.scopes || []), cacheManager);
57
- if (clientConfiguration.serializableCache &&
58
- clientConfiguration.persistencePlugin &&
59
- cacheContext) {
60
- await clientConfiguration.persistencePlugin.afterCacheAccess(cacheContext);
61
- }
62
- // must refresh due to non-existent access_token
63
- if (!cachedAccessToken) {
64
- serverTelemetryManager?.setCacheOutcome(CacheOutcome.NO_CACHED_ACCESS_TOKEN);
65
- return [null, CacheOutcome.NO_CACHED_ACCESS_TOKEN];
66
- }
67
- // must refresh due to the expires_in value
68
- if (TimeUtils.isTokenExpired(cachedAccessToken.expiresOn, clientConfiguration.systemOptions?.tokenRenewalOffsetSeconds ||
69
- DEFAULT_TOKEN_RENEWAL_OFFSET_SEC)) {
70
- serverTelemetryManager?.setCacheOutcome(CacheOutcome.CACHED_ACCESS_TOKEN_EXPIRED);
71
- return [null, CacheOutcome.CACHED_ACCESS_TOKEN_EXPIRED];
72
- }
73
- // must refresh (in the background) due to the refresh_in value
74
- if (cachedAccessToken.refreshOn &&
75
- TimeUtils.isTokenExpired(cachedAccessToken.refreshOn.toString(), 0)) {
76
- lastCacheOutcome = CacheOutcome.PROACTIVELY_REFRESHED;
77
- serverTelemetryManager?.setCacheOutcome(CacheOutcome.PROACTIVELY_REFRESHED);
78
- }
79
- return [
80
- await ResponseHandler.generateAuthenticationResult(cryptoUtils, authority, {
81
- account: null,
82
- idToken: null,
83
- accessToken: cachedAccessToken,
84
- refreshToken: null,
85
- appMetadata: null,
86
- }, true, request),
87
- lastCacheOutcome,
88
- ];
89
- }
90
- /**
91
- * Reads access token from the cache
92
- */
93
- readAccessTokenFromCache(authority, id, scopeSet, cacheManager) {
94
- const accessTokenFilter = {
95
- homeAccountId: Constants.EMPTY_STRING,
96
- environment: authority.canonicalAuthorityUrlComponents.HostNameAndPort,
97
- credentialType: CredentialType.ACCESS_TOKEN,
98
- clientId: id,
99
- realm: authority.tenant,
100
- target: ScopeSet.createSearchScopes(scopeSet.asArray()),
101
- };
102
- const accessTokens = cacheManager.getAccessTokensByFilter(accessTokenFilter);
103
- if (accessTokens.length < 1) {
104
- return null;
105
- }
106
- else if (accessTokens.length > 1) {
107
- throw createClientAuthError(ClientAuthErrorCodes.multipleMatchingTokens);
108
- }
109
- return accessTokens[0];
110
- }
111
- /**
112
- * Makes a network call to request the token from the service
113
- * @param request
114
- * @param authority
115
- */
116
- async executeTokenRequest(request, authority, refreshAccessToken) {
117
- let serverTokenResponse;
118
- let reqTimestamp;
119
- if (this.appTokenProvider) {
120
- this.logger.info("Using appTokenProvider extensibility.");
121
- const appTokenPropviderParameters = {
122
- correlationId: request.correlationId,
123
- tenantId: this.config.authOptions.authority.tenant,
124
- scopes: request.scopes,
125
- claims: request.claims,
126
- };
127
- reqTimestamp = TimeUtils.nowSeconds();
128
- const appTokenProviderResult = await this.appTokenProvider(appTokenPropviderParameters);
129
- serverTokenResponse = {
130
- access_token: appTokenProviderResult.accessToken,
131
- expires_in: appTokenProviderResult.expiresInSeconds,
132
- refresh_in: appTokenProviderResult.refreshInSeconds,
133
- token_type: AuthenticationScheme.BEARER,
134
- };
135
- }
136
- else {
137
- const queryParametersString = this.createTokenQueryParameters(request);
138
- const endpoint = UrlString.appendQueryString(authority.tokenEndpoint, queryParametersString);
139
- const requestBody = await this.createTokenRequestBody(request);
140
- const headers = this.createTokenRequestHeaders();
141
- const thumbprint = {
142
- clientId: this.config.authOptions.clientId,
143
- authority: request.authority,
144
- scopes: request.scopes,
145
- claims: request.claims,
146
- authenticationScheme: request.authenticationScheme,
147
- resourceRequestMethod: request.resourceRequestMethod,
148
- resourceRequestUri: request.resourceRequestUri,
149
- shrClaims: request.shrClaims,
150
- sshKid: request.sshKid,
151
- };
152
- this.logger.info("Sending token request to endpoint: " + authority.tokenEndpoint);
153
- reqTimestamp = TimeUtils.nowSeconds();
154
- const response = await this.executePostToTokenEndpoint(endpoint, requestBody, headers, thumbprint, request.correlationId);
155
- serverTokenResponse = response.body;
156
- serverTokenResponse.status = response.status;
157
- }
158
- const responseHandler = new ResponseHandler(this.config.authOptions.clientId, this.cacheManager, this.cryptoUtils, this.logger, this.config.serializableCache, this.config.persistencePlugin);
159
- responseHandler.validateTokenResponse(serverTokenResponse, refreshAccessToken);
160
- const tokenResponse = await responseHandler.handleServerTokenResponse(serverTokenResponse, this.authority, reqTimestamp, request);
161
- return tokenResponse;
162
- }
163
- /**
164
- * generate the request to the server in the acceptable format
165
- * @param request
166
- */
167
- async createTokenRequestBody(request) {
168
- const parameterBuilder = new RequestParameterBuilder();
169
- parameterBuilder.addClientId(this.config.authOptions.clientId);
170
- parameterBuilder.addScopes(request.scopes, false);
171
- parameterBuilder.addGrantType(GrantType.CLIENT_CREDENTIALS_GRANT);
172
- parameterBuilder.addLibraryInfo(this.config.libraryInfo);
173
- parameterBuilder.addApplicationTelemetry(this.config.telemetry.application);
174
- parameterBuilder.addThrottling();
175
- if (this.serverTelemetryManager) {
176
- parameterBuilder.addServerTelemetry(this.serverTelemetryManager);
177
- }
178
- const correlationId = request.correlationId ||
179
- this.config.cryptoInterface.createNewGuid();
180
- parameterBuilder.addCorrelationId(correlationId);
181
- if (this.config.clientCredentials.clientSecret) {
182
- parameterBuilder.addClientSecret(this.config.clientCredentials.clientSecret);
183
- }
184
- // Use clientAssertion from request, fallback to client assertion in base configuration
185
- const clientAssertion = request.clientAssertion ||
186
- this.config.clientCredentials.clientAssertion;
187
- if (clientAssertion) {
188
- parameterBuilder.addClientAssertion(await getClientAssertion(clientAssertion.assertion, this.config.authOptions.clientId, request.resourceRequestUri));
189
- parameterBuilder.addClientAssertionType(clientAssertion.assertionType);
190
- }
191
- if (!StringUtils.isEmptyObj(request.claims) ||
192
- (this.config.authOptions.clientCapabilities &&
193
- this.config.authOptions.clientCapabilities.length > 0)) {
194
- parameterBuilder.addClaims(request.claims, this.config.authOptions.clientCapabilities);
195
- }
196
- return parameterBuilder.createQueryString();
197
- }
5
+ /*
6
+ * Copyright (c) Microsoft Corporation. All rights reserved.
7
+ * Licensed under the MIT License.
8
+ */
9
+ /**
10
+ * OAuth2.0 client credential grant
11
+ */
12
+ class ClientCredentialClient extends BaseClient {
13
+ constructor(configuration, appTokenProvider) {
14
+ super(configuration);
15
+ this.appTokenProvider = appTokenProvider;
16
+ }
17
+ /**
18
+ * Public API to acquire a token with ClientCredential Flow for Confidential clients
19
+ * @param request
20
+ */
21
+ async acquireToken(request) {
22
+ if (request.skipCache || request.claims) {
23
+ return this.executeTokenRequest(request, this.authority);
24
+ }
25
+ const [cachedAuthenticationResult, lastCacheOutcome] = await this.getCachedAuthenticationResult(request, this.config, this.cryptoUtils, this.authority, this.cacheManager, this.serverTelemetryManager);
26
+ if (cachedAuthenticationResult) {
27
+ // if the token is not expired but must be refreshed; get a new one in the background
28
+ if (lastCacheOutcome === CacheOutcome.PROACTIVELY_REFRESHED) {
29
+ this.logger.info("ClientCredentialClient:getCachedAuthenticationResult - Cached access token's refreshOn property has been exceeded'. It's not expired, but must be refreshed.");
30
+ // refresh the access token in the background
31
+ const refreshAccessToken = true;
32
+ await this.executeTokenRequest(request, this.authority, refreshAccessToken);
33
+ }
34
+ // return the cached token
35
+ return cachedAuthenticationResult;
36
+ }
37
+ else {
38
+ return this.executeTokenRequest(request, this.authority);
39
+ }
40
+ }
41
+ /**
42
+ * looks up cache if the tokens are cached already
43
+ */
44
+ async getCachedAuthenticationResult(request, config, cryptoUtils, authority, cacheManager, serverTelemetryManager) {
45
+ const clientConfiguration = config;
46
+ const managedIdentityConfiguration = config;
47
+ let lastCacheOutcome = CacheOutcome.NOT_APPLICABLE;
48
+ // read the user-supplied cache into memory, if applicable
49
+ let cacheContext;
50
+ if (clientConfiguration.serializableCache &&
51
+ clientConfiguration.persistencePlugin) {
52
+ cacheContext = new TokenCacheContext(clientConfiguration.serializableCache, false);
53
+ await clientConfiguration.persistencePlugin.beforeCacheAccess(cacheContext);
54
+ }
55
+ const cachedAccessToken = this.readAccessTokenFromCache(authority, managedIdentityConfiguration.managedIdentityId?.id ||
56
+ clientConfiguration.authOptions.clientId, new ScopeSet(request.scopes || []), cacheManager);
57
+ if (clientConfiguration.serializableCache &&
58
+ clientConfiguration.persistencePlugin &&
59
+ cacheContext) {
60
+ await clientConfiguration.persistencePlugin.afterCacheAccess(cacheContext);
61
+ }
62
+ // must refresh due to non-existent access_token
63
+ if (!cachedAccessToken) {
64
+ serverTelemetryManager?.setCacheOutcome(CacheOutcome.NO_CACHED_ACCESS_TOKEN);
65
+ return [null, CacheOutcome.NO_CACHED_ACCESS_TOKEN];
66
+ }
67
+ // must refresh due to the expires_in value
68
+ if (TimeUtils.isTokenExpired(cachedAccessToken.expiresOn, clientConfiguration.systemOptions?.tokenRenewalOffsetSeconds ||
69
+ DEFAULT_TOKEN_RENEWAL_OFFSET_SEC)) {
70
+ serverTelemetryManager?.setCacheOutcome(CacheOutcome.CACHED_ACCESS_TOKEN_EXPIRED);
71
+ return [null, CacheOutcome.CACHED_ACCESS_TOKEN_EXPIRED];
72
+ }
73
+ // must refresh (in the background) due to the refresh_in value
74
+ if (cachedAccessToken.refreshOn &&
75
+ TimeUtils.isTokenExpired(cachedAccessToken.refreshOn.toString(), 0)) {
76
+ lastCacheOutcome = CacheOutcome.PROACTIVELY_REFRESHED;
77
+ serverTelemetryManager?.setCacheOutcome(CacheOutcome.PROACTIVELY_REFRESHED);
78
+ }
79
+ return [
80
+ await ResponseHandler.generateAuthenticationResult(cryptoUtils, authority, {
81
+ account: null,
82
+ idToken: null,
83
+ accessToken: cachedAccessToken,
84
+ refreshToken: null,
85
+ appMetadata: null,
86
+ }, true, request),
87
+ lastCacheOutcome,
88
+ ];
89
+ }
90
+ /**
91
+ * Reads access token from the cache
92
+ */
93
+ readAccessTokenFromCache(authority, id, scopeSet, cacheManager) {
94
+ const accessTokenFilter = {
95
+ homeAccountId: Constants.EMPTY_STRING,
96
+ environment: authority.canonicalAuthorityUrlComponents.HostNameAndPort,
97
+ credentialType: CredentialType.ACCESS_TOKEN,
98
+ clientId: id,
99
+ realm: authority.tenant,
100
+ target: ScopeSet.createSearchScopes(scopeSet.asArray()),
101
+ };
102
+ const accessTokens = cacheManager.getAccessTokensByFilter(accessTokenFilter);
103
+ if (accessTokens.length < 1) {
104
+ return null;
105
+ }
106
+ else if (accessTokens.length > 1) {
107
+ throw createClientAuthError(ClientAuthErrorCodes.multipleMatchingTokens);
108
+ }
109
+ return accessTokens[0];
110
+ }
111
+ /**
112
+ * Makes a network call to request the token from the service
113
+ * @param request
114
+ * @param authority
115
+ */
116
+ async executeTokenRequest(request, authority, refreshAccessToken) {
117
+ let serverTokenResponse;
118
+ let reqTimestamp;
119
+ if (this.appTokenProvider) {
120
+ this.logger.info("Using appTokenProvider extensibility.");
121
+ const appTokenPropviderParameters = {
122
+ correlationId: request.correlationId,
123
+ tenantId: this.config.authOptions.authority.tenant,
124
+ scopes: request.scopes,
125
+ claims: request.claims,
126
+ };
127
+ reqTimestamp = TimeUtils.nowSeconds();
128
+ const appTokenProviderResult = await this.appTokenProvider(appTokenPropviderParameters);
129
+ serverTokenResponse = {
130
+ access_token: appTokenProviderResult.accessToken,
131
+ expires_in: appTokenProviderResult.expiresInSeconds,
132
+ refresh_in: appTokenProviderResult.refreshInSeconds,
133
+ token_type: AuthenticationScheme.BEARER,
134
+ };
135
+ }
136
+ else {
137
+ const queryParametersString = this.createTokenQueryParameters(request);
138
+ const endpoint = UrlString.appendQueryString(authority.tokenEndpoint, queryParametersString);
139
+ const requestBody = await this.createTokenRequestBody(request);
140
+ const headers = this.createTokenRequestHeaders();
141
+ const thumbprint = {
142
+ clientId: this.config.authOptions.clientId,
143
+ authority: request.authority,
144
+ scopes: request.scopes,
145
+ claims: request.claims,
146
+ authenticationScheme: request.authenticationScheme,
147
+ resourceRequestMethod: request.resourceRequestMethod,
148
+ resourceRequestUri: request.resourceRequestUri,
149
+ shrClaims: request.shrClaims,
150
+ sshKid: request.sshKid,
151
+ };
152
+ this.logger.info("Sending token request to endpoint: " + authority.tokenEndpoint);
153
+ reqTimestamp = TimeUtils.nowSeconds();
154
+ const response = await this.executePostToTokenEndpoint(endpoint, requestBody, headers, thumbprint, request.correlationId);
155
+ serverTokenResponse = response.body;
156
+ serverTokenResponse.status = response.status;
157
+ }
158
+ const responseHandler = new ResponseHandler(this.config.authOptions.clientId, this.cacheManager, this.cryptoUtils, this.logger, this.config.serializableCache, this.config.persistencePlugin);
159
+ responseHandler.validateTokenResponse(serverTokenResponse, refreshAccessToken);
160
+ const tokenResponse = await responseHandler.handleServerTokenResponse(serverTokenResponse, this.authority, reqTimestamp, request);
161
+ return tokenResponse;
162
+ }
163
+ /**
164
+ * generate the request to the server in the acceptable format
165
+ * @param request
166
+ */
167
+ async createTokenRequestBody(request) {
168
+ const parameterBuilder = new RequestParameterBuilder();
169
+ parameterBuilder.addClientId(this.config.authOptions.clientId);
170
+ parameterBuilder.addScopes(request.scopes, false);
171
+ parameterBuilder.addGrantType(GrantType.CLIENT_CREDENTIALS_GRANT);
172
+ parameterBuilder.addLibraryInfo(this.config.libraryInfo);
173
+ parameterBuilder.addApplicationTelemetry(this.config.telemetry.application);
174
+ parameterBuilder.addThrottling();
175
+ if (this.serverTelemetryManager) {
176
+ parameterBuilder.addServerTelemetry(this.serverTelemetryManager);
177
+ }
178
+ const correlationId = request.correlationId ||
179
+ this.config.cryptoInterface.createNewGuid();
180
+ parameterBuilder.addCorrelationId(correlationId);
181
+ if (this.config.clientCredentials.clientSecret) {
182
+ parameterBuilder.addClientSecret(this.config.clientCredentials.clientSecret);
183
+ }
184
+ // Use clientAssertion from request, fallback to client assertion in base configuration
185
+ const clientAssertion = request.clientAssertion ||
186
+ this.config.clientCredentials.clientAssertion;
187
+ if (clientAssertion) {
188
+ parameterBuilder.addClientAssertion(await getClientAssertion(clientAssertion.assertion, this.config.authOptions.clientId, request.resourceRequestUri));
189
+ parameterBuilder.addClientAssertionType(clientAssertion.assertionType);
190
+ }
191
+ if (!StringUtils.isEmptyObj(request.claims) ||
192
+ (this.config.authOptions.clientCapabilities &&
193
+ this.config.authOptions.clientCapabilities.length > 0)) {
194
+ parameterBuilder.addClaims(request.claims, this.config.authOptions.clientCapabilities);
195
+ }
196
+ return parameterBuilder.createQueryString();
197
+ }
198
198
  }
199
199
 
200
200
  export { ClientCredentialClient };