@azure/msal-node 2.7.0-alpha.0 → 2.7.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 (225) 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 +167 -170
  17. package/dist/cache/serializer/Deserializer.cjs.map +1 -1
  18. package/dist/cache/serializer/Deserializer.d.ts +42 -42
  19. package/dist/cache/serializer/Deserializer.d.ts.map +1 -1
  20. package/dist/cache/serializer/Deserializer.mjs +168 -171
  21. package/dist/cache/serializer/Deserializer.mjs.map +1 -1
  22. package/dist/cache/serializer/Serializer.cjs +137 -137
  23. package/dist/cache/serializer/Serializer.d.ts +39 -39
  24. package/dist/cache/serializer/Serializer.mjs +137 -137
  25. package/dist/cache/serializer/SerializerTypes.d.ts +102 -102
  26. package/dist/client/ClientApplication.cjs +333 -333
  27. package/dist/client/ClientApplication.cjs.map +1 -1
  28. package/dist/client/ClientApplication.d.ts +142 -142
  29. package/dist/client/ClientApplication.d.ts.map +1 -1
  30. package/dist/client/ClientApplication.mjs +333 -333
  31. package/dist/client/ClientApplication.mjs.map +1 -1
  32. package/dist/client/ClientAssertion.cjs +116 -116
  33. package/dist/client/ClientAssertion.d.ts +46 -46
  34. package/dist/client/ClientAssertion.mjs +116 -116
  35. package/dist/client/ClientCredentialClient.cjs +194 -194
  36. package/dist/client/ClientCredentialClient.d.ts +33 -33
  37. package/dist/client/ClientCredentialClient.mjs +194 -194
  38. package/dist/client/ConfidentialClientApplication.cjs +163 -163
  39. package/dist/client/ConfidentialClientApplication.d.ts +58 -58
  40. package/dist/client/ConfidentialClientApplication.mjs +163 -163
  41. package/dist/client/DeviceCodeClient.cjs +207 -207
  42. package/dist/client/DeviceCodeClient.d.ts +53 -53
  43. package/dist/client/DeviceCodeClient.mjs +207 -207
  44. package/dist/client/IConfidentialClientApplication.d.ts +39 -39
  45. package/dist/client/IPublicClientApplication.d.ts +42 -42
  46. package/dist/client/ManagedIdentityApplication.cjs +79 -78
  47. package/dist/client/ManagedIdentityApplication.cjs.map +1 -1
  48. package/dist/client/ManagedIdentityApplication.d.ts +23 -23
  49. package/dist/client/ManagedIdentityApplication.d.ts.map +1 -1
  50. package/dist/client/ManagedIdentityApplication.mjs +79 -78
  51. package/dist/client/ManagedIdentityApplication.mjs.map +1 -1
  52. package/dist/client/ManagedIdentityClient.cjs +40 -36
  53. package/dist/client/ManagedIdentityClient.cjs.map +1 -1
  54. package/dist/client/ManagedIdentityClient.d.ts +19 -24
  55. package/dist/client/ManagedIdentityClient.d.ts.map +1 -1
  56. package/dist/client/ManagedIdentityClient.mjs +40 -36
  57. package/dist/client/ManagedIdentityClient.mjs.map +1 -1
  58. package/dist/client/ManagedIdentitySources/AppService.cjs +42 -69
  59. package/dist/client/ManagedIdentitySources/AppService.cjs.map +1 -1
  60. package/dist/client/ManagedIdentitySources/AppService.d.ts +16 -16
  61. package/dist/client/ManagedIdentitySources/AppService.d.ts.map +1 -1
  62. package/dist/client/ManagedIdentitySources/AppService.mjs +43 -70
  63. package/dist/client/ManagedIdentitySources/AppService.mjs.map +1 -1
  64. package/dist/client/ManagedIdentitySources/AzureArc.cjs +81 -92
  65. package/dist/client/ManagedIdentitySources/AzureArc.cjs.map +1 -1
  66. package/dist/client/ManagedIdentitySources/AzureArc.d.ts +18 -18
  67. package/dist/client/ManagedIdentitySources/AzureArc.d.ts.map +1 -1
  68. package/dist/client/ManagedIdentitySources/AzureArc.mjs +84 -95
  69. package/dist/client/ManagedIdentitySources/AzureArc.mjs.map +1 -1
  70. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs +112 -77
  71. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs.map +1 -1
  72. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts +30 -18
  73. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts.map +1 -1
  74. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs +114 -80
  75. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs.map +1 -1
  76. package/dist/client/ManagedIdentitySources/CloudShell.cjs +47 -0
  77. package/dist/client/ManagedIdentitySources/CloudShell.cjs.map +1 -0
  78. package/dist/client/ManagedIdentitySources/CloudShell.d.ts +15 -6
  79. package/dist/client/ManagedIdentitySources/CloudShell.d.ts.map +1 -1
  80. package/dist/client/ManagedIdentitySources/CloudShell.mjs +45 -0
  81. package/dist/client/ManagedIdentitySources/CloudShell.mjs.map +1 -0
  82. package/dist/client/ManagedIdentitySources/Imds.cjs +43 -52
  83. package/dist/client/ManagedIdentitySources/Imds.cjs.map +1 -1
  84. package/dist/client/ManagedIdentitySources/Imds.d.ts +12 -12
  85. package/dist/client/ManagedIdentitySources/Imds.d.ts.map +1 -1
  86. package/dist/client/ManagedIdentitySources/Imds.mjs +44 -53
  87. package/dist/client/ManagedIdentitySources/Imds.mjs.map +1 -1
  88. package/dist/client/ManagedIdentitySources/ServiceFabric.cjs +61 -0
  89. package/dist/client/ManagedIdentitySources/ServiceFabric.cjs.map +1 -0
  90. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts +16 -6
  91. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts.map +1 -1
  92. package/dist/client/ManagedIdentitySources/ServiceFabric.mjs +59 -0
  93. package/dist/client/ManagedIdentitySources/ServiceFabric.mjs.map +1 -0
  94. package/dist/client/OnBehalfOfClient.cjs +202 -202
  95. package/dist/client/OnBehalfOfClient.d.ts +47 -47
  96. package/dist/client/OnBehalfOfClient.mjs +202 -202
  97. package/dist/client/PublicClientApplication.cjs +229 -229
  98. package/dist/client/PublicClientApplication.d.ts +71 -71
  99. package/dist/client/PublicClientApplication.mjs +229 -229
  100. package/dist/client/UsernamePasswordClient.cjs +95 -95
  101. package/dist/client/UsernamePasswordClient.d.ts +25 -25
  102. package/dist/client/UsernamePasswordClient.mjs +95 -95
  103. package/dist/config/Configuration.cjs +102 -86
  104. package/dist/config/Configuration.cjs.map +1 -1
  105. package/dist/config/Configuration.d.ts +123 -122
  106. package/dist/config/Configuration.d.ts.map +1 -1
  107. package/dist/config/Configuration.mjs +102 -86
  108. package/dist/config/Configuration.mjs.map +1 -1
  109. package/dist/config/ManagedIdentityId.cjs +48 -48
  110. package/dist/config/ManagedIdentityId.d.ts +11 -11
  111. package/dist/config/ManagedIdentityId.mjs +48 -48
  112. package/dist/config/ManagedIdentityRequestParameters.cjs +28 -29
  113. package/dist/config/ManagedIdentityRequestParameters.cjs.map +1 -1
  114. package/dist/config/ManagedIdentityRequestParameters.d.ts +11 -11
  115. package/dist/config/ManagedIdentityRequestParameters.d.ts.map +1 -1
  116. package/dist/config/ManagedIdentityRequestParameters.mjs +28 -29
  117. package/dist/config/ManagedIdentityRequestParameters.mjs.map +1 -1
  118. package/dist/crypto/CryptoProvider.cjs +75 -75
  119. package/dist/crypto/CryptoProvider.d.ts +52 -52
  120. package/dist/crypto/CryptoProvider.mjs +75 -75
  121. package/dist/crypto/GuidGenerator.cjs +22 -22
  122. package/dist/crypto/GuidGenerator.d.ts +14 -14
  123. package/dist/crypto/GuidGenerator.mjs +22 -22
  124. package/dist/crypto/HashUtils.cjs +13 -13
  125. package/dist/crypto/HashUtils.d.ts +8 -8
  126. package/dist/crypto/HashUtils.mjs +13 -13
  127. package/dist/crypto/PkceGenerator.cjs +49 -49
  128. package/dist/crypto/PkceGenerator.d.ts +22 -22
  129. package/dist/crypto/PkceGenerator.mjs +49 -49
  130. package/dist/error/ManagedIdentityError.cjs +39 -28
  131. package/dist/error/ManagedIdentityError.cjs.map +1 -1
  132. package/dist/error/ManagedIdentityError.d.ts +27 -21
  133. package/dist/error/ManagedIdentityError.d.ts.map +1 -1
  134. package/dist/error/ManagedIdentityError.mjs +40 -29
  135. package/dist/error/ManagedIdentityError.mjs.map +1 -1
  136. package/dist/error/ManagedIdentityErrorCodes.cjs +27 -14
  137. package/dist/error/ManagedIdentityErrorCodes.cjs.map +1 -1
  138. package/dist/error/ManagedIdentityErrorCodes.d.ts +19 -9
  139. package/dist/error/ManagedIdentityErrorCodes.d.ts.map +1 -1
  140. package/dist/error/ManagedIdentityErrorCodes.mjs +24 -14
  141. package/dist/error/ManagedIdentityErrorCodes.mjs.map +1 -1
  142. package/dist/error/NodeAuthError.cjs +85 -85
  143. package/dist/error/NodeAuthError.d.ts +65 -65
  144. package/dist/error/NodeAuthError.mjs +85 -85
  145. package/dist/index.cjs +1 -1
  146. package/dist/index.d.ts +44 -44
  147. package/dist/index.mjs +1 -1
  148. package/dist/internals.cjs +8 -8
  149. package/dist/internals.d.ts +6 -6
  150. package/dist/internals.mjs +8 -8
  151. package/dist/network/HttpClient.cjs +283 -283
  152. package/dist/network/HttpClient.cjs.map +1 -1
  153. package/dist/network/HttpClient.d.ts +25 -26
  154. package/dist/network/HttpClient.d.ts.map +1 -1
  155. package/dist/network/HttpClient.mjs +283 -283
  156. package/dist/network/HttpClient.mjs.map +1 -1
  157. package/dist/network/HttpClientWithRetries.cjs +44 -0
  158. package/dist/network/HttpClientWithRetries.cjs.map +1 -0
  159. package/dist/network/HttpClientWithRetries.d.ts +12 -0
  160. package/dist/network/HttpClientWithRetries.d.ts.map +1 -0
  161. package/dist/network/HttpClientWithRetries.mjs +42 -0
  162. package/dist/network/HttpClientWithRetries.mjs.map +1 -0
  163. package/dist/network/ILoopbackClient.d.ts +10 -10
  164. package/dist/network/LoopbackClient.cjs +77 -77
  165. package/dist/network/LoopbackClient.d.ts +21 -21
  166. package/dist/network/LoopbackClient.mjs +77 -77
  167. package/dist/packageMetadata.cjs +4 -4
  168. package/dist/packageMetadata.d.ts +2 -2
  169. package/dist/packageMetadata.d.ts.map +1 -1
  170. package/dist/packageMetadata.mjs +4 -4
  171. package/dist/request/AuthorizationCodeRequest.d.ts +21 -21
  172. package/dist/request/AuthorizationUrlRequest.d.ts +33 -33
  173. package/dist/request/ClientCredentialRequest.d.ts +14 -14
  174. package/dist/request/DeviceCodeRequest.d.ts +15 -15
  175. package/dist/request/InteractiveRequest.d.ts +22 -22
  176. package/dist/request/ManagedIdentityRequest.d.ts +8 -8
  177. package/dist/request/ManagedIdentityRequestParams.d.ts +9 -9
  178. package/dist/request/OnBehalfOfRequest.d.ts +14 -14
  179. package/dist/request/RefreshTokenRequest.d.ts +17 -17
  180. package/dist/request/SignOutRequest.d.ts +5 -5
  181. package/dist/request/SilentFlowRequest.d.ts +16 -16
  182. package/dist/request/UsernamePasswordRequest.d.ts +19 -19
  183. package/dist/response/ManagedIdentityTokenResponse.d.ts +24 -24
  184. package/dist/retry/IHttpRetryPolicy.d.ts +6 -0
  185. package/dist/retry/IHttpRetryPolicy.d.ts.map +1 -0
  186. package/dist/retry/LinearRetryPolicy.cjs +47 -0
  187. package/dist/retry/LinearRetryPolicy.cjs.map +1 -0
  188. package/dist/retry/LinearRetryPolicy.d.ts +12 -0
  189. package/dist/retry/LinearRetryPolicy.d.ts.map +1 -0
  190. package/dist/retry/LinearRetryPolicy.mjs +45 -0
  191. package/dist/retry/LinearRetryPolicy.mjs.map +1 -0
  192. package/dist/utils/Constants.cjs +141 -103
  193. package/dist/utils/Constants.cjs.map +1 -1
  194. package/dist/utils/Constants.d.ts +131 -106
  195. package/dist/utils/Constants.d.ts.map +1 -1
  196. package/dist/utils/Constants.mjs +133 -100
  197. package/dist/utils/Constants.mjs.map +1 -1
  198. package/dist/utils/EncodingUtils.cjs +44 -44
  199. package/dist/utils/EncodingUtils.d.ts +26 -26
  200. package/dist/utils/EncodingUtils.mjs +44 -44
  201. package/dist/utils/NetworkUtils.cjs +38 -38
  202. package/dist/utils/NetworkUtils.d.ts +5 -5
  203. package/dist/utils/NetworkUtils.mjs +38 -38
  204. package/package.json +5 -5
  205. package/src/cache/NodeStorage.ts +4 -9
  206. package/src/cache/serializer/Deserializer.ts +1 -5
  207. package/src/client/ClientApplication.ts +5 -4
  208. package/src/client/ManagedIdentityApplication.ts +3 -3
  209. package/src/client/ManagedIdentityClient.ts +23 -11
  210. package/src/client/ManagedIdentitySources/AppService.ts +68 -97
  211. package/src/client/ManagedIdentitySources/AzureArc.ts +59 -68
  212. package/src/client/ManagedIdentitySources/BaseManagedIdentitySource.ts +70 -2
  213. package/src/client/ManagedIdentitySources/CloudShell.ts +93 -8
  214. package/src/client/ManagedIdentitySources/Imds.ts +54 -40
  215. package/src/client/ManagedIdentitySources/ServiceFabric.ts +126 -5
  216. package/src/config/Configuration.ts +44 -16
  217. package/src/config/ManagedIdentityRequestParameters.ts +1 -6
  218. package/src/error/ManagedIdentityError.ts +15 -2
  219. package/src/error/ManagedIdentityErrorCodes.ts +19 -0
  220. package/src/network/HttpClient.ts +6 -11
  221. package/src/network/HttpClientWithRetries.ts +80 -0
  222. package/src/packageMetadata.ts +1 -1
  223. package/src/retry/IHttpRetryPolicy.ts +18 -0
  224. package/src/retry/LinearRetryPolicy.ts +71 -0
  225. package/src/utils/Constants.ts +43 -7
@@ -3,17 +3,138 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
+ import { INetworkModule, Logger } from "@azure/msal-common";
6
7
  import { ManagedIdentityId } from "../../config/ManagedIdentityId";
7
8
  import { ManagedIdentityRequestParameters } from "../../config/ManagedIdentityRequestParameters";
8
9
  import { BaseManagedIdentitySource } from "./BaseManagedIdentitySource";
10
+ import { NodeStorage } from "../../cache/NodeStorage";
11
+ import { CryptoProvider } from "../../crypto/CryptoProvider";
12
+ import {
13
+ API_VERSION_QUERY_PARAMETER_NAME,
14
+ HttpMethod,
15
+ ManagedIdentityEnvironmentVariableNames,
16
+ ManagedIdentityIdType,
17
+ ManagedIdentitySourceNames,
18
+ RESOURCE_BODY_OR_QUERY_PARAMETER_NAME,
19
+ SERVICE_FABRIC_SECRET_HEADER_NAME,
20
+ } from "../../utils/Constants";
9
21
 
22
+ // MSI Constants. Docs for MSI are available here https://docs.microsoft.com/azure/app-service/overview-managed-identity
23
+ const SERVICE_FABRIC_MSI_API_VERSION: string = "2019-07-01-preview";
24
+
25
+ /**
26
+ * Original source of code: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/src/ServiceFabricManagedIdentitySource.cs
27
+ */
10
28
  export class ServiceFabric extends BaseManagedIdentitySource {
29
+ private identityEndpoint: string;
30
+ private identityHeader: string;
31
+
32
+ constructor(
33
+ logger: Logger,
34
+ nodeStorage: NodeStorage,
35
+ networkClient: INetworkModule,
36
+ cryptoProvider: CryptoProvider,
37
+ identityEndpoint: string,
38
+ identityHeader: string
39
+ ) {
40
+ super(logger, nodeStorage, networkClient, cryptoProvider);
41
+
42
+ this.identityEndpoint = identityEndpoint;
43
+ this.identityHeader = identityHeader;
44
+ }
45
+
46
+ public static tryCreate(
47
+ logger: Logger,
48
+ nodeStorage: NodeStorage,
49
+ networkClient: INetworkModule,
50
+ cryptoProvider: CryptoProvider,
51
+ managedIdentityId: ManagedIdentityId
52
+ ): ServiceFabric | null {
53
+ const identityEndpoint: string | undefined =
54
+ process.env[
55
+ ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT
56
+ ];
57
+ const identityHeader: string | undefined =
58
+ process.env[
59
+ ManagedIdentityEnvironmentVariableNames.IDENTITY_HEADER
60
+ ];
61
+ const identityServerThumbprint: string | undefined =
62
+ process.env[
63
+ ManagedIdentityEnvironmentVariableNames
64
+ .IDENTITY_SERVER_THUMBPRINT
65
+ ];
66
+
67
+ /*
68
+ * if either of the identity endpoint, identity header, or identity server thumbprint
69
+ * environment variables are undefined, this MSI provider is unavailable.
70
+ */
71
+ if (!identityEndpoint || !identityHeader || !identityServerThumbprint) {
72
+ logger.info(
73
+ `[Managed Identity] ${ManagedIdentitySourceNames.SERVICE_FABRIC} managed identity is unavailable because one or all of the '${ManagedIdentityEnvironmentVariableNames.IDENTITY_HEADER}', '${ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT}' or '${ManagedIdentityEnvironmentVariableNames.IDENTITY_SERVER_THUMBPRINT}' environment variables are not defined.`
74
+ );
75
+ return null;
76
+ }
77
+
78
+ const validatedIdentityEndpoint: string =
79
+ ServiceFabric.getValidatedEnvVariableUrlString(
80
+ ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT,
81
+ identityEndpoint,
82
+ ManagedIdentitySourceNames.SERVICE_FABRIC,
83
+ logger
84
+ );
85
+
86
+ logger.info(
87
+ `[Managed Identity] Environment variables validation passed for ${ManagedIdentitySourceNames.SERVICE_FABRIC} managed identity. Endpoint URI: ${validatedIdentityEndpoint}. Creating ${ManagedIdentitySourceNames.SERVICE_FABRIC} managed identity.`
88
+ );
89
+
90
+ if (
91
+ managedIdentityId.idType !== ManagedIdentityIdType.SYSTEM_ASSIGNED
92
+ ) {
93
+ logger.warning(
94
+ `[Managed Identity] ${ManagedIdentitySourceNames.SERVICE_FABRIC} user assigned managed identity is configured in the cluster, not during runtime. See also: https://learn.microsoft.com/en-us/azure/service-fabric/configure-existing-cluster-enable-managed-identity-token-service.`
95
+ );
96
+ }
97
+
98
+ return new ServiceFabric(
99
+ logger,
100
+ nodeStorage,
101
+ networkClient,
102
+ cryptoProvider,
103
+ identityEndpoint,
104
+ identityHeader
105
+ );
106
+ }
107
+
11
108
  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
109
+ resource: string,
110
+ managedIdentityId: ManagedIdentityId
16
111
  ): ManagedIdentityRequestParameters {
17
- throw new Error("Method not implemented.");
112
+ const request: ManagedIdentityRequestParameters =
113
+ new ManagedIdentityRequestParameters(
114
+ HttpMethod.GET,
115
+ this.identityEndpoint
116
+ );
117
+
118
+ request.headers[SERVICE_FABRIC_SECRET_HEADER_NAME] =
119
+ this.identityHeader;
120
+
121
+ request.queryParameters[API_VERSION_QUERY_PARAMETER_NAME] =
122
+ SERVICE_FABRIC_MSI_API_VERSION;
123
+ request.queryParameters[RESOURCE_BODY_OR_QUERY_PARAMETER_NAME] =
124
+ resource;
125
+
126
+ if (
127
+ managedIdentityId.idType !== ManagedIdentityIdType.SYSTEM_ASSIGNED
128
+ ) {
129
+ request.queryParameters[
130
+ this.getManagedIdentityUserAssignedIdQueryParameterKey(
131
+ managedIdentityId.idType
132
+ )
133
+ ] = managedIdentityId.id;
134
+ }
135
+
136
+ // bodyParameters calculated in BaseManagedIdentity.acquireTokenWithManagedIdentity
137
+
138
+ return request;
18
139
  }
19
140
  }
@@ -19,6 +19,13 @@ import { HttpClient } from "../network/HttpClient.js";
19
19
  import http from "http";
20
20
  import https from "https";
21
21
  import { ManagedIdentityId } from "./ManagedIdentityId.js";
22
+ import {
23
+ MANAGED_IDENTITY_HTTP_STATUS_CODES_TO_RETRY_ON,
24
+ MANAGED_IDENTITY_MAX_RETRIES,
25
+ MANAGED_IDENTITY_RETRY_DELAY,
26
+ } from "../utils/Constants.js";
27
+ import { LinearRetryPolicy } from "../retry/LinearRetryPolicy.js";
28
+ import { HttpClientWithRetries } from "../network/HttpClientWithRetries.js";
22
29
 
23
30
  /**
24
31
  * - clientId - Client id of the application.
@@ -84,6 +91,7 @@ export type NodeSystemOptions = {
84
91
  networkClient?: INetworkModule;
85
92
  proxyUrl?: string;
86
93
  customAgentOptions?: http.AgentOptions | https.AgentOptions;
94
+ disableInternalRetries?: boolean;
87
95
  };
88
96
 
89
97
  export type NodeTelemetryOptions = {
@@ -158,6 +166,7 @@ const DEFAULT_SYSTEM_OPTIONS: Required<NodeSystemOptions> = {
158
166
  networkClient: new HttpClient(),
159
167
  proxyUrl: Constants.EMPTY_STRING,
160
168
  customAgentOptions: {} as http.AgentOptions | https.AgentOptions,
169
+ disableInternalRetries: false,
161
170
  };
162
171
 
163
172
  const DEFAULT_TELEMETRY_OPTIONS: Required<NodeTelemetryOptions> = {
@@ -198,12 +207,10 @@ export function buildAppConfiguration({
198
207
  ...DEFAULT_SYSTEM_OPTIONS,
199
208
  networkClient: new HttpClient(
200
209
  system?.proxyUrl,
201
- system?.customAgentOptions as
202
- | http.AgentOptions
203
- | https.AgentOptions,
204
- false // Managed Identity
210
+ system?.customAgentOptions as http.AgentOptions | https.AgentOptions
205
211
  ),
206
212
  loggerOptions: system?.loggerOptions || DEFAULT_LOGGER_OPTIONS,
213
+ disableInternalRetries: system?.disableInternalRetries || false,
207
214
  };
208
215
 
209
216
  return {
@@ -217,7 +224,9 @@ export function buildAppConfiguration({
217
224
 
218
225
  export type ManagedIdentityNodeConfiguration = {
219
226
  managedIdentityId: ManagedIdentityId;
220
- system: Required<NodeSystemOptions>;
227
+ system: Required<
228
+ Pick<NodeSystemOptions, "loggerOptions" | "networkClient">
229
+ >;
221
230
  };
222
231
 
223
232
  export function buildManagedIdentityConfiguration({
@@ -228,20 +237,39 @@ export function buildManagedIdentityConfiguration({
228
237
  managedIdentityIdParams
229
238
  );
230
239
 
231
- const systemOptions: Required<NodeSystemOptions> = {
232
- ...DEFAULT_SYSTEM_OPTIONS,
233
- loggerOptions: system?.loggerOptions || DEFAULT_LOGGER_OPTIONS,
234
- networkClient: new HttpClient(
240
+ const loggerOptions: LoggerOptions =
241
+ system?.loggerOptions || DEFAULT_LOGGER_OPTIONS;
242
+
243
+ let networkClient: INetworkModule;
244
+ // use developer provided network client if passed in
245
+ if (system?.networkClient) {
246
+ networkClient = system.networkClient;
247
+ // otherwise, create a new one
248
+ } else {
249
+ networkClient = new HttpClient(
235
250
  system?.proxyUrl,
236
- system?.customAgentOptions as
237
- | http.AgentOptions
238
- | https.AgentOptions,
239
- true // Managed Identity
240
- ),
241
- };
251
+ system?.customAgentOptions as http.AgentOptions | https.AgentOptions
252
+ );
253
+ }
254
+
255
+ // wrap the network client with a retry policy if the developer has not disabled the option to do so
256
+ if (!system?.disableInternalRetries) {
257
+ const linearRetryPolicy: LinearRetryPolicy = new LinearRetryPolicy(
258
+ MANAGED_IDENTITY_MAX_RETRIES,
259
+ MANAGED_IDENTITY_RETRY_DELAY,
260
+ MANAGED_IDENTITY_HTTP_STATUS_CODES_TO_RETRY_ON
261
+ );
262
+ networkClient = new HttpClientWithRetries(
263
+ networkClient,
264
+ linearRetryPolicy
265
+ );
266
+ }
242
267
 
243
268
  return {
244
269
  managedIdentityId: managedIdentityId,
245
- system: { ...systemOptions, ...system },
270
+ system: {
271
+ loggerOptions,
272
+ networkClient,
273
+ },
246
274
  };
247
275
  }
@@ -43,11 +43,6 @@ export class ManagedIdentityRequestParameters {
43
43
  parameterBuilder.addExtraQueryParameters(this.bodyParameters);
44
44
  }
45
45
 
46
- const bodyParametersString = parameterBuilder.createQueryString();
47
-
48
- return UrlString.appendQueryString(
49
- this._baseEndpoint,
50
- bodyParametersString
51
- );
46
+ return parameterBuilder.createQueryString();
52
47
  }
53
48
  }
@@ -5,6 +5,7 @@
5
5
 
6
6
  import { AuthError } from "@azure/msal-common";
7
7
  import * as ManagedIdentityErrorCodes from "./ManagedIdentityErrorCodes";
8
+ import { ManagedIdentityEnvironmentVariableNames } from "../utils/Constants";
8
9
  export { ManagedIdentityErrorCodes };
9
10
 
10
11
  /**
@@ -17,14 +18,26 @@ export const ManagedIdentityErrorMessages = {
17
18
  "The supplied resource is an invalid URL.",
18
19
  [ManagedIdentityErrorCodes.missingId]:
19
20
  "A ManagedIdentityId id was not provided.",
21
+ [ManagedIdentityErrorCodes.MsiEnvironmentVariableUrlMalformedErrorCodes
22
+ .AZURE_POD_IDENTITY_AUTHORITY_HOST]: `The Managed Identity's '${ManagedIdentityEnvironmentVariableNames.AZURE_POD_IDENTITY_AUTHORITY_HOST}' environment variable is malformed.`,
23
+ [ManagedIdentityErrorCodes.MsiEnvironmentVariableUrlMalformedErrorCodes
24
+ .IDENTITY_ENDPOINT]: `The Managed Identity's '${ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT}' environment variable is malformed.`,
25
+ [ManagedIdentityErrorCodes.MsiEnvironmentVariableUrlMalformedErrorCodes
26
+ .IMDS_ENDPOINT]: `The Managed Identity's '${ManagedIdentityEnvironmentVariableNames.IMDS_ENDPOINT}' environment variable is malformed.`,
27
+ [ManagedIdentityErrorCodes.MsiEnvironmentVariableUrlMalformedErrorCodes
28
+ .MSI_ENDPOINT]: `The Managed Identity's '${ManagedIdentityEnvironmentVariableNames.MSI_ENDPOINT}' environment variable is malformed.`,
29
+ [ManagedIdentityErrorCodes.networkUnavailable]:
30
+ "Authentication unavailable. The request to the managed identity endpoint timed out.",
20
31
  [ManagedIdentityErrorCodes.unableToCreateAzureArc]:
21
32
  "Azure Arc Managed Identities can only be system assigned.",
33
+ [ManagedIdentityErrorCodes.unableToCreateCloudShell]:
34
+ "Cloud Shell Managed Identities can only be system assigned.",
22
35
  [ManagedIdentityErrorCodes.unableToCreateSource]:
23
36
  "Unable to create a Managed Identity source based on environment variables.",
24
37
  [ManagedIdentityErrorCodes.unableToReadSecretFile]:
25
38
  "Unable to read the secret file.",
26
- [ManagedIdentityErrorCodes.urlParseError]:
27
- "The Managed Identity's 'IDENTITY_ENDPOINT' environment variable is malformed.",
39
+ [ManagedIdentityErrorCodes.userAssignedNotAvailableAtRuntime]:
40
+ "Service Fabric user assigned managed identity ClientId or ResourceId is not configurable at runtime.",
28
41
  [ManagedIdentityErrorCodes.wwwAuthenticateHeaderMissing]:
29
42
  "A 401 response was received form the Azure Arc Managed Identity, but the www-authenticate header is missing.",
30
43
  [ManagedIdentityErrorCodes.wwwAuthenticateHeaderUnsupportedFormat]:
@@ -3,13 +3,32 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
+ import { ManagedIdentityEnvironmentVariableNames } from "../utils/Constants";
7
+
6
8
  export const invalidManagedIdentityIdType = "invalid_managed_identity_id_type";
7
9
  export const invalidResource = "invalid_resource";
8
10
  export const missingId = "missing_client_id";
11
+ export const networkUnavailable = "network_unavailable";
9
12
  export const unableToCreateAzureArc = "unable_to_create_azure_arc";
13
+ export const unableToCreateCloudShell = "unable_to_create_cloud_shell";
10
14
  export const unableToCreateSource = "unable_to_create_source";
11
15
  export const unableToReadSecretFile = "unable_to_read_secret_file";
12
16
  export const urlParseError = "url_parse_error";
17
+ export const userAssignedNotAvailableAtRuntime =
18
+ "user_assigned_not_available_at_runtime";
13
19
  export const wwwAuthenticateHeaderMissing = "www_authenticate_header_missing";
14
20
  export const wwwAuthenticateHeaderUnsupportedFormat =
15
21
  "www_authenticate_header_unsupported_format";
22
+
23
+ export const MsiEnvironmentVariableUrlMalformedErrorCodes = {
24
+ [ManagedIdentityEnvironmentVariableNames.AZURE_POD_IDENTITY_AUTHORITY_HOST]:
25
+ "azure_pod_identity_authority_host_url_malformed",
26
+ [ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT]:
27
+ "identity_endpoint_url_malformed",
28
+ [ManagedIdentityEnvironmentVariableNames.IMDS_ENDPOINT]:
29
+ "imds_endpoint_url_malformed",
30
+ [ManagedIdentityEnvironmentVariableNames.MSI_ENDPOINT]:
31
+ "msi_endpoint_url_malformed",
32
+ } as const;
33
+ export type MsiEnvironmentVariableErrorCodes =
34
+ (typeof MsiEnvironmentVariableUrlMalformedErrorCodes)[keyof typeof MsiEnvironmentVariableUrlMalformedErrorCodes];
@@ -20,16 +20,13 @@ 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;
24
23
 
25
24
  constructor(
26
25
  proxyUrl?: string,
27
- customAgentOptions?: http.AgentOptions | https.AgentOptions,
28
- managedIdentity?: boolean
26
+ customAgentOptions?: http.AgentOptions | https.AgentOptions
29
27
  ) {
30
28
  this.proxyUrl = proxyUrl || "";
31
29
  this.customAgentOptions = customAgentOptions || {};
32
- this.managedIdentity = managedIdentity || false;
33
30
  }
34
31
 
35
32
  /**
@@ -55,8 +52,7 @@ export class HttpClient implements INetworkModule {
55
52
  HttpMethod.GET,
56
53
  options,
57
54
  this.customAgentOptions as https.AgentOptions,
58
- undefined,
59
- this.managedIdentity
55
+ undefined
60
56
  );
61
57
  }
62
58
  }
@@ -86,8 +82,7 @@ export class HttpClient implements INetworkModule {
86
82
  HttpMethod.POST,
87
83
  options,
88
84
  this.customAgentOptions as https.AgentOptions,
89
- cancellationToken,
90
- this.managedIdentity
85
+ cancellationToken
91
86
  );
92
87
  }
93
88
  }
@@ -283,8 +278,7 @@ const networkRequestViaHttps = <T>(
283
278
  httpMethod: string,
284
279
  options?: NetworkRequestOptions,
285
280
  agentOptions?: https.AgentOptions,
286
- timeout?: number,
287
- managedIdentity?: boolean
281
+ timeout?: number
288
282
  ): Promise<NetworkResponse<T>> => {
289
283
  const isPostRequest = httpMethod === HttpMethod.POST;
290
284
  const body: string = options?.body || "";
@@ -315,7 +309,8 @@ const networkRequestViaHttps = <T>(
315
309
 
316
310
  return new Promise<NetworkResponse<T>>((resolve, reject) => {
317
311
  let request: http.ClientRequest;
318
- if (managedIdentity) {
312
+ // managed identity sources use http instead of https
313
+ if (customOptions.protocol === "http:") {
319
314
  request = http.request(customOptions);
320
315
  } else {
321
316
  request = https.request(customOptions);
@@ -0,0 +1,80 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import {
7
+ HeaderNames,
8
+ INetworkModule,
9
+ NetworkRequestOptions,
10
+ NetworkResponse,
11
+ } from "@azure/msal-common";
12
+ import { IHttpRetryPolicy } from "../retry/IHttpRetryPolicy";
13
+ import { HttpMethod } from "../utils/Constants";
14
+
15
+ export class HttpClientWithRetries implements INetworkModule {
16
+ private httpClientNoRetries: INetworkModule;
17
+ private retryPolicy: IHttpRetryPolicy;
18
+
19
+ constructor(
20
+ httpClientNoRetries: INetworkModule,
21
+ retryPolicy: IHttpRetryPolicy
22
+ ) {
23
+ this.httpClientNoRetries = httpClientNoRetries;
24
+ this.retryPolicy = retryPolicy;
25
+ }
26
+
27
+ private async sendNetworkRequestAsyncHelper<T>(
28
+ httpMethod: HttpMethod,
29
+ url: string,
30
+ options?: NetworkRequestOptions
31
+ ): Promise<NetworkResponse<T>> {
32
+ if (httpMethod === HttpMethod.GET) {
33
+ return this.httpClientNoRetries.sendGetRequestAsync(url, options);
34
+ } else {
35
+ return this.httpClientNoRetries.sendPostRequestAsync(url, options);
36
+ }
37
+ }
38
+
39
+ private async sendNetworkRequestAsync<T>(
40
+ httpMethod: HttpMethod,
41
+ url: string,
42
+ options?: NetworkRequestOptions
43
+ ): Promise<NetworkResponse<T>> {
44
+ // the underlying network module (custom or HttpClient) will make the call
45
+ let response: NetworkResponse<T> =
46
+ await this.sendNetworkRequestAsyncHelper(httpMethod, url, options);
47
+
48
+ let currentRetry: number = 0;
49
+ while (
50
+ await this.retryPolicy.pauseForRetry(
51
+ response.status,
52
+ currentRetry,
53
+ response.headers[HeaderNames.RETRY_AFTER]
54
+ )
55
+ ) {
56
+ response = await this.sendNetworkRequestAsyncHelper(
57
+ httpMethod,
58
+ url,
59
+ options
60
+ );
61
+ currentRetry++;
62
+ }
63
+
64
+ return response;
65
+ }
66
+
67
+ public async sendGetRequestAsync<T>(
68
+ url: string,
69
+ options?: NetworkRequestOptions
70
+ ): Promise<NetworkResponse<T>> {
71
+ return this.sendNetworkRequestAsync(HttpMethod.GET, url, options);
72
+ }
73
+
74
+ public async sendPostRequestAsync<T>(
75
+ url: string,
76
+ options?: NetworkRequestOptions
77
+ ): Promise<NetworkResponse<T>> {
78
+ return this.sendNetworkRequestAsync(HttpMethod.POST, url, options);
79
+ }
80
+ }
@@ -1,3 +1,3 @@
1
1
  /* eslint-disable header/header */
2
2
  export const name = "@azure/msal-node";
3
- export const version = "2.7.0-alpha.0";
3
+ export const version = "2.7.0";
@@ -0,0 +1,18 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import http from "http";
7
+
8
+ export interface IHttpRetryPolicy {
9
+ /*
10
+ * if retry conditions occur, pauses and returns true
11
+ * otherwise return false
12
+ */
13
+ pauseForRetry(
14
+ httpStatusCode: number,
15
+ currentRetry: number,
16
+ retryAfterHeader: http.IncomingHttpHeaders["retry-after"]
17
+ ): Promise<boolean>;
18
+ }
@@ -0,0 +1,71 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import http from "http";
7
+ import { IHttpRetryPolicy } from "./IHttpRetryPolicy";
8
+
9
+ export class LinearRetryPolicy implements IHttpRetryPolicy {
10
+ maxRetries: number;
11
+ retryDelay: number;
12
+ httpStatusCodesToRetryOn: Array<number>;
13
+
14
+ constructor(
15
+ maxRetries: number,
16
+ retryDelay: number,
17
+ httpStatusCodesToRetryOn: Array<number>
18
+ ) {
19
+ this.maxRetries = maxRetries;
20
+ this.retryDelay = retryDelay;
21
+ this.httpStatusCodesToRetryOn = httpStatusCodesToRetryOn;
22
+ }
23
+
24
+ private retryAfterMillisecondsToSleep(
25
+ retryHeader: http.IncomingHttpHeaders["retry-after"]
26
+ ): number {
27
+ if (!retryHeader) {
28
+ return 0;
29
+ }
30
+
31
+ // retry-after header is in seconds
32
+ let millisToSleep = Math.round(parseFloat(retryHeader) * 1000);
33
+
34
+ /*
35
+ * retry-after header is in HTTP Date format
36
+ * <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT
37
+ */
38
+ if (isNaN(millisToSleep)) {
39
+ millisToSleep = Math.max(
40
+ 0,
41
+ // .valueOf() is needed to subtract dates in TypeScript
42
+ new Date(retryHeader).valueOf() - new Date().valueOf()
43
+ );
44
+ }
45
+
46
+ return millisToSleep;
47
+ }
48
+
49
+ async pauseForRetry(
50
+ httpStatusCode: number,
51
+ currentRetry: number,
52
+ retryAfterHeader: http.IncomingHttpHeaders["retry-after"]
53
+ ): Promise<boolean> {
54
+ if (
55
+ this.httpStatusCodesToRetryOn.includes(httpStatusCode) &&
56
+ currentRetry < this.maxRetries
57
+ ) {
58
+ const retryAfterDelay: number =
59
+ this.retryAfterMillisecondsToSleep(retryAfterHeader);
60
+
61
+ await new Promise((resolve) => {
62
+ // retryAfterHeader value of 0 evaluates to false, and this.retryDelay will be used
63
+ return setTimeout(resolve, retryAfterDelay || this.retryDelay);
64
+ });
65
+
66
+ return true;
67
+ }
68
+
69
+ return false;
70
+ }
71
+ }
@@ -3,20 +3,45 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
+ import { HttpStatus } from "@azure/msal-common";
7
+
6
8
  // MSI Constants. Docs for MSI are available here https://docs.microsoft.com/azure/app-service/overview-managed-identity
7
9
  export const AUTHORIZATION_HEADER_NAME: string = "Authorization";
8
10
  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";
11
+ export const APP_SERVICE_SECRET_HEADER_NAME: string = "X-IDENTITY-HEADER";
12
+ export const SERVICE_FABRIC_SECRET_HEADER_NAME: string = "secret";
13
+ export const API_VERSION_QUERY_PARAMETER_NAME: string = "api-version";
14
+ export const RESOURCE_BODY_OR_QUERY_PARAMETER_NAME: string = "resource";
14
15
  export const DEFAULT_MANAGED_IDENTITY_ID = "system_assigned_managed_identity";
15
16
  export const MANAGED_IDENTITY_DEFAULT_TENANT = "managed_identity";
16
17
  export const DEFAULT_AUTHORITY_FOR_MANAGED_IDENTITY = `https://login.microsoftonline.com/${MANAGED_IDENTITY_DEFAULT_TENANT}/`;
17
18
 
18
- export const MANAGED_IDENTITY_TIMEOUT_ERROR: string =
19
- "[Managed Identity] Authentication unavailable. The request to the managed identity endpoint timed out.";
19
+ /**
20
+ * Managed Identity Environment Variable Names
21
+ */
22
+ export const ManagedIdentityEnvironmentVariableNames = {
23
+ AZURE_POD_IDENTITY_AUTHORITY_HOST: "AZURE_POD_IDENTITY_AUTHORITY_HOST",
24
+ IDENTITY_ENDPOINT: "IDENTITY_ENDPOINT",
25
+ IDENTITY_HEADER: "IDENTITY_HEADER",
26
+ IDENTITY_SERVER_THUMBPRINT: "IDENTITY_SERVER_THUMBPRINT",
27
+ IMDS_ENDPOINT: "IMDS_ENDPOINT",
28
+ MSI_ENDPOINT: "MSI_ENDPOINT",
29
+ } as const;
30
+ export type ManagedIdentityEnvironmentVariableNames =
31
+ (typeof ManagedIdentityEnvironmentVariableNames)[keyof typeof ManagedIdentityEnvironmentVariableNames];
32
+
33
+ /**
34
+ * Managed Identity Source Names
35
+ */
36
+ export const ManagedIdentitySourceNames = {
37
+ APP_SERVICE: "App Service",
38
+ AZURE_ARC: "Azure Arc",
39
+ CLOUD_SHELL: "Cloud Shell",
40
+ IMDS: "IMDS",
41
+ SERVICE_FABRIC: "Service Fabric",
42
+ } as const;
43
+ export type ManagedIdentitySourceNames =
44
+ (typeof ManagedIdentitySourceNames)[keyof typeof ManagedIdentitySourceNames];
20
45
 
21
46
  /**
22
47
  * Managed Identity Ids
@@ -128,3 +153,14 @@ export const LOOPBACK_SERVER_CONSTANTS = {
128
153
  INTERVAL_MS: 100,
129
154
  TIMEOUT_MS: 5000,
130
155
  };
156
+
157
+ export const MANAGED_IDENTITY_MAX_RETRIES = 3;
158
+ export const MANAGED_IDENTITY_RETRY_DELAY = 1000;
159
+ export const MANAGED_IDENTITY_HTTP_STATUS_CODES_TO_RETRY_ON = [
160
+ HttpStatus.NOT_FOUND,
161
+ HttpStatus.REQUEST_TIMEOUT,
162
+ HttpStatus.TOO_MANY_REQUESTS,
163
+ HttpStatus.INTERNAL_SERVER_ERROR,
164
+ HttpStatus.SERVICE_UNAVAILABLE,
165
+ HttpStatus.GATEWAY_TIMEOUT,
166
+ ];