@azure/msal-node 2.2.0 → 2.3.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/dist/cache/ITokenCache.d.ts +15 -15
  2. package/dist/cache/NodeStorage.cjs +401 -401
  3. package/dist/cache/NodeStorage.cjs.map +1 -1
  4. package/dist/cache/NodeStorage.d.ts +182 -182
  5. package/dist/cache/NodeStorage.d.ts.map +1 -1
  6. package/dist/cache/NodeStorage.mjs +401 -401
  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 +173 -173
  17. package/dist/cache/serializer/Deserializer.d.ts +42 -42
  18. package/dist/cache/serializer/Deserializer.mjs +173 -173
  19. package/dist/cache/serializer/Serializer.cjs +134 -134
  20. package/dist/cache/serializer/Serializer.d.ts +39 -39
  21. package/dist/cache/serializer/Serializer.mjs +134 -134
  22. package/dist/cache/serializer/SerializerTypes.d.ts +101 -101
  23. package/dist/client/ClientApplication.cjs +333 -333
  24. package/dist/client/ClientApplication.cjs.map +1 -1
  25. package/dist/client/ClientApplication.d.ts +142 -142
  26. package/dist/client/ClientApplication.d.ts.map +1 -1
  27. package/dist/client/ClientApplication.mjs +334 -334
  28. package/dist/client/ClientApplication.mjs.map +1 -1
  29. package/dist/client/ClientAssertion.cjs +116 -116
  30. package/dist/client/ClientAssertion.d.ts +46 -46
  31. package/dist/client/ClientAssertion.mjs +116 -116
  32. package/dist/client/ClientCredentialClient.cjs +193 -189
  33. package/dist/client/ClientCredentialClient.cjs.map +1 -1
  34. package/dist/client/ClientCredentialClient.d.ts +33 -33
  35. package/dist/client/ClientCredentialClient.d.ts.map +1 -1
  36. package/dist/client/ClientCredentialClient.mjs +194 -190
  37. package/dist/client/ClientCredentialClient.mjs.map +1 -1
  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 +96 -0
  47. package/dist/client/ManagedIdentityApplication.cjs.map +1 -0
  48. package/dist/client/ManagedIdentityApplication.d.ts +24 -0
  49. package/dist/client/ManagedIdentityApplication.d.ts.map +1 -0
  50. package/dist/client/ManagedIdentityApplication.mjs +94 -0
  51. package/dist/client/ManagedIdentityApplication.mjs.map +1 -0
  52. package/dist/client/ManagedIdentityClient.cjs +49 -0
  53. package/dist/client/ManagedIdentityClient.cjs.map +1 -0
  54. package/dist/client/ManagedIdentityClient.d.ts +25 -0
  55. package/dist/client/ManagedIdentityClient.d.ts.map +1 -0
  56. package/dist/client/ManagedIdentityClient.mjs +47 -0
  57. package/dist/client/ManagedIdentityClient.mjs.map +1 -0
  58. package/dist/client/ManagedIdentitySources/AppService.cjs +79 -0
  59. package/dist/client/ManagedIdentitySources/AppService.cjs.map +1 -0
  60. package/dist/client/ManagedIdentitySources/AppService.d.ts +17 -0
  61. package/dist/client/ManagedIdentitySources/AppService.d.ts.map +1 -0
  62. package/dist/client/ManagedIdentitySources/AppService.mjs +77 -0
  63. package/dist/client/ManagedIdentitySources/AppService.mjs.map +1 -0
  64. package/dist/client/ManagedIdentitySources/AzureArc.cjs +107 -0
  65. package/dist/client/ManagedIdentitySources/AzureArc.cjs.map +1 -0
  66. package/dist/client/ManagedIdentitySources/AzureArc.d.ts +19 -0
  67. package/dist/client/ManagedIdentitySources/AzureArc.d.ts.map +1 -0
  68. package/dist/client/ManagedIdentitySources/AzureArc.mjs +104 -0
  69. package/dist/client/ManagedIdentitySources/AzureArc.mjs.map +1 -0
  70. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs +87 -0
  71. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs.map +1 -0
  72. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts +19 -0
  73. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts.map +1 -0
  74. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs +85 -0
  75. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs.map +1 -0
  76. package/dist/client/ManagedIdentitySources/CloudShell.d.ts +7 -0
  77. package/dist/client/ManagedIdentitySources/CloudShell.d.ts.map +1 -0
  78. package/dist/client/ManagedIdentitySources/Imds.cjs +63 -0
  79. package/dist/client/ManagedIdentitySources/Imds.cjs.map +1 -0
  80. package/dist/client/ManagedIdentitySources/Imds.d.ts +13 -0
  81. package/dist/client/ManagedIdentitySources/Imds.d.ts.map +1 -0
  82. package/dist/client/ManagedIdentitySources/Imds.mjs +61 -0
  83. package/dist/client/ManagedIdentitySources/Imds.mjs.map +1 -0
  84. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts +7 -0
  85. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts.map +1 -0
  86. package/dist/client/OnBehalfOfClient.cjs +202 -202
  87. package/dist/client/OnBehalfOfClient.d.ts +47 -47
  88. package/dist/client/OnBehalfOfClient.mjs +202 -202
  89. package/dist/client/PublicClientApplication.cjs +229 -229
  90. package/dist/client/PublicClientApplication.d.ts +71 -71
  91. package/dist/client/PublicClientApplication.mjs +229 -229
  92. package/dist/client/UsernamePasswordClient.cjs +95 -95
  93. package/dist/client/UsernamePasswordClient.d.ts +25 -25
  94. package/dist/client/UsernamePasswordClient.mjs +95 -95
  95. package/dist/config/Configuration.cjs +88 -72
  96. package/dist/config/Configuration.cjs.map +1 -1
  97. package/dist/config/Configuration.d.ts +122 -107
  98. package/dist/config/Configuration.d.ts.map +1 -1
  99. package/dist/config/Configuration.mjs +88 -73
  100. package/dist/config/Configuration.mjs.map +1 -1
  101. package/dist/config/ManagedIdentityId.cjs +59 -0
  102. package/dist/config/ManagedIdentityId.cjs.map +1 -0
  103. package/dist/config/ManagedIdentityId.d.ts +12 -0
  104. package/dist/config/ManagedIdentityId.d.ts.map +1 -0
  105. package/dist/config/ManagedIdentityId.mjs +57 -0
  106. package/dist/config/ManagedIdentityId.mjs.map +1 -0
  107. package/dist/config/ManagedIdentityRequestParameters.cjs +38 -0
  108. package/dist/config/ManagedIdentityRequestParameters.cjs.map +1 -0
  109. package/dist/config/ManagedIdentityRequestParameters.d.ts +12 -0
  110. package/dist/config/ManagedIdentityRequestParameters.d.ts.map +1 -0
  111. package/dist/config/ManagedIdentityRequestParameters.mjs +36 -0
  112. package/dist/config/ManagedIdentityRequestParameters.mjs.map +1 -0
  113. package/dist/crypto/CryptoProvider.cjs +75 -75
  114. package/dist/crypto/CryptoProvider.d.ts +52 -52
  115. package/dist/crypto/CryptoProvider.mjs +75 -75
  116. package/dist/crypto/GuidGenerator.cjs +22 -22
  117. package/dist/crypto/GuidGenerator.d.ts +14 -14
  118. package/dist/crypto/GuidGenerator.mjs +22 -22
  119. package/dist/crypto/HashUtils.cjs +13 -13
  120. package/dist/crypto/HashUtils.d.ts +8 -8
  121. package/dist/crypto/HashUtils.mjs +13 -13
  122. package/dist/crypto/PkceGenerator.cjs +49 -49
  123. package/dist/crypto/PkceGenerator.d.ts +22 -22
  124. package/dist/crypto/PkceGenerator.mjs +49 -49
  125. package/dist/error/ManagedIdentityError.cjs +40 -0
  126. package/dist/error/ManagedIdentityError.cjs.map +1 -0
  127. package/dist/error/ManagedIdentityError.d.ts +22 -0
  128. package/dist/error/ManagedIdentityError.d.ts.map +1 -0
  129. package/dist/error/ManagedIdentityError.mjs +36 -0
  130. package/dist/error/ManagedIdentityError.mjs.map +1 -0
  131. package/dist/error/ManagedIdentityErrorCodes.cjs +28 -0
  132. package/dist/error/ManagedIdentityErrorCodes.cjs.map +1 -0
  133. package/dist/error/ManagedIdentityErrorCodes.d.ts +10 -0
  134. package/dist/error/ManagedIdentityErrorCodes.d.ts.map +1 -0
  135. package/dist/error/ManagedIdentityErrorCodes.mjs +18 -0
  136. package/dist/error/ManagedIdentityErrorCodes.mjs.map +1 -0
  137. package/dist/error/NodeAuthError.cjs +85 -85
  138. package/dist/error/NodeAuthError.d.ts +65 -65
  139. package/dist/error/NodeAuthError.mjs +85 -85
  140. package/dist/index.cjs +3 -1
  141. package/dist/index.cjs.map +1 -1
  142. package/dist/index.d.ts +44 -42
  143. package/dist/index.d.ts.map +1 -1
  144. package/dist/index.mjs +2 -1
  145. package/dist/index.mjs.map +1 -1
  146. package/dist/internals.cjs +8 -8
  147. package/dist/internals.d.ts +6 -6
  148. package/dist/internals.mjs +8 -8
  149. package/dist/network/HttpClient.cjs +283 -276
  150. package/dist/network/HttpClient.cjs.map +1 -1
  151. package/dist/network/HttpClient.d.ts +26 -25
  152. package/dist/network/HttpClient.d.ts.map +1 -1
  153. package/dist/network/HttpClient.mjs +283 -276
  154. package/dist/network/HttpClient.mjs.map +1 -1
  155. package/dist/network/ILoopbackClient.d.ts +10 -10
  156. package/dist/network/LoopbackClient.cjs +76 -76
  157. package/dist/network/LoopbackClient.d.ts +21 -21
  158. package/dist/network/LoopbackClient.mjs +76 -76
  159. package/dist/packageMetadata.cjs +4 -4
  160. package/dist/packageMetadata.cjs.map +1 -1
  161. package/dist/packageMetadata.d.ts +2 -2
  162. package/dist/packageMetadata.d.ts.map +1 -1
  163. package/dist/packageMetadata.mjs +4 -4
  164. package/dist/packageMetadata.mjs.map +1 -1
  165. package/dist/request/AuthorizationCodeRequest.d.ts +21 -21
  166. package/dist/request/AuthorizationUrlRequest.d.ts +33 -33
  167. package/dist/request/ClientCredentialRequest.d.ts +14 -14
  168. package/dist/request/DeviceCodeRequest.d.ts +15 -15
  169. package/dist/request/InteractiveRequest.d.ts +22 -22
  170. package/dist/request/ManagedIdentityRequest.d.ts +9 -0
  171. package/dist/request/ManagedIdentityRequest.d.ts.map +1 -0
  172. package/dist/request/ManagedIdentityRequestParams.d.ts +10 -0
  173. package/dist/request/ManagedIdentityRequestParams.d.ts.map +1 -0
  174. package/dist/request/OnBehalfOfRequest.d.ts +14 -14
  175. package/dist/request/RefreshTokenRequest.d.ts +17 -17
  176. package/dist/request/SignOutRequest.d.ts +5 -5
  177. package/dist/request/SilentFlowRequest.d.ts +16 -16
  178. package/dist/request/UsernamePasswordRequest.d.ts +19 -19
  179. package/dist/response/ManagedIdentityTokenResponse.d.ts +25 -0
  180. package/dist/response/ManagedIdentityTokenResponse.d.ts.map +1 -0
  181. package/dist/utils/Constants.cjs +109 -80
  182. package/dist/utils/Constants.cjs.map +1 -1
  183. package/dist/utils/Constants.d.ts +106 -86
  184. package/dist/utils/Constants.d.ts.map +1 -1
  185. package/dist/utils/Constants.mjs +100 -81
  186. package/dist/utils/Constants.mjs.map +1 -1
  187. package/dist/utils/EncodingUtils.cjs +44 -44
  188. package/dist/utils/EncodingUtils.d.ts +26 -26
  189. package/dist/utils/EncodingUtils.mjs +44 -44
  190. package/dist/utils/NetworkUtils.cjs +38 -38
  191. package/dist/utils/NetworkUtils.d.ts +5 -5
  192. package/dist/utils/NetworkUtils.mjs +38 -38
  193. package/package.json +2 -2
  194. package/src/cache/NodeStorage.ts +8 -2
  195. package/src/client/ClientApplication.ts +3 -1
  196. package/src/client/ClientCredentialClient.ts +65 -25
  197. package/src/client/ManagedIdentityApplication.ts +194 -0
  198. package/src/client/ManagedIdentityClient.ts +111 -0
  199. package/src/client/ManagedIdentitySources/AppService.ts +151 -0
  200. package/src/client/ManagedIdentitySources/AzureArc.ts +208 -0
  201. package/src/client/ManagedIdentitySources/BaseManagedIdentitySource.ts +169 -0
  202. package/src/client/ManagedIdentitySources/CloudShell.ts +19 -0
  203. package/src/client/ManagedIdentitySources/Imds.ts +112 -0
  204. package/src/client/ManagedIdentitySources/ServiceFabric.ts +19 -0
  205. package/src/config/Configuration.ts +47 -1
  206. package/src/config/ManagedIdentityId.ts +73 -0
  207. package/src/config/ManagedIdentityRequestParameters.ts +53 -0
  208. package/src/error/ManagedIdentityError.ts +46 -0
  209. package/src/error/ManagedIdentityErrorCodes.ts +15 -0
  210. package/src/index.ts +2 -0
  211. package/src/network/HttpClient.ts +17 -5
  212. package/src/packageMetadata.ts +1 -1
  213. package/src/request/ManagedIdentityRequest.ts +15 -0
  214. package/src/request/ManagedIdentityRequestParams.ts +14 -0
  215. package/src/response/ManagedIdentityTokenResponse.ts +33 -0
  216. package/src/utils/Constants.ts +27 -0
@@ -1,215 +1,215 @@
1
- /*! @azure/msal-node v2.2.0 2023-10-05 */
1
+ /*! @azure/msal-node v2.3.0-alpha.0 2023-12-07 */
2
2
  'use strict';
3
3
  'use strict';
4
4
 
5
5
  var msalCommon = require('@azure/msal-common');
6
6
 
7
- /*
8
- * Copyright (c) Microsoft Corporation. All rights reserved.
9
- * Licensed under the MIT License.
10
- */
11
- /**
12
- * OAuth2.0 Device code client
13
- */
14
- class DeviceCodeClient extends msalCommon.BaseClient {
15
- constructor(configuration) {
16
- super(configuration);
17
- }
18
- /**
19
- * Gets device code from device code endpoint, calls back to with device code response, and
20
- * polls token endpoint to exchange device code for tokens
21
- * @param request
22
- */
23
- async acquireToken(request) {
24
- const deviceCodeResponse = await this.getDeviceCode(request);
25
- request.deviceCodeCallback(deviceCodeResponse);
26
- const reqTimestamp = msalCommon.TimeUtils.nowSeconds();
27
- const response = await this.acquireTokenWithDeviceCode(request, deviceCodeResponse);
28
- const responseHandler = new msalCommon.ResponseHandler(this.config.authOptions.clientId, this.cacheManager, this.cryptoUtils, this.logger, this.config.serializableCache, this.config.persistencePlugin);
29
- // Validate response. This function throws a server error if an error is returned by the server.
30
- responseHandler.validateTokenResponse(response);
31
- return await responseHandler.handleServerTokenResponse(response, this.authority, reqTimestamp, request);
32
- }
33
- /**
34
- * Creates device code request and executes http GET
35
- * @param request
36
- */
37
- async getDeviceCode(request) {
38
- const queryParametersString = this.createExtraQueryParameters(request);
39
- const endpoint = msalCommon.UrlString.appendQueryString(this.authority.deviceCodeEndpoint, queryParametersString);
40
- const queryString = this.createQueryString(request);
41
- const headers = this.createTokenRequestHeaders();
42
- const thumbprint = {
43
- clientId: this.config.authOptions.clientId,
44
- authority: request.authority,
45
- scopes: request.scopes,
46
- claims: request.claims,
47
- authenticationScheme: request.authenticationScheme,
48
- resourceRequestMethod: request.resourceRequestMethod,
49
- resourceRequestUri: request.resourceRequestUri,
50
- shrClaims: request.shrClaims,
51
- sshKid: request.sshKid,
52
- };
53
- return this.executePostRequestToDeviceCodeEndpoint(endpoint, queryString, headers, thumbprint);
54
- }
55
- /**
56
- * Creates query string for the device code request
57
- * @param request
58
- */
59
- createExtraQueryParameters(request) {
60
- const parameterBuilder = new msalCommon.RequestParameterBuilder();
61
- if (request.extraQueryParameters) {
62
- parameterBuilder.addExtraQueryParameters(request.extraQueryParameters);
63
- }
64
- return parameterBuilder.createQueryString();
65
- }
66
- /**
67
- * Executes POST request to device code endpoint
68
- * @param deviceCodeEndpoint
69
- * @param queryString
70
- * @param headers
71
- */
72
- async executePostRequestToDeviceCodeEndpoint(deviceCodeEndpoint, queryString, headers, thumbprint) {
73
- const { body: { user_code: userCode, device_code: deviceCode, verification_uri: verificationUri, expires_in: expiresIn, interval, message, }, } = await this.networkManager.sendPostRequest(thumbprint, deviceCodeEndpoint, {
74
- body: queryString,
75
- headers: headers,
76
- });
77
- return {
78
- userCode,
79
- deviceCode,
80
- verificationUri,
81
- expiresIn,
82
- interval,
83
- message,
84
- };
85
- }
86
- /**
87
- * Create device code endpoint query parameters and returns string
88
- */
89
- createQueryString(request) {
90
- const parameterBuilder = new msalCommon.RequestParameterBuilder();
91
- parameterBuilder.addScopes(request.scopes);
92
- parameterBuilder.addClientId(this.config.authOptions.clientId);
93
- if (request.extraQueryParameters) {
94
- parameterBuilder.addExtraQueryParameters(request.extraQueryParameters);
95
- }
96
- if (request.claims ||
97
- (this.config.authOptions.clientCapabilities &&
98
- this.config.authOptions.clientCapabilities.length > 0)) {
99
- parameterBuilder.addClaims(request.claims, this.config.authOptions.clientCapabilities);
100
- }
101
- return parameterBuilder.createQueryString();
102
- }
103
- /**
104
- * Breaks the polling with specific conditions.
105
- * @param request CommonDeviceCodeRequest
106
- * @param deviceCodeResponse DeviceCodeResponse
107
- */
108
- continuePolling(deviceCodeExpirationTime, userSpecifiedTimeout, userSpecifiedCancelFlag) {
109
- if (userSpecifiedCancelFlag) {
110
- this.logger.error("Token request cancelled by setting DeviceCodeRequest.cancel = true");
111
- throw msalCommon.createClientAuthError(msalCommon.ClientAuthErrorCodes.deviceCodePollingCancelled);
112
- }
113
- else if (userSpecifiedTimeout &&
114
- userSpecifiedTimeout < deviceCodeExpirationTime &&
115
- msalCommon.TimeUtils.nowSeconds() > userSpecifiedTimeout) {
116
- this.logger.error(`User defined timeout for device code polling reached. The timeout was set for ${userSpecifiedTimeout}`);
117
- throw msalCommon.createClientAuthError(msalCommon.ClientAuthErrorCodes.userTimeoutReached);
118
- }
119
- else if (msalCommon.TimeUtils.nowSeconds() > deviceCodeExpirationTime) {
120
- if (userSpecifiedTimeout) {
121
- this.logger.verbose(`User specified timeout ignored as the device code has expired before the timeout elapsed. The user specified timeout was set for ${userSpecifiedTimeout}`);
122
- }
123
- this.logger.error(`Device code expired. Expiration time of device code was ${deviceCodeExpirationTime}`);
124
- throw msalCommon.createClientAuthError(msalCommon.ClientAuthErrorCodes.deviceCodeExpired);
125
- }
126
- return true;
127
- }
128
- /**
129
- * Creates token request with device code response and polls token endpoint at interval set by the device code
130
- * response
131
- * @param request
132
- * @param deviceCodeResponse
133
- */
134
- async acquireTokenWithDeviceCode(request, deviceCodeResponse) {
135
- const queryParametersString = this.createTokenQueryParameters(request);
136
- const endpoint = msalCommon.UrlString.appendQueryString(this.authority.tokenEndpoint, queryParametersString);
137
- const requestBody = this.createTokenRequestBody(request, deviceCodeResponse);
138
- const headers = this.createTokenRequestHeaders();
139
- const userSpecifiedTimeout = request.timeout
140
- ? msalCommon.TimeUtils.nowSeconds() + request.timeout
141
- : undefined;
142
- const deviceCodeExpirationTime = msalCommon.TimeUtils.nowSeconds() + deviceCodeResponse.expiresIn;
143
- const pollingIntervalMilli = deviceCodeResponse.interval * 1000;
144
- /*
145
- * Poll token endpoint while (device code is not expired AND operation has not been cancelled by
146
- * setting CancellationToken.cancel = true). POST request is sent at interval set by pollingIntervalMilli
147
- */
148
- while (this.continuePolling(deviceCodeExpirationTime, userSpecifiedTimeout, request.cancel)) {
149
- const thumbprint = {
150
- clientId: this.config.authOptions.clientId,
151
- authority: request.authority,
152
- scopes: request.scopes,
153
- claims: request.claims,
154
- authenticationScheme: request.authenticationScheme,
155
- resourceRequestMethod: request.resourceRequestMethod,
156
- resourceRequestUri: request.resourceRequestUri,
157
- shrClaims: request.shrClaims,
158
- sshKid: request.sshKid,
159
- };
160
- const response = await this.executePostToTokenEndpoint(endpoint, requestBody, headers, thumbprint, request.correlationId);
161
- if (response.body && response.body.error) {
162
- // user authorization is pending. Sleep for polling interval and try again
163
- if (response.body.error === msalCommon.Constants.AUTHORIZATION_PENDING) {
164
- this.logger.info("Authorization pending. Continue polling.");
165
- await msalCommon.TimeUtils.delay(pollingIntervalMilli);
166
- }
167
- else {
168
- // for any other error, throw
169
- this.logger.info("Unexpected error in polling from the server");
170
- throw msalCommon.createAuthError(msalCommon.AuthErrorCodes.postRequestFailed, response.body.error);
171
- }
172
- }
173
- else {
174
- this.logger.verbose("Authorization completed successfully. Polling stopped.");
175
- return response.body;
176
- }
177
- }
178
- /*
179
- * The above code should've thrown by this point, but to satisfy TypeScript,
180
- * and in the rare case the conditionals in continuePolling() may not catch everything...
181
- */
182
- this.logger.error("Polling stopped for unknown reasons.");
183
- throw msalCommon.createClientAuthError(msalCommon.ClientAuthErrorCodes.deviceCodeUnknownError);
184
- }
185
- /**
186
- * Creates query parameters and converts to string.
187
- * @param request
188
- * @param deviceCodeResponse
189
- */
190
- createTokenRequestBody(request, deviceCodeResponse) {
191
- const requestParameters = new msalCommon.RequestParameterBuilder();
192
- requestParameters.addScopes(request.scopes);
193
- requestParameters.addClientId(this.config.authOptions.clientId);
194
- requestParameters.addGrantType(msalCommon.GrantType.DEVICE_CODE_GRANT);
195
- requestParameters.addDeviceCode(deviceCodeResponse.deviceCode);
196
- const correlationId = request.correlationId ||
197
- this.config.cryptoInterface.createNewGuid();
198
- requestParameters.addCorrelationId(correlationId);
199
- requestParameters.addClientInfo();
200
- requestParameters.addLibraryInfo(this.config.libraryInfo);
201
- requestParameters.addApplicationTelemetry(this.config.telemetry.application);
202
- requestParameters.addThrottling();
203
- if (this.serverTelemetryManager) {
204
- requestParameters.addServerTelemetry(this.serverTelemetryManager);
205
- }
206
- if (!msalCommon.StringUtils.isEmptyObj(request.claims) ||
207
- (this.config.authOptions.clientCapabilities &&
208
- this.config.authOptions.clientCapabilities.length > 0)) {
209
- requestParameters.addClaims(request.claims, this.config.authOptions.clientCapabilities);
210
- }
211
- return requestParameters.createQueryString();
212
- }
7
+ /*
8
+ * Copyright (c) Microsoft Corporation. All rights reserved.
9
+ * Licensed under the MIT License.
10
+ */
11
+ /**
12
+ * OAuth2.0 Device code client
13
+ */
14
+ class DeviceCodeClient extends msalCommon.BaseClient {
15
+ constructor(configuration) {
16
+ super(configuration);
17
+ }
18
+ /**
19
+ * Gets device code from device code endpoint, calls back to with device code response, and
20
+ * polls token endpoint to exchange device code for tokens
21
+ * @param request
22
+ */
23
+ async acquireToken(request) {
24
+ const deviceCodeResponse = await this.getDeviceCode(request);
25
+ request.deviceCodeCallback(deviceCodeResponse);
26
+ const reqTimestamp = msalCommon.TimeUtils.nowSeconds();
27
+ const response = await this.acquireTokenWithDeviceCode(request, deviceCodeResponse);
28
+ const responseHandler = new msalCommon.ResponseHandler(this.config.authOptions.clientId, this.cacheManager, this.cryptoUtils, this.logger, this.config.serializableCache, this.config.persistencePlugin);
29
+ // Validate response. This function throws a server error if an error is returned by the server.
30
+ responseHandler.validateTokenResponse(response);
31
+ return await responseHandler.handleServerTokenResponse(response, this.authority, reqTimestamp, request);
32
+ }
33
+ /**
34
+ * Creates device code request and executes http GET
35
+ * @param request
36
+ */
37
+ async getDeviceCode(request) {
38
+ const queryParametersString = this.createExtraQueryParameters(request);
39
+ const endpoint = msalCommon.UrlString.appendQueryString(this.authority.deviceCodeEndpoint, queryParametersString);
40
+ const queryString = this.createQueryString(request);
41
+ const headers = this.createTokenRequestHeaders();
42
+ const thumbprint = {
43
+ clientId: this.config.authOptions.clientId,
44
+ authority: request.authority,
45
+ scopes: request.scopes,
46
+ claims: request.claims,
47
+ authenticationScheme: request.authenticationScheme,
48
+ resourceRequestMethod: request.resourceRequestMethod,
49
+ resourceRequestUri: request.resourceRequestUri,
50
+ shrClaims: request.shrClaims,
51
+ sshKid: request.sshKid,
52
+ };
53
+ return this.executePostRequestToDeviceCodeEndpoint(endpoint, queryString, headers, thumbprint);
54
+ }
55
+ /**
56
+ * Creates query string for the device code request
57
+ * @param request
58
+ */
59
+ createExtraQueryParameters(request) {
60
+ const parameterBuilder = new msalCommon.RequestParameterBuilder();
61
+ if (request.extraQueryParameters) {
62
+ parameterBuilder.addExtraQueryParameters(request.extraQueryParameters);
63
+ }
64
+ return parameterBuilder.createQueryString();
65
+ }
66
+ /**
67
+ * Executes POST request to device code endpoint
68
+ * @param deviceCodeEndpoint
69
+ * @param queryString
70
+ * @param headers
71
+ */
72
+ async executePostRequestToDeviceCodeEndpoint(deviceCodeEndpoint, queryString, headers, thumbprint) {
73
+ const { body: { user_code: userCode, device_code: deviceCode, verification_uri: verificationUri, expires_in: expiresIn, interval, message, }, } = await this.networkManager.sendPostRequest(thumbprint, deviceCodeEndpoint, {
74
+ body: queryString,
75
+ headers: headers,
76
+ });
77
+ return {
78
+ userCode,
79
+ deviceCode,
80
+ verificationUri,
81
+ expiresIn,
82
+ interval,
83
+ message,
84
+ };
85
+ }
86
+ /**
87
+ * Create device code endpoint query parameters and returns string
88
+ */
89
+ createQueryString(request) {
90
+ const parameterBuilder = new msalCommon.RequestParameterBuilder();
91
+ parameterBuilder.addScopes(request.scopes);
92
+ parameterBuilder.addClientId(this.config.authOptions.clientId);
93
+ if (request.extraQueryParameters) {
94
+ parameterBuilder.addExtraQueryParameters(request.extraQueryParameters);
95
+ }
96
+ if (request.claims ||
97
+ (this.config.authOptions.clientCapabilities &&
98
+ this.config.authOptions.clientCapabilities.length > 0)) {
99
+ parameterBuilder.addClaims(request.claims, this.config.authOptions.clientCapabilities);
100
+ }
101
+ return parameterBuilder.createQueryString();
102
+ }
103
+ /**
104
+ * Breaks the polling with specific conditions.
105
+ * @param request CommonDeviceCodeRequest
106
+ * @param deviceCodeResponse DeviceCodeResponse
107
+ */
108
+ continuePolling(deviceCodeExpirationTime, userSpecifiedTimeout, userSpecifiedCancelFlag) {
109
+ if (userSpecifiedCancelFlag) {
110
+ this.logger.error("Token request cancelled by setting DeviceCodeRequest.cancel = true");
111
+ throw msalCommon.createClientAuthError(msalCommon.ClientAuthErrorCodes.deviceCodePollingCancelled);
112
+ }
113
+ else if (userSpecifiedTimeout &&
114
+ userSpecifiedTimeout < deviceCodeExpirationTime &&
115
+ msalCommon.TimeUtils.nowSeconds() > userSpecifiedTimeout) {
116
+ this.logger.error(`User defined timeout for device code polling reached. The timeout was set for ${userSpecifiedTimeout}`);
117
+ throw msalCommon.createClientAuthError(msalCommon.ClientAuthErrorCodes.userTimeoutReached);
118
+ }
119
+ else if (msalCommon.TimeUtils.nowSeconds() > deviceCodeExpirationTime) {
120
+ if (userSpecifiedTimeout) {
121
+ this.logger.verbose(`User specified timeout ignored as the device code has expired before the timeout elapsed. The user specified timeout was set for ${userSpecifiedTimeout}`);
122
+ }
123
+ this.logger.error(`Device code expired. Expiration time of device code was ${deviceCodeExpirationTime}`);
124
+ throw msalCommon.createClientAuthError(msalCommon.ClientAuthErrorCodes.deviceCodeExpired);
125
+ }
126
+ return true;
127
+ }
128
+ /**
129
+ * Creates token request with device code response and polls token endpoint at interval set by the device code
130
+ * response
131
+ * @param request
132
+ * @param deviceCodeResponse
133
+ */
134
+ async acquireTokenWithDeviceCode(request, deviceCodeResponse) {
135
+ const queryParametersString = this.createTokenQueryParameters(request);
136
+ const endpoint = msalCommon.UrlString.appendQueryString(this.authority.tokenEndpoint, queryParametersString);
137
+ const requestBody = this.createTokenRequestBody(request, deviceCodeResponse);
138
+ const headers = this.createTokenRequestHeaders();
139
+ const userSpecifiedTimeout = request.timeout
140
+ ? msalCommon.TimeUtils.nowSeconds() + request.timeout
141
+ : undefined;
142
+ const deviceCodeExpirationTime = msalCommon.TimeUtils.nowSeconds() + deviceCodeResponse.expiresIn;
143
+ const pollingIntervalMilli = deviceCodeResponse.interval * 1000;
144
+ /*
145
+ * Poll token endpoint while (device code is not expired AND operation has not been cancelled by
146
+ * setting CancellationToken.cancel = true). POST request is sent at interval set by pollingIntervalMilli
147
+ */
148
+ while (this.continuePolling(deviceCodeExpirationTime, userSpecifiedTimeout, request.cancel)) {
149
+ const thumbprint = {
150
+ clientId: this.config.authOptions.clientId,
151
+ authority: request.authority,
152
+ scopes: request.scopes,
153
+ claims: request.claims,
154
+ authenticationScheme: request.authenticationScheme,
155
+ resourceRequestMethod: request.resourceRequestMethod,
156
+ resourceRequestUri: request.resourceRequestUri,
157
+ shrClaims: request.shrClaims,
158
+ sshKid: request.sshKid,
159
+ };
160
+ const response = await this.executePostToTokenEndpoint(endpoint, requestBody, headers, thumbprint, request.correlationId);
161
+ if (response.body && response.body.error) {
162
+ // user authorization is pending. Sleep for polling interval and try again
163
+ if (response.body.error === msalCommon.Constants.AUTHORIZATION_PENDING) {
164
+ this.logger.info("Authorization pending. Continue polling.");
165
+ await msalCommon.TimeUtils.delay(pollingIntervalMilli);
166
+ }
167
+ else {
168
+ // for any other error, throw
169
+ this.logger.info("Unexpected error in polling from the server");
170
+ throw msalCommon.createAuthError(msalCommon.AuthErrorCodes.postRequestFailed, response.body.error);
171
+ }
172
+ }
173
+ else {
174
+ this.logger.verbose("Authorization completed successfully. Polling stopped.");
175
+ return response.body;
176
+ }
177
+ }
178
+ /*
179
+ * The above code should've thrown by this point, but to satisfy TypeScript,
180
+ * and in the rare case the conditionals in continuePolling() may not catch everything...
181
+ */
182
+ this.logger.error("Polling stopped for unknown reasons.");
183
+ throw msalCommon.createClientAuthError(msalCommon.ClientAuthErrorCodes.deviceCodeUnknownError);
184
+ }
185
+ /**
186
+ * Creates query parameters and converts to string.
187
+ * @param request
188
+ * @param deviceCodeResponse
189
+ */
190
+ createTokenRequestBody(request, deviceCodeResponse) {
191
+ const requestParameters = new msalCommon.RequestParameterBuilder();
192
+ requestParameters.addScopes(request.scopes);
193
+ requestParameters.addClientId(this.config.authOptions.clientId);
194
+ requestParameters.addGrantType(msalCommon.GrantType.DEVICE_CODE_GRANT);
195
+ requestParameters.addDeviceCode(deviceCodeResponse.deviceCode);
196
+ const correlationId = request.correlationId ||
197
+ this.config.cryptoInterface.createNewGuid();
198
+ requestParameters.addCorrelationId(correlationId);
199
+ requestParameters.addClientInfo();
200
+ requestParameters.addLibraryInfo(this.config.libraryInfo);
201
+ requestParameters.addApplicationTelemetry(this.config.telemetry.application);
202
+ requestParameters.addThrottling();
203
+ if (this.serverTelemetryManager) {
204
+ requestParameters.addServerTelemetry(this.serverTelemetryManager);
205
+ }
206
+ if (!msalCommon.StringUtils.isEmptyObj(request.claims) ||
207
+ (this.config.authOptions.clientCapabilities &&
208
+ this.config.authOptions.clientCapabilities.length > 0)) {
209
+ requestParameters.addClaims(request.claims, this.config.authOptions.clientCapabilities);
210
+ }
211
+ return requestParameters.createQueryString();
212
+ }
213
213
  }
214
214
 
215
215
  exports.DeviceCodeClient = DeviceCodeClient;
@@ -1,54 +1,54 @@
1
- import { AuthenticationResult, BaseClient, ClientConfiguration, CommonDeviceCodeRequest } from "@azure/msal-common";
2
- /**
3
- * OAuth2.0 Device code client
4
- */
5
- export declare class DeviceCodeClient extends BaseClient {
6
- constructor(configuration: ClientConfiguration);
7
- /**
8
- * Gets device code from device code endpoint, calls back to with device code response, and
9
- * polls token endpoint to exchange device code for tokens
10
- * @param request
11
- */
12
- acquireToken(request: CommonDeviceCodeRequest): Promise<AuthenticationResult | null>;
13
- /**
14
- * Creates device code request and executes http GET
15
- * @param request
16
- */
17
- private getDeviceCode;
18
- /**
19
- * Creates query string for the device code request
20
- * @param request
21
- */
22
- createExtraQueryParameters(request: CommonDeviceCodeRequest): string;
23
- /**
24
- * Executes POST request to device code endpoint
25
- * @param deviceCodeEndpoint
26
- * @param queryString
27
- * @param headers
28
- */
29
- private executePostRequestToDeviceCodeEndpoint;
30
- /**
31
- * Create device code endpoint query parameters and returns string
32
- */
33
- private createQueryString;
34
- /**
35
- * Breaks the polling with specific conditions.
36
- * @param request CommonDeviceCodeRequest
37
- * @param deviceCodeResponse DeviceCodeResponse
38
- */
39
- private continuePolling;
40
- /**
41
- * Creates token request with device code response and polls token endpoint at interval set by the device code
42
- * response
43
- * @param request
44
- * @param deviceCodeResponse
45
- */
46
- private acquireTokenWithDeviceCode;
47
- /**
48
- * Creates query parameters and converts to string.
49
- * @param request
50
- * @param deviceCodeResponse
51
- */
52
- private createTokenRequestBody;
53
- }
1
+ import { AuthenticationResult, BaseClient, ClientConfiguration, CommonDeviceCodeRequest } from "@azure/msal-common";
2
+ /**
3
+ * OAuth2.0 Device code client
4
+ */
5
+ export declare class DeviceCodeClient extends BaseClient {
6
+ constructor(configuration: ClientConfiguration);
7
+ /**
8
+ * Gets device code from device code endpoint, calls back to with device code response, and
9
+ * polls token endpoint to exchange device code for tokens
10
+ * @param request
11
+ */
12
+ acquireToken(request: CommonDeviceCodeRequest): Promise<AuthenticationResult | null>;
13
+ /**
14
+ * Creates device code request and executes http GET
15
+ * @param request
16
+ */
17
+ private getDeviceCode;
18
+ /**
19
+ * Creates query string for the device code request
20
+ * @param request
21
+ */
22
+ createExtraQueryParameters(request: CommonDeviceCodeRequest): string;
23
+ /**
24
+ * Executes POST request to device code endpoint
25
+ * @param deviceCodeEndpoint
26
+ * @param queryString
27
+ * @param headers
28
+ */
29
+ private executePostRequestToDeviceCodeEndpoint;
30
+ /**
31
+ * Create device code endpoint query parameters and returns string
32
+ */
33
+ private createQueryString;
34
+ /**
35
+ * Breaks the polling with specific conditions.
36
+ * @param request CommonDeviceCodeRequest
37
+ * @param deviceCodeResponse DeviceCodeResponse
38
+ */
39
+ private continuePolling;
40
+ /**
41
+ * Creates token request with device code response and polls token endpoint at interval set by the device code
42
+ * response
43
+ * @param request
44
+ * @param deviceCodeResponse
45
+ */
46
+ private acquireTokenWithDeviceCode;
47
+ /**
48
+ * Creates query parameters and converts to string.
49
+ * @param request
50
+ * @param deviceCodeResponse
51
+ */
52
+ private createTokenRequestBody;
53
+ }
54
54
  //# sourceMappingURL=DeviceCodeClient.d.ts.map