@azure/msal-node 2.2.0-alpha.0 → 2.3.0-alpha.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 (183) hide show
  1. package/dist/cache/NodeStorage.cjs +3 -3
  2. package/dist/cache/NodeStorage.cjs.map +1 -1
  3. package/dist/cache/NodeStorage.d.ts +2 -2
  4. package/dist/cache/NodeStorage.d.ts.map +1 -1
  5. package/dist/cache/NodeStorage.mjs +3 -3
  6. package/dist/cache/NodeStorage.mjs.map +1 -1
  7. package/dist/cache/TokenCache.cjs +1 -1
  8. package/dist/cache/TokenCache.mjs +1 -1
  9. package/dist/cache/distributed/DistributedCachePlugin.cjs +1 -1
  10. package/dist/cache/distributed/DistributedCachePlugin.mjs +1 -1
  11. package/dist/cache/serializer/Deserializer.cjs +1 -1
  12. package/dist/cache/serializer/Deserializer.mjs +1 -1
  13. package/dist/cache/serializer/Serializer.cjs +1 -1
  14. package/dist/cache/serializer/Serializer.mjs +1 -1
  15. package/dist/client/ClientApplication.cjs +2 -2
  16. package/dist/client/ClientApplication.cjs.map +1 -1
  17. package/dist/client/ClientApplication.d.ts.map +1 -1
  18. package/dist/client/ClientApplication.mjs +3 -3
  19. package/dist/client/ClientApplication.mjs.map +1 -1
  20. package/dist/client/ClientAssertion.cjs +1 -1
  21. package/dist/client/ClientAssertion.mjs +1 -1
  22. package/dist/client/ClientCredentialClient.cjs +27 -23
  23. package/dist/client/ClientCredentialClient.cjs.map +1 -1
  24. package/dist/client/ClientCredentialClient.d.ts +3 -3
  25. package/dist/client/ClientCredentialClient.d.ts.map +1 -1
  26. package/dist/client/ClientCredentialClient.mjs +28 -24
  27. package/dist/client/ClientCredentialClient.mjs.map +1 -1
  28. package/dist/client/ConfidentialClientApplication.cjs +1 -1
  29. package/dist/client/ConfidentialClientApplication.mjs +1 -1
  30. package/dist/client/DeviceCodeClient.cjs +1 -1
  31. package/dist/client/DeviceCodeClient.mjs +1 -1
  32. package/dist/client/ManagedIdentityApplication.cjs +96 -0
  33. package/dist/client/ManagedIdentityApplication.cjs.map +1 -0
  34. package/dist/client/ManagedIdentityApplication.d.ts +24 -0
  35. package/dist/client/ManagedIdentityApplication.d.ts.map +1 -0
  36. package/dist/client/ManagedIdentityApplication.mjs +94 -0
  37. package/dist/client/ManagedIdentityApplication.mjs.map +1 -0
  38. package/dist/client/ManagedIdentityClient.cjs +49 -0
  39. package/dist/client/ManagedIdentityClient.cjs.map +1 -0
  40. package/dist/client/ManagedIdentityClient.d.ts +25 -0
  41. package/dist/client/ManagedIdentityClient.d.ts.map +1 -0
  42. package/dist/client/ManagedIdentityClient.mjs +47 -0
  43. package/dist/client/ManagedIdentityClient.mjs.map +1 -0
  44. package/dist/client/ManagedIdentitySources/AppService.cjs +79 -0
  45. package/dist/client/ManagedIdentitySources/AppService.cjs.map +1 -0
  46. package/dist/client/ManagedIdentitySources/AppService.d.ts +17 -0
  47. package/dist/client/ManagedIdentitySources/AppService.d.ts.map +1 -0
  48. package/dist/client/ManagedIdentitySources/AppService.mjs +77 -0
  49. package/dist/client/ManagedIdentitySources/AppService.mjs.map +1 -0
  50. package/dist/client/ManagedIdentitySources/AzureArc.cjs +107 -0
  51. package/dist/client/ManagedIdentitySources/AzureArc.cjs.map +1 -0
  52. package/dist/client/ManagedIdentitySources/AzureArc.d.ts +19 -0
  53. package/dist/client/ManagedIdentitySources/AzureArc.d.ts.map +1 -0
  54. package/dist/client/ManagedIdentitySources/AzureArc.mjs +104 -0
  55. package/dist/client/ManagedIdentitySources/AzureArc.mjs.map +1 -0
  56. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs +87 -0
  57. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs.map +1 -0
  58. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts +19 -0
  59. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts.map +1 -0
  60. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs +85 -0
  61. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs.map +1 -0
  62. package/dist/client/ManagedIdentitySources/CloudShell.d.ts +7 -0
  63. package/dist/client/ManagedIdentitySources/CloudShell.d.ts.map +1 -0
  64. package/dist/client/ManagedIdentitySources/Imds.cjs +63 -0
  65. package/dist/client/ManagedIdentitySources/Imds.cjs.map +1 -0
  66. package/dist/client/ManagedIdentitySources/Imds.d.ts +13 -0
  67. package/dist/client/ManagedIdentitySources/Imds.d.ts.map +1 -0
  68. package/dist/client/ManagedIdentitySources/Imds.mjs +61 -0
  69. package/dist/client/ManagedIdentitySources/Imds.mjs.map +1 -0
  70. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts +7 -0
  71. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts.map +1 -0
  72. package/dist/client/OnBehalfOfClient.cjs +1 -1
  73. package/dist/client/OnBehalfOfClient.mjs +1 -1
  74. package/dist/client/PublicClientApplication.cjs +1 -1
  75. package/dist/client/PublicClientApplication.mjs +1 -1
  76. package/dist/client/UsernamePasswordClient.cjs +1 -1
  77. package/dist/client/UsernamePasswordClient.mjs +1 -1
  78. package/dist/config/Configuration.cjs +18 -2
  79. package/dist/config/Configuration.cjs.map +1 -1
  80. package/dist/config/Configuration.d.ts +15 -0
  81. package/dist/config/Configuration.d.ts.map +1 -1
  82. package/dist/config/Configuration.mjs +18 -3
  83. package/dist/config/Configuration.mjs.map +1 -1
  84. package/dist/config/ManagedIdentityId.cjs +59 -0
  85. package/dist/config/ManagedIdentityId.cjs.map +1 -0
  86. package/dist/config/ManagedIdentityId.d.ts +12 -0
  87. package/dist/config/ManagedIdentityId.d.ts.map +1 -0
  88. package/dist/config/ManagedIdentityId.mjs +57 -0
  89. package/dist/config/ManagedIdentityId.mjs.map +1 -0
  90. package/dist/config/ManagedIdentityRequestParameters.cjs +38 -0
  91. package/dist/config/ManagedIdentityRequestParameters.cjs.map +1 -0
  92. package/dist/config/ManagedIdentityRequestParameters.d.ts +12 -0
  93. package/dist/config/ManagedIdentityRequestParameters.d.ts.map +1 -0
  94. package/dist/config/ManagedIdentityRequestParameters.mjs +36 -0
  95. package/dist/config/ManagedIdentityRequestParameters.mjs.map +1 -0
  96. package/dist/crypto/CryptoProvider.cjs +1 -1
  97. package/dist/crypto/CryptoProvider.mjs +1 -1
  98. package/dist/crypto/GuidGenerator.cjs +1 -1
  99. package/dist/crypto/GuidGenerator.mjs +1 -1
  100. package/dist/crypto/HashUtils.cjs +1 -1
  101. package/dist/crypto/HashUtils.mjs +1 -1
  102. package/dist/crypto/PkceGenerator.cjs +1 -1
  103. package/dist/crypto/PkceGenerator.mjs +1 -1
  104. package/dist/error/ManagedIdentityError.cjs +40 -0
  105. package/dist/error/ManagedIdentityError.cjs.map +1 -0
  106. package/dist/error/ManagedIdentityError.d.ts +22 -0
  107. package/dist/error/ManagedIdentityError.d.ts.map +1 -0
  108. package/dist/error/ManagedIdentityError.mjs +36 -0
  109. package/dist/error/ManagedIdentityError.mjs.map +1 -0
  110. package/dist/error/ManagedIdentityErrorCodes.cjs +28 -0
  111. package/dist/error/ManagedIdentityErrorCodes.cjs.map +1 -0
  112. package/dist/error/ManagedIdentityErrorCodes.d.ts +10 -0
  113. package/dist/error/ManagedIdentityErrorCodes.d.ts.map +1 -0
  114. package/dist/error/ManagedIdentityErrorCodes.mjs +18 -0
  115. package/dist/error/ManagedIdentityErrorCodes.mjs.map +1 -0
  116. package/dist/error/NodeAuthError.cjs +1 -1
  117. package/dist/error/NodeAuthError.mjs +1 -1
  118. package/dist/index.cjs +3 -1
  119. package/dist/index.cjs.map +1 -1
  120. package/dist/index.d.ts +3 -1
  121. package/dist/index.d.ts.map +1 -1
  122. package/dist/index.mjs +2 -1
  123. package/dist/index.mjs.map +1 -1
  124. package/dist/internals.cjs +1 -1
  125. package/dist/internals.mjs +1 -1
  126. package/dist/network/HttpClient.cjs +13 -6
  127. package/dist/network/HttpClient.cjs.map +1 -1
  128. package/dist/network/HttpClient.d.ts +2 -1
  129. package/dist/network/HttpClient.d.ts.map +1 -1
  130. package/dist/network/HttpClient.mjs +13 -6
  131. package/dist/network/HttpClient.mjs.map +1 -1
  132. package/dist/network/LoopbackClient.cjs +2 -1
  133. package/dist/network/LoopbackClient.cjs.map +1 -1
  134. package/dist/network/LoopbackClient.d.ts.map +1 -1
  135. package/dist/network/LoopbackClient.mjs +2 -1
  136. package/dist/network/LoopbackClient.mjs.map +1 -1
  137. package/dist/packageMetadata.cjs +2 -2
  138. package/dist/packageMetadata.cjs.map +1 -1
  139. package/dist/packageMetadata.d.ts +1 -1
  140. package/dist/packageMetadata.d.ts.map +1 -1
  141. package/dist/packageMetadata.mjs +2 -2
  142. package/dist/packageMetadata.mjs.map +1 -1
  143. package/dist/request/ManagedIdentityRequest.d.ts +9 -0
  144. package/dist/request/ManagedIdentityRequest.d.ts.map +1 -0
  145. package/dist/request/ManagedIdentityRequestParams.d.ts +10 -0
  146. package/dist/request/ManagedIdentityRequestParams.d.ts.map +1 -0
  147. package/dist/response/ManagedIdentityTokenResponse.d.ts +25 -0
  148. package/dist/response/ManagedIdentityTokenResponse.d.ts.map +1 -0
  149. package/dist/utils/Constants.cjs +30 -1
  150. package/dist/utils/Constants.cjs.map +1 -1
  151. package/dist/utils/Constants.d.ts +20 -0
  152. package/dist/utils/Constants.d.ts.map +1 -1
  153. package/dist/utils/Constants.mjs +21 -2
  154. package/dist/utils/Constants.mjs.map +1 -1
  155. package/dist/utils/EncodingUtils.cjs +1 -1
  156. package/dist/utils/EncodingUtils.mjs +1 -1
  157. package/dist/utils/NetworkUtils.cjs +1 -1
  158. package/dist/utils/NetworkUtils.mjs +1 -1
  159. package/package.json +2 -2
  160. package/src/cache/NodeStorage.ts +8 -2
  161. package/src/client/ClientApplication.ts +3 -1
  162. package/src/client/ClientCredentialClient.ts +65 -25
  163. package/src/client/ManagedIdentityApplication.ts +194 -0
  164. package/src/client/ManagedIdentityClient.ts +111 -0
  165. package/src/client/ManagedIdentitySources/AppService.ts +151 -0
  166. package/src/client/ManagedIdentitySources/AzureArc.ts +208 -0
  167. package/src/client/ManagedIdentitySources/BaseManagedIdentitySource.ts +169 -0
  168. package/src/client/ManagedIdentitySources/CloudShell.ts +19 -0
  169. package/src/client/ManagedIdentitySources/Imds.ts +112 -0
  170. package/src/client/ManagedIdentitySources/ServiceFabric.ts +19 -0
  171. package/src/config/Configuration.ts +47 -1
  172. package/src/config/ManagedIdentityId.ts +73 -0
  173. package/src/config/ManagedIdentityRequestParameters.ts +53 -0
  174. package/src/error/ManagedIdentityError.ts +46 -0
  175. package/src/error/ManagedIdentityErrorCodes.ts +15 -0
  176. package/src/index.ts +3 -0
  177. package/src/network/HttpClient.ts +17 -5
  178. package/src/network/LoopbackClient.ts +1 -0
  179. package/src/packageMetadata.ts +1 -1
  180. package/src/request/ManagedIdentityRequest.ts +15 -0
  181. package/src/request/ManagedIdentityRequestParams.ts +14 -0
  182. package/src/response/ManagedIdentityTokenResponse.ts +33 -0
  183. package/src/utils/Constants.ts +27 -0
@@ -1,4 +1,4 @@
1
- /*! @azure/msal-node v2.2.0-alpha.0 2023-09-20 */
1
+ /*! @azure/msal-node v2.3.0-alpha.0 2023-12-07 */
2
2
  'use strict';
3
3
  import { Constants } from '@azure/msal-common';
4
4
 
@@ -1,4 +1,4 @@
1
- /*! @azure/msal-node v2.2.0-alpha.0 2023-09-20 */
1
+ /*! @azure/msal-node v2.3.0-alpha.0 2023-12-07 */
2
2
  'use strict';
3
3
  'use strict';
4
4
 
@@ -1,4 +1,4 @@
1
- /*! @azure/msal-node v2.2.0-alpha.0 2023-09-20 */
1
+ /*! @azure/msal-node v2.3.0-alpha.0 2023-12-07 */
2
2
  'use strict';
3
3
  /*
4
4
  * Copyright (c) Microsoft Corporation. All rights reserved.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@azure/msal-node",
4
- "version": "2.2.0-alpha.0",
4
+ "version": "2.3.0-alpha.0",
5
5
  "author": {
6
6
  "name": "Microsoft",
7
7
  "email": "nugetaad@microsoft.com",
@@ -77,7 +77,7 @@
77
77
  "yargs": "^17.3.1"
78
78
  },
79
79
  "dependencies": {
80
- "@azure/msal-common": "14.1.0-alpha.0",
80
+ "@azure/msal-common": "14.2.0-alpha.0",
81
81
  "jsonwebtoken": "^9.0.0",
82
82
  "uuid": "^8.3.0"
83
83
  },
@@ -18,6 +18,7 @@ import {
18
18
  ICrypto,
19
19
  AuthorityMetadataEntity,
20
20
  ValidCredentialType,
21
+ StaticAuthorityOptions,
21
22
  } from "@azure/msal-common";
22
23
 
23
24
  import { Deserializer } from "./serializer/Deserializer.js";
@@ -38,8 +39,13 @@ export class NodeStorage extends CacheManager {
38
39
  private cache: CacheKVStore = {};
39
40
  private changeEmitters: Array<Function> = [];
40
41
 
41
- constructor(logger: Logger, clientId: string, cryptoImpl: ICrypto) {
42
- super(clientId, cryptoImpl, logger);
42
+ constructor(
43
+ logger: Logger,
44
+ clientId: string,
45
+ cryptoImpl: ICrypto,
46
+ staticAuthorityOptions?: StaticAuthorityOptions
47
+ ) {
48
+ super(clientId, cryptoImpl, logger, staticAuthorityOptions);
43
49
  this.logger = logger;
44
50
  }
45
51
 
@@ -32,6 +32,7 @@ import {
32
32
  StringUtils,
33
33
  createClientAuthError,
34
34
  ClientAuthErrorCodes,
35
+ buildStaticAuthorityOptions,
35
36
  } from "@azure/msal-common";
36
37
  import {
37
38
  Configuration,
@@ -95,7 +96,8 @@ export abstract class ClientApplication {
95
96
  this.storage = new NodeStorage(
96
97
  this.logger,
97
98
  this.config.auth.clientId,
98
- this.cryptoProvider
99
+ this.cryptoProvider,
100
+ buildStaticAuthorityOptions(this.config.auth)
99
101
  );
100
102
  this.tokenCache = new TokenCache(
101
103
  this.storage,
@@ -9,6 +9,7 @@ import {
9
9
  AuthenticationScheme,
10
10
  Authority,
11
11
  BaseClient,
12
+ CacheManager,
12
13
  CacheOutcome,
13
14
  ClientAuthErrorCodes,
14
15
  ClientConfiguration,
@@ -16,25 +17,31 @@ import {
16
17
  Constants,
17
18
  CredentialFilter,
18
19
  CredentialType,
20
+ DEFAULT_TOKEN_RENEWAL_OFFSET_SEC,
19
21
  GrantType,
20
22
  IAppTokenProvider,
23
+ ICrypto,
21
24
  RequestParameterBuilder,
22
25
  RequestThumbprint,
23
26
  ResponseHandler,
24
27
  ScopeSet,
25
28
  ServerAuthorizationTokenResponse,
29
+ ServerTelemetryManager,
26
30
  StringUtils,
27
31
  TimeUtils,
28
32
  TokenCacheContext,
29
33
  UrlString,
30
34
  createClientAuthError,
31
35
  } from "@azure/msal-common";
36
+ import {
37
+ ManagedIdentityConfiguration,
38
+ ManagedIdentityNodeConfiguration,
39
+ } from "../config/Configuration";
32
40
 
33
41
  /**
34
42
  * OAuth2.0 client credential grant
35
43
  */
36
44
  export class ClientCredentialClient extends BaseClient {
37
- private scopeSet: ScopeSet;
38
45
  private readonly appTokenProvider?: IAppTokenProvider;
39
46
 
40
47
  constructor(
@@ -52,14 +59,19 @@ export class ClientCredentialClient extends BaseClient {
52
59
  public async acquireToken(
53
60
  request: CommonClientCredentialRequest
54
61
  ): Promise<AuthenticationResult | null> {
55
- this.scopeSet = new ScopeSet(request.scopes || []);
56
-
57
62
  if (request.skipCache) {
58
63
  return await this.executeTokenRequest(request, this.authority);
59
64
  }
60
65
 
61
66
  const [cachedAuthenticationResult, lastCacheOutcome] =
62
- await this.getCachedAuthenticationResult(request);
67
+ await this.getCachedAuthenticationResult(
68
+ request,
69
+ this.config,
70
+ this.cryptoUtils,
71
+ this.authority,
72
+ this.cacheManager,
73
+ this.serverTelemetryManager
74
+ );
63
75
 
64
76
  if (cachedAuthenticationResult) {
65
77
  // if the token is not expired but must be refreshed; get a new one in the background
@@ -87,34 +99,56 @@ export class ClientCredentialClient extends BaseClient {
87
99
  /**
88
100
  * looks up cache if the tokens are cached already
89
101
  */
90
- private async getCachedAuthenticationResult(
91
- request: CommonClientCredentialRequest
102
+ public async getCachedAuthenticationResult(
103
+ request: CommonClientCredentialRequest,
104
+ config: ClientConfiguration | ManagedIdentityConfiguration,
105
+ cryptoUtils: ICrypto,
106
+ authority: Authority,
107
+ cacheManager: CacheManager,
108
+ serverTelemetryManager?: ServerTelemetryManager | null
92
109
  ): Promise<[AuthenticationResult | null, CacheOutcome]> {
110
+ const clientConfiguration = config as ClientConfiguration;
111
+ const managedIdentityConfiguration =
112
+ config as ManagedIdentityNodeConfiguration;
113
+
93
114
  let lastCacheOutcome: CacheOutcome = CacheOutcome.NOT_APPLICABLE;
94
115
 
95
116
  // read the user-supplied cache into memory, if applicable
96
117
  let cacheContext;
97
- if (this.config.serializableCache && this.config.persistencePlugin) {
118
+ if (
119
+ clientConfiguration.serializableCache &&
120
+ clientConfiguration.persistencePlugin
121
+ ) {
98
122
  cacheContext = new TokenCacheContext(
99
- this.config.serializableCache,
123
+ clientConfiguration.serializableCache,
100
124
  false
101
125
  );
102
- await this.config.persistencePlugin.beforeCacheAccess(cacheContext);
126
+ await clientConfiguration.persistencePlugin.beforeCacheAccess(
127
+ cacheContext
128
+ );
103
129
  }
104
130
 
105
- const cachedAccessToken = this.readAccessTokenFromCache();
131
+ const cachedAccessToken = this.readAccessTokenFromCache(
132
+ authority,
133
+ managedIdentityConfiguration.managedIdentityId?.id ||
134
+ clientConfiguration.authOptions.clientId,
135
+ new ScopeSet(request.scopes || []),
136
+ cacheManager
137
+ );
106
138
 
107
139
  if (
108
- this.config.serializableCache &&
109
- this.config.persistencePlugin &&
140
+ clientConfiguration.serializableCache &&
141
+ clientConfiguration.persistencePlugin &&
110
142
  cacheContext
111
143
  ) {
112
- await this.config.persistencePlugin.afterCacheAccess(cacheContext);
144
+ await clientConfiguration.persistencePlugin.afterCacheAccess(
145
+ cacheContext
146
+ );
113
147
  }
114
148
 
115
149
  // must refresh due to non-existent access_token
116
150
  if (!cachedAccessToken) {
117
- this.serverTelemetryManager?.setCacheOutcome(
151
+ serverTelemetryManager?.setCacheOutcome(
118
152
  CacheOutcome.NO_CACHED_ACCESS_TOKEN
119
153
  );
120
154
  return [null, CacheOutcome.NO_CACHED_ACCESS_TOKEN];
@@ -124,10 +158,11 @@ export class ClientCredentialClient extends BaseClient {
124
158
  if (
125
159
  TimeUtils.isTokenExpired(
126
160
  cachedAccessToken.expiresOn,
127
- this.config.systemOptions.tokenRenewalOffsetSeconds
161
+ clientConfiguration.systemOptions?.tokenRenewalOffsetSeconds ||
162
+ DEFAULT_TOKEN_RENEWAL_OFFSET_SEC
128
163
  )
129
164
  ) {
130
- this.serverTelemetryManager?.setCacheOutcome(
165
+ serverTelemetryManager?.setCacheOutcome(
131
166
  CacheOutcome.CACHED_ACCESS_TOKEN_EXPIRED
132
167
  );
133
168
  return [null, CacheOutcome.CACHED_ACCESS_TOKEN_EXPIRED];
@@ -139,15 +174,15 @@ export class ClientCredentialClient extends BaseClient {
139
174
  TimeUtils.isTokenExpired(cachedAccessToken.refreshOn.toString(), 0)
140
175
  ) {
141
176
  lastCacheOutcome = CacheOutcome.PROACTIVELY_REFRESHED;
142
- this.serverTelemetryManager?.setCacheOutcome(
177
+ serverTelemetryManager?.setCacheOutcome(
143
178
  CacheOutcome.PROACTIVELY_REFRESHED
144
179
  );
145
180
  }
146
181
 
147
182
  return [
148
183
  await ResponseHandler.generateAuthenticationResult(
149
- this.cryptoUtils,
150
- this.authority,
184
+ cryptoUtils,
185
+ authority,
151
186
  {
152
187
  account: null,
153
188
  idToken: null,
@@ -165,19 +200,24 @@ export class ClientCredentialClient extends BaseClient {
165
200
  /**
166
201
  * Reads access token from the cache
167
202
  */
168
- private readAccessTokenFromCache(): AccessTokenEntity | null {
203
+ private readAccessTokenFromCache(
204
+ authority: Authority,
205
+ id: string,
206
+ scopeSet: ScopeSet,
207
+ cacheManager: CacheManager
208
+ ): AccessTokenEntity | null {
169
209
  const accessTokenFilter: CredentialFilter = {
170
210
  homeAccountId: Constants.EMPTY_STRING,
171
211
  environment:
172
- this.authority.canonicalAuthorityUrlComponents.HostNameAndPort,
212
+ authority.canonicalAuthorityUrlComponents.HostNameAndPort,
173
213
  credentialType: CredentialType.ACCESS_TOKEN,
174
- clientId: this.config.authOptions.clientId,
175
- realm: this.authority.tenant,
176
- target: ScopeSet.createSearchScopes(this.scopeSet.asArray()),
214
+ clientId: id,
215
+ realm: authority.tenant,
216
+ target: ScopeSet.createSearchScopes(scopeSet.asArray()),
177
217
  };
178
218
 
179
219
  const accessTokens =
180
- this.cacheManager.getAccessTokensByFilter(accessTokenFilter);
220
+ cacheManager.getAccessTokensByFilter(accessTokenFilter);
181
221
  if (accessTokens.length < 1) {
182
222
  return null;
183
223
  } else if (accessTokens.length > 1) {
@@ -0,0 +1,194 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import {
7
+ AuthOptions,
8
+ Authority,
9
+ AuthorityOptions,
10
+ CacheOutcome,
11
+ ClientConfiguration,
12
+ Constants,
13
+ DEFAULT_CRYPTO_IMPLEMENTATION,
14
+ INetworkModule,
15
+ Logger,
16
+ ProtocolMode,
17
+ StaticAuthorityOptions,
18
+ AuthenticationResult,
19
+ UrlString,
20
+ } from "@azure/msal-common";
21
+ import {
22
+ ManagedIdentityConfiguration,
23
+ ManagedIdentityNodeConfiguration,
24
+ buildManagedIdentityConfiguration,
25
+ } from "../config/Configuration";
26
+ import { version, name } from "../packageMetadata.js";
27
+ import { ManagedIdentityRequest } from "../request/ManagedIdentityRequest";
28
+ import { CryptoProvider } from "../crypto/CryptoProvider";
29
+ import { ClientCredentialClient } from "./ClientCredentialClient";
30
+ import { ManagedIdentityClient } from "./ManagedIdentityClient";
31
+ import { ManagedIdentityRequestParams } from "../request/ManagedIdentityRequestParams";
32
+ import { NodeStorage } from "../cache/NodeStorage";
33
+ import { DEFAULT_AUTHORITY_FOR_MANAGED_IDENTITY } from "../utils/Constants";
34
+ import {
35
+ ManagedIdentityErrorCodes,
36
+ createManagedIdentityError,
37
+ } from "../error/ManagedIdentityError";
38
+
39
+ /**
40
+ * Class to initialize a managed identity and identify the service
41
+ */
42
+ export class ManagedIdentityApplication {
43
+ private config: ManagedIdentityNodeConfiguration;
44
+
45
+ private logger: Logger;
46
+ private static nodeStorage: NodeStorage | undefined;
47
+ private networkClient: INetworkModule;
48
+ private cryptoProvider: CryptoProvider;
49
+
50
+ // authority needs to be faked to re-use existing functionality in msal-common: caching in responseHandler, etc.
51
+ private fakeAuthority: Authority;
52
+
53
+ // the ClientCredentialClient class needs to be faked to call it's getCachedAuthenticationResult method
54
+ private fakeClientCredentialClient: ClientCredentialClient;
55
+
56
+ private managedIdentityClient: ManagedIdentityClient;
57
+
58
+ constructor(configuration?: ManagedIdentityConfiguration) {
59
+ // undefined config means the managed identity is system-assigned
60
+ this.config = buildManagedIdentityConfiguration(configuration || {});
61
+
62
+ this.logger = new Logger(
63
+ this.config.system.loggerOptions,
64
+ name,
65
+ version
66
+ );
67
+
68
+ const fakeStatusAuthorityOptions: StaticAuthorityOptions = {
69
+ knownAuthorities: [Constants.DEFAULT_AUTHORITY],
70
+ };
71
+
72
+ if (!ManagedIdentityApplication.nodeStorage) {
73
+ ManagedIdentityApplication.nodeStorage = new NodeStorage(
74
+ this.logger,
75
+ this.config.managedIdentityId.id,
76
+ DEFAULT_CRYPTO_IMPLEMENTATION,
77
+ fakeStatusAuthorityOptions
78
+ );
79
+ }
80
+
81
+ this.networkClient = this.config.system.networkClient;
82
+
83
+ this.cryptoProvider = new CryptoProvider();
84
+
85
+ const fakeAuthorityOptions: AuthorityOptions = {
86
+ protocolMode: ProtocolMode.AAD,
87
+ knownAuthorities: [DEFAULT_AUTHORITY_FOR_MANAGED_IDENTITY],
88
+ cloudDiscoveryMetadata: "",
89
+ authorityMetadata: "",
90
+ };
91
+ this.fakeAuthority = new Authority(
92
+ DEFAULT_AUTHORITY_FOR_MANAGED_IDENTITY,
93
+ this.networkClient,
94
+ ManagedIdentityApplication.nodeStorage as NodeStorage,
95
+ fakeAuthorityOptions,
96
+ this.logger,
97
+ undefined,
98
+ undefined,
99
+ true
100
+ );
101
+
102
+ this.fakeClientCredentialClient = new ClientCredentialClient({
103
+ authOptions: {
104
+ clientId: this.config.managedIdentityId.id,
105
+ authority: this.fakeAuthority,
106
+ } as AuthOptions,
107
+ } as ClientConfiguration);
108
+
109
+ this.managedIdentityClient = new ManagedIdentityClient(
110
+ this.logger,
111
+ ManagedIdentityApplication.nodeStorage as NodeStorage,
112
+ this.networkClient,
113
+ this.cryptoProvider
114
+ );
115
+ }
116
+
117
+ /**
118
+ * Acquire an access token from the cache or the managed identity
119
+ * @param managedIdentityRequest
120
+ * @returns the access token
121
+ */
122
+ public async acquireToken(
123
+ managedIdentityRequestParams: ManagedIdentityRequestParams
124
+ ): Promise<AuthenticationResult> {
125
+ const resourceUrlString = new UrlString(
126
+ managedIdentityRequestParams.resource.replace("/.default", "")
127
+ );
128
+ try {
129
+ resourceUrlString.validateAsUri();
130
+ } catch (e) {
131
+ throw createManagedIdentityError(
132
+ ManagedIdentityErrorCodes.invalidResource
133
+ );
134
+ }
135
+
136
+ const managedIdentityRequest: ManagedIdentityRequest = {
137
+ forceRefresh: managedIdentityRequestParams.forceRefresh,
138
+ resource: managedIdentityRequestParams.resource.replace(
139
+ "/.default",
140
+ ""
141
+ ),
142
+ scopes: [
143
+ managedIdentityRequestParams.resource.replace("/.default", ""),
144
+ ],
145
+ authority: this.fakeAuthority.canonicalAuthority,
146
+ correlationId: this.config.managedIdentityId.id,
147
+ };
148
+
149
+ if (managedIdentityRequest.forceRefresh) {
150
+ // make a network call to the managed identity source
151
+ return await this.managedIdentityClient.sendManagedIdentityTokenRequest(
152
+ managedIdentityRequest,
153
+ this.config.managedIdentityId,
154
+ this.fakeAuthority
155
+ );
156
+ }
157
+
158
+ const [cachedAuthenticationResult, lastCacheOutcome] =
159
+ await this.fakeClientCredentialClient.getCachedAuthenticationResult(
160
+ managedIdentityRequest,
161
+ this.config,
162
+ this.cryptoProvider,
163
+ this.fakeAuthority,
164
+ ManagedIdentityApplication.nodeStorage as NodeStorage
165
+ );
166
+
167
+ if (cachedAuthenticationResult) {
168
+ // if the token is not expired but must be refreshed; get a new one in the background
169
+ if (lastCacheOutcome === CacheOutcome.PROACTIVELY_REFRESHED) {
170
+ this.logger.info(
171
+ "ClientCredentialClient:getCachedAuthenticationResult - Cached access token's refreshOn property has been exceeded'. It's not expired, but must be refreshed."
172
+ );
173
+
174
+ // make a network call to the managed identity source; refresh the access token in the background
175
+ const refreshAccessToken = true;
176
+ await this.managedIdentityClient.sendManagedIdentityTokenRequest(
177
+ managedIdentityRequest,
178
+ this.config.managedIdentityId,
179
+ this.fakeAuthority,
180
+ refreshAccessToken
181
+ );
182
+ }
183
+
184
+ return cachedAuthenticationResult;
185
+ } else {
186
+ // make a network call to the managed identity source
187
+ return await this.managedIdentityClient.sendManagedIdentityTokenRequest(
188
+ managedIdentityRequest,
189
+ this.config.managedIdentityId,
190
+ this.fakeAuthority
191
+ );
192
+ }
193
+ }
194
+ }
@@ -0,0 +1,111 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import {
7
+ Authority,
8
+ INetworkModule,
9
+ Logger,
10
+ AuthenticationResult,
11
+ } from "@azure/msal-common";
12
+ import { AppService } from "./ManagedIdentitySources/AppService";
13
+ import { AzureArc } from "./ManagedIdentitySources/AzureArc";
14
+ import { CloudShell } from "./ManagedIdentitySources/CloudShell";
15
+ import { Imds } from "./ManagedIdentitySources/Imds";
16
+ import { ServiceFabric } from "./ManagedIdentitySources/ServiceFabric";
17
+ import { CryptoProvider } from "../crypto/CryptoProvider";
18
+ import {
19
+ ManagedIdentityErrorCodes,
20
+ createManagedIdentityError,
21
+ } from "../error/ManagedIdentityError";
22
+ import { ManagedIdentityRequest } from "../request/ManagedIdentityRequest";
23
+ import { ManagedIdentityId } from "../config/ManagedIdentityId";
24
+ import { NodeStorage } from "../cache/NodeStorage";
25
+
26
+ /*
27
+ * Class to initialize a managed identity and identify the service.
28
+ * Original source of code: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/src/ManagedIdentityClient.cs
29
+ */
30
+ export class ManagedIdentityClient {
31
+ private logger: Logger;
32
+ private nodeStorage: NodeStorage;
33
+ private networkClient: INetworkModule;
34
+ private cryptoProvider: CryptoProvider;
35
+
36
+ static identitySource:
37
+ | ServiceFabric
38
+ | AppService
39
+ | CloudShell
40
+ | AzureArc
41
+ | Imds
42
+ | undefined;
43
+
44
+ constructor(
45
+ logger: Logger,
46
+ nodeStorage: NodeStorage,
47
+ networkClient: INetworkModule,
48
+ cryptoProvider: CryptoProvider
49
+ ) {
50
+ this.logger = logger;
51
+ this.nodeStorage = nodeStorage;
52
+ this.networkClient = networkClient;
53
+ this.cryptoProvider = cryptoProvider;
54
+ }
55
+
56
+ public async sendManagedIdentityTokenRequest(
57
+ managedIdentityRequest: ManagedIdentityRequest,
58
+ managedIdentityId: ManagedIdentityId,
59
+ fakeAuthority: Authority,
60
+ refreshAccessToken?: boolean
61
+ ): Promise<AuthenticationResult> {
62
+ if (!ManagedIdentityClient.identitySource) {
63
+ ManagedIdentityClient.identitySource =
64
+ this.selectManagedIdentitySource(
65
+ this.logger,
66
+ this.nodeStorage,
67
+ this.networkClient,
68
+ this.cryptoProvider
69
+ );
70
+ }
71
+
72
+ return await ManagedIdentityClient.identitySource.acquireTokenWithManagedIdentity(
73
+ managedIdentityRequest,
74
+ managedIdentityId,
75
+ fakeAuthority,
76
+ refreshAccessToken
77
+ );
78
+ }
79
+
80
+ /**
81
+ * Tries to create a managed identity source for all sources
82
+ * @returns the managed identity Source
83
+ */
84
+ private selectManagedIdentitySource(
85
+ logger: Logger,
86
+ nodeStorage: NodeStorage,
87
+ networkClient: INetworkModule,
88
+ cryptoProvider: CryptoProvider
89
+ ): ServiceFabric | AppService | CloudShell | AzureArc | Imds {
90
+ const source =
91
+ AppService.tryCreate(
92
+ logger,
93
+ nodeStorage,
94
+ networkClient,
95
+ cryptoProvider
96
+ ) ||
97
+ AzureArc.tryCreate(
98
+ logger,
99
+ nodeStorage,
100
+ networkClient,
101
+ cryptoProvider
102
+ ) ||
103
+ Imds.tryCreate(logger, nodeStorage, networkClient, cryptoProvider);
104
+ if (!source) {
105
+ throw createManagedIdentityError(
106
+ ManagedIdentityErrorCodes.unableToCreateSource
107
+ );
108
+ }
109
+ return source;
110
+ }
111
+ }