@ic-reactor/react 3.13.0 → 4.0.0-beta.2

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 (174) hide show
  1. package/README.md +237 -787
  2. package/dist/index.d.ts +260 -16
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +471 -23
  5. package/dist/index.js.map +1 -1
  6. package/llms.txt +82 -278
  7. package/package.json +11 -39
  8. package/src/index.tsx +612 -0
  9. package/dist/auth/auth-client-compat.d.ts +0 -122
  10. package/dist/auth/auth-client-compat.d.ts.map +0 -1
  11. package/dist/auth/auth-client-compat.js +0 -162
  12. package/dist/auth/auth-client-compat.js.map +0 -1
  13. package/dist/auth/authentication-manager.d.ts +0 -405
  14. package/dist/auth/authentication-manager.d.ts.map +0 -1
  15. package/dist/auth/authentication-manager.js +0 -1537
  16. package/dist/auth/authentication-manager.js.map +0 -1
  17. package/dist/auth/constants.d.ts +0 -24
  18. package/dist/auth/constants.d.ts.map +0 -1
  19. package/dist/auth/constants.js +0 -24
  20. package/dist/auth/constants.js.map +0 -1
  21. package/dist/auth/createIdentityAttributeHooks.d.ts +0 -14
  22. package/dist/auth/createIdentityAttributeHooks.d.ts.map +0 -1
  23. package/dist/auth/createIdentityAttributeHooks.js +0 -122
  24. package/dist/auth/createIdentityAttributeHooks.js.map +0 -1
  25. package/dist/auth/identity-attributes-manager.d.ts +0 -27
  26. package/dist/auth/identity-attributes-manager.d.ts.map +0 -1
  27. package/dist/auth/identity-attributes-manager.js +0 -191
  28. package/dist/auth/identity-attributes-manager.js.map +0 -1
  29. package/dist/auth/identity-attributes.d.ts +0 -19
  30. package/dist/auth/identity-attributes.d.ts.map +0 -1
  31. package/dist/auth/identity-attributes.js +0 -227
  32. package/dist/auth/identity-attributes.js.map +0 -1
  33. package/dist/auth/index.d.ts +0 -8
  34. package/dist/auth/index.d.ts.map +0 -1
  35. package/dist/auth/index.js +0 -8
  36. package/dist/auth/index.js.map +0 -1
  37. package/dist/auth/local-ii-probe.d.ts +0 -57
  38. package/dist/auth/local-ii-probe.d.ts.map +0 -1
  39. package/dist/auth/local-ii-probe.js +0 -121
  40. package/dist/auth/local-ii-probe.js.map +0 -1
  41. package/dist/auth/types.d.ts +0 -222
  42. package/dist/auth/types.d.ts.map +0 -1
  43. package/dist/auth/types.js +0 -2
  44. package/dist/auth/types.js.map +0 -1
  45. package/dist/createActorHooks.d.ts +0 -41
  46. package/dist/createActorHooks.d.ts.map +0 -1
  47. package/dist/createActorHooks.js +0 -17
  48. package/dist/createActorHooks.js.map +0 -1
  49. package/dist/createInfiniteQuery.d.ts +0 -185
  50. package/dist/createInfiniteQuery.d.ts.map +0 -1
  51. package/dist/createInfiniteQuery.js +0 -198
  52. package/dist/createInfiniteQuery.js.map +0 -1
  53. package/dist/createMutation.d.ts +0 -33
  54. package/dist/createMutation.d.ts.map +0 -1
  55. package/dist/createMutation.js +0 -199
  56. package/dist/createMutation.js.map +0 -1
  57. package/dist/createQuery.d.ts +0 -63
  58. package/dist/createQuery.d.ts.map +0 -1
  59. package/dist/createQuery.js +0 -204
  60. package/dist/createQuery.js.map +0 -1
  61. package/dist/createReactorProvider.d.ts +0 -158
  62. package/dist/createReactorProvider.d.ts.map +0 -1
  63. package/dist/createReactorProvider.js +0 -256
  64. package/dist/createReactorProvider.js.map +0 -1
  65. package/dist/createSuspenseInfiniteQuery.d.ts +0 -154
  66. package/dist/createSuspenseInfiniteQuery.d.ts.map +0 -1
  67. package/dist/createSuspenseInfiniteQuery.js +0 -209
  68. package/dist/createSuspenseInfiniteQuery.js.map +0 -1
  69. package/dist/createSuspenseQuery.d.ts +0 -46
  70. package/dist/createSuspenseQuery.d.ts.map +0 -1
  71. package/dist/createSuspenseQuery.js +0 -158
  72. package/dist/createSuspenseQuery.js.map +0 -1
  73. package/dist/defineDisplayReactor.d.ts +0 -43
  74. package/dist/defineDisplayReactor.d.ts.map +0 -1
  75. package/dist/defineDisplayReactor.js +0 -42
  76. package/dist/defineDisplayReactor.js.map +0 -1
  77. package/dist/defineReactor.d.ts +0 -99
  78. package/dist/defineReactor.d.ts.map +0 -1
  79. package/dist/defineReactor.js +0 -15
  80. package/dist/defineReactor.js.map +0 -1
  81. package/dist/defineReactorShared.d.ts +0 -84
  82. package/dist/defineReactorShared.d.ts.map +0 -1
  83. package/dist/defineReactorShared.js +0 -139
  84. package/dist/defineReactorShared.js.map +0 -1
  85. package/dist/hooks/createAuthHooks.d.ts +0 -50
  86. package/dist/hooks/createAuthHooks.d.ts.map +0 -1
  87. package/dist/hooks/createAuthHooks.js +0 -291
  88. package/dist/hooks/createAuthHooks.js.map +0 -1
  89. package/dist/hooks/index.d.ts +0 -21
  90. package/dist/hooks/index.d.ts.map +0 -1
  91. package/dist/hooks/index.js +0 -24
  92. package/dist/hooks/index.js.map +0 -1
  93. package/dist/hooks/useActorInfiniteQuery.d.ts +0 -67
  94. package/dist/hooks/useActorInfiniteQuery.d.ts.map +0 -1
  95. package/dist/hooks/useActorInfiniteQuery.js +0 -89
  96. package/dist/hooks/useActorInfiniteQuery.js.map +0 -1
  97. package/dist/hooks/useActorMethod.d.ts +0 -148
  98. package/dist/hooks/useActorMethod.d.ts.map +0 -1
  99. package/dist/hooks/useActorMethod.js +0 -394
  100. package/dist/hooks/useActorMethod.js.map +0 -1
  101. package/dist/hooks/useActorMutation.d.ts +0 -51
  102. package/dist/hooks/useActorMutation.d.ts.map +0 -1
  103. package/dist/hooks/useActorMutation.js +0 -70
  104. package/dist/hooks/useActorMutation.js.map +0 -1
  105. package/dist/hooks/useActorQuery.d.ts +0 -45
  106. package/dist/hooks/useActorQuery.d.ts.map +0 -1
  107. package/dist/hooks/useActorQuery.js +0 -67
  108. package/dist/hooks/useActorQuery.js.map +0 -1
  109. package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts +0 -51
  110. package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts.map +0 -1
  111. package/dist/hooks/useActorSuspenseInfiniteQuery.js +0 -76
  112. package/dist/hooks/useActorSuspenseInfiniteQuery.js.map +0 -1
  113. package/dist/hooks/useActorSuspenseQuery.d.ts +0 -32
  114. package/dist/hooks/useActorSuspenseQuery.d.ts.map +0 -1
  115. package/dist/hooks/useActorSuspenseQuery.js +0 -58
  116. package/dist/hooks/useActorSuspenseQuery.js.map +0 -1
  117. package/dist/ownedAuthentication.d.ts +0 -52
  118. package/dist/ownedAuthentication.d.ts.map +0 -1
  119. package/dist/ownedAuthentication.js +0 -49
  120. package/dist/ownedAuthentication.js.map +0 -1
  121. package/dist/server.d.ts +0 -21
  122. package/dist/server.d.ts.map +0 -1
  123. package/dist/server.js +0 -23
  124. package/dist/server.js.map +0 -1
  125. package/dist/testing.d.ts +0 -19
  126. package/dist/testing.d.ts.map +0 -1
  127. package/dist/testing.js +0 -19
  128. package/dist/testing.js.map +0 -1
  129. package/dist/types.d.ts +0 -671
  130. package/dist/types.d.ts.map +0 -1
  131. package/dist/types.js +0 -5
  132. package/dist/types.js.map +0 -1
  133. package/dist/utils.d.ts +0 -207
  134. package/dist/utils.d.ts.map +0 -1
  135. package/dist/utils.js +0 -405
  136. package/dist/utils.js.map +0 -1
  137. package/dist/validation.d.ts +0 -136
  138. package/dist/validation.d.ts.map +0 -1
  139. package/dist/validation.js +0 -144
  140. package/dist/validation.js.map +0 -1
  141. package/src/auth/auth-client-compat.ts +0 -273
  142. package/src/auth/authentication-manager.ts +0 -1682
  143. package/src/auth/constants.ts +0 -32
  144. package/src/auth/createIdentityAttributeHooks.ts +0 -169
  145. package/src/auth/identity-attributes-manager.ts +0 -226
  146. package/src/auth/identity-attributes.ts +0 -345
  147. package/src/auth/index.ts +0 -7
  148. package/src/auth/local-ii-probe.ts +0 -173
  149. package/src/auth/types.ts +0 -243
  150. package/src/createActorHooks.ts +0 -208
  151. package/src/createInfiniteQuery.ts +0 -670
  152. package/src/createMutation.ts +0 -324
  153. package/src/createQuery.ts +0 -369
  154. package/src/createReactorProvider.ts +0 -365
  155. package/src/createSuspenseInfiniteQuery.ts +0 -651
  156. package/src/createSuspenseQuery.ts +0 -304
  157. package/src/defineDisplayReactor.ts +0 -62
  158. package/src/defineReactor.ts +0 -142
  159. package/src/defineReactorShared.ts +0 -268
  160. package/src/hooks/createAuthHooks.ts +0 -371
  161. package/src/hooks/index.ts +0 -103
  162. package/src/hooks/useActorInfiniteQuery.ts +0 -278
  163. package/src/hooks/useActorMethod.ts +0 -710
  164. package/src/hooks/useActorMutation.ts +0 -205
  165. package/src/hooks/useActorQuery.ts +0 -157
  166. package/src/hooks/useActorSuspenseInfiniteQuery.ts +0 -248
  167. package/src/hooks/useActorSuspenseQuery.ts +0 -147
  168. package/src/index.ts +0 -31
  169. package/src/ownedAuthentication.ts +0 -81
  170. package/src/server.ts +0 -23
  171. package/src/testing.ts +0 -18
  172. package/src/types.ts +0 -948
  173. package/src/utils.ts +0 -505
  174. package/src/validation.ts +0 -226
@@ -1,1537 +0,0 @@
1
- import { AnonymousIdentity } from "@icp-sdk/core/agent";
2
- import { isDelegationValid } from "@icp-sdk/core/identity";
3
- import { isDev } from "@ic-reactor/core";
4
- import { Principal } from "@icp-sdk/core/principal";
5
- import { safeGetCanisterEnv } from "@icp-sdk/core/agent/canister-env";
6
- import { probeLocalInternetIdentity, localInternetIdentityUnavailableError, } from "./local-ii-probe.js";
7
- import { IC_INTERNET_IDENTITY_PROVIDER, INTERNET_IDENTITY_PROVIDER_ENV_KEY, LOCAL_INTERNET_IDENTITY_CANISTER_ID, localInternetIdentityProvider, } from "./constants.js";
8
- import { recordAuthentication } from "../ownedAuthentication.js";
9
- import { detectAuthClientFlavor, detectAuthClientInstanceFlavor, toAuthClientConstructorOptions, toAuthClientSignInOptions, } from "./auth-client-compat.js";
10
- /**
11
- * Manages Internet Identity sign-in, session restoration, and authentication
12
- * state for a {@link ClientManager}.
13
- *
14
- * @example
15
- * ```ts
16
- * const authentication = new AuthenticationManager({ clientManager })
17
- * const identity = await authentication.authenticate()
18
- * ```
19
- */
20
- export class AuthenticationManager {
21
- /** The current authentication state. */
22
- get authState() {
23
- return this.authStateValue;
24
- }
25
- constructor({ clientManager, authClient, identityProvider, internetIdentityId, ...clientOptions }) {
26
- Object.defineProperty(this, "authClient", {
27
- enumerable: true,
28
- configurable: true,
29
- writable: true,
30
- value: void 0
31
- });
32
- Object.defineProperty(this, "authPromise", {
33
- enumerable: true,
34
- configurable: true,
35
- writable: true,
36
- value: void 0
37
- });
38
- Object.defineProperty(this, "authClientWasProvided", {
39
- enumerable: true,
40
- configurable: true,
41
- writable: true,
42
- value: false
43
- });
44
- Object.defineProperty(this, "authStateRevision", {
45
- enumerable: true,
46
- configurable: true,
47
- writable: true,
48
- value: 0
49
- });
50
- Object.defineProperty(this, "authStateSubscribers", {
51
- enumerable: true,
52
- configurable: true,
53
- writable: true,
54
- value: []
55
- });
56
- Object.defineProperty(this, "authClientConstructor", {
57
- enumerable: true,
58
- configurable: true,
59
- writable: true,
60
- value: void 0
61
- });
62
- Object.defineProperty(this, "authClientConstructorPromise", {
63
- enumerable: true,
64
- configurable: true,
65
- writable: true,
66
- value: void 0
67
- });
68
- Object.defineProperty(this, "authModuleMissing", {
69
- enumerable: true,
70
- configurable: true,
71
- writable: true,
72
- value: false
73
- });
74
- Object.defineProperty(this, "authClientFlavor", {
75
- enumerable: true,
76
- configurable: true,
77
- writable: true,
78
- value: "legacy"
79
- });
80
- Object.defineProperty(this, "authClientOptions", {
81
- enumerable: true,
82
- configurable: true,
83
- writable: true,
84
- value: void 0
85
- });
86
- Object.defineProperty(this, "authStateValue", {
87
- enumerable: true,
88
- configurable: true,
89
- writable: true,
90
- value: {
91
- identity: null,
92
- isAuthenticating: false,
93
- isAuthenticated: false,
94
- error: undefined,
95
- }
96
- });
97
- /** See {@link sessionChecked}. */
98
- Object.defineProperty(this, "sessionCheckedValue", {
99
- enumerable: true,
100
- configurable: true,
101
- writable: true,
102
- value: false
103
- });
104
- Object.defineProperty(this, "sessionCheckedSubscribers", {
105
- enumerable: true,
106
- configurable: true,
107
- writable: true,
108
- value: []
109
- });
110
- /** Stops following the current client's session record; see `watchClient()`. */
111
- Object.defineProperty(this, "unwatchClient", {
112
- enumerable: true,
113
- configurable: true,
114
- writable: true,
115
- value: void 0
116
- });
117
- /**
118
- * Set when the client's session record changed while an operation of this
119
- * manager's own was running; see `followClient()`.
120
- */
121
- Object.defineProperty(this, "clientChangedDuringOperation", {
122
- enumerable: true,
123
- configurable: true,
124
- writable: true,
125
- value: false
126
- });
127
- /** Counts `followClient()` passes, so that only the latest one publishes. */
128
- Object.defineProperty(this, "followRevision", {
129
- enumerable: true,
130
- configurable: true,
131
- writable: true,
132
- value: 0
133
- });
134
- /** Counts `dispose()` calls; see {@link releaseCount}. */
135
- Object.defineProperty(this, "releases", {
136
- enumerable: true,
137
- configurable: true,
138
- writable: true,
139
- value: 0
140
- });
141
- Object.defineProperty(this, "identityProvider", {
142
- enumerable: true,
143
- configurable: true,
144
- writable: true,
145
- value: void 0
146
- });
147
- /** The provider taken from the `ic_env` cookie, when no caller set one. */
148
- Object.defineProperty(this, "envIdentityProvider", {
149
- enumerable: true,
150
- configurable: true,
151
- writable: true,
152
- value: void 0
153
- });
154
- Object.defineProperty(this, "internetIdentityId", {
155
- enumerable: true,
156
- configurable: true,
157
- writable: true,
158
- value: void 0
159
- });
160
- /** Whether `internetIdentityId` came from the caller rather than the cookie. */
161
- Object.defineProperty(this, "internetIdentityIdIsExplicit", {
162
- enumerable: true,
163
- configurable: true,
164
- writable: true,
165
- value: void 0
166
- });
167
- /**
168
- * Which authorize path the locally deployed Internet Identity serves, once
169
- * probed. `undefined` means not probed yet; `null` means it serves no sign-in
170
- * UI and login should fail with an explanation rather than open a popup onto
171
- * a gateway error page.
172
- */
173
- Object.defineProperty(this, "localAuthorizePath", {
174
- enumerable: true,
175
- configurable: true,
176
- writable: true,
177
- value: void 0
178
- });
179
- Object.defineProperty(this, "defaultClientOptions", {
180
- enumerable: true,
181
- configurable: true,
182
- writable: true,
183
- value: void 0
184
- });
185
- Object.defineProperty(this, "clientManager", {
186
- enumerable: true,
187
- configurable: true,
188
- writable: true,
189
- value: void 0
190
- });
191
- Object.defineProperty(this, "authenticate", {
192
- enumerable: true,
193
- configurable: true,
194
- writable: true,
195
- value: async () => {
196
- const releases = this.releases;
197
- try {
198
- return await this.checkSession();
199
- }
200
- finally {
201
- // Marked once the result is published, so the hooks never show the
202
- // starting state as the answer. A failed restore settles it too. One
203
- // that `dispose()` cut short read nothing, and leaves the check to the
204
- // restore of whatever mounts this manager again.
205
- if (releases === this.releases || this.authClient) {
206
- this.markSessionChecked();
207
- }
208
- }
209
- }
210
- });
211
- Object.defineProperty(this, "login", {
212
- enumerable: true,
213
- configurable: true,
214
- writable: true,
215
- value: async (loginOptions) => {
216
- let didCompleteSignIn = false;
217
- try {
218
- const authClientOptions = this.resolveClientOptions(loginOptions);
219
- // Stays synchronous when `prepareClient()` has already loaded the auth
220
- // module, so `signIn()` still runs inside the caller's click handler.
221
- if (!this.ensurePreparedClient(authClientOptions)) {
222
- await this.initializeClient(authClientOptions);
223
- }
224
- if (!this.authClient) {
225
- await this.authenticate();
226
- }
227
- if (!this.authClient) {
228
- throw new Error("Authentication module is missing or failed to initialize. To use login, install the optional auth peer: npm install @icp-sdk/auth. If it is already installed and your bundler could not resolve it, pass a pre-constructed client instead: new AuthenticationManager({ clientManager, authClient: new AuthClient(...) })");
229
- }
230
- this.updateState({ isAuthenticating: true, error: undefined });
231
- const identity = await this.signInOrRecoverIdentity(getSignInOptions(loginOptions));
232
- if (!this.clientManager.agentState.isInitialized) {
233
- await this.clientManager.initializeAgent();
234
- }
235
- this.clientManager.updateAgent(identity);
236
- this.publishSession({
237
- identity,
238
- isAuthenticated: true,
239
- isAuthenticating: false,
240
- });
241
- didCompleteSignIn = true;
242
- try {
243
- await loginOptions?.onSuccess?.();
244
- }
245
- catch (callbackError) {
246
- this.updateState({ error: callbackError });
247
- await loginOptions?.onError?.(callbackError.message);
248
- throw callbackError;
249
- }
250
- }
251
- catch (error) {
252
- if (!didCompleteSignIn) {
253
- // Recorded before the callback runs, as on success: an onError that
254
- // rejected used to skip this and strand `isAuthenticating: true` with
255
- // no error on record.
256
- this.updateState({
257
- error: error,
258
- isAuthenticating: false,
259
- });
260
- await loginOptions?.onError?.(error.message);
261
- }
262
- throw error;
263
- }
264
- }
265
- });
266
- Object.defineProperty(this, "logout", {
267
- enumerable: true,
268
- configurable: true,
269
- writable: true,
270
- value: async (options) => {
271
- // None built yet, or released by `dispose()`. Signing out needs no user
272
- // gesture, so one can be built here.
273
- const client = this.authClient ?? (await this.ensureClient());
274
- if (!client) {
275
- throw new Error("Authentication module is missing or failed to initialize. To use logout, install the optional auth peer: npm install @icp-sdk/auth. If it is already installed and your bundler could not resolve it, pass a pre-constructed client instead: new AuthenticationManager({ clientManager, authClient: new AuthClient(...) })");
276
- }
277
- this.updateState({ isAuthenticating: true, error: undefined });
278
- try {
279
- // The client the sign-out started on, even once `dispose()` has released
280
- // it, as when the sign-out closes the widget that built this manager.
281
- // Reading `this.authClient` then failed the sign-out with a TypeError.
282
- await client.signOut(options);
283
- const identity = await client.getIdentity();
284
- this.clientManager.updateAgent(identity);
285
- this.publishSession({
286
- identity,
287
- isAuthenticated: false,
288
- isAuthenticating: false,
289
- });
290
- }
291
- catch (error) {
292
- // A failed signOut does not always mean the session survived. v10 wipes
293
- // the device and drops to an anonymous identity before it raises a revoke
294
- // the canister did not answer, so keeping the session here left the app
295
- // signed in and the agent signing as the user who had just signed out.
296
- // Follow the client instead: once it no longer vouches for the session,
297
- // nothing may sign with it, which is the rule `authenticate()` applies
298
- // too. A v8 client that failed before forgetting anything still vouches
299
- // for its session and keeps it, unless its delegation has lapsed (see
300
- // vouchesFor()). A check that throws keeps it as well.
301
- //
302
- // Anything that changes auth state while that check is in flight, such
303
- // as a login that finishes meanwhile, bumps this. The check then
304
- // describes a client that has been used since, and its answer must not
305
- // replace the newer state, as in `authenticate()`.
306
- const revision = this.authStateRevision;
307
- const stillSignedIn = await Promise.resolve()
308
- .then(() => client.isAuthenticated())
309
- .catch(() => true);
310
- if (revision !== this.authStateRevision) {
311
- throw error;
312
- }
313
- if (this.vouchesFor(this.authState.identity, stillSignedIn)) {
314
- // Without this the manager was left with `isAuthenticating: true` and
315
- // no recorded error, so a button disabled on `isAuthenticating` stayed
316
- // stuck and nothing told the app why.
317
- this.publishSession({ error: error, isAuthenticating: false });
318
- }
319
- else {
320
- const identity = new AnonymousIdentity();
321
- this.clientManager.updateAgent(identity);
322
- // The error stays recorded: the device is signed out, but the session
323
- // may still be live at the identity provider.
324
- this.publishSession({
325
- identity,
326
- isAuthenticated: false,
327
- isAuthenticating: false,
328
- error: error,
329
- });
330
- }
331
- throw error;
332
- }
333
- }
334
- });
335
- this.clientManager = clientManager;
336
- // For `createReactorProvider`, which disposes the managers built while
337
- // its factory ran, and leaves alone those built elsewhere.
338
- recordAuthentication(this);
339
- const canisterEnv = typeof window !== "undefined" ? getAuthenticationCanisterEnv() : undefined;
340
- this.envIdentityProvider = identityProvider
341
- ? undefined
342
- : acceptEnvIdentityProvider(canisterEnv?.[INTERNET_IDENTITY_PROVIDER_ENV_KEY] ||
343
- canisterEnv?.["PUBLIC_INTERNET_IDENTITY_PROVIDER"], clientManager);
344
- this.identityProvider = identityProvider || this.envIdentityProvider;
345
- // Same cookie, same decision. This one only ever reaches a local provider
346
- // URL, but `allowEnvConfig: false` has to mean the cookie is not consulted
347
- // rather than mostly not consulted.
348
- this.internetIdentityId =
349
- internetIdentityId ||
350
- (clientManager.trustsEnvConfig
351
- ? acceptEnvCanisterId(canisterEnv?.["internet_identity"] ||
352
- canisterEnv?.["PUBLIC_CANISTER_ID:internet_identity"] ||
353
- canisterEnv?.["CANISTER_ID_INTERNET_IDENTITY"])
354
- : undefined);
355
- this.internetIdentityIdIsExplicit = Boolean(internetIdentityId);
356
- this.defaultClientOptions = clientOptions;
357
- if (authClient) {
358
- this.authClientWasProvided = true;
359
- this.authClient = authClient;
360
- // A caller-built client never goes through the module loader that
361
- // detects the flavor, so read it off the instance. Without this a v10
362
- // client handed in here was treated as v8, and `targets` reached it
363
- // without the warning that it is ignored.
364
- this.authClientFlavor = detectAuthClientInstanceFlavor(authClient);
365
- this.watchClient(authClient);
366
- this.syncStateFromClient(this.authStateRevision).catch((error) => {
367
- this.updateState({ error: error, isAuthenticating: false });
368
- });
369
- }
370
- else if (typeof window !== "undefined") {
371
- this.loadAuthClientConstructor().catch(() => {
372
- // Optional auth support is reported when an auth method is used.
373
- });
374
- }
375
- }
376
- /** @internal Used by IdentityAttributesManager. */
377
- get client() {
378
- return this.authClient;
379
- }
380
- /**
381
- * @internal Used by the auth hooks.
382
- *
383
- * Whether this manager has checked its client for a session: a restore has
384
- * settled, whether it found a session, found none or failed, or a sign-in or
385
- * sign-out has completed. Until then {@link authState} is the signed-out state
386
- * the manager starts in, which says nothing about the session, so the auth
387
- * hooks report `isAuthenticating: true` instead.
388
- */
389
- get sessionChecked() {
390
- return this.sessionCheckedValue;
391
- }
392
- /**
393
- * @internal Used by the auth hooks.
394
- *
395
- * Records that the session has been checked, and tells the auth hooks the
396
- * first time. A restore that failed counts: waiting on one that will not be
397
- * retried would leave the hooks reporting `isAuthenticating: true` for good.
398
- */
399
- markSessionChecked() {
400
- if (this.sessionCheckedValue)
401
- return;
402
- this.sessionCheckedValue = true;
403
- const subscribers = this.sessionCheckedSubscribers;
404
- this.sessionCheckedSubscribers = [];
405
- for (const subscriber of subscribers)
406
- subscriber();
407
- }
408
- /**
409
- * @internal Used by the auth hooks.
410
- *
411
- * How many times {@link dispose} has run. A restore the hooks started
412
- * compares it with the count it began with, to tell whether the manager was
413
- * released while it ran.
414
- */
415
- get releaseCount() {
416
- return this.releases;
417
- }
418
- /**
419
- * @internal Used by the auth hooks.
420
- *
421
- * Calls `callback` once, when {@link sessionChecked} turns true. Nothing else
422
- * announces it when the check that settles it publishes no state, as a
423
- * restore that failed before it read the client does not.
424
- *
425
- * @returns An unsubscribe function.
426
- */
427
- subscribeSessionChecked(callback) {
428
- if (this.sessionCheckedValue)
429
- return () => { };
430
- const subscription = () => callback();
431
- this.sessionCheckedSubscribers.push(subscription);
432
- return () => {
433
- this.sessionCheckedSubscribers = this.sessionCheckedSubscribers.filter((subscriber) => subscriber !== subscription);
434
- };
435
- }
436
- /**
437
- * Releases the `@icp-sdk/auth` client this manager built.
438
- *
439
- * A v10 client hooks the page when it is built: activity listeners on
440
- * `document`, focus and visibility listeners, a watch on the session record
441
- * that every tab shares and, once signed in, a refresh timer. Nothing
442
- * releases them when the manager is dropped, so a manager built per mount,
443
- * as a server-rendered app builds one in its provider, left one live client
444
- * behind on each remount. Call this when you discard the manager, from the
445
- * cleanup of whatever built it.
446
- *
447
- * The client is disposed and forgotten, so a later {@link prepareClient},
448
- * {@link login}, {@link logout} or restore that needs a client builds a new
449
- * one. That keeps it safe in an effect cleanup under React's StrictMode,
450
- * which runs the cleanup and then the effect again on the same manager. With
451
- * `@icp-sdk/auth` v8 there is nothing to release, and the manager only drops
452
- * its reference. A client passed in as `authClient` belongs to the caller: it
453
- * is never disposed, and the manager goes on using it, but stops following its
454
- * session record until its next {@link prepareClient}, {@link login} or
455
- * {@link authenticate}, so that the caller's client does not keep a
456
- * discarded manager alive.
457
- *
458
- * The auth state and the identity on the agent are left as they are. A
459
- * restore reading the client when it is released ends without publishing
460
- * what it read, and a sign-in or sign-out under way ends with what the
461
- * released client reports. A restore `useAuth()` started stops, and
462
- * releases any client it built meanwhile, once no `useAuth()` of this
463
- * manager is mounted; one mounted later restores again.
464
- *
465
- * @example
466
- * ```tsx
467
- * const [authentication] = useState(
468
- * () => new AuthenticationManager({ clientManager })
469
- * )
470
- * useEffect(() => () => authentication.dispose(), [authentication])
471
- * ```
472
- */
473
- dispose() {
474
- this.releases++;
475
- this.stopWatchingClient();
476
- if (this.authClientWasProvided) {
477
- return;
478
- }
479
- const client = this.authClient;
480
- this.authClient = undefined;
481
- this.authClientOptions = undefined;
482
- if (client) {
483
- disposeClient(client);
484
- }
485
- }
486
- /**
487
- * Subscribes to auth state changes.
488
- *
489
- * Callbacks run in the order they subscribed, after the state has changed. A
490
- * callback that throws does not stop the others; the first error is rethrown
491
- * to whatever made the change once they have all run. When a callback itself
492
- * changes the state, the newer state is the last each callback hears.
493
- *
494
- * @param callback - Function called with the new state.
495
- * @returns An unsubscribe function.
496
- */
497
- subscribeAuthState(callback) {
498
- // Each subscription gets an entry of its own, so the unsubscribe it returns
499
- // removes that one registration and no other. Filtering on the callback
500
- // itself removed every registration of a function subscribed twice, as
501
- // `ClientManager.subscribe` did before #513.
502
- const subscription = (state) => callback(state);
503
- this.authStateSubscribers.push(subscription);
504
- return () => {
505
- this.authStateSubscribers = this.authStateSubscribers.filter((subscriber) => subscriber !== subscription);
506
- };
507
- }
508
- /**
509
- * Preloads the auth module and creates an AuthClient ahead of time.
510
- *
511
- * Call (and await) this before wiring up a login button: it makes
512
- * {@link login} and {@link IdentityAttributesManager.request} able to open
513
- * the identity provider synchronously inside the click handler, which is
514
- * what browser popup blockers and the ICRC-29 transport require.
515
- */
516
- async prepareClient(options) {
517
- // Before resolving options, because resolving them is what picks the
518
- // provider URL. This is the last async point before `login()` has to stay
519
- // inside the user gesture, so the answer has to be cached by now.
520
- await this.ensureLocalAuthorizePath();
521
- const clientOptions = this.resolveClientOptions(options);
522
- if (this.authClient && !this.shouldRecreateClient(clientOptions)) {
523
- this.watchClient(this.authClient);
524
- return this.authClient;
525
- }
526
- return this.initializeClient(clientOptions);
527
- }
528
- /**
529
- * Probe the local Internet Identity canister once, and remember what it
530
- * serves.
531
- *
532
- * Only for the derived local provider: an explicitly configured
533
- * `identityProvider` is the caller's business, and mainnet is fixed.
534
- */
535
- async ensureLocalAuthorizePath() {
536
- if (this.localAuthorizePath !== undefined)
537
- return;
538
- if (this.identityProvider)
539
- return;
540
- if (!this.clientManager.isLocal)
541
- return;
542
- const canisterId = this.internetIdentityId ?? LOCAL_INTERNET_IDENTITY_CANISTER_ID;
543
- const { path, inconclusive } = await probeLocalInternetIdentity(this.clientManager.agent, canisterId);
544
- // An inconclusive probe must not change behaviour: the canister may be
545
- // fine and merely unreachable from here, and a diagnostic that blocks a
546
- // working login is worse than the failure it explains.
547
- const authorizePath = inconclusive ? "/authorize" : path;
548
- // With no sign-in UI, login fails with advice that depends on the
549
- // installed @icp-sdk/auth major. A client IC Reactor builds itself shows
550
- // its major only once the module has loaded, and that import is usually
551
- // still in flight when the probe answers. Wait for it before recording the
552
- // finding, so nothing reads `null` while the flavor still holds its
553
- // `legacy` default and hands a v10 app the v8 advice.
554
- if (authorizePath === null && !this.authClientWasProvided) {
555
- await this.loadAuthClientConstructor().catch(() => undefined);
556
- }
557
- this.localAuthorizePath = authorizePath;
558
- if (path === "/#authorize" && !inconclusive) {
559
- console.warn(`[ic-reactor] Internet Identity canister ${canisterId} serves its sign-in UI at ` +
560
- `"/" rather than "/authorize" — using the legacy #authorize flow. This is a ` +
561
- `pre-2026 build; newer ones through release-2026-03-16 serve /authorize directly.`);
562
- }
563
- }
564
- /**
565
- * Returns an AuthClient without awaiting, or `undefined` when the auth
566
- * module has not been loaded yet.
567
- *
568
- * Callers that must stay inside a user gesture use this instead of
569
- * `ensureClient`; awaiting anything before opening the identity
570
- * provider window loses the gesture.
571
- */
572
- getPreparedClient(options) {
573
- return this.ensurePreparedClient(this.resolveClientOptions(options));
574
- }
575
- async checkSession() {
576
- if (this.authClient) {
577
- this.watchClient(this.authClient);
578
- }
579
- if (this.authState.isAuthenticated) {
580
- // Returning on the cached flag alone meant a delegation that expired
581
- // mid-session was never re-observed: the UI kept rendering a signed-in
582
- // state while every update call failed, and only a reload recovered.
583
- // Re-ask the client, which reads the cached expiry rather than hitting
584
- // storage. A throw here is treated as "still valid" so a transient
585
- // failure cannot sign anyone out. A v8 delegation that has expired is
586
- // not valid whatever the client answers: see vouchesFor().
587
- if (!this.authClient) {
588
- return this.authState.identity || undefined;
589
- }
590
- const isAuthenticated = await Promise.resolve(this.authClient.isAuthenticated()).catch(() => true);
591
- if (this.vouchesFor(this.authState.identity, isAuthenticated)) {
592
- return this.authState.identity || undefined;
593
- }
594
- // Expired, or ended in another tab. The v8 client keeps handing out the
595
- // lapsed delegation from getIdentity() until signOut() runs (only a
596
- // fresh page load purges it). End the session explicitly, in this tab
597
- // only: see expireSession().
598
- await this.expireSession(isAuthenticated);
599
- return undefined;
600
- }
601
- if (this.authPromise) {
602
- return this.authPromise;
603
- }
604
- if (this.authModuleMissing) {
605
- return undefined;
606
- }
607
- this.authPromise = (async () => {
608
- if (isDev() && typeof window !== "undefined") {
609
- console.info(`%cic-reactor:%c Authenticating...`, "color: #3b82f6; font-weight: bold", "color: inherit", {
610
- network: this.clientManager.network,
611
- authClient: this.authClient ? "Shared Instance" : "Dynamic Import",
612
- });
613
- }
614
- this.updateState({ isAuthenticating: true });
615
- // Anything that changes auth state — a logout, notably — bumps this. If
616
- // it moves while the awaits below are in flight, the result we are
617
- // holding describes a session that has since ended, and installing it
618
- // would put the signed-out user's delegation back on the agent.
619
- const revision = this.authStateRevision;
620
- try {
621
- let client = this.authClient ??
622
- (await this.initializeClient(this.resolveClientOptions()));
623
- if (!client) {
624
- this.updateState({ isAuthenticating: false });
625
- return undefined;
626
- }
627
- let { clientIdentity, isAuthenticated } = await this.readClientSession(client);
628
- // Per-call options can replace the client while it is read, and
629
- // `dispose()` can release it. Both answers used to be read from
630
- // whichever client was current at the time, so they could come from two
631
- // clients. A v10 client disposed while it restores hands out the
632
- // anonymous identity, and the record its replacement reads still says
633
- // signed in: the manager reported the anonymous principal signed in.
634
- // The client in use now is read instead, and a manager that let go of
635
- // its client publishes nothing. Nothing may be signed with what a
636
- // released client held.
637
- while (revision === this.authStateRevision &&
638
- client !== this.authClient) {
639
- if (!this.authClient) {
640
- this.updateState({ isAuthenticating: false });
641
- return this.authState.identity || undefined;
642
- }
643
- client = this.authClient;
644
- ({ clientIdentity, isAuthenticated } =
645
- await this.readClientSession(client));
646
- }
647
- if (revision !== this.authStateRevision) {
648
- // Superseded — leave whatever ran in the meantime in place.
649
- return this.authState.identity || undefined;
650
- }
651
- // A client that says it is not authenticated but still hands out a
652
- // non-anonymous identity is holding a delegation it will no longer
653
- // vouch for (expired, mid-session). So is a v8 client that says it is,
654
- // once another tab has signed in again, while it hands out the
655
- // delegation that lapsed in this one. Nothing may be signed with it.
656
- const identity = isAuthenticated || clientIdentity.getPrincipal().isAnonymous()
657
- ? clientIdentity
658
- : new AnonymousIdentity();
659
- // Restoring an anonymous session is the common first-load case; pushing
660
- // it through updateAgent would invalidate the whole query cache on
661
- // every mount for nothing. It is only skipped while the agent is
662
- // anonymous too, so a lapsed delegation still gets replaced.
663
- if (isAuthenticated ||
664
- !identity.getPrincipal().isAnonymous() ||
665
- !this.agentIsAnonymous()) {
666
- this.clientManager.updateAgent(identity);
667
- }
668
- this.updateState({
669
- identity,
670
- isAuthenticated,
671
- isAuthenticating: false,
672
- });
673
- return identity;
674
- }
675
- catch (error) {
676
- this.updateState({ error: error, isAuthenticating: false });
677
- console.error("Authentication failed:", error);
678
- throw error;
679
- }
680
- finally {
681
- this.authPromise = undefined;
682
- }
683
- })();
684
- return this.authPromise;
685
- }
686
- async initializeClient(options) {
687
- const AuthClient = await this.loadAuthClientConstructor();
688
- if (!AuthClient) {
689
- return undefined;
690
- }
691
- // Every mounted `useAuth()` prepares the client at the same moment, and
692
- // each call arrives here after the same await. Building one per caller
693
- // left all but the last running with nothing able to reach them: on v8
694
- // each registered the app's `onIdle` on the shared IdleManager again, so it
695
- // fired once per consumer, and on v10 each kept its browser listeners and
696
- // its session's refresh timer. Take the client an earlier caller built for
697
- // the same options instead.
698
- if (this.authClient && !this.shouldRecreateClient(options)) {
699
- return this.authClient;
700
- }
701
- return this.installClient(new AuthClient(this.toClientOptions(options)), options);
702
- }
703
- /**
704
- * Makes `client`, which this manager built for `options`, the current one.
705
- *
706
- * The client it replaces was built here too, for other options: a caller's
707
- * `authClient` is never replaced. It was dropped with nothing released, so a
708
- * v10 client kept its browser listeners, its state subscription and its
709
- * session's refresh timer for the life of the page, one more for each switch
710
- * between option sets, such as a one-click sign-in and a plain one (#729).
711
- * v10 asks for `dispose()` on a client being discarded, and a new
712
- * interaction already takes its signer channel from the old client, so this
713
- * adds no failure of its own. A v8 client has nothing to dispose; see
714
- * `withSharedIdleCallback()` for the callback it leaves registered.
715
- */
716
- installClient(client, options) {
717
- const replaced = this.authClient;
718
- this.stopWatchingClient();
719
- this.authClient = client;
720
- this.authClientOptions = options;
721
- if (replaced && replaced !== client) {
722
- disposeClient(replaced);
723
- }
724
- this.watchClient(client);
725
- return client;
726
- }
727
- /**
728
- * Follows a v10 client's session record, which every tab of the origin
729
- * shares.
730
- *
731
- * The manager learned about the session only through its own calls, while a
732
- * v10 client follows the other tabs. After a sign-out in another tab, this
733
- * tab's client dropped the session and its manager went on reporting the
734
- * user signed in, with the replaced identity on the agent signing calls as
735
- * the account the user had left. After a sign-in there as another account,
736
- * the manager kept the old account while the client held the new one. When
737
- * the old identity's app delegation then lapsed, its mint was refused and
738
- * the client removed the record every tab reads, signing the new account out
739
- * of every tab (#754). `subscribe()` fires after the record changes, here or
740
- * in another tab, and the manager then reads the client again.
741
- *
742
- * v8 has no notification and never revokes a session, so a v8 client is not
743
- * followed.
744
- */
745
- watchClient(client) {
746
- if (this.unwatchClient || this.authClientFlavor !== "session") {
747
- return;
748
- }
749
- const { subscribe } = client;
750
- if (typeof subscribe !== "function") {
751
- return;
752
- }
753
- this.unwatchClient = subscribe.call(client, () => {
754
- // The client tells its listeners before it starts restoring for the new
755
- // record, which it does right after they return. Reading it a microtask
756
- // later waits for that restore. The state is published whether or not a
757
- // subscriber throws, and there is no caller to hand that error to.
758
- void Promise.resolve()
759
- .then(() => this.followClient(client))
760
- .catch(() => undefined);
761
- });
762
- }
763
- stopWatchingClient() {
764
- this.unwatchClient?.();
765
- this.unwatchClient = undefined;
766
- }
767
- /**
768
- * Reads the session from `client` after its record changed, and publishes
769
- * it when it differs from what this manager holds, as `syncStateFromClient()`
770
- * derives it. An error recorded for the session it replaces goes with it.
771
- *
772
- * An operation of the manager's own, which sets `isAuthenticating`, writes
773
- * the record itself and publishes what the client holds when it ends, so a
774
- * change during one is read again once it has. So is a change whose read
775
- * something else published over.
776
- */
777
- async followClient(client) {
778
- if (client !== this.authClient) {
779
- return;
780
- }
781
- if (this.authState.isAuthenticating) {
782
- this.clientChangedDuringOperation = true;
783
- return;
784
- }
785
- const pass = ++this.followRevision;
786
- const revision = this.authStateRevision;
787
- const session = await this.readFollowedSession(client);
788
- if (pass !== this.followRevision || client !== this.authClient) {
789
- return;
790
- }
791
- if (revision !== this.authStateRevision) {
792
- return this.followClient(client);
793
- }
794
- if (!session) {
795
- return;
796
- }
797
- const current = this.authState;
798
- const anonymous = session.identity.getPrincipal().isAnonymous();
799
- const unchanged = session.isAuthenticated === current.isAuthenticated &&
800
- (session.identity === current.identity ||
801
- (!session.isAuthenticated &&
802
- anonymous &&
803
- current.identity?.getPrincipal().isAnonymous() === true));
804
- if (unchanged) {
805
- this.markSessionChecked();
806
- return;
807
- }
808
- // As in `authenticate()`, an agent that is anonymous already is left as
809
- // it is.
810
- if (!anonymous || !this.agentIsAnonymous()) {
811
- this.clientManager.updateAgent(session.identity);
812
- }
813
- this.publishSession({
814
- identity: session.identity,
815
- isAuthenticated: session.isAuthenticated,
816
- isAuthenticating: false,
817
- error: undefined,
818
- });
819
- }
820
- /**
821
- * The identity `client` hands out, and whether it vouches for it (see
822
- * `vouchesFor()`). Both are read from the same client.
823
- */
824
- async readClientSession(client) {
825
- const clientIdentity = await client.getIdentity();
826
- const isAuthenticated = this.vouchesFor(clientIdentity, await client.isAuthenticated());
827
- return { clientIdentity, isAuthenticated };
828
- }
829
- /**
830
- * The session `client` holds, or `undefined` to leave the manager's as it is.
831
- *
832
- * v10 refuses to hand out an identity while the record names a sign-in it
833
- * holds no credential for, as when restoring the account another tab signed
834
- * in as failed. The session this manager holds is kept then only while the
835
- * record still names its account. One for any other account must not stay
836
- * on the agent.
837
- */
838
- async readFollowedSession(client) {
839
- try {
840
- const { clientIdentity, isAuthenticated } = await this.readClientSession(client);
841
- // The rule `authenticate()` applies to an identity the client no longer
842
- // vouches for.
843
- const identity = isAuthenticated || clientIdentity.getPrincipal().isAnonymous()
844
- ? clientIdentity
845
- : new AnonymousIdentity();
846
- return { identity, isAuthenticated };
847
- }
848
- catch {
849
- const { isAuthenticated, identity } = this.authState;
850
- const held = isAuthenticated
851
- ? identity?.getPrincipal().toText()
852
- : undefined;
853
- const named = client
854
- .getPrincipal?.()
855
- ?.toText();
856
- if (held === undefined || held === named) {
857
- return undefined;
858
- }
859
- return { identity: new AnonymousIdentity(), isAuthenticated: false };
860
- }
861
- }
862
- /** @internal Used by IdentityAttributesManager. */
863
- async signInOrRecoverIdentity(options) {
864
- // Held, because `dispose()` can release it while the popup is open. The
865
- // recovery below then failed with a TypeError instead of the client's own
866
- // error.
867
- const client = this.authClient;
868
- if (!client) {
869
- throw new Error("Authentication module is missing or failed to initialize. To use login, install the optional auth peer: npm install @icp-sdk/auth. If it is already installed and your bundler could not resolve it, pass a pre-constructed client instead: new AuthenticationManager({ clientManager, authClient: new AuthClient(...) })");
870
- }
871
- try {
872
- return await client.signIn(toAuthClientSignInOptions(options, this.authClientFlavor));
873
- }
874
- catch (error) {
875
- const identity = await Promise.resolve(client.getIdentity()).catch(() => null);
876
- const isAuthenticated = await Promise.resolve(client.isAuthenticated()).catch(() => false);
877
- if (identity && this.vouchesFor(identity, isAuthenticated)) {
878
- return identity;
879
- }
880
- throw error;
881
- }
882
- }
883
- ensurePreparedClient(options) {
884
- if (this.authClient && !this.shouldRecreateClient(options)) {
885
- this.watchClient(this.authClient);
886
- return this.authClient;
887
- }
888
- const AuthClient = this.authClientConstructor;
889
- if (!AuthClient || this.authClientWasProvided) {
890
- return undefined;
891
- }
892
- return this.installClient(new AuthClient(this.toClientOptions(options)), options);
893
- }
894
- /**
895
- * Hands the installed client the option shape it actually accepts.
896
- *
897
- * `authClientOptions` keeps the untranslated values, and `shouldRecreateClient`
898
- * compares them with the installed major in mind: two calls that differ only
899
- * in a key that major drops count as the same options, so the client is not
900
- * rebuilt for them.
901
- */
902
- toClientOptions(options) {
903
- return toAuthClientConstructorOptions(this.authClientFlavor === "legacy"
904
- ? withSharedIdleCallback(options)
905
- : options, this.authClientFlavor, this.identityProviderPairing(options?.identityProvider), this.sessionAgentOptions());
906
- }
907
- /**
908
- * Options for the agent a v9+ client mints delegations with, off mainnet.
909
- *
910
- * That client makes its own calls to the Internet Identity canister, through
911
- * an agent built from these options alone. Without a root key it checks every
912
- * certificate against mainnet's, which a local replica or testnet cannot
913
- * satisfy, so sign-in would fail at the first mint. Off mainnet it gets the
914
- * replica this app already talks to and fetches that network's root key, the
915
- * same trust the app's own agent needs there. When the app passed its own
916
- * `agentOptions.rootKey`, which its agent keeps, the minting agent gets that
917
- * key instead and verifies against it too. On mainnet nothing is passed, and
918
- * the client keeps its defaults.
919
- */
920
- sessionAgentOptions() {
921
- if (!this.clientManager.isLocal) {
922
- return undefined;
923
- }
924
- const host = this.clientManager.agentHost;
925
- const rootKey = this.clientManager.explicitRootKey;
926
- return {
927
- ...(host ? { host: host.toString() } : {}),
928
- ...(rootKey ? { rootKey } : { shouldFetchRootKey: true }),
929
- };
930
- }
931
- /**
932
- * Which canister a v9+ client should pair with `identityProvider`.
933
- *
934
- * v9+ names a provider by its authorize URL and the canister that mints its
935
- * delegations, and nothing about the canister follows from the URL. The
936
- * mainnet URL goes with mainnet's canister unless the caller named another.
937
- * A canister read from the `ic_env` cookie belongs to a local deployment, so
938
- * it never overrides that. Any other URL takes `internetIdentityId`, or the
939
- * well-known local canister when the URL is one IC Reactor derived for a
940
- * local deployment. A URL the caller set with no canister stays `unknown`.
941
- */
942
- identityProviderPairing(identityProvider) {
943
- if (String(identityProvider) === IC_INTERNET_IDENTITY_PROVIDER) {
944
- return this.internetIdentityIdIsExplicit && this.internetIdentityId
945
- ? { kind: "pair", canisterId: this.internetIdentityId }
946
- : { kind: "mainnet" };
947
- }
948
- if (this.internetIdentityId) {
949
- return { kind: "pair", canisterId: this.internetIdentityId };
950
- }
951
- if (identityProvider !== undefined &&
952
- this.isDerivedLocalProvider(identityProvider)) {
953
- return { kind: "pair", canisterId: LOCAL_INTERNET_IDENTITY_CANISTER_ID };
954
- }
955
- return { kind: "unknown" };
956
- }
957
- /**
958
- * Whether `identityProvider` is a local provider IC Reactor chose, from the
959
- * `ic_env` cookie or built for the local replica, rather than one a caller
960
- * configured.
961
- */
962
- isDerivedLocalProvider(identityProvider) {
963
- if (this.envIdentityProvider !== undefined) {
964
- return String(identityProvider) === String(this.envIdentityProvider);
965
- }
966
- if (this.identityProvider !== undefined ||
967
- !this.clientManager.isLocal ||
968
- this.localAuthorizePath === null) {
969
- return false;
970
- }
971
- return (String(identityProvider) ===
972
- localInternetIdentityProvider(Number(this.clientManager.agentHost?.port) || 4943, this.internetIdentityId, this.localAuthorizePath));
973
- }
974
- /**
975
- * Only rebuild the client when the effective options actually changed.
976
- *
977
- * Recreating on every call would discard the client warmed up by
978
- * `prepareClient()` and register a duplicate sign-out callback on the
979
- * shared IdleManager singleton each time.
980
- */
981
- shouldRecreateClient(options) {
982
- if (this.authClientWasProvided) {
983
- return false;
984
- }
985
- return !isSameAuthClientOptions(this.authClientOptions, options, this.authClientFlavor);
986
- }
987
- /**
988
- * Merges constructor-level defaults with per-call overrides and fills in the
989
- * network-appropriate identity provider.
990
- */
991
- resolveClientOptions(options) {
992
- const merged = getAuthClientOptions({
993
- ...this.defaultClientOptions,
994
- ...options,
995
- });
996
- return {
997
- ...merged,
998
- identityProvider: merged?.identityProvider ?? this.getDefaultIdentityProvider(),
999
- };
1000
- }
1001
- /** Whether the shared agent currently signs as the anonymous principal. */
1002
- agentIsAnonymous() {
1003
- const installed = this.clientManager.identity;
1004
- return installed === undefined || installed.getPrincipal().isAnonymous();
1005
- }
1006
- /**
1007
- * @internal Used by IdentityAttributesManager.
1008
- *
1009
- * Whether the client vouches for `identity`, given what its
1010
- * `isAuthenticated()` answered.
1011
- *
1012
- * A v8 client's answer is not about the identity it holds. It reads the
1013
- * delegation expiry v8 keeps in the `localStorage` every tab shares, while
1014
- * `getIdentity()` returns the identity this client restored or signed in
1015
- * with, and v8 never reads storage again once it has loaded. When the session
1016
- * lapses and the user signs in again in another tab, that tab writes a new
1017
- * expiry, and the answer is yes again for the delegation this tab still
1018
- * holds, which the replica refuses. So a v8 identity whose own delegation
1019
- * has expired is not vouched for, whatever the answer. Nor is the anonymous
1020
- * identity, which a v8 client that loaded signed out, or signed out, goes on
1021
- * handing out once another tab signs in and the answer turns yes. It signs
1022
- * no one in.
1023
- *
1024
- * A v10 client's answer is about the session it holds, and its identity
1025
- * replaces its short-lived delegation as it ages, so the delegation it holds
1026
- * can be past its expiry while the session is live. The answer stands alone.
1027
- */
1028
- vouchesFor(identity, isAuthenticated) {
1029
- return (isAuthenticated &&
1030
- !(this.authClientFlavor === "legacy" &&
1031
- (identity?.getPrincipal().isAnonymous() ||
1032
- hasExpiredDelegation(identity))));
1033
- }
1034
- /**
1035
- * End a session the client no longer vouches for: put the anonymous
1036
- * identity on the agent -- which also sweeps the previous user's
1037
- * caller-scoped cache entries and refetches the rest anonymously -- and
1038
- * publish the signed-out state.
1039
- *
1040
- * A v8 client is also asked to forget the session when its
1041
- * `isAuthenticated()` answered no, since it keeps handing out the lapsed
1042
- * delegation until `signOut()` runs. That answer reads the expiry kept in
1043
- * the `localStorage` every tab shares, so it is no only once the session
1044
- * stored for every tab is over, and v8's `signOut()` takes no lock and
1045
- * revokes nothing. `signOut` failing changes nothing here: the delegation is
1046
- * already unusable, and the agent must not keep it. When the answer was yes,
1047
- * the delegation this tab holds lapsed under a session another tab has
1048
- * signed in to since, and `signOut()` would delete that session from the
1049
- * storage every tab shares. The client is left alone, and `vouchesFor()`
1050
- * keeps the lapsed delegation it goes on handing out off the agent.
1051
- *
1052
- * A v10 client is left alone, as `commitSignedOut()` leaves it. Its
1053
- * `signOut()` ends the sign-in for every tab of the origin: it takes the
1054
- * sign-in lock from a sign-in another tab has in progress, which then fails,
1055
- * revokes whatever session the shared store holds, and removes the record
1056
- * every tab reads. Another tab may have signed in again since this one last
1057
- * looked, and finding a session over is not the user asking to sign out.
1058
- * The client already stopped vouching for the session on its own.
1059
- *
1060
- * @param isAuthenticated - What the client's `isAuthenticated()` answered.
1061
- */
1062
- async expireSession(isAuthenticated) {
1063
- if (this.authClientFlavor !== "session" && !isAuthenticated) {
1064
- try {
1065
- await this.authClient?.signOut();
1066
- }
1067
- catch {
1068
- // Nothing to keep; fall through to anonymous either way.
1069
- }
1070
- }
1071
- const identity = new AnonymousIdentity();
1072
- this.clientManager.updateAgent(identity);
1073
- this.updateState({
1074
- identity,
1075
- isAuthenticated: false,
1076
- isAuthenticating: false,
1077
- error: undefined,
1078
- });
1079
- }
1080
- async syncStateFromClient(revision = this.authStateRevision) {
1081
- if (!this.authClient) {
1082
- return;
1083
- }
1084
- try {
1085
- const clientIdentity = await this.authClient.getIdentity();
1086
- const isAuthenticated = this.vouchesFor(clientIdentity, await this.authClient.isAuthenticated());
1087
- if (revision !== this.authStateRevision) {
1088
- return;
1089
- }
1090
- // The rule `authenticate()` applies. A caller-built client can outlive
1091
- // the manager it was first given to, and once its session has lapsed it
1092
- // still hands out the lapsed identity while no longer vouching for it.
1093
- // Nothing may be signed with that.
1094
- const identity = isAuthenticated || clientIdentity.getPrincipal().isAnonymous()
1095
- ? clientIdentity
1096
- : new AnonymousIdentity();
1097
- this.clientManager.updateAgent(identity);
1098
- this.updateState({
1099
- identity,
1100
- isAuthenticated,
1101
- isAuthenticating: false,
1102
- error: undefined,
1103
- });
1104
- }
1105
- finally {
1106
- this.markSessionChecked();
1107
- }
1108
- }
1109
- /** @internal Used by IdentityAttributesManager. */
1110
- async ensureClient(options) {
1111
- const clientOptions = this.resolveClientOptions(options);
1112
- if (!this.ensurePreparedClient(clientOptions)) {
1113
- await this.initializeClient(clientOptions);
1114
- }
1115
- return this.authClient;
1116
- }
1117
- /** @internal Used by IdentityAttributesManager. */
1118
- async commitIdentity(identity, isAuthenticated) {
1119
- if (!this.clientManager.agentState.isInitialized) {
1120
- await this.clientManager.initializeAgent();
1121
- }
1122
- this.clientManager.updateAgent(identity);
1123
- this.publishSession({ identity, isAuthenticated, isAuthenticating: false });
1124
- }
1125
- /** @internal Used by IdentityAttributesManager. */
1126
- setAuthenticating() {
1127
- this.updateState({ isAuthenticating: true, error: undefined });
1128
- }
1129
- /** @internal Used by IdentityAttributesManager. */
1130
- setAuthenticationError(error) {
1131
- this.updateState({ error, isAuthenticating: false });
1132
- }
1133
- /** @internal Used by IdentityAttributesManager. */
1134
- settleAuthenticating() {
1135
- this.updateState({ isAuthenticating: false });
1136
- }
1137
- /**
1138
- * @internal Used by IdentityAttributesManager.
1139
- *
1140
- * Signs this manager out after the client lost its session under it, as when
1141
- * another tab signed out. The client itself is left alone: a v10 sign-out
1142
- * clears the storage every tab shares and takes the sign-in lock, so it would
1143
- * also end a sign-in the other tab has made or started since.
1144
- */
1145
- commitSignedOut() {
1146
- const identity = new AnonymousIdentity();
1147
- // As in `authenticate()`, an agent that is anonymous already is left as it
1148
- // is: its cache holds no signed-in user's data, and re-installing it would
1149
- // only refetch every query.
1150
- if (!this.agentIsAnonymous()) {
1151
- this.clientManager.updateAgent(identity);
1152
- }
1153
- this.publishSession({
1154
- identity,
1155
- isAuthenticated: false,
1156
- isAuthenticating: false,
1157
- });
1158
- }
1159
- getDefaultIdentityProvider() {
1160
- if (this.identityProvider) {
1161
- return this.identityProvider;
1162
- }
1163
- if (!this.clientManager.isLocal) {
1164
- return IC_INTERNET_IDENTITY_PROVIDER;
1165
- }
1166
- const canisterId = this.internetIdentityId ?? LOCAL_INTERNET_IDENTITY_CANISTER_ID;
1167
- // `null` is the probe's positive finding that this build serves no sign-in
1168
- // UI. Throwing here surfaces an actionable message where the caller can see
1169
- // it, instead of opening a popup onto the gateway's verification-error page
1170
- // and leaving the app waiting until the user closes it.
1171
- if (this.localAuthorizePath === null) {
1172
- throw localInternetIdentityUnavailableError(canisterId, this.authClientFlavor);
1173
- }
1174
- return localInternetIdentityProvider(Number(this.clientManager.agentHost?.port) || 4943, this.internetIdentityId, this.localAuthorizePath);
1175
- }
1176
- /**
1177
- * Publishes what a check of the session found, then marks the session
1178
- * checked. In that order, so the auth hooks never take the state before it
1179
- * for the answer.
1180
- */
1181
- publishSession(newState) {
1182
- try {
1183
- this.updateState(newState);
1184
- }
1185
- finally {
1186
- this.markSessionChecked();
1187
- }
1188
- }
1189
- /**
1190
- * Records a change, then tells every subscriber about it.
1191
- *
1192
- * Every subscriber is called even when one throws, and the first error is
1193
- * rethrown once they all have been, so the caller still sees it. A throw
1194
- * used to end the loop: an app's subscriber registered at module scope comes
1195
- * before every `useAuth()`, and one that failed on a sign-in left them all
1196
- * showing `isAuthenticating: true` while the agent signed as the user.
1197
- *
1198
- * A subscriber that changes the state again from its callback has told every
1199
- * subscriber about that newer state, so the loop stops rather than deliver
1200
- * this older one after it. The list is copied first, so a subscriber added
1201
- * during the loop is first called for the next change. `ClientManager`
1202
- * notifies its subscribers the same way.
1203
- */
1204
- updateState(newState) {
1205
- if (isDev())
1206
- console.debug("[ic-reactor] Updating Auth State:", newState);
1207
- const revision = ++this.authStateRevision;
1208
- const state = { ...this.authStateValue, ...newState };
1209
- this.authStateValue = state;
1210
- let failure;
1211
- for (const subscriber of [...this.authStateSubscribers]) {
1212
- if (revision !== this.authStateRevision)
1213
- break;
1214
- try {
1215
- subscriber(state);
1216
- }
1217
- catch (error) {
1218
- failure ?? (failure = { error });
1219
- }
1220
- }
1221
- // The operation that held off `followClient()` is over: read the client
1222
- // again, once its caller has moved on.
1223
- const client = this.authClient;
1224
- if (this.clientChangedDuringOperation &&
1225
- !this.authStateValue.isAuthenticating &&
1226
- client) {
1227
- this.clientChangedDuringOperation = false;
1228
- void Promise.resolve()
1229
- .then(() => this.followClient(client))
1230
- .catch(() => undefined);
1231
- }
1232
- if (failure)
1233
- throw failure.error;
1234
- }
1235
- async loadAuthClientConstructor() {
1236
- if (this.authClientConstructor) {
1237
- return this.authClientConstructor;
1238
- }
1239
- if (!this.authClientConstructorPromise) {
1240
- this.authClientConstructorPromise = importAuthClientModule()
1241
- .then((authModule) => {
1242
- const AuthClient = authModule.AuthClient;
1243
- if (!AuthClient) {
1244
- throw new Error("@icp-sdk/auth/client did not export AuthClient");
1245
- }
1246
- this.authClientConstructor = AuthClient;
1247
- this.authClientFlavor = detectAuthClientFlavor(AuthClient);
1248
- return AuthClient;
1249
- })
1250
- .catch((error) => {
1251
- this.authModuleMissing = true;
1252
- this.authClientConstructorPromise = undefined;
1253
- if (error instanceof Error &&
1254
- error.message.includes("did not export AuthClient")) {
1255
- throw error;
1256
- }
1257
- return undefined;
1258
- });
1259
- }
1260
- return this.authClientConstructorPromise;
1261
- }
1262
- }
1263
- /**
1264
- * Loads the optional `@icp-sdk/auth` peer.
1265
- *
1266
- * The `try` is load-bearing, not defensive. webpack flags a dynamic import
1267
- * that sits lexically inside a `try` block as an *optional* dependency
1268
- * (`ImportParserPlugin`: `dep.optional = Boolean(parser.scope.inTry)`), and
1269
- * `Compilation` reports an unresolvable optional dependency as a build
1270
- * *warning* instead of a fatal "Module not found" error, emitting a module
1271
- * that rejects at runtime. Without the `try`, every webpack-based toolchain
1272
- * (webpack, `next build --webpack`, Rspack) fails to build for consumers who
1273
- * never touch authentication, because npm/pnpm do not install optional peers.
1274
- * Do not hoist this import out of the `try`, and do not wrap it in a nested
1275
- * function — webpack resets `inTry` at every function boundary.
1276
- *
1277
- * The specifier must also stay a literal: a variable specifier (or
1278
- * `@vite-ignore`) makes bundlers skip this import entirely, so the bare
1279
- * specifier survives into the browser, where it cannot be resolved without an
1280
- * import map and every login path fails. A literal lets Vite/Rollup/webpack
1281
- * code-split the optional peer, and lets them tree-shake it away for apps that
1282
- * never reference this class.
1283
- */
1284
- function importAuthClientModule() {
1285
- try {
1286
- return import("@icp-sdk/auth/client");
1287
- }
1288
- catch (error) {
1289
- // Native ESM without an import map (and bundlers that neither resolve nor
1290
- // stub the specifier) throw synchronously rather than rejecting.
1291
- return Promise.reject(error);
1292
- }
1293
- }
1294
- /**
1295
- * Releases a client IC Reactor built and no longer uses. `dispose()` exists
1296
- * from `@icp-sdk/auth` v9; a v8 client has nothing to release. A throw is
1297
- * ignored: the client is being discarded either way.
1298
- */
1299
- function disposeClient(client) {
1300
- try {
1301
- ;
1302
- client.dispose?.();
1303
- }
1304
- catch {
1305
- // Nothing more can be done for a client that failed to let go.
1306
- }
1307
- }
1308
- /** The wrapper each app `onIdle` gets; see {@link withSharedIdleCallback}. */
1309
- const sharedIdleCallbacks = new WeakMap();
1310
- /**
1311
- * Hands every v8 client the same wrapper around the app's `idleOptions.onIdle`.
1312
- *
1313
- * v8's `IdleManager` is one per page. Each client registers its `onIdle` on it
1314
- * once it signs in or restores a session, and a callback cannot be removed, so
1315
- * a manager that rebuilt its client for per-call options ran the app's `onIdle`
1316
- * once per client it had built on every idle period: three times after a
1317
- * sign-in, a one-click sign-in and another sign-in (#729). The `IdleManager`
1318
- * runs its callbacks in one synchronous loop, so the wrapper runs `onIdle` on
1319
- * the first call of a loop and skips the calls after it. The same function
1320
- * gets the same wrapper however many managers pass it.
1321
- */
1322
- function withSharedIdleCallback(options) {
1323
- const onIdle = options?.idleOptions?.onIdle;
1324
- if (!onIdle) {
1325
- return options;
1326
- }
1327
- let shared = sharedIdleCallbacks.get(onIdle);
1328
- if (!shared) {
1329
- let running = false;
1330
- shared = () => {
1331
- if (running)
1332
- return undefined;
1333
- running = true;
1334
- // Cleared once the loop that called it is over, so the next idle period
1335
- // runs `onIdle` again.
1336
- queueMicrotask(() => {
1337
- running = false;
1338
- });
1339
- return onIdle();
1340
- };
1341
- sharedIdleCallbacks.set(onIdle, shared);
1342
- }
1343
- return { ...options, idleOptions: { ...options.idleOptions, onIdle: shared } };
1344
- }
1345
- /**
1346
- * Whether `identity` signs with a delegation chain that has expired, as a
1347
- * `DelegationIdentity` or `PartialDelegationIdentity` does once its session
1348
- * lapses. Read by shape rather than `instanceof`, which a second copy of
1349
- * `@icp-sdk/core` in the app would defeat.
1350
- */
1351
- function hasExpiredDelegation(identity) {
1352
- const delegated = identity;
1353
- if (typeof delegated?.getDelegation !== "function") {
1354
- return false;
1355
- }
1356
- return !isDelegationValid(delegated.getDelegation());
1357
- }
1358
- function getAuthClientOptions(options) {
1359
- if (!options) {
1360
- return undefined;
1361
- }
1362
- return {
1363
- identityProvider: options.identityProvider,
1364
- windowOpenerFeatures: options.windowOpenerFeatures,
1365
- openIdProvider: getAuthClientOpenIdProvider(options.openIdProvider),
1366
- derivationOrigin: options.derivationOrigin,
1367
- storage: options.storage,
1368
- keyType: options.keyType,
1369
- idleOptions: options.idleOptions,
1370
- identity: options.identity,
1371
- transport: options.transport,
1372
- disableBrowserActivity: options.disableBrowserActivity,
1373
- };
1374
- }
1375
- function getAuthClientOpenIdProvider(openIdProvider) {
1376
- return openIdProvider === "google" ||
1377
- openIdProvider === "apple" ||
1378
- openIdProvider === "microsoft"
1379
- ? openIdProvider
1380
- : undefined;
1381
- }
1382
- /**
1383
- * Compares the options two AuthClients would be built from. Object-valued
1384
- * options (`storage`, `identity`, `idleOptions`) are compared by reference,
1385
- * which is what module-scoped configuration produces.
1386
- */
1387
- /**
1388
- * Whether two option sets build the same client on the installed major.
1389
- *
1390
- * A key that major drops cannot change the client it builds, so a difference
1391
- * there must not cause a rebuild, which would throw away the prepared client:
1392
- * the v8-only `storage`, `keyType`, `idleOptions` and `identity` on v10, and the
1393
- * v10-only `disableBrowserActivity` on v8.
1394
- */
1395
- function isSameAuthClientOptions(current, next, flavor) {
1396
- if (current === next) {
1397
- return true;
1398
- }
1399
- if (!current || !next) {
1400
- return false;
1401
- }
1402
- const sameShared = String(current.identityProvider ?? "") ===
1403
- String(next.identityProvider ?? "") &&
1404
- current.windowOpenerFeatures === next.windowOpenerFeatures &&
1405
- current.openIdProvider === next.openIdProvider &&
1406
- String(current.derivationOrigin ?? "") ===
1407
- String(next.derivationOrigin ?? "") &&
1408
- current.transport === next.transport;
1409
- const sameForFlavor = flavor === "session"
1410
- ? current.disableBrowserActivity === next.disableBrowserActivity
1411
- : current.storage === next.storage &&
1412
- current.keyType === next.keyType &&
1413
- current.idleOptions === next.idleOptions &&
1414
- current.identity === next.identity;
1415
- return sameShared && sameForFlavor;
1416
- }
1417
- /**
1418
- * Decides whether an Internet Identity provider carried by the `ic_env` cookie
1419
- * may be used.
1420
- *
1421
- * The cookie is written by the replica serving a local or testnet deployment,
1422
- * and it is only authoritative in that setting: cookies are not origin-isolated
1423
- * the way script-accessible storage is, so on a mainnet deployment the identity
1424
- * provider is configuration that belongs in code rather than something read
1425
- * back out of the environment at runtime. `AuthClient` treats `identityProvider`
1426
- * as trusted developer configuration and does not re-check it.
1427
- *
1428
- * This mirrors the root-key guard in `ClientManager` (see `packages/core`):
1429
- * both values arrive from the same cookie and get the same treatment — adopted
1430
- * off-mainnet, ignored on mainnet in favour of the pinned default.
1431
- *
1432
- * A provider on the app's own origin is always accepted: it is exactly as
1433
- * trustworthy as the page doing the asking. Callers that need a custom provider
1434
- * on mainnet pass `identityProvider` explicitly.
1435
- */
1436
- function acceptEnvIdentityProvider(value, clientManager) {
1437
- if (!value)
1438
- return undefined;
1439
- const pageOrigin = typeof window !== "undefined" ? window.location?.origin : undefined;
1440
- let url;
1441
- try {
1442
- url = new URL(value, pageOrigin);
1443
- }
1444
- catch {
1445
- return undefined;
1446
- }
1447
- if (pageOrigin && url.origin === pageOrigin) {
1448
- return url.toString();
1449
- }
1450
- // Positive allowlist, not `!isMainnetHost(...)`.
1451
- //
1452
- // The old comment here claimed this defaulted to true for an unknown host so
1453
- // that "anything that looks like mainnet fails closed". It did not:
1454
- // isMainnetHost defaults to true only for `undefined`, and returns false for
1455
- // every unrecognised host — so a dapp on a custom domain accepted an identity
1456
- // provider chosen by a cookie, and the ic_env cookie is writable by any
1457
- // sibling subdomain. Redirecting the Internet Identity flow is as severe as
1458
- // substituting the root key, so both now use the same test.
1459
- //
1460
- // That test is the ClientManager's single resolved decision rather than a
1461
- // recomputed host test, which silently ignored a caller
1462
- // who had opted in: `allowEnvConfig: true` accepted the cookie's root key and
1463
- // then discarded its identity provider, from the same cookie, on the same
1464
- // host.
1465
- if (!clientManager.trustsEnvConfig) {
1466
- console.warn(`[ic-reactor] Ignoring the Internet Identity provider from the ic_env cookie ` +
1467
- `("${url.origin}") because this reactor does not target a local replica, where ` +
1468
- `the provider is taken from configuration rather than the environment. Pass ` +
1469
- `\`identityProvider\` to AuthenticationManager to use a custom provider.`);
1470
- return undefined;
1471
- }
1472
- return url.toString();
1473
- }
1474
- /**
1475
- * Accepts an Internet Identity canister ID from `ic_env` only when it parses as
1476
- * a principal.
1477
- *
1478
- * The value is interpolated into a provider URL
1479
- * (`http://<id>.localhost:<port>/authorize`), so anything that is not a bare
1480
- * principal can change the shape of that URL rather than just its subdomain.
1481
- * Only reachable on local networks, since mainnet uses the pinned provider.
1482
- */
1483
- function acceptEnvCanisterId(value) {
1484
- if (!value)
1485
- return undefined;
1486
- try {
1487
- return Principal.fromText(value).toText();
1488
- }
1489
- catch {
1490
- return undefined;
1491
- }
1492
- }
1493
- function getAuthenticationCanisterEnv() {
1494
- const canisterEnv = safeGetCanisterEnv();
1495
- if (canisterEnv) {
1496
- return canisterEnv;
1497
- }
1498
- if (typeof document === "undefined") {
1499
- return undefined;
1500
- }
1501
- const cookie = document.cookie
1502
- .split(";")
1503
- .find((part) => part.trim().startsWith("ic_env="));
1504
- const encodedValue = cookie?.split("=").slice(1).join("=")?.trim();
1505
- if (!encodedValue) {
1506
- return undefined;
1507
- }
1508
- // Anything able to set a cookie here can write this one, a sibling subdomain
1509
- // or, on localhost, an app on another port. A value that is not valid
1510
- // percent-encoding is ignored, as `safeGetCanisterEnv` ignores it: throwing
1511
- // would fail the constructor, and every `useAuth()` render with it.
1512
- let decodedValue;
1513
- try {
1514
- decodedValue = decodeURIComponent(encodedValue);
1515
- }
1516
- catch {
1517
- return undefined;
1518
- }
1519
- const env = Object.fromEntries(decodedValue.split("&").map((entry) => {
1520
- const separatorIndex = entry.indexOf("=");
1521
- return separatorIndex === -1
1522
- ? [entry, ""]
1523
- : [entry.slice(0, separatorIndex), entry.slice(separatorIndex + 1)];
1524
- }));
1525
- return Object.keys(env).length ? env : undefined;
1526
- }
1527
- function getSignInOptions(options) {
1528
- if (!options) {
1529
- return undefined;
1530
- }
1531
- return {
1532
- maxTimeToLive: options.maxTimeToLive,
1533
- maxTimeToIdle: options.maxTimeToIdle,
1534
- targets: options.targets,
1535
- };
1536
- }
1537
- //# sourceMappingURL=authentication-manager.js.map