@azure/msal-node 2.5.1 → 2.7.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 (265) hide show
  1. package/dist/cache/ITokenCache.d.ts +15 -15
  2. package/dist/cache/NodeStorage.cjs +423 -405
  3. package/dist/cache/NodeStorage.cjs.map +1 -1
  4. package/dist/cache/NodeStorage.d.ts +193 -182
  5. package/dist/cache/NodeStorage.d.ts.map +1 -1
  6. package/dist/cache/NodeStorage.mjs +424 -406
  7. package/dist/cache/NodeStorage.mjs.map +1 -1
  8. package/dist/cache/TokenCache.cjs +265 -265
  9. package/dist/cache/TokenCache.cjs.map +1 -1
  10. package/dist/cache/TokenCache.d.ts +90 -90
  11. package/dist/cache/TokenCache.mjs +265 -265
  12. package/dist/cache/TokenCache.mjs.map +1 -1
  13. package/dist/cache/distributed/DistributedCachePlugin.cjs +26 -26
  14. package/dist/cache/distributed/DistributedCachePlugin.cjs.map +1 -1
  15. package/dist/cache/distributed/DistributedCachePlugin.d.ts +10 -10
  16. package/dist/cache/distributed/DistributedCachePlugin.mjs +26 -26
  17. package/dist/cache/distributed/DistributedCachePlugin.mjs.map +1 -1
  18. package/dist/cache/distributed/ICacheClient.d.ts +17 -17
  19. package/dist/cache/distributed/IPartitionManager.d.ts +28 -28
  20. package/dist/cache/serializer/Deserializer.cjs +170 -167
  21. package/dist/cache/serializer/Deserializer.cjs.map +1 -1
  22. package/dist/cache/serializer/Deserializer.d.ts +42 -42
  23. package/dist/cache/serializer/Deserializer.d.ts.map +1 -1
  24. package/dist/cache/serializer/Deserializer.mjs +170 -167
  25. package/dist/cache/serializer/Deserializer.mjs.map +1 -1
  26. package/dist/cache/serializer/Serializer.cjs +137 -134
  27. package/dist/cache/serializer/Serializer.cjs.map +1 -1
  28. package/dist/cache/serializer/Serializer.d.ts +39 -39
  29. package/dist/cache/serializer/Serializer.d.ts.map +1 -1
  30. package/dist/cache/serializer/Serializer.mjs +137 -134
  31. package/dist/cache/serializer/Serializer.mjs.map +1 -1
  32. package/dist/cache/serializer/SerializerTypes.d.ts +102 -101
  33. package/dist/cache/serializer/SerializerTypes.d.ts.map +1 -1
  34. package/dist/client/ClientApplication.cjs +333 -333
  35. package/dist/client/ClientApplication.cjs.map +1 -1
  36. package/dist/client/ClientApplication.d.ts +142 -142
  37. package/dist/client/ClientApplication.d.ts.map +1 -1
  38. package/dist/client/ClientApplication.mjs +333 -333
  39. package/dist/client/ClientApplication.mjs.map +1 -1
  40. package/dist/client/ClientAssertion.cjs +116 -116
  41. package/dist/client/ClientAssertion.cjs.map +1 -1
  42. package/dist/client/ClientAssertion.d.ts +46 -46
  43. package/dist/client/ClientAssertion.mjs +116 -116
  44. package/dist/client/ClientAssertion.mjs.map +1 -1
  45. package/dist/client/ClientCredentialClient.cjs +194 -189
  46. package/dist/client/ClientCredentialClient.cjs.map +1 -1
  47. package/dist/client/ClientCredentialClient.d.ts +33 -33
  48. package/dist/client/ClientCredentialClient.d.ts.map +1 -1
  49. package/dist/client/ClientCredentialClient.mjs +195 -190
  50. package/dist/client/ClientCredentialClient.mjs.map +1 -1
  51. package/dist/client/ConfidentialClientApplication.cjs +163 -163
  52. package/dist/client/ConfidentialClientApplication.cjs.map +1 -1
  53. package/dist/client/ConfidentialClientApplication.d.ts +58 -58
  54. package/dist/client/ConfidentialClientApplication.mjs +163 -163
  55. package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
  56. package/dist/client/DeviceCodeClient.cjs +207 -207
  57. package/dist/client/DeviceCodeClient.cjs.map +1 -1
  58. package/dist/client/DeviceCodeClient.d.ts +53 -53
  59. package/dist/client/DeviceCodeClient.mjs +207 -207
  60. package/dist/client/DeviceCodeClient.mjs.map +1 -1
  61. package/dist/client/IConfidentialClientApplication.d.ts +39 -39
  62. package/dist/client/IPublicClientApplication.d.ts +42 -42
  63. package/dist/client/ManagedIdentityApplication.cjs +96 -0
  64. package/dist/client/ManagedIdentityApplication.cjs.map +1 -0
  65. package/dist/client/ManagedIdentityApplication.d.ts +24 -0
  66. package/dist/client/ManagedIdentityApplication.d.ts.map +1 -0
  67. package/dist/client/ManagedIdentityApplication.mjs +94 -0
  68. package/dist/client/ManagedIdentityApplication.mjs.map +1 -0
  69. package/dist/client/ManagedIdentityClient.cjs +49 -0
  70. package/dist/client/ManagedIdentityClient.cjs.map +1 -0
  71. package/dist/client/ManagedIdentityClient.d.ts +25 -0
  72. package/dist/client/ManagedIdentityClient.d.ts.map +1 -0
  73. package/dist/client/ManagedIdentityClient.mjs +47 -0
  74. package/dist/client/ManagedIdentityClient.mjs.map +1 -0
  75. package/dist/client/ManagedIdentitySources/AppService.cjs +79 -0
  76. package/dist/client/ManagedIdentitySources/AppService.cjs.map +1 -0
  77. package/dist/client/ManagedIdentitySources/AppService.d.ts +17 -0
  78. package/dist/client/ManagedIdentitySources/AppService.d.ts.map +1 -0
  79. package/dist/client/ManagedIdentitySources/AppService.mjs +77 -0
  80. package/dist/client/ManagedIdentitySources/AppService.mjs.map +1 -0
  81. package/dist/client/ManagedIdentitySources/AzureArc.cjs +107 -0
  82. package/dist/client/ManagedIdentitySources/AzureArc.cjs.map +1 -0
  83. package/dist/client/ManagedIdentitySources/AzureArc.d.ts +19 -0
  84. package/dist/client/ManagedIdentitySources/AzureArc.d.ts.map +1 -0
  85. package/dist/client/ManagedIdentitySources/AzureArc.mjs +104 -0
  86. package/dist/client/ManagedIdentitySources/AzureArc.mjs.map +1 -0
  87. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs +87 -0
  88. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs.map +1 -0
  89. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts +19 -0
  90. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts.map +1 -0
  91. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs +85 -0
  92. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs.map +1 -0
  93. package/dist/client/ManagedIdentitySources/CloudShell.d.ts +7 -0
  94. package/dist/client/ManagedIdentitySources/CloudShell.d.ts.map +1 -0
  95. package/dist/client/ManagedIdentitySources/Imds.cjs +63 -0
  96. package/dist/client/ManagedIdentitySources/Imds.cjs.map +1 -0
  97. package/dist/client/ManagedIdentitySources/Imds.d.ts +13 -0
  98. package/dist/client/ManagedIdentitySources/Imds.d.ts.map +1 -0
  99. package/dist/client/ManagedIdentitySources/Imds.mjs +61 -0
  100. package/dist/client/ManagedIdentitySources/Imds.mjs.map +1 -0
  101. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts +7 -0
  102. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts.map +1 -0
  103. package/dist/client/OnBehalfOfClient.cjs +202 -202
  104. package/dist/client/OnBehalfOfClient.cjs.map +1 -1
  105. package/dist/client/OnBehalfOfClient.d.ts +47 -47
  106. package/dist/client/OnBehalfOfClient.mjs +202 -202
  107. package/dist/client/OnBehalfOfClient.mjs.map +1 -1
  108. package/dist/client/PublicClientApplication.cjs +229 -229
  109. package/dist/client/PublicClientApplication.cjs.map +1 -1
  110. package/dist/client/PublicClientApplication.d.ts +71 -71
  111. package/dist/client/PublicClientApplication.mjs +229 -229
  112. package/dist/client/PublicClientApplication.mjs.map +1 -1
  113. package/dist/client/UsernamePasswordClient.cjs +95 -95
  114. package/dist/client/UsernamePasswordClient.cjs.map +1 -1
  115. package/dist/client/UsernamePasswordClient.d.ts +25 -25
  116. package/dist/client/UsernamePasswordClient.mjs +95 -95
  117. package/dist/client/UsernamePasswordClient.mjs.map +1 -1
  118. package/dist/config/Configuration.cjs +88 -72
  119. package/dist/config/Configuration.cjs.map +1 -1
  120. package/dist/config/Configuration.d.ts +122 -107
  121. package/dist/config/Configuration.d.ts.map +1 -1
  122. package/dist/config/Configuration.mjs +88 -73
  123. package/dist/config/Configuration.mjs.map +1 -1
  124. package/dist/config/ManagedIdentityId.cjs +59 -0
  125. package/dist/config/ManagedIdentityId.cjs.map +1 -0
  126. package/dist/config/ManagedIdentityId.d.ts +12 -0
  127. package/dist/config/ManagedIdentityId.d.ts.map +1 -0
  128. package/dist/config/ManagedIdentityId.mjs +57 -0
  129. package/dist/config/ManagedIdentityId.mjs.map +1 -0
  130. package/dist/config/ManagedIdentityRequestParameters.cjs +38 -0
  131. package/dist/config/ManagedIdentityRequestParameters.cjs.map +1 -0
  132. package/dist/config/ManagedIdentityRequestParameters.d.ts +12 -0
  133. package/dist/config/ManagedIdentityRequestParameters.d.ts.map +1 -0
  134. package/dist/config/ManagedIdentityRequestParameters.mjs +36 -0
  135. package/dist/config/ManagedIdentityRequestParameters.mjs.map +1 -0
  136. package/dist/crypto/CryptoProvider.cjs +75 -75
  137. package/dist/crypto/CryptoProvider.cjs.map +1 -1
  138. package/dist/crypto/CryptoProvider.d.ts +52 -52
  139. package/dist/crypto/CryptoProvider.mjs +75 -75
  140. package/dist/crypto/CryptoProvider.mjs.map +1 -1
  141. package/dist/crypto/GuidGenerator.cjs +22 -22
  142. package/dist/crypto/GuidGenerator.cjs.map +1 -1
  143. package/dist/crypto/GuidGenerator.d.ts +14 -14
  144. package/dist/crypto/GuidGenerator.mjs +22 -22
  145. package/dist/crypto/GuidGenerator.mjs.map +1 -1
  146. package/dist/crypto/HashUtils.cjs +13 -13
  147. package/dist/crypto/HashUtils.cjs.map +1 -1
  148. package/dist/crypto/HashUtils.d.ts +8 -8
  149. package/dist/crypto/HashUtils.mjs +13 -13
  150. package/dist/crypto/HashUtils.mjs.map +1 -1
  151. package/dist/crypto/PkceGenerator.cjs +49 -49
  152. package/dist/crypto/PkceGenerator.cjs.map +1 -1
  153. package/dist/crypto/PkceGenerator.d.ts +22 -22
  154. package/dist/crypto/PkceGenerator.mjs +49 -49
  155. package/dist/crypto/PkceGenerator.mjs.map +1 -1
  156. package/dist/error/ManagedIdentityError.cjs +40 -0
  157. package/dist/error/ManagedIdentityError.cjs.map +1 -0
  158. package/dist/error/ManagedIdentityError.d.ts +22 -0
  159. package/dist/error/ManagedIdentityError.d.ts.map +1 -0
  160. package/dist/error/ManagedIdentityError.mjs +36 -0
  161. package/dist/error/ManagedIdentityError.mjs.map +1 -0
  162. package/dist/error/ManagedIdentityErrorCodes.cjs +28 -0
  163. package/dist/error/ManagedIdentityErrorCodes.cjs.map +1 -0
  164. package/dist/error/ManagedIdentityErrorCodes.d.ts +10 -0
  165. package/dist/error/ManagedIdentityErrorCodes.d.ts.map +1 -0
  166. package/dist/error/ManagedIdentityErrorCodes.mjs +18 -0
  167. package/dist/error/ManagedIdentityErrorCodes.mjs.map +1 -0
  168. package/dist/error/NodeAuthError.cjs +85 -85
  169. package/dist/error/NodeAuthError.cjs.map +1 -1
  170. package/dist/error/NodeAuthError.d.ts +65 -65
  171. package/dist/error/NodeAuthError.mjs +85 -85
  172. package/dist/error/NodeAuthError.mjs.map +1 -1
  173. package/dist/index.cjs +3 -1
  174. package/dist/index.cjs.map +1 -1
  175. package/dist/index.d.ts +44 -42
  176. package/dist/index.d.ts.map +1 -1
  177. package/dist/index.mjs +2 -1
  178. package/dist/index.mjs.map +1 -1
  179. package/dist/internals.cjs +8 -8
  180. package/dist/internals.cjs.map +1 -1
  181. package/dist/internals.d.ts +6 -6
  182. package/dist/internals.mjs +8 -8
  183. package/dist/internals.mjs.map +1 -1
  184. package/dist/network/HttpClient.cjs +283 -276
  185. package/dist/network/HttpClient.cjs.map +1 -1
  186. package/dist/network/HttpClient.d.ts +26 -25
  187. package/dist/network/HttpClient.d.ts.map +1 -1
  188. package/dist/network/HttpClient.mjs +283 -276
  189. package/dist/network/HttpClient.mjs.map +1 -1
  190. package/dist/network/ILoopbackClient.d.ts +10 -10
  191. package/dist/network/LoopbackClient.cjs +77 -77
  192. package/dist/network/LoopbackClient.cjs.map +1 -1
  193. package/dist/network/LoopbackClient.d.ts +21 -21
  194. package/dist/network/LoopbackClient.mjs +77 -77
  195. package/dist/network/LoopbackClient.mjs.map +1 -1
  196. package/dist/packageMetadata.cjs +4 -4
  197. package/dist/packageMetadata.cjs.map +1 -1
  198. package/dist/packageMetadata.d.ts +2 -2
  199. package/dist/packageMetadata.d.ts.map +1 -1
  200. package/dist/packageMetadata.mjs +4 -4
  201. package/dist/packageMetadata.mjs.map +1 -1
  202. package/dist/request/AuthorizationCodeRequest.d.ts +21 -21
  203. package/dist/request/AuthorizationUrlRequest.d.ts +33 -33
  204. package/dist/request/ClientCredentialRequest.d.ts +14 -14
  205. package/dist/request/DeviceCodeRequest.d.ts +15 -15
  206. package/dist/request/InteractiveRequest.d.ts +22 -22
  207. package/dist/request/ManagedIdentityRequest.d.ts +9 -0
  208. package/dist/request/ManagedIdentityRequest.d.ts.map +1 -0
  209. package/dist/request/ManagedIdentityRequestParams.d.ts +10 -0
  210. package/dist/request/ManagedIdentityRequestParams.d.ts.map +1 -0
  211. package/dist/request/OnBehalfOfRequest.d.ts +14 -14
  212. package/dist/request/RefreshTokenRequest.d.ts +17 -17
  213. package/dist/request/SignOutRequest.d.ts +5 -5
  214. package/dist/request/SilentFlowRequest.d.ts +16 -16
  215. package/dist/request/UsernamePasswordRequest.d.ts +19 -19
  216. package/dist/response/ManagedIdentityTokenResponse.d.ts +25 -0
  217. package/dist/response/ManagedIdentityTokenResponse.d.ts.map +1 -0
  218. package/dist/utils/Constants.cjs +109 -80
  219. package/dist/utils/Constants.cjs.map +1 -1
  220. package/dist/utils/Constants.d.ts +106 -86
  221. package/dist/utils/Constants.d.ts.map +1 -1
  222. package/dist/utils/Constants.mjs +100 -81
  223. package/dist/utils/Constants.mjs.map +1 -1
  224. package/dist/utils/EncodingUtils.cjs +44 -44
  225. package/dist/utils/EncodingUtils.cjs.map +1 -1
  226. package/dist/utils/EncodingUtils.d.ts +26 -26
  227. package/dist/utils/EncodingUtils.mjs +44 -44
  228. package/dist/utils/EncodingUtils.mjs.map +1 -1
  229. package/dist/utils/NetworkUtils.cjs +38 -38
  230. package/dist/utils/NetworkUtils.cjs.map +1 -1
  231. package/dist/utils/NetworkUtils.d.ts +5 -5
  232. package/dist/utils/NetworkUtils.mjs +38 -38
  233. package/dist/utils/NetworkUtils.mjs.map +1 -1
  234. package/package.json +3 -3
  235. package/src/cache/NodeStorage.ts +24 -7
  236. package/src/cache/TokenCache.ts +1 -1
  237. package/src/cache/serializer/Deserializer.ts +5 -0
  238. package/src/cache/serializer/Serializer.ts +5 -0
  239. package/src/cache/serializer/SerializerTypes.ts +1 -0
  240. package/src/client/ClientApplication.ts +10 -9
  241. package/src/client/ClientCredentialClient.ts +72 -27
  242. package/src/client/ConfidentialClientApplication.ts +2 -2
  243. package/src/client/DeviceCodeClient.ts +1 -1
  244. package/src/client/ManagedIdentityApplication.ts +194 -0
  245. package/src/client/ManagedIdentityClient.ts +111 -0
  246. package/src/client/ManagedIdentitySources/AppService.ts +151 -0
  247. package/src/client/ManagedIdentitySources/AzureArc.ts +208 -0
  248. package/src/client/ManagedIdentitySources/BaseManagedIdentitySource.ts +169 -0
  249. package/src/client/ManagedIdentitySources/CloudShell.ts +19 -0
  250. package/src/client/ManagedIdentitySources/Imds.ts +112 -0
  251. package/src/client/ManagedIdentitySources/ServiceFabric.ts +19 -0
  252. package/src/client/OnBehalfOfClient.ts +5 -5
  253. package/src/client/PublicClientApplication.ts +1 -1
  254. package/src/config/Configuration.ts +47 -1
  255. package/src/config/ManagedIdentityId.ts +73 -0
  256. package/src/config/ManagedIdentityRequestParameters.ts +53 -0
  257. package/src/error/ManagedIdentityError.ts +46 -0
  258. package/src/error/ManagedIdentityErrorCodes.ts +15 -0
  259. package/src/index.ts +4 -0
  260. package/src/network/HttpClient.ts +17 -5
  261. package/src/packageMetadata.ts +1 -1
  262. package/src/request/ManagedIdentityRequest.ts +15 -0
  263. package/src/request/ManagedIdentityRequestParams.ts +14 -0
  264. package/src/response/ManagedIdentityTokenResponse.ts +33 -0
  265. package/src/utils/Constants.ts +27 -0
@@ -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
+ canonicalAuthority: 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 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 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 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
+ }
@@ -0,0 +1,151 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { INetworkModule, Logger, UrlString } from "@azure/msal-common";
7
+ import { BaseManagedIdentitySource } from "./BaseManagedIdentitySource";
8
+ import {
9
+ HttpMethod,
10
+ MANAGED_IDENTITY_CLIENT_ID,
11
+ MANAGED_IDENTITY_OBJECT_ID,
12
+ MANAGED_IDENTITY_RESOURCE_ID,
13
+ ManagedIdentityIdType,
14
+ SECRET_HEADER_NAME,
15
+ } from "../../utils/Constants";
16
+ import { CryptoProvider } from "../../crypto/CryptoProvider";
17
+ import { ManagedIdentityRequestParameters } from "../../config/ManagedIdentityRequestParameters";
18
+ import { ManagedIdentityId } from "../../config/ManagedIdentityId";
19
+ import {
20
+ ManagedIdentityErrorCodes,
21
+ createManagedIdentityError,
22
+ } from "../../error/ManagedIdentityError";
23
+ import { NodeStorage } from "../../cache/NodeStorage";
24
+
25
+ // MSI Constants. Docs for MSI are available here https://docs.microsoft.com/azure/app-service/overview-managed-identity
26
+ const APP_SERVICE_MSI_API_VERSION: string = "2019-08-01";
27
+
28
+ /**
29
+ * Original source of code: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/src/AppServiceManagedIdentitySource.cs
30
+ */
31
+ export class AppService extends BaseManagedIdentitySource {
32
+ private endpoint: string;
33
+ private secret: string;
34
+
35
+ constructor(
36
+ logger: Logger,
37
+ nodeStorage: NodeStorage,
38
+ networkClient: INetworkModule,
39
+ cryptoProvider: CryptoProvider,
40
+ endpoint: string,
41
+ secret: string
42
+ ) {
43
+ super(logger, nodeStorage, networkClient, cryptoProvider);
44
+
45
+ this.endpoint = endpoint;
46
+ this.secret = secret;
47
+ }
48
+
49
+ public static tryCreate(
50
+ logger: Logger,
51
+ nodeStorage: NodeStorage,
52
+ networkClient: INetworkModule,
53
+ cryptoProvider: CryptoProvider
54
+ ): AppService | null {
55
+ const secret: string | undefined = process.env["IDENTITY_HEADER"];
56
+
57
+ const [areEnvironmentVariablesValidated, endpoint]: [
58
+ boolean,
59
+ string | undefined
60
+ ] = validateEnvironmentVariables(
61
+ process.env["IDENTITY_ENDPOINT"] || undefined,
62
+ secret,
63
+ logger
64
+ );
65
+
66
+ return areEnvironmentVariablesValidated
67
+ ? new AppService(
68
+ logger,
69
+ nodeStorage,
70
+ networkClient,
71
+ cryptoProvider,
72
+ endpoint as string,
73
+ secret as string
74
+ )
75
+ : null;
76
+ }
77
+
78
+ public createRequest(
79
+ resource: string,
80
+ managedIdentityId: ManagedIdentityId
81
+ ): ManagedIdentityRequestParameters {
82
+ const request: ManagedIdentityRequestParameters =
83
+ new ManagedIdentityRequestParameters(HttpMethod.GET, this.endpoint);
84
+
85
+ request.headers[SECRET_HEADER_NAME] = this.secret;
86
+ request.queryParameters["api-version"] = APP_SERVICE_MSI_API_VERSION;
87
+ request.queryParameters["resource"] = resource;
88
+ // bodyParameters calculated in BaseManagedIdentity.acquireTokenWithManagedIdentity
89
+
90
+ switch (managedIdentityId.idType) {
91
+ case ManagedIdentityIdType.USER_ASSIGNED_CLIENT_ID:
92
+ this.logger.info(
93
+ "[Managed Identity] Adding user assigned client id to the request."
94
+ );
95
+ request.queryParameters[MANAGED_IDENTITY_CLIENT_ID] =
96
+ managedIdentityId.id;
97
+ break;
98
+
99
+ case ManagedIdentityIdType.USER_ASSIGNED_RESOURCE_ID:
100
+ this.logger.info(
101
+ "[Managed Identity] Adding user assigned resource id to the request."
102
+ );
103
+ request.queryParameters[MANAGED_IDENTITY_RESOURCE_ID] =
104
+ managedIdentityId.id;
105
+ break;
106
+
107
+ case ManagedIdentityIdType.USER_ASSIGNED_OBJECT_ID:
108
+ this.logger.info(
109
+ "[Managed Identity] Adding user assigned object id to the request."
110
+ );
111
+ request.queryParameters[MANAGED_IDENTITY_OBJECT_ID] =
112
+ managedIdentityId.id;
113
+ break;
114
+ }
115
+
116
+ return request;
117
+ }
118
+ }
119
+
120
+ const validateEnvironmentVariables = (
121
+ endpoint: string | undefined,
122
+ secret: string | undefined,
123
+ logger: Logger
124
+ ): [boolean, string | undefined] => {
125
+ let endpointUrlString: string | undefined;
126
+
127
+ // if either of the endpoint or secret environment variables are undefined, this MSI provider is unavailable.
128
+ if (!endpoint || !secret) {
129
+ logger.info(
130
+ "[Managed Identity] App Service managed identity is unavailable because one or both of the 'IDENTITY_HEADER' and 'IDENTITY_ENDPOINT' environment variables are missing."
131
+ );
132
+ return [false, endpointUrlString];
133
+ }
134
+
135
+ try {
136
+ endpointUrlString = new UrlString(endpoint).urlString;
137
+ } catch (error) {
138
+ logger.info(
139
+ "[Managed Identity] App Service managed identity is unavailable because the 'IDENTITY_ENDPOINT' environment variable is malformed."
140
+ );
141
+
142
+ throw createManagedIdentityError(
143
+ ManagedIdentityErrorCodes.urlParseError
144
+ );
145
+ }
146
+
147
+ logger.info(
148
+ `[Managed Identity] Environment variables validation passed for App Service managed identity. Endpoint URI: ${endpointUrlString}. Creating App Service managed identity.`
149
+ );
150
+ return [true, endpointUrlString];
151
+ };
@@ -0,0 +1,208 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import {
7
+ AuthError,
8
+ ClientAuthErrorCodes,
9
+ createClientAuthError,
10
+ HttpStatus,
11
+ INetworkModule,
12
+ NetworkResponse,
13
+ NetworkRequestOptions,
14
+ Logger,
15
+ UrlString,
16
+ ServerAuthorizationTokenResponse,
17
+ } from "@azure/msal-common";
18
+ import { ManagedIdentityId } from "../../config/ManagedIdentityId";
19
+ import { ManagedIdentityRequestParameters } from "../../config/ManagedIdentityRequestParameters";
20
+ import { BaseManagedIdentitySource } from "./BaseManagedIdentitySource";
21
+ import { CryptoProvider } from "../../crypto/CryptoProvider";
22
+ import {
23
+ ManagedIdentityErrorCodes,
24
+ createManagedIdentityError,
25
+ } from "../../error/ManagedIdentityError";
26
+ import {
27
+ AUTHORIZATION_HEADER_NAME,
28
+ HttpMethod,
29
+ METADATA_HEADER_NAME,
30
+ ManagedIdentityIdType,
31
+ } from "../../utils/Constants";
32
+ import { NodeStorage } from "../../cache/NodeStorage";
33
+ import { readFileSync } from "fs";
34
+ import { ManagedIdentityTokenResponse } from "../../response/ManagedIdentityTokenResponse";
35
+
36
+ export const ARC_API_VERSION: string = "2019-11-01";
37
+
38
+ /**
39
+ * Original source of code: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/src/AzureArcManagedIdentitySource.cs
40
+ */
41
+ export class AzureArc extends BaseManagedIdentitySource {
42
+ private endpoint: string;
43
+
44
+ constructor(
45
+ logger: Logger,
46
+ nodeStorage: NodeStorage,
47
+ networkClient: INetworkModule,
48
+ cryptoProvider: CryptoProvider,
49
+ endpoint: string
50
+ ) {
51
+ super(logger, nodeStorage, networkClient, cryptoProvider);
52
+
53
+ this.endpoint = endpoint;
54
+ }
55
+
56
+ public static tryCreate(
57
+ logger: Logger,
58
+ nodeStorage: NodeStorage,
59
+ networkClient: INetworkModule,
60
+ cryptoProvider: CryptoProvider
61
+ ): AzureArc | null {
62
+ const imdsEndpoint: string | undefined = process.env["IMDS_ENDPOINT"];
63
+
64
+ const [areEnvironmentVariablesValidated, endpoint]: [
65
+ boolean,
66
+ string | undefined
67
+ ] = validateEnvironmentVariables(
68
+ process.env["IDENTITY_ENDPOINT"] || undefined,
69
+ imdsEndpoint,
70
+ logger
71
+ );
72
+
73
+ return areEnvironmentVariablesValidated
74
+ ? new AzureArc(
75
+ logger,
76
+ nodeStorage,
77
+ networkClient,
78
+ cryptoProvider,
79
+ endpoint as string
80
+ )
81
+ : null;
82
+ }
83
+
84
+ public createRequest(
85
+ resource: string,
86
+ managedIdentityId: ManagedIdentityId
87
+ ): ManagedIdentityRequestParameters {
88
+ if (
89
+ managedIdentityId.idType !== ManagedIdentityIdType.SYSTEM_ASSIGNED
90
+ ) {
91
+ throw createManagedIdentityError(
92
+ ManagedIdentityErrorCodes.unableToCreateAzureArc
93
+ );
94
+ }
95
+
96
+ const request: ManagedIdentityRequestParameters =
97
+ new ManagedIdentityRequestParameters(
98
+ HttpMethod.GET,
99
+ this.endpoint.replace("localhost", "127.0.0.1")
100
+ );
101
+
102
+ request.headers[METADATA_HEADER_NAME] = "true";
103
+ request.queryParameters["api-version"] = ARC_API_VERSION;
104
+ request.queryParameters["resource"] = resource;
105
+ // bodyParameters calculated in BaseManagedIdentity.acquireTokenWithManagedIdentity
106
+
107
+ return request;
108
+ }
109
+
110
+ public async getServerTokenResponseAsync(
111
+ originalResponse: NetworkResponse<ManagedIdentityTokenResponse>,
112
+ networkClient: INetworkModule,
113
+ networkRequest: ManagedIdentityRequestParameters,
114
+ networkRequestOptions: NetworkRequestOptions
115
+ ): Promise<ServerAuthorizationTokenResponse> {
116
+ let retryResponse:
117
+ | NetworkResponse<ManagedIdentityTokenResponse>
118
+ | undefined;
119
+
120
+ if (originalResponse.status === HttpStatus.UNAUTHORIZED) {
121
+ const wwwAuthHeader: string =
122
+ originalResponse.headers["www-authenticate"];
123
+ if (!wwwAuthHeader) {
124
+ throw createManagedIdentityError(
125
+ ManagedIdentityErrorCodes.wwwAuthenticateHeaderMissing
126
+ );
127
+ }
128
+ if (!wwwAuthHeader.includes("Basic realm=")) {
129
+ throw createManagedIdentityError(
130
+ ManagedIdentityErrorCodes.wwwAuthenticateHeaderUnsupportedFormat
131
+ );
132
+ }
133
+
134
+ const secretFile = wwwAuthHeader.split("Basic realm=")[1];
135
+ let secret;
136
+ try {
137
+ secret = readFileSync(secretFile, "utf-8");
138
+ } catch (e) {
139
+ throw createManagedIdentityError(
140
+ ManagedIdentityErrorCodes.unableToReadSecretFile
141
+ );
142
+ }
143
+ const authHeaderValue = `Basic ${secret}`;
144
+
145
+ this.logger.info(
146
+ `[Managed Identity] Adding authorization header to the request.`
147
+ );
148
+ networkRequest.headers[AUTHORIZATION_HEADER_NAME] = authHeaderValue;
149
+
150
+ try {
151
+ retryResponse =
152
+ await networkClient.sendGetRequestAsync<ManagedIdentityTokenResponse>(
153
+ networkRequest.computeUri(),
154
+ networkRequestOptions
155
+ );
156
+ } catch (error) {
157
+ if (error instanceof AuthError) {
158
+ throw error;
159
+ } else {
160
+ throw createClientAuthError(
161
+ ClientAuthErrorCodes.networkError
162
+ );
163
+ }
164
+ }
165
+ }
166
+
167
+ return this.getServerTokenResponse(retryResponse || originalResponse);
168
+ }
169
+ }
170
+
171
+ const validateEnvironmentVariables = (
172
+ identityEndpoint: string | undefined,
173
+ imdsEndpoint: string | undefined,
174
+ logger: Logger
175
+ ): [boolean, string | undefined] => {
176
+ let endpointUrlString: string | undefined;
177
+
178
+ // if either of the identity or imds endpoints are undefined, this MSI provider is unavailable.
179
+ if (!identityEndpoint || !imdsEndpoint) {
180
+ logger.info(
181
+ "[Managed Identity] Azure Arc managed identity is unavailable because one or both of the 'IDENTITY_ENDPOINT' and 'IMDS_ENDPOINT' environment variables are missing."
182
+ );
183
+ return [false, endpointUrlString];
184
+ }
185
+
186
+ try {
187
+ endpointUrlString = new UrlString(identityEndpoint).urlString;
188
+ } catch (error) {
189
+ logger.info(
190
+ "[Managed Identity] App service managed identity is unavailable because the 'IDENTITY_ENDPOINT' environment variable is malformed."
191
+ );
192
+
193
+ throw createManagedIdentityError(
194
+ ManagedIdentityErrorCodes.urlParseError
195
+ );
196
+ }
197
+
198
+ logger.info(
199
+ `[Managed Identity] Environment variables validation passed for Azure Arc managed identity. Endpoint URI: ${endpointUrlString}. Creating Azure Arc managed identity.`
200
+ );
201
+ return [
202
+ true,
203
+ // remove trailing slash
204
+ endpointUrlString.endsWith("/")
205
+ ? endpointUrlString.slice(0, -1)
206
+ : endpointUrlString,
207
+ ];
208
+ };