@typespec/ts-http-runtime 0.1.0-alpha.20250325.2 → 0.2.0-alpha.20250326.3

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 (261) hide show
  1. package/dist/browser/auth/credentials.d.ts +77 -0
  2. package/dist/browser/auth/credentials.js +27 -0
  3. package/dist/browser/auth/credentials.js.map +1 -0
  4. package/dist/browser/auth/oauth2Flows.d.ts +57 -0
  5. package/dist/browser/{abort-controller/AbortSignalLike.js → auth/oauth2Flows.js} +1 -1
  6. package/dist/browser/auth/oauth2Flows.js.map +1 -0
  7. package/dist/browser/auth/schemes.d.ts +53 -0
  8. package/dist/{esm/abort-controller/AbortSignalLike.js → browser/auth/schemes.js} +1 -1
  9. package/dist/browser/auth/schemes.js.map +1 -0
  10. package/dist/browser/client/clientHelpers.d.ts +1 -20
  11. package/dist/browser/client/clientHelpers.js +21 -30
  12. package/dist/browser/client/clientHelpers.js.map +1 -1
  13. package/dist/browser/client/common.d.ts +13 -15
  14. package/dist/browser/client/common.js.map +1 -1
  15. package/dist/browser/client/getClient.d.ts +1 -9
  16. package/dist/browser/client/getClient.js +8 -16
  17. package/dist/browser/client/getClient.js.map +1 -1
  18. package/dist/browser/constants.js +1 -1
  19. package/dist/browser/constants.js.map +1 -1
  20. package/dist/browser/index.d.ts +3 -3
  21. package/dist/browser/index.js +2 -2
  22. package/dist/browser/index.js.map +1 -1
  23. package/dist/browser/interfaces.d.ts +10 -2
  24. package/dist/browser/interfaces.js.map +1 -1
  25. package/dist/browser/pipelineRequest.d.ts +1 -2
  26. package/dist/browser/pipelineRequest.js.map +1 -1
  27. package/dist/browser/policies/auth/apiKeyAuthenticationPolicy.d.ts +30 -0
  28. package/dist/browser/policies/auth/apiKeyAuthenticationPolicy.js +31 -0
  29. package/dist/browser/policies/auth/apiKeyAuthenticationPolicy.js.map +1 -0
  30. package/dist/browser/policies/auth/basicAuthenticationPolicy.d.ts +30 -0
  31. package/dist/browser/policies/auth/basicAuthenticationPolicy.js +31 -0
  32. package/dist/browser/policies/auth/basicAuthenticationPolicy.js.map +1 -0
  33. package/dist/browser/policies/auth/bearerAuthenticationPolicy.d.ts +30 -0
  34. package/dist/browser/policies/auth/bearerAuthenticationPolicy.js +31 -0
  35. package/dist/browser/policies/auth/bearerAuthenticationPolicy.js.map +1 -0
  36. package/dist/browser/policies/auth/checkInsecureConnection.d.ts +9 -0
  37. package/dist/browser/policies/auth/checkInsecureConnection.js +50 -0
  38. package/dist/browser/policies/auth/checkInsecureConnection.js.map +1 -0
  39. package/dist/browser/policies/auth/oauth2AuthenticationPolicy.d.ts +31 -0
  40. package/dist/browser/policies/auth/oauth2AuthenticationPolicy.js +31 -0
  41. package/dist/browser/policies/auth/oauth2AuthenticationPolicy.js.map +1 -0
  42. package/dist/browser/util/aborterUtils.d.ts +3 -4
  43. package/dist/browser/util/aborterUtils.js.map +1 -1
  44. package/dist/browser/util/helpers.d.ts +1 -2
  45. package/dist/browser/util/helpers.js.map +1 -1
  46. package/dist/commonjs/auth/credentials.d.ts +77 -0
  47. package/dist/commonjs/auth/credentials.js +33 -0
  48. package/dist/commonjs/auth/credentials.js.map +1 -0
  49. package/dist/commonjs/auth/oauth2Flows.d.ts +57 -0
  50. package/dist/commonjs/{abort-controller/AbortSignalLike.js → auth/oauth2Flows.js} +1 -1
  51. package/dist/commonjs/auth/oauth2Flows.js.map +1 -0
  52. package/dist/commonjs/auth/schemes.d.ts +53 -0
  53. package/dist/commonjs/auth/schemes.js +5 -0
  54. package/dist/commonjs/auth/schemes.js.map +1 -0
  55. package/dist/commonjs/client/clientHelpers.d.ts +1 -20
  56. package/dist/commonjs/client/clientHelpers.js +21 -31
  57. package/dist/commonjs/client/clientHelpers.js.map +1 -1
  58. package/dist/commonjs/client/common.d.ts +13 -15
  59. package/dist/commonjs/client/common.js.map +1 -1
  60. package/dist/commonjs/client/getClient.d.ts +1 -9
  61. package/dist/commonjs/client/getClient.js +8 -16
  62. package/dist/commonjs/client/getClient.js.map +1 -1
  63. package/dist/commonjs/constants.js +1 -1
  64. package/dist/commonjs/constants.js.map +1 -1
  65. package/dist/commonjs/index.d.ts +3 -3
  66. package/dist/commonjs/index.js +4 -5
  67. package/dist/commonjs/index.js.map +1 -1
  68. package/dist/commonjs/interfaces.d.ts +10 -2
  69. package/dist/commonjs/interfaces.js.map +1 -1
  70. package/dist/commonjs/pipelineRequest.d.ts +1 -2
  71. package/dist/commonjs/pipelineRequest.js.map +1 -1
  72. package/dist/commonjs/policies/auth/apiKeyAuthenticationPolicy.d.ts +30 -0
  73. package/dist/commonjs/policies/auth/apiKeyAuthenticationPolicy.js +35 -0
  74. package/dist/commonjs/policies/auth/apiKeyAuthenticationPolicy.js.map +1 -0
  75. package/dist/commonjs/policies/auth/basicAuthenticationPolicy.d.ts +30 -0
  76. package/dist/commonjs/policies/auth/basicAuthenticationPolicy.js +35 -0
  77. package/dist/commonjs/policies/auth/basicAuthenticationPolicy.js.map +1 -0
  78. package/dist/commonjs/policies/auth/bearerAuthenticationPolicy.d.ts +30 -0
  79. package/dist/commonjs/policies/auth/bearerAuthenticationPolicy.js +35 -0
  80. package/dist/commonjs/policies/auth/bearerAuthenticationPolicy.js.map +1 -0
  81. package/dist/commonjs/policies/auth/checkInsecureConnection.d.ts +9 -0
  82. package/dist/commonjs/policies/auth/checkInsecureConnection.js +53 -0
  83. package/dist/commonjs/policies/auth/checkInsecureConnection.js.map +1 -0
  84. package/dist/commonjs/policies/auth/oauth2AuthenticationPolicy.d.ts +31 -0
  85. package/dist/commonjs/policies/auth/oauth2AuthenticationPolicy.js +35 -0
  86. package/dist/commonjs/policies/auth/oauth2AuthenticationPolicy.js.map +1 -0
  87. package/dist/commonjs/util/aborterUtils.d.ts +3 -4
  88. package/dist/commonjs/util/aborterUtils.js.map +1 -1
  89. package/dist/commonjs/util/helpers.d.ts +1 -2
  90. package/dist/commonjs/util/helpers.js.map +1 -1
  91. package/dist/esm/auth/credentials.d.ts +77 -0
  92. package/dist/esm/auth/credentials.js +27 -0
  93. package/dist/esm/auth/credentials.js.map +1 -0
  94. package/dist/esm/auth/oauth2Flows.d.ts +57 -0
  95. package/dist/{react-native/abort-controller/AbortSignalLike.js → esm/auth/oauth2Flows.js} +1 -1
  96. package/dist/esm/auth/oauth2Flows.js.map +1 -0
  97. package/dist/esm/auth/schemes.d.ts +53 -0
  98. package/dist/esm/auth/schemes.js +4 -0
  99. package/dist/esm/auth/schemes.js.map +1 -0
  100. package/dist/esm/client/clientHelpers.d.ts +1 -20
  101. package/dist/esm/client/clientHelpers.js +21 -30
  102. package/dist/esm/client/clientHelpers.js.map +1 -1
  103. package/dist/esm/client/common.d.ts +13 -15
  104. package/dist/esm/client/common.js.map +1 -1
  105. package/dist/esm/client/getClient.d.ts +1 -9
  106. package/dist/esm/client/getClient.js +8 -16
  107. package/dist/esm/client/getClient.js.map +1 -1
  108. package/dist/esm/constants.js +1 -1
  109. package/dist/esm/constants.js.map +1 -1
  110. package/dist/esm/index.d.ts +3 -3
  111. package/dist/esm/index.js +2 -2
  112. package/dist/esm/index.js.map +1 -1
  113. package/dist/esm/interfaces.d.ts +10 -2
  114. package/dist/esm/interfaces.js.map +1 -1
  115. package/dist/esm/pipelineRequest.d.ts +1 -2
  116. package/dist/esm/pipelineRequest.js.map +1 -1
  117. package/dist/esm/policies/auth/apiKeyAuthenticationPolicy.d.ts +30 -0
  118. package/dist/esm/policies/auth/apiKeyAuthenticationPolicy.js +31 -0
  119. package/dist/esm/policies/auth/apiKeyAuthenticationPolicy.js.map +1 -0
  120. package/dist/esm/policies/auth/basicAuthenticationPolicy.d.ts +30 -0
  121. package/dist/esm/policies/auth/basicAuthenticationPolicy.js +31 -0
  122. package/dist/esm/policies/auth/basicAuthenticationPolicy.js.map +1 -0
  123. package/dist/esm/policies/auth/bearerAuthenticationPolicy.d.ts +30 -0
  124. package/dist/esm/policies/auth/bearerAuthenticationPolicy.js +31 -0
  125. package/dist/esm/policies/auth/bearerAuthenticationPolicy.js.map +1 -0
  126. package/dist/esm/policies/auth/checkInsecureConnection.d.ts +9 -0
  127. package/dist/esm/policies/auth/checkInsecureConnection.js +50 -0
  128. package/dist/esm/policies/auth/checkInsecureConnection.js.map +1 -0
  129. package/dist/esm/policies/auth/oauth2AuthenticationPolicy.d.ts +31 -0
  130. package/dist/esm/policies/auth/oauth2AuthenticationPolicy.js +31 -0
  131. package/dist/esm/policies/auth/oauth2AuthenticationPolicy.js.map +1 -0
  132. package/dist/esm/util/aborterUtils.d.ts +3 -4
  133. package/dist/esm/util/aborterUtils.js.map +1 -1
  134. package/dist/esm/util/helpers.d.ts +1 -2
  135. package/dist/esm/util/helpers.js.map +1 -1
  136. package/dist/react-native/auth/credentials.d.ts +77 -0
  137. package/dist/react-native/auth/credentials.js +27 -0
  138. package/dist/react-native/auth/credentials.js.map +1 -0
  139. package/dist/react-native/auth/oauth2Flows.d.ts +57 -0
  140. package/dist/react-native/auth/oauth2Flows.js +4 -0
  141. package/dist/react-native/auth/oauth2Flows.js.map +1 -0
  142. package/dist/react-native/auth/schemes.d.ts +53 -0
  143. package/dist/react-native/auth/schemes.js +4 -0
  144. package/dist/react-native/auth/schemes.js.map +1 -0
  145. package/dist/react-native/client/clientHelpers.d.ts +1 -20
  146. package/dist/react-native/client/clientHelpers.js +21 -30
  147. package/dist/react-native/client/clientHelpers.js.map +1 -1
  148. package/dist/react-native/client/common.d.ts +13 -15
  149. package/dist/react-native/client/common.js.map +1 -1
  150. package/dist/react-native/client/getClient.d.ts +1 -9
  151. package/dist/react-native/client/getClient.js +8 -16
  152. package/dist/react-native/client/getClient.js.map +1 -1
  153. package/dist/react-native/constants.js +1 -1
  154. package/dist/react-native/constants.js.map +1 -1
  155. package/dist/react-native/index.d.ts +3 -3
  156. package/dist/react-native/index.js +2 -2
  157. package/dist/react-native/index.js.map +1 -1
  158. package/dist/react-native/interfaces.d.ts +10 -2
  159. package/dist/react-native/interfaces.js.map +1 -1
  160. package/dist/react-native/pipelineRequest.d.ts +1 -2
  161. package/dist/react-native/pipelineRequest.js.map +1 -1
  162. package/dist/react-native/policies/auth/apiKeyAuthenticationPolicy.d.ts +30 -0
  163. package/dist/react-native/policies/auth/apiKeyAuthenticationPolicy.js +31 -0
  164. package/dist/react-native/policies/auth/apiKeyAuthenticationPolicy.js.map +1 -0
  165. package/dist/react-native/policies/auth/basicAuthenticationPolicy.d.ts +30 -0
  166. package/dist/react-native/policies/auth/basicAuthenticationPolicy.js +31 -0
  167. package/dist/react-native/policies/auth/basicAuthenticationPolicy.js.map +1 -0
  168. package/dist/react-native/policies/auth/bearerAuthenticationPolicy.d.ts +30 -0
  169. package/dist/react-native/policies/auth/bearerAuthenticationPolicy.js +31 -0
  170. package/dist/react-native/policies/auth/bearerAuthenticationPolicy.js.map +1 -0
  171. package/dist/react-native/policies/auth/checkInsecureConnection.d.ts +9 -0
  172. package/dist/react-native/policies/auth/checkInsecureConnection.js +50 -0
  173. package/dist/react-native/policies/auth/checkInsecureConnection.js.map +1 -0
  174. package/dist/react-native/policies/auth/oauth2AuthenticationPolicy.d.ts +31 -0
  175. package/dist/react-native/policies/auth/oauth2AuthenticationPolicy.js +31 -0
  176. package/dist/react-native/policies/auth/oauth2AuthenticationPolicy.js.map +1 -0
  177. package/dist/react-native/util/aborterUtils.d.ts +3 -4
  178. package/dist/react-native/util/aborterUtils.js.map +1 -1
  179. package/dist/react-native/util/helpers.d.ts +1 -2
  180. package/dist/react-native/util/helpers.js.map +1 -1
  181. package/package.json +1 -1
  182. package/dist/browser/abort-controller/AbortSignalLike.d.ts +0 -19
  183. package/dist/browser/abort-controller/AbortSignalLike.js.map +0 -1
  184. package/dist/browser/accessTokenCache.d.ts +0 -40
  185. package/dist/browser/accessTokenCache.js +0 -32
  186. package/dist/browser/accessTokenCache.js.map +0 -1
  187. package/dist/browser/auth/keyCredential.d.ts +0 -16
  188. package/dist/browser/auth/keyCredential.js +0 -12
  189. package/dist/browser/auth/keyCredential.js.map +0 -1
  190. package/dist/browser/auth/tokenCredential.d.ts +0 -71
  191. package/dist/browser/auth/tokenCredential.js +0 -19
  192. package/dist/browser/auth/tokenCredential.js.map +0 -1
  193. package/dist/browser/client/keyCredentialAuthenticationPolicy.d.ts +0 -8
  194. package/dist/browser/client/keyCredentialAuthenticationPolicy.js +0 -16
  195. package/dist/browser/client/keyCredentialAuthenticationPolicy.js.map +0 -1
  196. package/dist/browser/policies/bearerTokenAuthenticationPolicy.d.ts +0 -99
  197. package/dist/browser/policies/bearerTokenAuthenticationPolicy.js +0 -107
  198. package/dist/browser/policies/bearerTokenAuthenticationPolicy.js.map +0 -1
  199. package/dist/browser/util/tokenCycler.d.ts +0 -45
  200. package/dist/browser/util/tokenCycler.js +0 -162
  201. package/dist/browser/util/tokenCycler.js.map +0 -1
  202. package/dist/commonjs/abort-controller/AbortSignalLike.d.ts +0 -19
  203. package/dist/commonjs/abort-controller/AbortSignalLike.js.map +0 -1
  204. package/dist/commonjs/accessTokenCache.d.ts +0 -40
  205. package/dist/commonjs/accessTokenCache.js +0 -36
  206. package/dist/commonjs/accessTokenCache.js.map +0 -1
  207. package/dist/commonjs/auth/keyCredential.d.ts +0 -16
  208. package/dist/commonjs/auth/keyCredential.js +0 -15
  209. package/dist/commonjs/auth/keyCredential.js.map +0 -1
  210. package/dist/commonjs/auth/tokenCredential.d.ts +0 -71
  211. package/dist/commonjs/auth/tokenCredential.js +0 -22
  212. package/dist/commonjs/auth/tokenCredential.js.map +0 -1
  213. package/dist/commonjs/client/keyCredentialAuthenticationPolicy.d.ts +0 -8
  214. package/dist/commonjs/client/keyCredentialAuthenticationPolicy.js +0 -20
  215. package/dist/commonjs/client/keyCredentialAuthenticationPolicy.js.map +0 -1
  216. package/dist/commonjs/policies/bearerTokenAuthenticationPolicy.d.ts +0 -99
  217. package/dist/commonjs/policies/bearerTokenAuthenticationPolicy.js +0 -111
  218. package/dist/commonjs/policies/bearerTokenAuthenticationPolicy.js.map +0 -1
  219. package/dist/commonjs/util/tokenCycler.d.ts +0 -45
  220. package/dist/commonjs/util/tokenCycler.js +0 -166
  221. package/dist/commonjs/util/tokenCycler.js.map +0 -1
  222. package/dist/esm/abort-controller/AbortSignalLike.d.ts +0 -19
  223. package/dist/esm/abort-controller/AbortSignalLike.js.map +0 -1
  224. package/dist/esm/accessTokenCache.d.ts +0 -40
  225. package/dist/esm/accessTokenCache.js +0 -32
  226. package/dist/esm/accessTokenCache.js.map +0 -1
  227. package/dist/esm/auth/keyCredential.d.ts +0 -16
  228. package/dist/esm/auth/keyCredential.js +0 -12
  229. package/dist/esm/auth/keyCredential.js.map +0 -1
  230. package/dist/esm/auth/tokenCredential.d.ts +0 -71
  231. package/dist/esm/auth/tokenCredential.js +0 -19
  232. package/dist/esm/auth/tokenCredential.js.map +0 -1
  233. package/dist/esm/client/keyCredentialAuthenticationPolicy.d.ts +0 -8
  234. package/dist/esm/client/keyCredentialAuthenticationPolicy.js +0 -16
  235. package/dist/esm/client/keyCredentialAuthenticationPolicy.js.map +0 -1
  236. package/dist/esm/policies/bearerTokenAuthenticationPolicy.d.ts +0 -99
  237. package/dist/esm/policies/bearerTokenAuthenticationPolicy.js +0 -107
  238. package/dist/esm/policies/bearerTokenAuthenticationPolicy.js.map +0 -1
  239. package/dist/esm/util/tokenCycler.d.ts +0 -45
  240. package/dist/esm/util/tokenCycler.js +0 -162
  241. package/dist/esm/util/tokenCycler.js.map +0 -1
  242. package/dist/react-native/abort-controller/AbortSignalLike.d.ts +0 -19
  243. package/dist/react-native/abort-controller/AbortSignalLike.js.map +0 -1
  244. package/dist/react-native/accessTokenCache.d.ts +0 -40
  245. package/dist/react-native/accessTokenCache.js +0 -32
  246. package/dist/react-native/accessTokenCache.js.map +0 -1
  247. package/dist/react-native/auth/keyCredential.d.ts +0 -16
  248. package/dist/react-native/auth/keyCredential.js +0 -12
  249. package/dist/react-native/auth/keyCredential.js.map +0 -1
  250. package/dist/react-native/auth/tokenCredential.d.ts +0 -71
  251. package/dist/react-native/auth/tokenCredential.js +0 -19
  252. package/dist/react-native/auth/tokenCredential.js.map +0 -1
  253. package/dist/react-native/client/keyCredentialAuthenticationPolicy.d.ts +0 -8
  254. package/dist/react-native/client/keyCredentialAuthenticationPolicy.js +0 -16
  255. package/dist/react-native/client/keyCredentialAuthenticationPolicy.js.map +0 -1
  256. package/dist/react-native/policies/bearerTokenAuthenticationPolicy.d.ts +0 -99
  257. package/dist/react-native/policies/bearerTokenAuthenticationPolicy.js +0 -107
  258. package/dist/react-native/policies/bearerTokenAuthenticationPolicy.js.map +0 -1
  259. package/dist/react-native/util/tokenCycler.d.ts +0 -45
  260. package/dist/react-native/util/tokenCycler.js +0 -162
  261. package/dist/react-native/util/tokenCycler.js.map +0 -1
@@ -1,162 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
- import { delay } from "./helpers.js";
4
- // Default options for the cycler if none are provided
5
- export const DEFAULT_CYCLER_OPTIONS = {
6
- forcedRefreshWindowInMs: 1000, // Force waiting for a refresh 1s before the token expires
7
- retryIntervalInMs: 3000, // Allow refresh attempts every 3s
8
- refreshWindowInMs: 1000 * 60 * 2, // Start refreshing 2m before expiry
9
- };
10
- /**
11
- * Converts an an unreliable access token getter (which may resolve with null)
12
- * into an AccessTokenGetter by retrying the unreliable getter in a regular
13
- * interval.
14
- *
15
- * @param getAccessToken - A function that produces a promise of an access token that may fail by returning null.
16
- * @param retryIntervalInMs - The time (in milliseconds) to wait between retry attempts.
17
- * @param refreshTimeout - The timestamp after which the refresh attempt will fail, throwing an exception.
18
- * @returns - A promise that, if it resolves, will resolve with an access token.
19
- */
20
- async function beginRefresh(getAccessToken, retryIntervalInMs, refreshTimeout) {
21
- // This wrapper handles exceptions gracefully as long as we haven't exceeded
22
- // the timeout.
23
- async function tryGetAccessToken() {
24
- if (Date.now() < refreshTimeout) {
25
- try {
26
- return await getAccessToken();
27
- }
28
- catch (_a) {
29
- return null;
30
- }
31
- }
32
- else {
33
- const finalToken = await getAccessToken();
34
- // Timeout is up, so throw if it's still null
35
- if (finalToken === null) {
36
- throw new Error("Failed to refresh access token.");
37
- }
38
- return finalToken;
39
- }
40
- }
41
- let token = await tryGetAccessToken();
42
- while (token === null) {
43
- await delay(retryIntervalInMs);
44
- token = await tryGetAccessToken();
45
- }
46
- return token;
47
- }
48
- /**
49
- * Creates a token cycler from a credential, scopes, and optional settings.
50
- *
51
- * A token cycler represents a way to reliably retrieve a valid access token
52
- * from a TokenCredential. It will handle initializing the token, refreshing it
53
- * when it nears expiration, and synchronizes refresh attempts to avoid
54
- * concurrency hazards.
55
- *
56
- * @param credential - the underlying TokenCredential that provides the access
57
- * token
58
- * @param tokenCyclerOptions - optionally override default settings for the cycler
59
- *
60
- * @returns - a function that reliably produces a valid access token
61
- */
62
- export function createTokenCycler(credential, tokenCyclerOptions) {
63
- let refreshWorker = null;
64
- let token = null;
65
- let tenantId;
66
- const options = Object.assign(Object.assign({}, DEFAULT_CYCLER_OPTIONS), tokenCyclerOptions);
67
- /**
68
- * This little holder defines several predicates that we use to construct
69
- * the rules of refreshing the token.
70
- */
71
- const cycler = {
72
- /**
73
- * Produces true if a refresh job is currently in progress.
74
- */
75
- get isRefreshing() {
76
- return refreshWorker !== null;
77
- },
78
- /**
79
- * Produces true if the cycler SHOULD refresh (we are within the refresh
80
- * window and not already refreshing)
81
- */
82
- get shouldRefresh() {
83
- var _a;
84
- if (cycler.isRefreshing) {
85
- return false;
86
- }
87
- if ((token === null || token === void 0 ? void 0 : token.refreshAfterTimestamp) && token.refreshAfterTimestamp < Date.now()) {
88
- return true;
89
- }
90
- return ((_a = token === null || token === void 0 ? void 0 : token.expiresOnTimestamp) !== null && _a !== void 0 ? _a : 0) - options.refreshWindowInMs < Date.now();
91
- },
92
- /**
93
- * Produces true if the cycler MUST refresh (null or nearly-expired
94
- * token).
95
- */
96
- get mustRefresh() {
97
- return (token === null || token.expiresOnTimestamp - options.forcedRefreshWindowInMs < Date.now());
98
- },
99
- };
100
- /**
101
- * Starts a refresh job or returns the existing job if one is already
102
- * running.
103
- */
104
- function refresh(scopes, getTokenOptions) {
105
- var _a;
106
- if (!cycler.isRefreshing) {
107
- // We bind `scopes` here to avoid passing it around a lot
108
- const tryGetAccessToken = () => credential.getToken(scopes, getTokenOptions);
109
- // Take advantage of promise chaining to insert an assignment to `token`
110
- // before the refresh can be considered done.
111
- refreshWorker = beginRefresh(tryGetAccessToken, options.retryIntervalInMs,
112
- // If we don't have a token, then we should timeout immediately
113
- (_a = token === null || token === void 0 ? void 0 : token.expiresOnTimestamp) !== null && _a !== void 0 ? _a : Date.now())
114
- .then((_token) => {
115
- refreshWorker = null;
116
- token = _token;
117
- tenantId = getTokenOptions.tenantId;
118
- return token;
119
- })
120
- .catch((reason) => {
121
- // We also should reset the refresher if we enter a failed state. All
122
- // existing awaiters will throw, but subsequent requests will start a
123
- // new retry chain.
124
- refreshWorker = null;
125
- token = null;
126
- tenantId = undefined;
127
- throw reason;
128
- });
129
- }
130
- return refreshWorker;
131
- }
132
- return async (scopes, tokenOptions) => {
133
- //
134
- // Simple rules:
135
- // - If we MUST refresh, then return the refresh task, blocking
136
- // the pipeline until a token is available.
137
- // - If we SHOULD refresh, then run refresh but don't return it
138
- // (we can still use the cached token).
139
- // - Return the token, since it's fine if we didn't return in
140
- // step 1.
141
- //
142
- const hasClaimChallenge = Boolean(tokenOptions.claims);
143
- const tenantIdChanged = tenantId !== tokenOptions.tenantId;
144
- if (hasClaimChallenge) {
145
- // If we've received a claim, we know the existing token isn't valid
146
- // We want to clear it so that that refresh worker won't use the old expiration time as a timeout
147
- token = null;
148
- }
149
- // If the tenantId passed in token options is different to the one we have
150
- // Or if we are in claim challenge and the token was rejected and a new access token need to be issued, we need to
151
- // refresh the token with the new tenantId or token.
152
- const mustRefresh = tenantIdChanged || hasClaimChallenge || cycler.mustRefresh;
153
- if (mustRefresh) {
154
- return refresh(scopes, tokenOptions);
155
- }
156
- if (cycler.shouldRefresh) {
157
- refresh(scopes, tokenOptions);
158
- }
159
- return token;
160
- };
161
- }
162
- //# sourceMappingURL=tokenCycler.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tokenCycler.js","sourceRoot":"","sources":["../../../src/util/tokenCycler.ts"],"names":[],"mappings":"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","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { AccessToken, GetTokenOptions, TokenCredential } from \"../auth/tokenCredential.js\";\nimport { delay } from \"./helpers.js\";\n\n/**\n * A function that gets a promise of an access token and allows providing\n * options.\n *\n * @param options - the options to pass to the underlying token provider\n */\nexport type AccessTokenGetter = (\n scopes: string | string[],\n options: GetTokenOptions,\n) => Promise<AccessToken>;\n\nexport interface TokenCyclerOptions {\n /**\n * The window of time before token expiration during which the token will be\n * considered unusable due to risk of the token expiring before sending the\n * request.\n *\n * This will only become meaningful if the refresh fails for over\n * (refreshWindow - forcedRefreshWindow) milliseconds.\n */\n forcedRefreshWindowInMs: number;\n /**\n * Interval in milliseconds to retry failed token refreshes.\n */\n retryIntervalInMs: number;\n /**\n * The window of time before token expiration during which\n * we will attempt to refresh the token.\n */\n refreshWindowInMs: number;\n}\n\n// Default options for the cycler if none are provided\nexport const DEFAULT_CYCLER_OPTIONS: TokenCyclerOptions = {\n forcedRefreshWindowInMs: 1000, // Force waiting for a refresh 1s before the token expires\n retryIntervalInMs: 3000, // Allow refresh attempts every 3s\n refreshWindowInMs: 1000 * 60 * 2, // Start refreshing 2m before expiry\n};\n\n/**\n * Converts an an unreliable access token getter (which may resolve with null)\n * into an AccessTokenGetter by retrying the unreliable getter in a regular\n * interval.\n *\n * @param getAccessToken - A function that produces a promise of an access token that may fail by returning null.\n * @param retryIntervalInMs - The time (in milliseconds) to wait between retry attempts.\n * @param refreshTimeout - The timestamp after which the refresh attempt will fail, throwing an exception.\n * @returns - A promise that, if it resolves, will resolve with an access token.\n */\nasync function beginRefresh(\n getAccessToken: () => Promise<AccessToken | null>,\n retryIntervalInMs: number,\n refreshTimeout: number,\n): Promise<AccessToken> {\n // This wrapper handles exceptions gracefully as long as we haven't exceeded\n // the timeout.\n async function tryGetAccessToken(): Promise<AccessToken | null> {\n if (Date.now() < refreshTimeout) {\n try {\n return await getAccessToken();\n } catch {\n return null;\n }\n } else {\n const finalToken = await getAccessToken();\n\n // Timeout is up, so throw if it's still null\n if (finalToken === null) {\n throw new Error(\"Failed to refresh access token.\");\n }\n\n return finalToken;\n }\n }\n\n let token: AccessToken | null = await tryGetAccessToken();\n\n while (token === null) {\n await delay(retryIntervalInMs);\n\n token = await tryGetAccessToken();\n }\n\n return token;\n}\n\n/**\n * Creates a token cycler from a credential, scopes, and optional settings.\n *\n * A token cycler represents a way to reliably retrieve a valid access token\n * from a TokenCredential. It will handle initializing the token, refreshing it\n * when it nears expiration, and synchronizes refresh attempts to avoid\n * concurrency hazards.\n *\n * @param credential - the underlying TokenCredential that provides the access\n * token\n * @param tokenCyclerOptions - optionally override default settings for the cycler\n *\n * @returns - a function that reliably produces a valid access token\n */\nexport function createTokenCycler(\n credential: TokenCredential,\n tokenCyclerOptions?: Partial<TokenCyclerOptions>,\n): AccessTokenGetter {\n let refreshWorker: Promise<AccessToken> | null = null;\n let token: AccessToken | null = null;\n let tenantId: string | undefined;\n\n const options = {\n ...DEFAULT_CYCLER_OPTIONS,\n ...tokenCyclerOptions,\n };\n\n /**\n * This little holder defines several predicates that we use to construct\n * the rules of refreshing the token.\n */\n const cycler = {\n /**\n * Produces true if a refresh job is currently in progress.\n */\n get isRefreshing(): boolean {\n return refreshWorker !== null;\n },\n /**\n * Produces true if the cycler SHOULD refresh (we are within the refresh\n * window and not already refreshing)\n */\n get shouldRefresh(): boolean {\n if (cycler.isRefreshing) {\n return false;\n }\n if (token?.refreshAfterTimestamp && token.refreshAfterTimestamp < Date.now()) {\n return true;\n }\n\n return (token?.expiresOnTimestamp ?? 0) - options.refreshWindowInMs < Date.now();\n },\n /**\n * Produces true if the cycler MUST refresh (null or nearly-expired\n * token).\n */\n get mustRefresh(): boolean {\n return (\n token === null || token.expiresOnTimestamp - options.forcedRefreshWindowInMs < Date.now()\n );\n },\n };\n\n /**\n * Starts a refresh job or returns the existing job if one is already\n * running.\n */\n function refresh(\n scopes: string | string[],\n getTokenOptions: GetTokenOptions,\n ): Promise<AccessToken> {\n if (!cycler.isRefreshing) {\n // We bind `scopes` here to avoid passing it around a lot\n const tryGetAccessToken = (): Promise<AccessToken | null> =>\n credential.getToken(scopes, getTokenOptions);\n\n // Take advantage of promise chaining to insert an assignment to `token`\n // before the refresh can be considered done.\n refreshWorker = beginRefresh(\n tryGetAccessToken,\n options.retryIntervalInMs,\n // If we don't have a token, then we should timeout immediately\n token?.expiresOnTimestamp ?? Date.now(),\n )\n .then((_token) => {\n refreshWorker = null;\n token = _token;\n tenantId = getTokenOptions.tenantId;\n return token;\n })\n .catch((reason) => {\n // We also should reset the refresher if we enter a failed state. All\n // existing awaiters will throw, but subsequent requests will start a\n // new retry chain.\n refreshWorker = null;\n token = null;\n tenantId = undefined;\n throw reason;\n });\n }\n\n return refreshWorker as Promise<AccessToken>;\n }\n\n return async (scopes: string | string[], tokenOptions: GetTokenOptions): Promise<AccessToken> => {\n //\n // Simple rules:\n // - If we MUST refresh, then return the refresh task, blocking\n // the pipeline until a token is available.\n // - If we SHOULD refresh, then run refresh but don't return it\n // (we can still use the cached token).\n // - Return the token, since it's fine if we didn't return in\n // step 1.\n //\n\n const hasClaimChallenge = Boolean(tokenOptions.claims);\n const tenantIdChanged = tenantId !== tokenOptions.tenantId;\n\n if (hasClaimChallenge) {\n // If we've received a claim, we know the existing token isn't valid\n // We want to clear it so that that refresh worker won't use the old expiration time as a timeout\n token = null;\n }\n\n // If the tenantId passed in token options is different to the one we have\n // Or if we are in claim challenge and the token was rejected and a new access token need to be issued, we need to\n // refresh the token with the new tenantId or token.\n const mustRefresh = tenantIdChanged || hasClaimChallenge || cycler.mustRefresh;\n\n if (mustRefresh) {\n return refresh(scopes, tokenOptions);\n }\n\n if (cycler.shouldRefresh) {\n refresh(scopes, tokenOptions);\n }\n\n return token as AccessToken;\n };\n}\n"]}
@@ -1,19 +0,0 @@
1
- /**
2
- * Allows the request to be aborted upon firing of the "abort" event.
3
- * Compatible with the browser built-in AbortSignal and common polyfills.
4
- */
5
- export interface AbortSignalLike {
6
- /**
7
- * Indicates if the signal has already been aborted.
8
- */
9
- readonly aborted: boolean;
10
- /**
11
- * Add new "abort" event listener, only support "abort" event.
12
- */
13
- addEventListener(type: "abort", listener: (this: AbortSignalLike, ev: any) => any, options?: any): void;
14
- /**
15
- * Remove "abort" event listener, only support "abort" event.
16
- */
17
- removeEventListener(type: "abort", listener: (this: AbortSignalLike, ev: any) => any, options?: any): void;
18
- }
19
- //# sourceMappingURL=AbortSignalLike.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AbortSignalLike.js","sourceRoot":"","sources":["../../../src/abort-controller/AbortSignalLike.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\n/**\n * Allows the request to be aborted upon firing of the \"abort\" event.\n * Compatible with the browser built-in AbortSignal and common polyfills.\n */\nexport interface AbortSignalLike {\n /**\n * Indicates if the signal has already been aborted.\n */\n readonly aborted: boolean;\n /**\n * Add new \"abort\" event listener, only support \"abort\" event.\n */\n addEventListener(\n type: \"abort\",\n listener: (this: AbortSignalLike, ev: any) => any,\n options?: any,\n ): void;\n /**\n * Remove \"abort\" event listener, only support \"abort\" event.\n */\n removeEventListener(\n type: \"abort\",\n listener: (this: AbortSignalLike, ev: any) => any,\n options?: any,\n ): void;\n}\n"]}
@@ -1,40 +0,0 @@
1
- import type { AccessToken } from "./auth/tokenCredential.js";
2
- /**
3
- * Defines the default token refresh buffer duration.
4
- */
5
- export declare const DefaultTokenRefreshBufferMs: number;
6
- /**
7
- * Provides a cache for an AccessToken that was that
8
- * was returned from a TokenCredential.
9
- */
10
- export interface AccessTokenCache {
11
- /**
12
- * Sets the cached token.
13
- *
14
- * @param accessToken - The AccessToken to be cached or null to
15
- * clear the cached token.
16
- */
17
- setCachedToken(accessToken: AccessToken | undefined): void;
18
- /**
19
- * Returns the cached AccessToken or undefined if nothing is cached.
20
- */
21
- getCachedToken(): AccessToken | undefined;
22
- }
23
- /**
24
- * Provides an AccessTokenCache implementation which clears
25
- * the cached AccessToken's after the expiresOnTimestamp has
26
- * passed.
27
- * @internal
28
- */
29
- export declare class ExpiringAccessTokenCache implements AccessTokenCache {
30
- private tokenRefreshBufferMs;
31
- private cachedToken?;
32
- /**
33
- * Constructs an instance of ExpiringAccessTokenCache with
34
- * an optional expiration buffer time.
35
- */
36
- constructor(tokenRefreshBufferMs?: number);
37
- setCachedToken(accessToken: AccessToken | undefined): void;
38
- getCachedToken(): AccessToken | undefined;
39
- }
40
- //# sourceMappingURL=accessTokenCache.d.ts.map
@@ -1,32 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
- /**
4
- * Defines the default token refresh buffer duration.
5
- */
6
- export const DefaultTokenRefreshBufferMs = 2 * 60 * 1000; // 2 Minutes
7
- /**
8
- * Provides an AccessTokenCache implementation which clears
9
- * the cached AccessToken's after the expiresOnTimestamp has
10
- * passed.
11
- * @internal
12
- */
13
- export class ExpiringAccessTokenCache {
14
- /**
15
- * Constructs an instance of ExpiringAccessTokenCache with
16
- * an optional expiration buffer time.
17
- */
18
- constructor(tokenRefreshBufferMs = DefaultTokenRefreshBufferMs) {
19
- this.tokenRefreshBufferMs = tokenRefreshBufferMs;
20
- }
21
- setCachedToken(accessToken) {
22
- this.cachedToken = accessToken;
23
- }
24
- getCachedToken() {
25
- if (this.cachedToken &&
26
- Date.now() + this.tokenRefreshBufferMs >= this.cachedToken.expiresOnTimestamp) {
27
- this.cachedToken = undefined;
28
- }
29
- return this.cachedToken;
30
- }
31
- }
32
- //# sourceMappingURL=accessTokenCache.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"accessTokenCache.js","sourceRoot":"","sources":["../../src/accessTokenCache.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAIlC;;GAEG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,YAAY;AAqBtE;;;;;GAKG;AACH,MAAM,OAAO,wBAAwB;IAInC;;;OAGG;IACH,YAAY,uBAA+B,2BAA2B;QACpE,IAAI,CAAC,oBAAoB,GAAG,oBAAoB,CAAC;IACnD,CAAC;IAED,cAAc,CAAC,WAAoC;QACjD,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;IACjC,CAAC;IAED,cAAc;QACZ,IACE,IAAI,CAAC,WAAW;YAChB,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,oBAAoB,IAAI,IAAI,CAAC,WAAW,CAAC,kBAAkB,EAC7E,CAAC;YACD,IAAI,CAAC,WAAW,GAAG,SAAS,CAAC;QAC/B,CAAC;QAED,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { AccessToken } from \"./auth/tokenCredential.js\";\n\n/**\n * Defines the default token refresh buffer duration.\n */\nexport const DefaultTokenRefreshBufferMs = 2 * 60 * 1000; // 2 Minutes\n\n/**\n * Provides a cache for an AccessToken that was that\n * was returned from a TokenCredential.\n */\nexport interface AccessTokenCache {\n /**\n * Sets the cached token.\n *\n * @param accessToken - The AccessToken to be cached or null to\n * clear the cached token.\n */\n setCachedToken(accessToken: AccessToken | undefined): void;\n\n /**\n * Returns the cached AccessToken or undefined if nothing is cached.\n */\n getCachedToken(): AccessToken | undefined;\n}\n\n/**\n * Provides an AccessTokenCache implementation which clears\n * the cached AccessToken's after the expiresOnTimestamp has\n * passed.\n * @internal\n */\nexport class ExpiringAccessTokenCache implements AccessTokenCache {\n private tokenRefreshBufferMs: number;\n private cachedToken?: AccessToken;\n\n /**\n * Constructs an instance of ExpiringAccessTokenCache with\n * an optional expiration buffer time.\n */\n constructor(tokenRefreshBufferMs: number = DefaultTokenRefreshBufferMs) {\n this.tokenRefreshBufferMs = tokenRefreshBufferMs;\n }\n\n setCachedToken(accessToken: AccessToken | undefined): void {\n this.cachedToken = accessToken;\n }\n\n getCachedToken(): AccessToken | undefined {\n if (\n this.cachedToken &&\n Date.now() + this.tokenRefreshBufferMs >= this.cachedToken.expiresOnTimestamp\n ) {\n this.cachedToken = undefined;\n }\n\n return this.cachedToken;\n }\n}\n"]}
@@ -1,16 +0,0 @@
1
- /**
2
- * Represents a credential defined by a static API key.
3
- */
4
- export interface KeyCredential {
5
- /**
6
- * The value of the API key represented as a string
7
- */
8
- readonly key: string;
9
- }
10
- /**
11
- * Tests an object to determine whether it implements KeyCredential.
12
- *
13
- * @param credential - The assumed KeyCredential to be tested.
14
- */
15
- export declare function isKeyCredential(credential: unknown): credential is KeyCredential;
16
- //# sourceMappingURL=keyCredential.d.ts.map
@@ -1,12 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
- import { isObjectWithProperties } from "../util/typeGuards.js";
4
- /**
5
- * Tests an object to determine whether it implements KeyCredential.
6
- *
7
- * @param credential - The assumed KeyCredential to be tested.
8
- */
9
- export function isKeyCredential(credential) {
10
- return isObjectWithProperties(credential, ["key"]) && typeof credential.key === "string";
11
- }
12
- //# sourceMappingURL=keyCredential.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"keyCredential.js","sourceRoot":"","sources":["../../../src/auth/keyCredential.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAY/D;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,UAAmB;IACjD,OAAO,sBAAsB,CAAC,UAAU,EAAE,CAAC,KAAK,CAAC,CAAC,IAAI,OAAO,UAAU,CAAC,GAAG,KAAK,QAAQ,CAAC;AAC3F,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport { isObjectWithProperties } from \"../util/typeGuards.js\";\n\n/**\n * Represents a credential defined by a static API key.\n */\nexport interface KeyCredential {\n /**\n * The value of the API key represented as a string\n */\n readonly key: string;\n}\n\n/**\n * Tests an object to determine whether it implements KeyCredential.\n *\n * @param credential - The assumed KeyCredential to be tested.\n */\nexport function isKeyCredential(credential: unknown): credential is KeyCredential {\n return isObjectWithProperties(credential, [\"key\"]) && typeof credential.key === \"string\";\n}\n"]}
@@ -1,71 +0,0 @@
1
- import type { AbortSignalLike } from "../abort-controller/AbortSignalLike.js";
2
- /**
3
- * Represents a credential capable of providing an authentication token.
4
- */
5
- export interface TokenCredential {
6
- /**
7
- * Gets the token provided by this credential.
8
- *
9
- * This method is called automatically by Azure SDK client libraries. You may call this method
10
- * directly, but you must also handle token caching and token refreshing.
11
- *
12
- * @param scopes - The list of scopes for which the token will have access.
13
- * @param options - The options used to configure any requests this
14
- * TokenCredential implementation might make.
15
- */
16
- getToken(scopes: string | string[], options?: GetTokenOptions): Promise<AccessToken | null>;
17
- }
18
- /**
19
- * Defines options for TokenCredential.getToken.
20
- */
21
- export interface GetTokenOptions {
22
- /**
23
- * The signal which can be used to abort requests.
24
- */
25
- abortSignal?: AbortSignalLike;
26
- /**
27
- * Options used when creating and sending HTTP requests for this operation.
28
- */
29
- requestOptions?: {
30
- /**
31
- * The number of milliseconds a request can take before automatically being terminated.
32
- */
33
- timeout?: number;
34
- };
35
- /**
36
- * Claim details to perform the Continuous Access Evaluation authentication flow
37
- */
38
- claims?: string;
39
- /**
40
- * Indicates whether to enable the Continuous Access Evaluation authentication flow
41
- */
42
- enableCae?: boolean;
43
- /**
44
- * Allows specifying a tenantId. Useful to handle challenges that provide tenant Id hints.
45
- */
46
- tenantId?: string;
47
- }
48
- /**
49
- * Represents an access token with an expiration time.
50
- */
51
- export interface AccessToken {
52
- /**
53
- * The access token returned by the authentication service.
54
- */
55
- token: string;
56
- /**
57
- * The access token's expiration timestamp in milliseconds, UNIX epoch time.
58
- */
59
- expiresOnTimestamp: number;
60
- /**
61
- * The timestamp when the access token should be refreshed, in milliseconds, UNIX epoch time.
62
- */
63
- refreshAfterTimestamp?: number;
64
- }
65
- /**
66
- * Tests an object to determine whether it implements TokenCredential.
67
- *
68
- * @param credential - The assumed TokenCredential to be tested.
69
- */
70
- export declare function isTokenCredential(credential: unknown): credential is TokenCredential;
71
- //# sourceMappingURL=tokenCredential.d.ts.map
@@ -1,19 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
- /**
4
- * Tests an object to determine whether it implements TokenCredential.
5
- *
6
- * @param credential - The assumed TokenCredential to be tested.
7
- */
8
- export function isTokenCredential(credential) {
9
- // Check for an object with a 'getToken' function and possibly with
10
- // a 'signRequest' function. We do this check to make sure that
11
- // a ServiceClientCredentials implementor (like TokenClientCredentials
12
- // in ms-rest-nodeauth) doesn't get mistaken for a TokenCredential if
13
- // it doesn't actually implement TokenCredential also.
14
- const castCredential = credential;
15
- return (castCredential &&
16
- typeof castCredential.getToken === "function" &&
17
- (castCredential.signRequest === undefined || castCredential.getToken.length > 0));
18
- }
19
- //# sourceMappingURL=tokenCredential.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tokenCredential.js","sourceRoot":"","sources":["../../../src/auth/tokenCredential.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AA0ElC;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,UAAmB;IACnD,mEAAmE;IACnE,gEAAgE;IAChE,sEAAsE;IACtE,qEAAqE;IACrE,sDAAsD;IACtD,MAAM,cAAc,GAAG,UAGtB,CAAC;IACF,OAAO,CACL,cAAc;QACd,OAAO,cAAc,CAAC,QAAQ,KAAK,UAAU;QAC7C,CAAC,cAAc,CAAC,WAAW,KAAK,SAAS,IAAI,cAAc,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CACjF,CAAC;AACJ,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { AbortSignalLike } from \"../abort-controller/AbortSignalLike.js\";\n\n/**\n * Represents a credential capable of providing an authentication token.\n */\nexport interface TokenCredential {\n /**\n * Gets the token provided by this credential.\n *\n * This method is called automatically by Azure SDK client libraries. You may call this method\n * directly, but you must also handle token caching and token refreshing.\n *\n * @param scopes - The list of scopes for which the token will have access.\n * @param options - The options used to configure any requests this\n * TokenCredential implementation might make.\n */\n getToken(scopes: string | string[], options?: GetTokenOptions): Promise<AccessToken | null>;\n}\n\n/**\n * Defines options for TokenCredential.getToken.\n */\nexport interface GetTokenOptions {\n /**\n * The signal which can be used to abort requests.\n */\n abortSignal?: AbortSignalLike;\n /**\n * Options used when creating and sending HTTP requests for this operation.\n */\n requestOptions?: {\n /**\n * The number of milliseconds a request can take before automatically being terminated.\n */\n timeout?: number;\n };\n /**\n * Claim details to perform the Continuous Access Evaluation authentication flow\n */\n claims?: string;\n /**\n * Indicates whether to enable the Continuous Access Evaluation authentication flow\n */\n enableCae?: boolean;\n /**\n * Allows specifying a tenantId. Useful to handle challenges that provide tenant Id hints.\n */\n tenantId?: string;\n}\n\n/**\n * Represents an access token with an expiration time.\n */\nexport interface AccessToken {\n /**\n * The access token returned by the authentication service.\n */\n token: string;\n\n /**\n * The access token's expiration timestamp in milliseconds, UNIX epoch time.\n */\n expiresOnTimestamp: number;\n\n /**\n * The timestamp when the access token should be refreshed, in milliseconds, UNIX epoch time.\n */\n refreshAfterTimestamp?: number;\n\n // UNBRANDED DIFFERENCE: Unbranded Core does not support PoP (\"Proof-of-Presence\") tokens.\n}\n\n/**\n * Tests an object to determine whether it implements TokenCredential.\n *\n * @param credential - The assumed TokenCredential to be tested.\n */\nexport function isTokenCredential(credential: unknown): credential is TokenCredential {\n // Check for an object with a 'getToken' function and possibly with\n // a 'signRequest' function. We do this check to make sure that\n // a ServiceClientCredentials implementor (like TokenClientCredentials\n // in ms-rest-nodeauth) doesn't get mistaken for a TokenCredential if\n // it doesn't actually implement TokenCredential also.\n const castCredential = credential as {\n getToken: unknown;\n signRequest: unknown;\n };\n return (\n castCredential &&\n typeof castCredential.getToken === \"function\" &&\n (castCredential.signRequest === undefined || castCredential.getToken.length > 0)\n );\n}\n"]}
@@ -1,8 +0,0 @@
1
- import type { KeyCredential } from "../auth/keyCredential.js";
2
- import type { PipelinePolicy } from "../pipeline.js";
3
- /**
4
- * The programmatic identifier of the bearerTokenAuthenticationPolicy.
5
- */
6
- export declare const keyCredentialAuthenticationPolicyName = "keyCredentialAuthenticationPolicy";
7
- export declare function keyCredentialAuthenticationPolicy(credential: KeyCredential, apiKeyHeaderName: string): PipelinePolicy;
8
- //# sourceMappingURL=keyCredentialAuthenticationPolicy.d.ts.map
@@ -1,16 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
- /**
4
- * The programmatic identifier of the bearerTokenAuthenticationPolicy.
5
- */
6
- export const keyCredentialAuthenticationPolicyName = "keyCredentialAuthenticationPolicy";
7
- export function keyCredentialAuthenticationPolicy(credential, apiKeyHeaderName) {
8
- return {
9
- name: keyCredentialAuthenticationPolicyName,
10
- async sendRequest(request, next) {
11
- request.headers.set(apiKeyHeaderName, credential.key);
12
- return next(request);
13
- },
14
- };
15
- }
16
- //# sourceMappingURL=keyCredentialAuthenticationPolicy.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"keyCredentialAuthenticationPolicy.js","sourceRoot":"","sources":["../../../src/client/keyCredentialAuthenticationPolicy.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAMlC;;GAEG;AACH,MAAM,CAAC,MAAM,qCAAqC,GAAG,mCAAmC,CAAC;AAEzF,MAAM,UAAU,iCAAiC,CAC/C,UAAyB,EACzB,gBAAwB;IAExB,OAAO;QACL,IAAI,EAAE,qCAAqC;QAC3C,KAAK,CAAC,WAAW,CAAC,OAAwB,EAAE,IAAiB;YAC3D,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC;YACtD,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC;QACvB,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { KeyCredential } from \"../auth/keyCredential.js\";\nimport type { PipelineRequest, PipelineResponse, SendRequest } from \"../interfaces.js\";\nimport type { PipelinePolicy } from \"../pipeline.js\";\n\n/**\n * The programmatic identifier of the bearerTokenAuthenticationPolicy.\n */\nexport const keyCredentialAuthenticationPolicyName = \"keyCredentialAuthenticationPolicy\";\n\nexport function keyCredentialAuthenticationPolicy(\n credential: KeyCredential,\n apiKeyHeaderName: string,\n): PipelinePolicy {\n return {\n name: keyCredentialAuthenticationPolicyName,\n async sendRequest(request: PipelineRequest, next: SendRequest): Promise<PipelineResponse> {\n request.headers.set(apiKeyHeaderName, credential.key);\n return next(request);\n },\n };\n}\n"]}
@@ -1,99 +0,0 @@
1
- import type { AccessToken, GetTokenOptions, TokenCredential } from "../auth/tokenCredential.js";
2
- import type { TypeSpecRuntimeLogger } from "../logger/logger.js";
3
- import type { PipelineRequest, PipelineResponse } from "../interfaces.js";
4
- import type { PipelinePolicy } from "../pipeline.js";
5
- /**
6
- * The programmatic identifier of the bearerTokenAuthenticationPolicy.
7
- */
8
- export declare const bearerTokenAuthenticationPolicyName = "bearerTokenAuthenticationPolicy";
9
- /**
10
- * Options sent to the authorizeRequest callback
11
- */
12
- export interface AuthorizeRequestOptions {
13
- /**
14
- * The scopes for which the bearer token applies.
15
- */
16
- scopes: string[];
17
- /**
18
- * Function that retrieves either a cached access token or a new access token.
19
- */
20
- getAccessToken: (scopes: string[], options: GetTokenOptions) => Promise<AccessToken | null>;
21
- /**
22
- * Request that the policy is trying to fulfill.
23
- */
24
- request: PipelineRequest;
25
- /**
26
- * A logger, if one was sent through the HTTP pipeline.
27
- */
28
- logger?: TypeSpecRuntimeLogger;
29
- }
30
- /**
31
- * Options sent to the authorizeRequestOnChallenge callback
32
- */
33
- export interface AuthorizeRequestOnChallengeOptions {
34
- /**
35
- * The scopes for which the bearer token applies.
36
- */
37
- scopes: string[];
38
- /**
39
- * Function that retrieves either a cached access token or a new access token.
40
- */
41
- getAccessToken: (scopes: string[], options: GetTokenOptions) => Promise<AccessToken | null>;
42
- /**
43
- * Request that the policy is trying to fulfill.
44
- */
45
- request: PipelineRequest;
46
- /**
47
- * Response containing the challenge.
48
- */
49
- response: PipelineResponse;
50
- /**
51
- * A logger, if one was sent through the HTTP pipeline.
52
- */
53
- logger?: TypeSpecRuntimeLogger;
54
- }
55
- /**
56
- * Options to override the processing of [Continuous Access Evaluation](https://learn.microsoft.com/azure/active-directory/conditional-access/concept-continuous-access-evaluation) challenges.
57
- */
58
- export interface ChallengeCallbacks {
59
- /**
60
- * Allows for the authorization of the main request of this policy before it's sent.
61
- */
62
- authorizeRequest?(options: AuthorizeRequestOptions): Promise<void>;
63
- /**
64
- * Allows to handle authentication challenges and to re-authorize the request.
65
- * The response containing the challenge is `options.response`.
66
- * If this method returns true, the underlying request will be sent once again.
67
- * The request may be modified before being sent.
68
- */
69
- authorizeRequestOnChallenge?(options: AuthorizeRequestOnChallengeOptions): Promise<boolean>;
70
- }
71
- /**
72
- * Options to configure the bearerTokenAuthenticationPolicy
73
- */
74
- export interface BearerTokenAuthenticationPolicyOptions {
75
- /**
76
- * The TokenCredential implementation that can supply the bearer token.
77
- */
78
- credential?: TokenCredential;
79
- /**
80
- * The scopes for which the bearer token applies.
81
- */
82
- scopes: string | string[];
83
- /**
84
- * Allows for the processing of [Continuous Access Evaluation](https://learn.microsoft.com/azure/active-directory/conditional-access/concept-continuous-access-evaluation) challenges.
85
- * If provided, it must contain at least the `authorizeRequestOnChallenge` method.
86
- * If provided, after a request is sent, if it has a challenge, it can be processed to re-send the original request with the relevant challenge information.
87
- */
88
- challengeCallbacks?: ChallengeCallbacks;
89
- /**
90
- * A logger can be sent for debugging purposes.
91
- */
92
- logger?: TypeSpecRuntimeLogger;
93
- }
94
- /**
95
- * A policy that can request a token from a TokenCredential implementation and
96
- * then apply it to the Authorization header of a request as a Bearer token.
97
- */
98
- export declare function bearerTokenAuthenticationPolicy(options: BearerTokenAuthenticationPolicyOptions): PipelinePolicy;
99
- //# sourceMappingURL=bearerTokenAuthenticationPolicy.d.ts.map