@azure/msal-node 2.6.0 → 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 (210) hide show
  1. package/dist/cache/ITokenCache.d.ts +15 -15
  2. package/dist/cache/NodeStorage.cjs +423 -423
  3. package/dist/cache/NodeStorage.cjs.map +1 -1
  4. package/dist/cache/NodeStorage.d.ts +193 -193
  5. package/dist/cache/NodeStorage.d.ts.map +1 -1
  6. package/dist/cache/NodeStorage.mjs +424 -424
  7. package/dist/cache/NodeStorage.mjs.map +1 -1
  8. package/dist/cache/TokenCache.cjs +265 -265
  9. package/dist/cache/TokenCache.d.ts +90 -90
  10. package/dist/cache/TokenCache.mjs +265 -265
  11. package/dist/cache/distributed/DistributedCachePlugin.cjs +26 -26
  12. package/dist/cache/distributed/DistributedCachePlugin.d.ts +10 -10
  13. package/dist/cache/distributed/DistributedCachePlugin.mjs +26 -26
  14. package/dist/cache/distributed/ICacheClient.d.ts +17 -17
  15. package/dist/cache/distributed/IPartitionManager.d.ts +28 -28
  16. package/dist/cache/serializer/Deserializer.cjs +170 -170
  17. package/dist/cache/serializer/Deserializer.d.ts +42 -42
  18. package/dist/cache/serializer/Deserializer.mjs +170 -170
  19. package/dist/cache/serializer/Serializer.cjs +137 -137
  20. package/dist/cache/serializer/Serializer.d.ts +39 -39
  21. package/dist/cache/serializer/Serializer.mjs +137 -137
  22. package/dist/cache/serializer/SerializerTypes.d.ts +102 -102
  23. package/dist/client/ClientApplication.cjs +333 -333
  24. package/dist/client/ClientApplication.d.ts +142 -142
  25. package/dist/client/ClientApplication.mjs +333 -333
  26. package/dist/client/ClientAssertion.cjs +116 -116
  27. package/dist/client/ClientAssertion.d.ts +46 -46
  28. package/dist/client/ClientAssertion.mjs +116 -116
  29. package/dist/client/ClientCredentialClient.cjs +194 -190
  30. package/dist/client/ClientCredentialClient.cjs.map +1 -1
  31. package/dist/client/ClientCredentialClient.d.ts +33 -33
  32. package/dist/client/ClientCredentialClient.d.ts.map +1 -1
  33. package/dist/client/ClientCredentialClient.mjs +195 -191
  34. package/dist/client/ClientCredentialClient.mjs.map +1 -1
  35. package/dist/client/ConfidentialClientApplication.cjs +163 -163
  36. package/dist/client/ConfidentialClientApplication.d.ts +58 -58
  37. package/dist/client/ConfidentialClientApplication.mjs +163 -163
  38. package/dist/client/DeviceCodeClient.cjs +207 -207
  39. package/dist/client/DeviceCodeClient.d.ts +53 -53
  40. package/dist/client/DeviceCodeClient.mjs +207 -207
  41. package/dist/client/IConfidentialClientApplication.d.ts +39 -39
  42. package/dist/client/IPublicClientApplication.d.ts +42 -42
  43. package/dist/client/ManagedIdentityApplication.cjs +96 -0
  44. package/dist/client/ManagedIdentityApplication.cjs.map +1 -0
  45. package/dist/client/ManagedIdentityApplication.d.ts +24 -0
  46. package/dist/client/ManagedIdentityApplication.d.ts.map +1 -0
  47. package/dist/client/ManagedIdentityApplication.mjs +94 -0
  48. package/dist/client/ManagedIdentityApplication.mjs.map +1 -0
  49. package/dist/client/ManagedIdentityClient.cjs +49 -0
  50. package/dist/client/ManagedIdentityClient.cjs.map +1 -0
  51. package/dist/client/ManagedIdentityClient.d.ts +25 -0
  52. package/dist/client/ManagedIdentityClient.d.ts.map +1 -0
  53. package/dist/client/ManagedIdentityClient.mjs +47 -0
  54. package/dist/client/ManagedIdentityClient.mjs.map +1 -0
  55. package/dist/client/ManagedIdentitySources/AppService.cjs +79 -0
  56. package/dist/client/ManagedIdentitySources/AppService.cjs.map +1 -0
  57. package/dist/client/ManagedIdentitySources/AppService.d.ts +17 -0
  58. package/dist/client/ManagedIdentitySources/AppService.d.ts.map +1 -0
  59. package/dist/client/ManagedIdentitySources/AppService.mjs +77 -0
  60. package/dist/client/ManagedIdentitySources/AppService.mjs.map +1 -0
  61. package/dist/client/ManagedIdentitySources/AzureArc.cjs +107 -0
  62. package/dist/client/ManagedIdentitySources/AzureArc.cjs.map +1 -0
  63. package/dist/client/ManagedIdentitySources/AzureArc.d.ts +19 -0
  64. package/dist/client/ManagedIdentitySources/AzureArc.d.ts.map +1 -0
  65. package/dist/client/ManagedIdentitySources/AzureArc.mjs +104 -0
  66. package/dist/client/ManagedIdentitySources/AzureArc.mjs.map +1 -0
  67. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs +87 -0
  68. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs.map +1 -0
  69. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts +19 -0
  70. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts.map +1 -0
  71. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs +85 -0
  72. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs.map +1 -0
  73. package/dist/client/ManagedIdentitySources/CloudShell.d.ts +7 -0
  74. package/dist/client/ManagedIdentitySources/CloudShell.d.ts.map +1 -0
  75. package/dist/client/ManagedIdentitySources/Imds.cjs +63 -0
  76. package/dist/client/ManagedIdentitySources/Imds.cjs.map +1 -0
  77. package/dist/client/ManagedIdentitySources/Imds.d.ts +13 -0
  78. package/dist/client/ManagedIdentitySources/Imds.d.ts.map +1 -0
  79. package/dist/client/ManagedIdentitySources/Imds.mjs +61 -0
  80. package/dist/client/ManagedIdentitySources/Imds.mjs.map +1 -0
  81. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts +7 -0
  82. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts.map +1 -0
  83. package/dist/client/OnBehalfOfClient.cjs +202 -202
  84. package/dist/client/OnBehalfOfClient.d.ts +47 -47
  85. package/dist/client/OnBehalfOfClient.mjs +202 -202
  86. package/dist/client/PublicClientApplication.cjs +229 -229
  87. package/dist/client/PublicClientApplication.d.ts +71 -71
  88. package/dist/client/PublicClientApplication.mjs +229 -229
  89. package/dist/client/UsernamePasswordClient.cjs +95 -95
  90. package/dist/client/UsernamePasswordClient.d.ts +25 -25
  91. package/dist/client/UsernamePasswordClient.mjs +95 -95
  92. package/dist/config/Configuration.cjs +88 -72
  93. package/dist/config/Configuration.cjs.map +1 -1
  94. package/dist/config/Configuration.d.ts +122 -107
  95. package/dist/config/Configuration.d.ts.map +1 -1
  96. package/dist/config/Configuration.mjs +88 -73
  97. package/dist/config/Configuration.mjs.map +1 -1
  98. package/dist/config/ManagedIdentityId.cjs +59 -0
  99. package/dist/config/ManagedIdentityId.cjs.map +1 -0
  100. package/dist/config/ManagedIdentityId.d.ts +12 -0
  101. package/dist/config/ManagedIdentityId.d.ts.map +1 -0
  102. package/dist/config/ManagedIdentityId.mjs +57 -0
  103. package/dist/config/ManagedIdentityId.mjs.map +1 -0
  104. package/dist/config/ManagedIdentityRequestParameters.cjs +38 -0
  105. package/dist/config/ManagedIdentityRequestParameters.cjs.map +1 -0
  106. package/dist/config/ManagedIdentityRequestParameters.d.ts +12 -0
  107. package/dist/config/ManagedIdentityRequestParameters.d.ts.map +1 -0
  108. package/dist/config/ManagedIdentityRequestParameters.mjs +36 -0
  109. package/dist/config/ManagedIdentityRequestParameters.mjs.map +1 -0
  110. package/dist/crypto/CryptoProvider.cjs +75 -75
  111. package/dist/crypto/CryptoProvider.d.ts +52 -52
  112. package/dist/crypto/CryptoProvider.mjs +75 -75
  113. package/dist/crypto/GuidGenerator.cjs +22 -22
  114. package/dist/crypto/GuidGenerator.d.ts +14 -14
  115. package/dist/crypto/GuidGenerator.mjs +22 -22
  116. package/dist/crypto/HashUtils.cjs +13 -13
  117. package/dist/crypto/HashUtils.d.ts +8 -8
  118. package/dist/crypto/HashUtils.mjs +13 -13
  119. package/dist/crypto/PkceGenerator.cjs +49 -49
  120. package/dist/crypto/PkceGenerator.d.ts +22 -22
  121. package/dist/crypto/PkceGenerator.mjs +49 -49
  122. package/dist/error/ManagedIdentityError.cjs +40 -0
  123. package/dist/error/ManagedIdentityError.cjs.map +1 -0
  124. package/dist/error/ManagedIdentityError.d.ts +22 -0
  125. package/dist/error/ManagedIdentityError.d.ts.map +1 -0
  126. package/dist/error/ManagedIdentityError.mjs +36 -0
  127. package/dist/error/ManagedIdentityError.mjs.map +1 -0
  128. package/dist/error/ManagedIdentityErrorCodes.cjs +28 -0
  129. package/dist/error/ManagedIdentityErrorCodes.cjs.map +1 -0
  130. package/dist/error/ManagedIdentityErrorCodes.d.ts +10 -0
  131. package/dist/error/ManagedIdentityErrorCodes.d.ts.map +1 -0
  132. package/dist/error/ManagedIdentityErrorCodes.mjs +18 -0
  133. package/dist/error/ManagedIdentityErrorCodes.mjs.map +1 -0
  134. package/dist/error/NodeAuthError.cjs +85 -85
  135. package/dist/error/NodeAuthError.d.ts +65 -65
  136. package/dist/error/NodeAuthError.mjs +85 -85
  137. package/dist/index.cjs +3 -1
  138. package/dist/index.cjs.map +1 -1
  139. package/dist/index.d.ts +44 -42
  140. package/dist/index.d.ts.map +1 -1
  141. package/dist/index.mjs +2 -1
  142. package/dist/index.mjs.map +1 -1
  143. package/dist/internals.cjs +8 -8
  144. package/dist/internals.d.ts +6 -6
  145. package/dist/internals.mjs +8 -8
  146. package/dist/network/HttpClient.cjs +283 -276
  147. package/dist/network/HttpClient.cjs.map +1 -1
  148. package/dist/network/HttpClient.d.ts +26 -25
  149. package/dist/network/HttpClient.d.ts.map +1 -1
  150. package/dist/network/HttpClient.mjs +283 -276
  151. package/dist/network/HttpClient.mjs.map +1 -1
  152. package/dist/network/ILoopbackClient.d.ts +10 -10
  153. package/dist/network/LoopbackClient.cjs +77 -77
  154. package/dist/network/LoopbackClient.d.ts +21 -21
  155. package/dist/network/LoopbackClient.mjs +77 -77
  156. package/dist/packageMetadata.cjs +4 -4
  157. package/dist/packageMetadata.d.ts +2 -2
  158. package/dist/packageMetadata.d.ts.map +1 -1
  159. package/dist/packageMetadata.mjs +4 -4
  160. package/dist/request/AuthorizationCodeRequest.d.ts +21 -21
  161. package/dist/request/AuthorizationUrlRequest.d.ts +33 -33
  162. package/dist/request/ClientCredentialRequest.d.ts +14 -14
  163. package/dist/request/DeviceCodeRequest.d.ts +15 -15
  164. package/dist/request/InteractiveRequest.d.ts +22 -22
  165. package/dist/request/ManagedIdentityRequest.d.ts +9 -0
  166. package/dist/request/ManagedIdentityRequest.d.ts.map +1 -0
  167. package/dist/request/ManagedIdentityRequestParams.d.ts +10 -0
  168. package/dist/request/ManagedIdentityRequestParams.d.ts.map +1 -0
  169. package/dist/request/OnBehalfOfRequest.d.ts +14 -14
  170. package/dist/request/RefreshTokenRequest.d.ts +17 -17
  171. package/dist/request/SignOutRequest.d.ts +5 -5
  172. package/dist/request/SilentFlowRequest.d.ts +16 -16
  173. package/dist/request/UsernamePasswordRequest.d.ts +19 -19
  174. package/dist/response/ManagedIdentityTokenResponse.d.ts +25 -0
  175. package/dist/response/ManagedIdentityTokenResponse.d.ts.map +1 -0
  176. package/dist/utils/Constants.cjs +109 -80
  177. package/dist/utils/Constants.cjs.map +1 -1
  178. package/dist/utils/Constants.d.ts +106 -86
  179. package/dist/utils/Constants.d.ts.map +1 -1
  180. package/dist/utils/Constants.mjs +100 -81
  181. package/dist/utils/Constants.mjs.map +1 -1
  182. package/dist/utils/EncodingUtils.cjs +44 -44
  183. package/dist/utils/EncodingUtils.d.ts +26 -26
  184. package/dist/utils/EncodingUtils.mjs +44 -44
  185. package/dist/utils/NetworkUtils.cjs +38 -38
  186. package/dist/utils/NetworkUtils.d.ts +5 -5
  187. package/dist/utils/NetworkUtils.mjs +38 -38
  188. package/package.json +3 -3
  189. package/src/cache/NodeStorage.ts +1 -4
  190. package/src/client/ClientCredentialClient.ts +65 -25
  191. package/src/client/ManagedIdentityApplication.ts +194 -0
  192. package/src/client/ManagedIdentityClient.ts +111 -0
  193. package/src/client/ManagedIdentitySources/AppService.ts +151 -0
  194. package/src/client/ManagedIdentitySources/AzureArc.ts +208 -0
  195. package/src/client/ManagedIdentitySources/BaseManagedIdentitySource.ts +169 -0
  196. package/src/client/ManagedIdentitySources/CloudShell.ts +19 -0
  197. package/src/client/ManagedIdentitySources/Imds.ts +112 -0
  198. package/src/client/ManagedIdentitySources/ServiceFabric.ts +19 -0
  199. package/src/config/Configuration.ts +47 -1
  200. package/src/config/ManagedIdentityId.ts +73 -0
  201. package/src/config/ManagedIdentityRequestParameters.ts +53 -0
  202. package/src/error/ManagedIdentityError.ts +46 -0
  203. package/src/error/ManagedIdentityErrorCodes.ts +15 -0
  204. package/src/index.ts +4 -0
  205. package/src/network/HttpClient.ts +17 -5
  206. package/src/packageMetadata.ts +1 -1
  207. package/src/request/ManagedIdentityRequest.ts +15 -0
  208. package/src/request/ManagedIdentityRequestParams.ts +14 -0
  209. package/src/response/ManagedIdentityTokenResponse.ts +33 -0
  210. package/src/utils/Constants.ts +27 -0
@@ -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
+ }
@@ -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 {
@@ -20,13 +20,16 @@ import https from "https";
20
20
  export class HttpClient implements INetworkModule {
21
21
  private proxyUrl: string;
22
22
  private customAgentOptions: http.AgentOptions | https.AgentOptions;
23
+ private managedIdentity: boolean;
23
24
 
24
25
  constructor(
25
26
  proxyUrl?: string,
26
- customAgentOptions?: http.AgentOptions | https.AgentOptions
27
+ customAgentOptions?: http.AgentOptions | https.AgentOptions,
28
+ managedIdentity?: boolean
27
29
  ) {
28
30
  this.proxyUrl = proxyUrl || "";
29
31
  this.customAgentOptions = customAgentOptions || {};
32
+ this.managedIdentity = managedIdentity || false;
30
33
  }
31
34
 
32
35
  /**
@@ -51,7 +54,9 @@ export class HttpClient implements INetworkModule {
51
54
  url,
52
55
  HttpMethod.GET,
53
56
  options,
54
- this.customAgentOptions as https.AgentOptions
57
+ this.customAgentOptions as https.AgentOptions,
58
+ undefined,
59
+ this.managedIdentity
55
60
  );
56
61
  }
57
62
  }
@@ -81,7 +86,8 @@ export class HttpClient implements INetworkModule {
81
86
  HttpMethod.POST,
82
87
  options,
83
88
  this.customAgentOptions as https.AgentOptions,
84
- cancellationToken
89
+ cancellationToken,
90
+ this.managedIdentity
85
91
  );
86
92
  }
87
93
  }
@@ -277,7 +283,8 @@ const networkRequestViaHttps = <T>(
277
283
  httpMethod: string,
278
284
  options?: NetworkRequestOptions,
279
285
  agentOptions?: https.AgentOptions,
280
- timeout?: number
286
+ timeout?: number,
287
+ managedIdentity?: boolean
281
288
  ): Promise<NetworkResponse<T>> => {
282
289
  const isPostRequest = httpMethod === HttpMethod.POST;
283
290
  const body: string = options?.body || "";
@@ -307,7 +314,12 @@ const networkRequestViaHttps = <T>(
307
314
  }
308
315
 
309
316
  return new Promise<NetworkResponse<T>>((resolve, reject) => {
310
- const request = https.request(customOptions);
317
+ let request: http.ClientRequest;
318
+ if (managedIdentity) {
319
+ request = http.request(customOptions);
320
+ } else {
321
+ request = https.request(customOptions);
322
+ }
311
323
 
312
324
  if (timeout) {
313
325
  request.on("timeout", () => {
@@ -1,3 +1,3 @@
1
1
  /* eslint-disable header/header */
2
2
  export const name = "@azure/msal-node";
3
- export const version = "2.6.0";
3
+ export const version = "2.7.0-alpha.0";
@@ -0,0 +1,15 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { CommonClientCredentialRequest } from "@azure/msal-common";
7
+ import { ManagedIdentityRequestParams } from "./ManagedIdentityRequestParams";
8
+
9
+ /**
10
+ * ManagedIdentityRequest
11
+ * - forceRefresh - forces managed identity requests to skip the cache and make network calls if true
12
+ * - resource - resource requested to access the protected API. It should be of the form "{ResourceIdUri}" or {ResourceIdUri/.default}. For instance https://management.azure.net or, for Microsoft Graph, https://graph.microsoft.com/.default
13
+ */
14
+ export type ManagedIdentityRequest = ManagedIdentityRequestParams &
15
+ CommonClientCredentialRequest;
@@ -0,0 +1,14 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * ManagedIdentityRequest
8
+ * - forceRefresh - forces managed identity requests to skip the cache and make network calls if true
9
+ * - resource - resource requested to access the protected API. It should be of the form "{ResourceIdUri}" or {ResourceIdUri/.default}. For instance https://management.azure.net or, for Microsoft Graph, https://graph.microsoft.com/.default
10
+ */
11
+ export type ManagedIdentityRequestParams = {
12
+ forceRefresh?: boolean;
13
+ resource: string;
14
+ };
@@ -0,0 +1,33 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { AuthenticationScheme } from "@azure/msal-common";
7
+
8
+ /**
9
+ * Deserialized response object from server managed identity request.
10
+ *
11
+ * In case of success:
12
+ * - access_token - The requested access token. When called via a secured REST API, the token is embedded in the Authorization request header field as a "bearer" token, allowing the API to authenticate the caller
13
+ * - client_id - A unique identifier generated by Azure AD for the Azure Resource. The Client ID is a GUID value that uniquely identifies the application and its configuration within the identity platform
14
+ * - expires_on - The timespan when the access token expires. The date is represented as the number of seconds from "1970-01-01T0:0:0Z UTC" (corresponds to the token's exp claim)
15
+ * - resource - The resource the access token was requested for. It matches the resource query string parameter of the request
16
+ * - token_type - The type of token returned by the Managed Identity endpoint. It's a "Bearer" access token, which means the resource can give access to the bearer of this token
17
+ *
18
+ * In case of error:
19
+ * - message: A specific error message that can help a developer identify the root cause of an authentication error.
20
+ * - correlationId: A unique identifier for the request that can help in diagnostics across components.
21
+ */
22
+ export type ManagedIdentityTokenResponse = {
23
+ // success
24
+ access_token?: string;
25
+ client_id?: string;
26
+ expires_on?: number; // will be converted to expires_in
27
+ resource?: string; // equivalent to ServerAuthorizationTokenResponse's "scope" field
28
+ token_type?: AuthenticationScheme;
29
+
30
+ // error
31
+ message?: string; // will be converted to error
32
+ correlationId?: string; // will be converted to correlation_id
33
+ };
@@ -3,6 +3,33 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
+ // MSI Constants. Docs for MSI are available here https://docs.microsoft.com/azure/app-service/overview-managed-identity
7
+ export const AUTHORIZATION_HEADER_NAME: string = "Authorization";
8
+ export const METADATA_HEADER_NAME: string = "Metadata";
9
+ export const SECRET_HEADER_NAME: string = "X-IDENTITY-HEADER";
10
+
11
+ export const MANAGED_IDENTITY_CLIENT_ID = "client_id";
12
+ export const MANAGED_IDENTITY_OBJECT_ID = "object_id";
13
+ export const MANAGED_IDENTITY_RESOURCE_ID = "mi_res_id";
14
+ export const DEFAULT_MANAGED_IDENTITY_ID = "system_assigned_managed_identity";
15
+ export const MANAGED_IDENTITY_DEFAULT_TENANT = "managed_identity";
16
+ export const DEFAULT_AUTHORITY_FOR_MANAGED_IDENTITY = `https://login.microsoftonline.com/${MANAGED_IDENTITY_DEFAULT_TENANT}/`;
17
+
18
+ export const MANAGED_IDENTITY_TIMEOUT_ERROR: string =
19
+ "[Managed Identity] Authentication unavailable. The request to the managed identity endpoint timed out.";
20
+
21
+ /**
22
+ * Managed Identity Ids
23
+ */
24
+ export const ManagedIdentityIdType = {
25
+ SYSTEM_ASSIGNED: "system-assigned",
26
+ USER_ASSIGNED_CLIENT_ID: "user-assigned-client-id",
27
+ USER_ASSIGNED_RESOURCE_ID: "user-assigned-resource-id",
28
+ USER_ASSIGNED_OBJECT_ID: "user-assigned-object-id",
29
+ } as const;
30
+ export type ManagedIdentityIdType =
31
+ (typeof ManagedIdentityIdType)[keyof typeof ManagedIdentityIdType];
32
+
6
33
  /**
7
34
  * http methods
8
35
  */