@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,169 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import {
7
+ AuthError,
8
+ Authority,
9
+ ClientAuthErrorCodes,
10
+ Constants,
11
+ HeaderNames,
12
+ INetworkModule,
13
+ Logger,
14
+ NetworkRequestOptions,
15
+ NetworkResponse,
16
+ ResponseHandler,
17
+ ServerAuthorizationTokenResponse,
18
+ TimeUtils,
19
+ createClientAuthError,
20
+ AuthenticationResult,
21
+ } from "@azure/msal-common";
22
+ import { ManagedIdentityId } from "../../config/ManagedIdentityId";
23
+ import { ManagedIdentityRequestParameters } from "../../config/ManagedIdentityRequestParameters";
24
+ import { CryptoProvider } from "../../crypto/CryptoProvider";
25
+ import { ManagedIdentityRequest } from "../../request/ManagedIdentityRequest";
26
+ import { HttpMethod } from "../../utils/Constants";
27
+ import { ManagedIdentityTokenResponse } from "../../response/ManagedIdentityTokenResponse";
28
+ import { NodeStorage } from "../../cache/NodeStorage";
29
+
30
+ export abstract class BaseManagedIdentitySource {
31
+ protected logger: Logger;
32
+ private nodeStorage: NodeStorage;
33
+ private networkClient: INetworkModule;
34
+ private cryptoProvider: CryptoProvider;
35
+
36
+ constructor(
37
+ logger: Logger,
38
+ nodeStorage: NodeStorage,
39
+ networkClient: INetworkModule,
40
+ cryptoProvider: CryptoProvider
41
+ ) {
42
+ this.logger = logger;
43
+ this.nodeStorage = nodeStorage;
44
+ this.networkClient = networkClient;
45
+ this.cryptoProvider = cryptoProvider;
46
+ }
47
+
48
+ abstract createRequest(
49
+ request: string,
50
+ managedIdentityId: ManagedIdentityId
51
+ ): ManagedIdentityRequestParameters;
52
+
53
+ public async getServerTokenResponseAsync(
54
+ response: NetworkResponse<ManagedIdentityTokenResponse>,
55
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
56
+ _networkClient: INetworkModule,
57
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
58
+ _networkRequest: ManagedIdentityRequestParameters,
59
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
60
+ _networkRequestOptions: NetworkRequestOptions
61
+ ): Promise<ServerAuthorizationTokenResponse> {
62
+ return this.getServerTokenResponse(response);
63
+ }
64
+
65
+ public getServerTokenResponse(
66
+ response: NetworkResponse<ManagedIdentityTokenResponse>
67
+ ): ServerAuthorizationTokenResponse {
68
+ const serverTokenResponse: ServerAuthorizationTokenResponse = {
69
+ status: response.status,
70
+
71
+ // success
72
+ access_token: response.body.access_token,
73
+ expires_in: response.body.expires_on,
74
+ scope: response.body.resource,
75
+ token_type: response.body.token_type,
76
+
77
+ // error
78
+ error: response.body.message,
79
+ correlation_id: response.body.correlationId,
80
+ };
81
+
82
+ // compute refresh_in as 1/2 of expires_in, but only if expires_in > 2h
83
+ if (
84
+ serverTokenResponse.expires_in &&
85
+ serverTokenResponse.expires_in > 2 * 3600
86
+ ) {
87
+ serverTokenResponse.refresh_in = serverTokenResponse.expires_in / 2;
88
+ }
89
+
90
+ return serverTokenResponse;
91
+ }
92
+
93
+ public async acquireTokenWithManagedIdentity(
94
+ managedIdentityRequest: ManagedIdentityRequest,
95
+ managedIdentityId: ManagedIdentityId,
96
+ fakeAuthority: Authority,
97
+ refreshAccessToken?: boolean
98
+ ): Promise<AuthenticationResult> {
99
+ const networkRequest: ManagedIdentityRequestParameters =
100
+ this.createRequest(
101
+ managedIdentityRequest.resource,
102
+ managedIdentityId
103
+ );
104
+
105
+ const headers: Record<string, string> = networkRequest.headers;
106
+ headers[HeaderNames.CONTENT_TYPE] = Constants.URL_FORM_CONTENT_TYPE;
107
+ const networkRequestOptions: NetworkRequestOptions = { headers };
108
+ if (managedIdentityRequest.forceRefresh) {
109
+ networkRequestOptions.body =
110
+ networkRequest.computeParametersBodyString();
111
+ }
112
+
113
+ const reqTimestamp = TimeUtils.nowSeconds();
114
+ let response: NetworkResponse<ManagedIdentityTokenResponse>;
115
+ try {
116
+ // Sources that send POST requests: Cloud Shell
117
+ if (networkRequest.httpMethod === HttpMethod.POST) {
118
+ response =
119
+ await this.networkClient.sendPostRequestAsync<ManagedIdentityTokenResponse>(
120
+ networkRequest.computeUri(),
121
+ networkRequestOptions
122
+ );
123
+ // Sources that send GET requests: App Service, Azure Arc, IMDS, Service Fabric
124
+ } else {
125
+ response =
126
+ await this.networkClient.sendGetRequestAsync<ManagedIdentityTokenResponse>(
127
+ networkRequest.computeUri(),
128
+ networkRequestOptions
129
+ );
130
+ }
131
+ } catch (error) {
132
+ if (error instanceof AuthError) {
133
+ throw error;
134
+ } else {
135
+ throw createClientAuthError(ClientAuthErrorCodes.networkError);
136
+ }
137
+ }
138
+
139
+ const responseHandler = new ResponseHandler(
140
+ managedIdentityId.id,
141
+ this.nodeStorage,
142
+ this.cryptoProvider,
143
+ this.logger,
144
+ null,
145
+ null
146
+ );
147
+
148
+ const serverTokenResponse: ServerAuthorizationTokenResponse =
149
+ await this.getServerTokenResponseAsync(
150
+ response,
151
+ this.networkClient,
152
+ networkRequest,
153
+ networkRequestOptions
154
+ );
155
+
156
+ responseHandler.validateTokenResponse(
157
+ serverTokenResponse,
158
+ refreshAccessToken
159
+ );
160
+
161
+ // caches the token
162
+ return responseHandler.handleServerTokenResponse(
163
+ serverTokenResponse,
164
+ fakeAuthority,
165
+ reqTimestamp,
166
+ managedIdentityRequest
167
+ );
168
+ }
169
+ }
@@ -0,0 +1,19 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { ManagedIdentityId } from "../../config/ManagedIdentityId";
7
+ import { ManagedIdentityRequestParameters } from "../../config/ManagedIdentityRequestParameters";
8
+ import { BaseManagedIdentitySource } from "./BaseManagedIdentitySource";
9
+
10
+ export class CloudShell extends BaseManagedIdentitySource {
11
+ public createRequest(
12
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
13
+ _resource: string,
14
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
15
+ _managedIdentityId: ManagedIdentityId
16
+ ): ManagedIdentityRequestParameters {
17
+ throw new Error("Method not implemented.");
18
+ }
19
+ }
@@ -0,0 +1,112 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { INetworkModule, Logger } from "@azure/msal-common";
7
+ import { ManagedIdentityId } from "../../config/ManagedIdentityId";
8
+ import { ManagedIdentityRequestParameters } from "../../config/ManagedIdentityRequestParameters";
9
+ import { BaseManagedIdentitySource } from "./BaseManagedIdentitySource";
10
+ import { CryptoProvider } from "../../crypto/CryptoProvider";
11
+ import {
12
+ HttpMethod,
13
+ MANAGED_IDENTITY_CLIENT_ID,
14
+ MANAGED_IDENTITY_OBJECT_ID,
15
+ MANAGED_IDENTITY_RESOURCE_ID,
16
+ METADATA_HEADER_NAME,
17
+ ManagedIdentityIdType,
18
+ } from "../../utils/Constants";
19
+ import { NodeStorage } from "../../cache/NodeStorage";
20
+
21
+ // IMDS constants. Docs for IMDS are available here https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/how-to-use-vm-token#get-a-token-using-http
22
+ const IMDS_TOKEN_PATH: string = "/metadata/identity/oauth2/token";
23
+ const DEFAULT_IMDS_ENDPOINT: string = `http://169.254.169.254${IMDS_TOKEN_PATH}`;
24
+
25
+ const IMDS_API_VERSION: string = "2018-02-01";
26
+
27
+ // Original source of code: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/src/ImdsManagedIdentitySource.cs
28
+ export class Imds extends BaseManagedIdentitySource {
29
+ private endpoint: string;
30
+
31
+ constructor(
32
+ logger: Logger,
33
+ nodeStorage: NodeStorage,
34
+ networkClient: INetworkModule,
35
+ cryptoProvider: CryptoProvider,
36
+ endpoint: string
37
+ ) {
38
+ super(logger, nodeStorage, networkClient, cryptoProvider);
39
+
40
+ this.endpoint = endpoint;
41
+ }
42
+
43
+ public static tryCreate(
44
+ logger: Logger,
45
+ nodeStorage: NodeStorage,
46
+ networkClient: INetworkModule,
47
+ cryptoProvider: CryptoProvider
48
+ ): Imds {
49
+ let endpoint: string;
50
+
51
+ if (process.env["AZURE_POD_IDENTITY_AUTHORITY_HOST"]) {
52
+ logger.info(
53
+ `[Managed Identity] Environment variable AZURE_POD_IDENTITY_AUTHORITY_HOST for IMDS returned endpoint: ${process.env["AZURE_POD_IDENTITY_AUTHORITY_HOST"]}`
54
+ );
55
+ endpoint = `${process.env["AZURE_POD_IDENTITY_AUTHORITY_HOST"]}${IMDS_TOKEN_PATH}`;
56
+ } else {
57
+ logger.info(
58
+ "[Managed Identity] Unable to find AZURE_POD_IDENTITY_AUTHORITY_HOST environment variable for IMDS, using the default endpoint."
59
+ );
60
+ endpoint = DEFAULT_IMDS_ENDPOINT;
61
+ }
62
+
63
+ return new Imds(
64
+ logger,
65
+ nodeStorage,
66
+ networkClient,
67
+ cryptoProvider,
68
+ endpoint
69
+ );
70
+ }
71
+
72
+ public createRequest(
73
+ resource: string,
74
+ managedIdentityId: ManagedIdentityId
75
+ ): ManagedIdentityRequestParameters {
76
+ const request: ManagedIdentityRequestParameters =
77
+ new ManagedIdentityRequestParameters(HttpMethod.GET, this.endpoint);
78
+
79
+ request.headers[METADATA_HEADER_NAME] = "true";
80
+ request.queryParameters["api-version"] = IMDS_API_VERSION;
81
+ request.queryParameters["resource"] = resource;
82
+ // bodyParameters calculated in BaseManagedIdentity.acquireTokenWithManagedIdentity
83
+
84
+ switch (managedIdentityId.idType) {
85
+ case ManagedIdentityIdType.USER_ASSIGNED_CLIENT_ID:
86
+ this.logger.info(
87
+ "[Managed Identity] Adding user assigned client id to the request."
88
+ );
89
+ request.queryParameters[MANAGED_IDENTITY_CLIENT_ID] =
90
+ managedIdentityId.id;
91
+ break;
92
+
93
+ case ManagedIdentityIdType.USER_ASSIGNED_RESOURCE_ID:
94
+ this.logger.info(
95
+ "[Managed Identity] Adding user assigned resource id to the request."
96
+ );
97
+ request.queryParameters[MANAGED_IDENTITY_RESOURCE_ID] =
98
+ managedIdentityId.id;
99
+ break;
100
+
101
+ case ManagedIdentityIdType.USER_ASSIGNED_OBJECT_ID:
102
+ this.logger.info(
103
+ "[Managed Identity] Adding user assigned object id to the request."
104
+ );
105
+ request.queryParameters[MANAGED_IDENTITY_OBJECT_ID] =
106
+ managedIdentityId.id;
107
+ break;
108
+ }
109
+
110
+ return request;
111
+ }
112
+ }
@@ -0,0 +1,19 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { ManagedIdentityId } from "../../config/ManagedIdentityId";
7
+ import { ManagedIdentityRequestParameters } from "../../config/ManagedIdentityRequestParameters";
8
+ import { BaseManagedIdentitySource } from "./BaseManagedIdentitySource";
9
+
10
+ export class ServiceFabric extends BaseManagedIdentitySource {
11
+ public createRequest(
12
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
13
+ _resource: string,
14
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
15
+ _managedIdentityId: ManagedIdentityId
16
+ ): ManagedIdentityRequestParameters {
17
+ throw new Error("Method not implemented.");
18
+ }
19
+ }
@@ -59,7 +59,7 @@ export class OnBehalfOfClient extends BaseClient {
59
59
  );
60
60
 
61
61
  if (request.skipCache) {
62
- return await this.executeTokenRequest(
62
+ return this.executeTokenRequest(
63
63
  request,
64
64
  this.authority,
65
65
  this.userAssertionHash
@@ -151,7 +151,7 @@ export class OnBehalfOfClient extends BaseClient {
151
151
  this.config.serverTelemetryManager.incrementCacheHits();
152
152
  }
153
153
 
154
- return await ResponseHandler.generateAuthenticationResult(
154
+ return ResponseHandler.generateAuthenticationResult(
155
155
  this.cryptoUtils,
156
156
  this.authority,
157
157
  {
@@ -184,14 +184,14 @@ export class OnBehalfOfClient extends BaseClient {
184
184
  realm: this.authority.tenant,
185
185
  };
186
186
 
187
- const idTokens: IdTokenEntity[] =
187
+ const idTokenMap: Map<string, IdTokenEntity> =
188
188
  this.cacheManager.getIdTokensByFilter(idTokenFilter);
189
189
 
190
190
  // When acquiring a token on behalf of an application, there might not be an id token in the cache
191
- if (idTokens.length < 1) {
191
+ if (Object.values(idTokenMap).length < 1) {
192
192
  return null;
193
193
  }
194
- return idTokens[0] as IdTokenEntity;
194
+ return Object.values(idTokenMap)[0] as IdTokenEntity;
195
195
  }
196
196
 
197
197
  /**
@@ -117,7 +117,7 @@ export class PublicClientApplication
117
117
  "Device code client created",
118
118
  validRequest.correlationId
119
119
  );
120
- return deviceCodeClient.acquireToken(validRequest);
120
+ return await deviceCodeClient.acquireToken(validRequest);
121
121
  } catch (e) {
122
122
  if (e instanceof AuthError) {
123
123
  e.setCorrelationId(validRequest.correlationId);
@@ -18,6 +18,7 @@ import {
18
18
  import { HttpClient } from "../network/HttpClient.js";
19
19
  import http from "http";
20
20
  import https from "https";
21
+ import { ManagedIdentityId } from "./ManagedIdentityId.js";
21
22
 
22
23
  /**
23
24
  * - clientId - Client id of the application.
@@ -107,6 +108,17 @@ export type Configuration = {
107
108
  telemetry?: NodeTelemetryOptions;
108
109
  };
109
110
 
111
+ export type ManagedIdentityIdParams = {
112
+ userAssignedClientId?: string;
113
+ userAssignedResourceId?: string;
114
+ userAssignedObjectId?: string;
115
+ };
116
+
117
+ export type ManagedIdentityConfiguration = {
118
+ managedIdentityIdParams?: ManagedIdentityIdParams;
119
+ system?: NodeSystemOptions;
120
+ };
121
+
110
122
  const DEFAULT_AUTH_OPTIONS: Required<NodeAuthOptions> = {
111
123
  clientId: Constants.EMPTY_STRING,
112
124
  authority: Constants.DEFAULT_AUTHORITY,
@@ -186,7 +198,10 @@ export function buildAppConfiguration({
186
198
  ...DEFAULT_SYSTEM_OPTIONS,
187
199
  networkClient: new HttpClient(
188
200
  system?.proxyUrl,
189
- system?.customAgentOptions as http.AgentOptions | https.AgentOptions
201
+ system?.customAgentOptions as
202
+ | http.AgentOptions
203
+ | https.AgentOptions,
204
+ false // Managed Identity
190
205
  ),
191
206
  loggerOptions: system?.loggerOptions || DEFAULT_LOGGER_OPTIONS,
192
207
  };
@@ -199,3 +214,34 @@ export function buildAppConfiguration({
199
214
  telemetry: { ...DEFAULT_TELEMETRY_OPTIONS, ...telemetry },
200
215
  };
201
216
  }
217
+
218
+ export type ManagedIdentityNodeConfiguration = {
219
+ managedIdentityId: ManagedIdentityId;
220
+ system: Required<NodeSystemOptions>;
221
+ };
222
+
223
+ export function buildManagedIdentityConfiguration({
224
+ managedIdentityIdParams,
225
+ system,
226
+ }: ManagedIdentityConfiguration): ManagedIdentityNodeConfiguration {
227
+ const managedIdentityId: ManagedIdentityId = new ManagedIdentityId(
228
+ managedIdentityIdParams
229
+ );
230
+
231
+ const systemOptions: Required<NodeSystemOptions> = {
232
+ ...DEFAULT_SYSTEM_OPTIONS,
233
+ loggerOptions: system?.loggerOptions || DEFAULT_LOGGER_OPTIONS,
234
+ networkClient: new HttpClient(
235
+ system?.proxyUrl,
236
+ system?.customAgentOptions as
237
+ | http.AgentOptions
238
+ | https.AgentOptions,
239
+ true // Managed Identity
240
+ ),
241
+ };
242
+
243
+ return {
244
+ managedIdentityId: managedIdentityId,
245
+ system: { ...systemOptions, ...system },
246
+ };
247
+ }
@@ -0,0 +1,73 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import {
7
+ ManagedIdentityErrorCodes,
8
+ createManagedIdentityError,
9
+ } from "../error/ManagedIdentityError";
10
+ import {
11
+ DEFAULT_MANAGED_IDENTITY_ID,
12
+ ManagedIdentityIdType,
13
+ } from "../utils/Constants";
14
+ import { ManagedIdentityIdParams } from "./Configuration";
15
+
16
+ export class ManagedIdentityId {
17
+ private _id: string;
18
+ public get id(): string {
19
+ return this._id;
20
+ }
21
+ private set id(value: string) {
22
+ this._id = value;
23
+ }
24
+
25
+ private _idType: ManagedIdentityIdType;
26
+ public get idType(): ManagedIdentityIdType {
27
+ return this._idType;
28
+ }
29
+ private set idType(value: ManagedIdentityIdType) {
30
+ this._idType = value;
31
+ }
32
+
33
+ constructor(managedIdentityIdParams?: ManagedIdentityIdParams) {
34
+ const userAssignedClientId =
35
+ managedIdentityIdParams?.userAssignedClientId;
36
+ const userAssignedResourceId =
37
+ managedIdentityIdParams?.userAssignedResourceId;
38
+ const userAssignedObjectId =
39
+ managedIdentityIdParams?.userAssignedObjectId;
40
+
41
+ if (userAssignedClientId) {
42
+ if (userAssignedResourceId || userAssignedObjectId) {
43
+ throw createManagedIdentityError(
44
+ ManagedIdentityErrorCodes.invalidManagedIdentityIdType
45
+ );
46
+ }
47
+
48
+ this.id = userAssignedClientId;
49
+ this.idType = ManagedIdentityIdType.USER_ASSIGNED_CLIENT_ID;
50
+ } else if (userAssignedResourceId) {
51
+ if (userAssignedClientId || userAssignedObjectId) {
52
+ throw createManagedIdentityError(
53
+ ManagedIdentityErrorCodes.invalidManagedIdentityIdType
54
+ );
55
+ }
56
+
57
+ this.id = userAssignedResourceId;
58
+ this.idType = ManagedIdentityIdType.USER_ASSIGNED_RESOURCE_ID;
59
+ } else if (userAssignedObjectId) {
60
+ if (userAssignedClientId || userAssignedResourceId) {
61
+ throw createManagedIdentityError(
62
+ ManagedIdentityErrorCodes.invalidManagedIdentityIdType
63
+ );
64
+ }
65
+
66
+ this.id = userAssignedObjectId;
67
+ this.idType = ManagedIdentityIdType.USER_ASSIGNED_OBJECT_ID;
68
+ } else {
69
+ this.id = DEFAULT_MANAGED_IDENTITY_ID;
70
+ this.idType = ManagedIdentityIdType.SYSTEM_ASSIGNED;
71
+ }
72
+ }
73
+ }
@@ -0,0 +1,53 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { RequestParameterBuilder, UrlString } from "@azure/msal-common";
7
+ import { HttpMethod } from "../utils/Constants";
8
+
9
+ export class ManagedIdentityRequestParameters {
10
+ private _baseEndpoint: string;
11
+ public httpMethod: HttpMethod;
12
+ public headers: Record<string, string>;
13
+ public bodyParameters: Record<string, string>;
14
+ public queryParameters: Record<string, string>;
15
+
16
+ constructor(httpMethod: HttpMethod, endpoint: string) {
17
+ this.httpMethod = httpMethod;
18
+ this._baseEndpoint = endpoint;
19
+ this.headers = {} as Record<string, string>;
20
+ this.bodyParameters = {} as Record<string, string>;
21
+ this.queryParameters = {} as Record<string, string>;
22
+ }
23
+
24
+ public computeUri(): string {
25
+ const parameterBuilder = new RequestParameterBuilder();
26
+
27
+ if (this.queryParameters) {
28
+ parameterBuilder.addExtraQueryParameters(this.queryParameters);
29
+ }
30
+
31
+ const queryParametersString = parameterBuilder.createQueryString();
32
+
33
+ return UrlString.appendQueryString(
34
+ this._baseEndpoint,
35
+ queryParametersString
36
+ );
37
+ }
38
+
39
+ public computeParametersBodyString(): string {
40
+ const parameterBuilder = new RequestParameterBuilder();
41
+
42
+ if (this.bodyParameters) {
43
+ parameterBuilder.addExtraQueryParameters(this.bodyParameters);
44
+ }
45
+
46
+ const bodyParametersString = parameterBuilder.createQueryString();
47
+
48
+ return UrlString.appendQueryString(
49
+ this._baseEndpoint,
50
+ bodyParametersString
51
+ );
52
+ }
53
+ }
@@ -0,0 +1,46 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { AuthError } from "@azure/msal-common";
7
+ import * as ManagedIdentityErrorCodes from "./ManagedIdentityErrorCodes";
8
+ export { ManagedIdentityErrorCodes };
9
+
10
+ /**
11
+ * ManagedIdentityErrorMessage class containing string constants used by error codes and messages.
12
+ */
13
+ export const ManagedIdentityErrorMessages = {
14
+ [ManagedIdentityErrorCodes.invalidManagedIdentityIdType]:
15
+ "More than one ManagedIdentityIdType was provided.",
16
+ [ManagedIdentityErrorCodes.invalidResource]:
17
+ "The supplied resource is an invalid URL.",
18
+ [ManagedIdentityErrorCodes.missingId]:
19
+ "A ManagedIdentityId id was not provided.",
20
+ [ManagedIdentityErrorCodes.unableToCreateAzureArc]:
21
+ "Azure Arc Managed Identities can only be system assigned.",
22
+ [ManagedIdentityErrorCodes.unableToCreateSource]:
23
+ "Unable to create a Managed Identity source based on environment variables.",
24
+ [ManagedIdentityErrorCodes.unableToReadSecretFile]:
25
+ "Unable to read the secret file.",
26
+ [ManagedIdentityErrorCodes.urlParseError]:
27
+ "The Managed Identity's 'IDENTITY_ENDPOINT' environment variable is malformed.",
28
+ [ManagedIdentityErrorCodes.wwwAuthenticateHeaderMissing]:
29
+ "A 401 response was received form the Azure Arc Managed Identity, but the www-authenticate header is missing.",
30
+ [ManagedIdentityErrorCodes.wwwAuthenticateHeaderUnsupportedFormat]:
31
+ "A 401 response was received form the Azure Arc Managed Identity, but the www-authenticate header is in an unsupported format.",
32
+ };
33
+
34
+ export class ManagedIdentityError extends AuthError {
35
+ constructor(errorCode: string) {
36
+ super(errorCode, ManagedIdentityErrorMessages[errorCode]);
37
+ this.name = "ManagedIdentityError";
38
+ Object.setPrototypeOf(this, ManagedIdentityError.prototype);
39
+ }
40
+ }
41
+
42
+ export function createManagedIdentityError(
43
+ errorCode: string
44
+ ): ManagedIdentityError {
45
+ return new ManagedIdentityError(errorCode);
46
+ }
@@ -0,0 +1,15 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ export const invalidManagedIdentityIdType = "invalid_managed_identity_id_type";
7
+ export const invalidResource = "invalid_resource";
8
+ export const missingId = "missing_client_id";
9
+ export const unableToCreateAzureArc = "unable_to_create_azure_arc";
10
+ export const unableToCreateSource = "unable_to_create_source";
11
+ export const unableToReadSecretFile = "unable_to_read_secret_file";
12
+ export const urlParseError = "url_parse_error";
13
+ export const wwwAuthenticateHeaderMissing = "www_authenticate_header_missing";
14
+ export const wwwAuthenticateHeaderUnsupportedFormat =
15
+ "www_authenticate_header_unsupported_format";
package/src/index.ts CHANGED
@@ -31,10 +31,13 @@ export { ClientApplication } from "./client/ClientApplication.js";
31
31
  export { ClientCredentialClient } from "./client/ClientCredentialClient.js";
32
32
  export { DeviceCodeClient } from "./client/DeviceCodeClient.js";
33
33
  export { OnBehalfOfClient } from "./client/OnBehalfOfClient.js";
34
+ export { ManagedIdentityApplication } from "./client/ManagedIdentityApplication.js";
34
35
  export { UsernamePasswordClient } from "./client/UsernamePasswordClient.js";
35
36
 
36
37
  export {
37
38
  Configuration,
39
+ ManagedIdentityConfiguration,
40
+ ManagedIdentityIdParams,
38
41
  buildAppConfiguration,
39
42
  NodeAuthOptions,
40
43
  NodeSystemOptions,
@@ -73,6 +76,7 @@ export type { RefreshTokenRequest } from "./request/RefreshTokenRequest.js";
73
76
  export type { SilentFlowRequest } from "./request/SilentFlowRequest.js";
74
77
  export type { InteractiveRequest } from "./request/InteractiveRequest.js";
75
78
  export type { SignOutRequest } from "./request/SignOutRequest.js";
79
+ export type { ManagedIdentityRequestParams } from "./request/ManagedIdentityRequestParams.js";
76
80
 
77
81
  // Common Object Formats
78
82
  export {