@flipstream/sdk 0.13.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (278) hide show
  1. package/CHANGELOG.md +120 -0
  2. package/README.md +143 -21
  3. package/dist/api/backend.d.ts +7 -0
  4. package/dist/api/backend.js +7 -0
  5. package/dist/api/backend.js.map +1 -1
  6. package/dist/api/credentials.js +4 -4
  7. package/dist/api/credentials.js.map +1 -1
  8. package/dist/api/datasets.js +30 -50
  9. package/dist/api/datasets.js.map +1 -1
  10. package/dist/api/handoff.d.ts +1 -1
  11. package/dist/api/handoff.js +7 -0
  12. package/dist/api/handoff.js.map +1 -1
  13. package/dist/api/log.d.ts +4 -4
  14. package/dist/api/log.js +23 -42
  15. package/dist/api/log.js.map +1 -1
  16. package/dist/api/me.d.ts +5 -3
  17. package/dist/api/me.js +3 -3
  18. package/dist/api/me.js.map +1 -1
  19. package/dist/api/members.d.ts +40 -17
  20. package/dist/api/members.js +66 -29
  21. package/dist/api/members.js.map +1 -1
  22. package/dist/api/ownRecord.d.ts +46 -0
  23. package/dist/api/ownRecord.js +57 -0
  24. package/dist/api/ownRecord.js.map +1 -0
  25. package/dist/api/refusal.d.ts +14 -0
  26. package/dist/api/refusal.js +23 -0
  27. package/dist/api/refusal.js.map +1 -0
  28. package/dist/api/wallet.d.ts +2 -1
  29. package/dist/api/wallet.js +18 -24
  30. package/dist/api/wallet.js.map +1 -1
  31. package/dist/api/workspaces.d.ts +7 -2
  32. package/dist/api/workspaces.js +14 -8
  33. package/dist/api/workspaces.js.map +1 -1
  34. package/dist/auth/discovery.js +4 -5
  35. package/dist/auth/discovery.js.map +1 -1
  36. package/dist/auth/transport.d.ts +6 -2
  37. package/dist/auth/transport.js +5 -3
  38. package/dist/auth/transport.js.map +1 -1
  39. package/dist/core/call.d.ts +9 -4
  40. package/dist/core/call.js +12 -8
  41. package/dist/core/call.js.map +1 -1
  42. package/dist/core/fetch.d.ts +20 -1
  43. package/dist/core/fetch.js +50 -3
  44. package/dist/core/fetch.js.map +1 -1
  45. package/dist/core/hosts.d.ts +11 -0
  46. package/dist/core/hosts.js +13 -0
  47. package/dist/core/hosts.js.map +1 -0
  48. package/dist/core/identify.d.ts +12 -2
  49. package/dist/core/identify.js +31 -19
  50. package/dist/core/identify.js.map +1 -1
  51. package/dist/core/inPlaceOf.d.ts +6 -0
  52. package/dist/core/inPlaceOf.js +17 -0
  53. package/dist/core/inPlaceOf.js.map +1 -0
  54. package/dist/core/invalid.d.ts +32 -9
  55. package/dist/core/invalid.js +36 -23
  56. package/dist/core/invalid.js.map +1 -1
  57. package/dist/core/notFound.d.ts +2 -2
  58. package/dist/core/notFound.js.map +1 -1
  59. package/dist/core/read.d.ts +4 -10
  60. package/dist/core/read.js +1 -11
  61. package/dist/core/read.js.map +1 -1
  62. package/dist/core/result.d.ts +14 -10
  63. package/dist/core/result.js +1 -0
  64. package/dist/core/result.js.map +1 -1
  65. package/dist/core/shared.d.ts +15 -9
  66. package/dist/core/shared.js +8 -6
  67. package/dist/core/shared.js.map +1 -1
  68. package/dist/core/signal.d.ts +7 -0
  69. package/dist/core/signal.js +9 -0
  70. package/dist/core/signal.js.map +1 -1
  71. package/dist/first-party/asPerson.d.ts +13 -0
  72. package/dist/first-party/asPerson.js +144 -0
  73. package/dist/first-party/asPerson.js.map +1 -0
  74. package/dist/first-party/attempts.d.ts +71 -0
  75. package/dist/first-party/attempts.js +132 -0
  76. package/dist/first-party/attempts.js.map +1 -0
  77. package/dist/first-party/authProxyMark.d.ts +7 -0
  78. package/dist/first-party/authProxyMark.js +8 -0
  79. package/dist/first-party/authProxyMark.js.map +1 -0
  80. package/dist/first-party/browser.d.ts +55 -0
  81. package/dist/first-party/browser.js +137 -0
  82. package/dist/first-party/browser.js.map +1 -0
  83. package/dist/first-party/browserSession.d.ts +95 -0
  84. package/dist/first-party/browserSession.js +242 -0
  85. package/dist/first-party/browserSession.js.map +1 -0
  86. package/dist/first-party/calls/consent.d.ts +77 -0
  87. package/dist/first-party/calls/consent.js +174 -0
  88. package/dist/first-party/calls/consent.js.map +1 -0
  89. package/dist/first-party/calls/device.d.ts +75 -0
  90. package/dist/first-party/calls/device.js +133 -0
  91. package/dist/first-party/calls/device.js.map +1 -0
  92. package/dist/first-party/calls/invitation.d.ts +198 -0
  93. package/dist/first-party/calls/invitation.js +240 -0
  94. package/dist/first-party/calls/invitation.js.map +1 -0
  95. package/dist/first-party/calls/logIn.d.ts +41 -0
  96. package/dist/first-party/calls/logIn.js +48 -0
  97. package/dist/first-party/calls/logIn.js.map +1 -0
  98. package/dist/first-party/calls/reset.d.ts +58 -0
  99. package/dist/first-party/calls/reset.js +163 -0
  100. package/dist/first-party/calls/reset.js.map +1 -0
  101. package/dist/first-party/calls/signUp.d.ts +21 -0
  102. package/dist/first-party/calls/signUp.js +72 -0
  103. package/dist/first-party/calls/signUp.js.map +1 -0
  104. package/dist/first-party/calls/twoStepSetup.d.ts +55 -0
  105. package/dist/first-party/calls/twoStepSetup.js +168 -0
  106. package/dist/first-party/calls/twoStepSetup.js.map +1 -0
  107. package/dist/first-party/claims.d.ts +88 -0
  108. package/dist/first-party/claims.js +181 -0
  109. package/dist/first-party/claims.js.map +1 -0
  110. package/dist/first-party/enrollment.d.ts +10 -0
  111. package/dist/first-party/enrollment.js +44 -0
  112. package/dist/first-party/enrollment.js.map +1 -0
  113. package/dist/first-party/entry.d.ts +549 -0
  114. package/dist/first-party/entry.js +580 -0
  115. package/dist/first-party/entry.js.map +1 -0
  116. package/dist/first-party/hooks.d.ts +66 -0
  117. package/dist/first-party/hooks.js +19 -0
  118. package/dist/first-party/hooks.js.map +1 -0
  119. package/dist/first-party/http.d.ts +191 -0
  120. package/dist/first-party/http.js +364 -0
  121. package/dist/first-party/http.js.map +1 -0
  122. package/dist/first-party/index.d.ts +14 -0
  123. package/dist/first-party/index.js +3 -0
  124. package/dist/first-party/index.js.map +1 -0
  125. package/dist/first-party/landed.d.ts +32 -0
  126. package/dist/first-party/landed.js +50 -0
  127. package/dist/first-party/landed.js.map +1 -0
  128. package/dist/first-party/landedProof.d.ts +12 -0
  129. package/dist/first-party/landedProof.js +20 -0
  130. package/dist/first-party/landedProof.js.map +1 -0
  131. package/dist/first-party/linkToken.d.ts +16 -0
  132. package/dist/first-party/linkToken.js +29 -0
  133. package/dist/first-party/linkToken.js.map +1 -0
  134. package/dist/first-party/links.d.ts +100 -0
  135. package/dist/first-party/links.js +84 -0
  136. package/dist/first-party/links.js.map +1 -0
  137. package/dist/first-party/locks.d.ts +36 -0
  138. package/dist/first-party/locks.js +64 -0
  139. package/dist/first-party/locks.js.map +1 -0
  140. package/dist/first-party/personTransport.d.ts +38 -0
  141. package/dist/first-party/personTransport.js +177 -0
  142. package/dist/first-party/personTransport.js.map +1 -0
  143. package/dist/first-party/proxy/app.d.ts +25 -0
  144. package/dist/first-party/proxy/app.js +94 -0
  145. package/dist/first-party/proxy/app.js.map +1 -0
  146. package/dist/first-party/proxy/bound.d.ts +5 -0
  147. package/dist/first-party/proxy/bound.js +13 -0
  148. package/dist/first-party/proxy/bound.js.map +1 -0
  149. package/dist/first-party/proxy/cases.d.ts +54 -0
  150. package/dist/first-party/proxy/cases.js +412 -0
  151. package/dist/first-party/proxy/cases.js.map +1 -0
  152. package/dist/first-party/proxy/decide.d.ts +29 -0
  153. package/dist/first-party/proxy/decide.js +140 -0
  154. package/dist/first-party/proxy/decide.js.map +1 -0
  155. package/dist/first-party/proxy/freeze.d.ts +2 -0
  156. package/dist/first-party/proxy/freeze.js +10 -0
  157. package/dist/first-party/proxy/freeze.js.map +1 -0
  158. package/dist/first-party/proxy/handler.d.ts +23 -0
  159. package/dist/first-party/proxy/handler.js +190 -0
  160. package/dist/first-party/proxy/handler.js.map +1 -0
  161. package/dist/first-party/proxy/headers.d.ts +8 -0
  162. package/dist/first-party/proxy/headers.js +20 -0
  163. package/dist/first-party/proxy/headers.js.map +1 -0
  164. package/dist/first-party/proxy/hop.d.ts +9 -0
  165. package/dist/first-party/proxy/hop.js +37 -0
  166. package/dist/first-party/proxy/hop.js.map +1 -0
  167. package/dist/first-party/proxy/host.d.ts +5 -0
  168. package/dist/first-party/proxy/host.js +11 -0
  169. package/dist/first-party/proxy/host.js.map +1 -0
  170. package/dist/first-party/proxy/index.d.ts +7 -0
  171. package/dist/first-party/proxy/index.js +6 -0
  172. package/dist/first-party/proxy/index.js.map +1 -0
  173. package/dist/first-party/proxy/pass-back.d.ts +21 -0
  174. package/dist/first-party/proxy/pass-back.js +51 -0
  175. package/dist/first-party/proxy/pass-back.js.map +1 -0
  176. package/dist/first-party/proxy/paths.d.ts +17 -0
  177. package/dist/first-party/proxy/paths.js +36 -0
  178. package/dist/first-party/proxy/paths.js.map +1 -0
  179. package/dist/first-party/proxy/shown.d.ts +5 -0
  180. package/dist/first-party/proxy/shown.js +16 -0
  181. package/dist/first-party/proxy/shown.js.map +1 -0
  182. package/dist/first-party/proxy/stand-in.d.ts +47 -0
  183. package/dist/first-party/proxy/stand-in.js +318 -0
  184. package/dist/first-party/proxy/stand-in.js.map +1 -0
  185. package/dist/first-party/report.d.ts +47 -0
  186. package/dist/first-party/report.js +38 -0
  187. package/dist/first-party/report.js.map +1 -0
  188. package/dist/first-party/result.d.ts +82 -0
  189. package/dist/first-party/result.js +40 -0
  190. package/dist/first-party/result.js.map +1 -0
  191. package/dist/first-party/session/cell.d.ts +177 -0
  192. package/dist/first-party/session/cell.js +89 -0
  193. package/dist/first-party/session/cell.js.map +1 -0
  194. package/dist/first-party/session/closed.d.ts +8 -0
  195. package/dist/first-party/session/closed.js +12 -0
  196. package/dist/first-party/session/closed.js.map +1 -0
  197. package/dist/first-party/session/index.d.ts +3 -0
  198. package/dist/first-party/session/index.js +3 -0
  199. package/dist/first-party/session/index.js.map +1 -0
  200. package/dist/first-party/session/logout.d.ts +82 -0
  201. package/dist/first-party/session/logout.js +328 -0
  202. package/dist/first-party/session/logout.js.map +1 -0
  203. package/dist/first-party/session/owed.d.ts +30 -0
  204. package/dist/first-party/session/owed.js +27 -0
  205. package/dist/first-party/session/owed.js.map +1 -0
  206. package/dist/first-party/session/refresh.d.ts +19 -0
  207. package/dist/first-party/session/refresh.js +229 -0
  208. package/dist/first-party/session/refresh.js.map +1 -0
  209. package/dist/first-party/session/results.d.ts +38 -0
  210. package/dist/first-party/session/results.js +74 -0
  211. package/dist/first-party/session/results.js.map +1 -0
  212. package/dist/first-party/session/session.d.ts +2 -0
  213. package/dist/first-party/session/session.js +379 -0
  214. package/dist/first-party/session/session.js.map +1 -0
  215. package/dist/first-party/session/shared.d.ts +34 -0
  216. package/dist/first-party/session/shared.js +104 -0
  217. package/dist/first-party/session/shared.js.map +1 -0
  218. package/dist/first-party/session/sharedSignal.d.ts +27 -0
  219. package/dist/first-party/session/sharedSignal.js +55 -0
  220. package/dist/first-party/session/sharedSignal.js.map +1 -0
  221. package/dist/first-party/session/signIn.d.ts +43 -0
  222. package/dist/first-party/session/signIn.js +294 -0
  223. package/dist/first-party/session/signIn.js.map +1 -0
  224. package/dist/first-party/session/signInThrough.d.ts +5 -0
  225. package/dist/first-party/session/signInThrough.js +95 -0
  226. package/dist/first-party/session/signInThrough.js.map +1 -0
  227. package/dist/first-party/session/sync.d.ts +17 -0
  228. package/dist/first-party/session/sync.js +143 -0
  229. package/dist/first-party/session/sync.js.map +1 -0
  230. package/dist/first-party/session/tokens.d.ts +18 -0
  231. package/dist/first-party/session/tokens.js +64 -0
  232. package/dist/first-party/session/tokens.js.map +1 -0
  233. package/dist/first-party/session/types.d.ts +270 -0
  234. package/dist/first-party/session/types.js +3 -0
  235. package/dist/first-party/session/types.js.map +1 -0
  236. package/dist/first-party/sessionWire.d.ts +31 -0
  237. package/dist/first-party/sessionWire.js +91 -0
  238. package/dist/first-party/sessionWire.js.map +1 -0
  239. package/dist/first-party/signInHooks.d.ts +57 -0
  240. package/dist/first-party/signInHooks.js +2 -0
  241. package/dist/first-party/signInHooks.js.map +1 -0
  242. package/dist/first-party/state.d.ts +51 -0
  243. package/dist/first-party/state.js +52 -0
  244. package/dist/first-party/state.js.map +1 -0
  245. package/dist/first-party/strip.d.ts +1 -0
  246. package/dist/first-party/strip.js +8 -0
  247. package/dist/first-party/strip.js.map +1 -0
  248. package/dist/first-party/twoStepLock.d.ts +26 -0
  249. package/dist/first-party/twoStepLock.js +58 -0
  250. package/dist/first-party/twoStepLock.js.map +1 -0
  251. package/dist/first-party/twoStepSession.d.ts +21 -0
  252. package/dist/first-party/twoStepSession.js +31 -0
  253. package/dist/first-party/twoStepSession.js.map +1 -0
  254. package/dist/first-party/wire.d.ts +63 -0
  255. package/dist/first-party/wire.js +5 -0
  256. package/dist/first-party/wire.js.map +1 -0
  257. package/dist/flipstream.d.ts +132 -36
  258. package/dist/flipstream.js +94 -47
  259. package/dist/flipstream.js.map +1 -1
  260. package/dist/index.d.ts +2 -2
  261. package/dist/index.js +3 -2
  262. package/dist/index.js.map +1 -1
  263. package/dist/node/keychainChild.js +60 -25
  264. package/dist/node/keychainChild.js.map +1 -1
  265. package/dist/node/keychainError.d.ts +4 -1
  266. package/dist/node/keychainError.js.map +1 -1
  267. package/dist/node/keychainStore.js +42 -8
  268. package/dist/node/keychainStore.js.map +1 -1
  269. package/dist/node/session.js +9 -8
  270. package/dist/node/session.js.map +1 -1
  271. package/dist/query/query.d.ts +52 -8
  272. package/dist/query/query.js +133 -19
  273. package/dist/query/query.js.map +1 -1
  274. package/dist/version.gen.js +1 -1
  275. package/docs/admin.md +53 -4
  276. package/docs/first-party.md +278 -0
  277. package/docs/node-sign-in.md +8 -4
  278. package/package.json +34 -5
@@ -0,0 +1,43 @@
1
+ import type { SignInHooks } from '../signInHooks.js';
2
+ import type { SignInAnswer } from '../http.js';
3
+ import { type PendingTwoStep, type SessionCell } from './cell.js';
4
+ import type { Logout } from './logout.js';
5
+ import type { Settled } from './results.js';
6
+ import type { SharedState } from './shared.js';
7
+ import type { SharedSignal } from './sharedSignal.js';
8
+ import type { Tokens } from './tokens.js';
9
+ import type { SessionDeps } from './types.js';
10
+ type Through = Awaited<ReturnType<SignInHooks['signInThrough']>>;
11
+ type How = Parameters<SignInHooks['signInThrough']>[1];
12
+ type Origin = {
13
+ readonly attemptId: string | null;
14
+ readonly email: string | null;
15
+ readonly purpose?: PendingTwoStep['purpose'];
16
+ };
17
+ /** Redeeming a link and checking a two-step code, under the cookie lock. */
18
+ export interface SignIn {
19
+ /**
20
+ * What a redeem and a verify share: pinned to the generation they started in, inside the
21
+ * cookie lock, a pending logout sent first, the answer settled before anything is
22
+ * adopted. A generation that moved while this waited for the lock means a sign-out came
23
+ * after the click: nothing is sent.
24
+ */
25
+ readonly signIn: (call: () => Promise<SignInAnswer>, origin: Origin, step: 'redeem' | 'two-step', pinnedAt?: number,
26
+ /** The caller's own: ends the wait for the cookie lock, so nothing is sent (#465). */
27
+ signal?: AbortSignal,
28
+ /**
29
+ * Called right before the request goes out, in the same turn as the last look at `signal`:
30
+ * from then on the caller's abort can't stop it (SDK-D1931).
31
+ */
32
+ onSend?: () => void,
33
+ /** Own-tab sign-outs at the press, for challenge adoption during redeem. */
34
+ localSignOuts?: number) => Promise<Settled>;
35
+ /** A family's call (`signInThrough`), pinned to the generation and the sign-out count of its press. */
36
+ readonly through: (how: How, body: Readonly<Record<string, unknown>>, pinned: number, localSignOuts: number, callers: Pick<SharedSignal, 'signal' | 'sent' | 'gone'>,
37
+ /** Called once pulse-auth answered a 2xx that is not an HTML page, before anything reads it. */
38
+ received: () => void) => Promise<Through>;
39
+ /** The code screen's challenge belongs to a generation that has passed. */
40
+ readonly endTwoStep: () => void;
41
+ }
42
+ export declare function createSignIn(deps: SessionDeps, cell: SessionCell, { shared, canDeliverCodes, withState, withCookie }: SharedState, { hold }: Tokens, { sendLogout, recordSignOut, notConfirmed }: Logout): SignIn;
43
+ export {};
@@ -0,0 +1,294 @@
1
+ import { spendAttempt } from '../attempts.js';
2
+ import { unhandled as unhandledError } from '../report.js';
3
+ import { endTwoStep as endCodeScreen, isKeptFor } from './cell.js';
4
+ const routes = { 'log-in': '/login', accept: '/invitations/accept' };
5
+ /** SDK-D1907. The only extra fields that may leave a signed-in response, per caller. */
6
+ const signedInExtra = {
7
+ 'log-in': () => ({}),
8
+ accept: (body) => typeof body === 'object' && body !== null && 'continue_link' in body
9
+ ? { continue_link: body.continue_link } : {},
10
+ };
11
+ /** The server's own path of each sign-in step, for a report (SDK-D1903): never under authPath. */
12
+ const PATH = { ...routes, redeem: '/login-link', 'two-step': '/mfa/verify' };
13
+ export function createSignIn(deps, cell, { shared, canDeliverCodes, withState, withCookie }, { hold }, { sendLogout, recordSignOut, notConfirmed }) {
14
+ const { http, report } = deps;
15
+ const storage = deps.storage;
16
+ /**
17
+ * SDK-D1975: the one check before a challenge is kept, for a redeem and for a family's call. The
18
+ * press still speaks: no sign-in or sign-out moved the generation since (#481), and no sign-out
19
+ * was pressed in this tab since, even one whose write still waits for the state lock
20
+ * (SDK-D1970). And someone is left to answer it: a family's challenge that comes once every
21
+ * caller has left is dropped, for nobody is there to be asked, and a code screen up for another
22
+ * sign-in would silently change the challenge its code goes to. A redeem's callers are never
23
+ * gone this way: its record keeps the answer, and the next press of the link reads it.
24
+ */
25
+ const adoption = (pinned, press) => {
26
+ if (shared().generation !== pinned || cell.localSignOuts !== press.localSignOuts)
27
+ return 'superseded';
28
+ return press.gone?.() === true ? 'nobody' : 'adopt';
29
+ };
30
+ /**
31
+ * A sign-in in this tab took a challenge or signed in (`settled`): every kept redeem that is not
32
+ * its own answers `superseded` from now on, for every later press of its link, whatever was
33
+ * pending at this moment (SDK-D1976, SDK-D2007). A family's sign-in has no record, so it marks
34
+ * them all. Its own record is unmarked when it installs its own challenge, since that one is the
35
+ * code screen now: a link pressed while a log-in was out waits behind it in the cookie lock, and
36
+ * the log-in's challenge marked it. Its own code finishing it overtakes nothing.
37
+ */
38
+ const overtake = (origin, settled) => {
39
+ for (const record of cell.redeems.values()) {
40
+ if (record.attemptId !== origin.attemptId)
41
+ record.overtaken = true;
42
+ else if (settled === 'challenge')
43
+ record.overtaken = false;
44
+ }
45
+ };
46
+ /**
47
+ * The cookie pulse-auth just set must be revoked before anyone refreshes with it, and the revoke
48
+ * didn't get through, or couldn't be recorded. A logout already pending at the current generation
49
+ * does it; otherwise it is recorded as a sign-out the shared record missed, so it outlives this
50
+ * object (sdk#483). Answers the generation whose pending logout revokes it, and the record's
51
+ * serial with it, read under the caller's cookie lock.
52
+ */
53
+ const oweRevoke = async () => {
54
+ const state = shared();
55
+ if (state.logoutPending?.generation === state.generation)
56
+ return { generation: state.generation, serial: state.serial };
57
+ // The challenge is left to whoever answers: `unsettled` spends a code's, and a superseded code's
58
+ // screen is ended by verifyTwoStep through endTwoStep (SDK-D1978).
59
+ return recordSignOut();
60
+ };
61
+ /** Settles a redeem's, a verify's or a family's answer inside the cookie lock. */
62
+ const settleSignIn = async (answer, pinned, origin, step, press) => {
63
+ switch (answer.kind) {
64
+ case 'signed-in': {
65
+ const decided = await withState((state) => {
66
+ if (state.generation !== pinned) {
67
+ return { write: null, result: 'superseded' };
68
+ }
69
+ const next = {
70
+ generation: state.generation + 1,
71
+ signedIn: true,
72
+ serial: state.serial + 1,
73
+ // A new session cookie replaced whatever logout was pending.
74
+ logoutPending: null,
75
+ principal: answer.access.fingerprint,
76
+ person: answer.access.person,
77
+ };
78
+ return {
79
+ write: next,
80
+ result: 'signed-in',
81
+ then: () => {
82
+ if (storage !== null && origin.attemptId !== null)
83
+ spendAttempt(storage, origin.attemptId);
84
+ overtake(origin, 'signed-in');
85
+ cell.twoStep = null;
86
+ // The new session's cookie replaced any a closed session in this page owed (sdk#483).
87
+ if (deps.owed !== null)
88
+ deps.owed.left = null;
89
+ hold(answer.access, next.generation, next.serial, 'here');
90
+ },
91
+ };
92
+ });
93
+ if (decided.ran && decided.value === 'signed-in') {
94
+ return { kind: 'signed-in', backupCodes: answer.backupCodes };
95
+ }
96
+ // A sign-out moved the state while this was in flight (or the state couldn't be
97
+ // written): the sign-out wins. pulse-auth made the session anyway and set its
98
+ // cookie, so revoke it now, still inside the cookie lock; the pending logout stays. A
99
+ // revoke that doesn't get through is owed as a sign-out the shared record missed
100
+ // (oweRevoke): every tab publishes `signed_out`, since no session can be renewed over
101
+ // that cookie (SDK-D1968), and what it earns is `notConfirmed`'s (SDK-D1963).
102
+ const revoked = await http.logout();
103
+ // Confirmed under the cookie lock, as every logout: the slot's cookie went with it (S3),
104
+ // and so did the owed cookie a kept mark was for. A confirmation by any route is the
105
+ // newest answer about that cookie, so the mark ends (PR 547 round 2).
106
+ if (revoked.kind === 'confirmed') {
107
+ if (deps.owed !== null)
108
+ deps.owed.left = null;
109
+ if (isKeptFor(cell.logoutKept, shared()))
110
+ cell.logoutKept = null;
111
+ }
112
+ if (revoked.kind !== 'confirmed') {
113
+ // Only an answer outside the contract is reported.
114
+ if (revoked.kind === 'broken') {
115
+ report({ kind: 'answer', path: '/logout', status: revoked.httpStatus });
116
+ }
117
+ const owed = await oweRevoke();
118
+ cell.logoutBackoffStep = 0;
119
+ notConfirmed(revoked, owed.generation, owed.serial);
120
+ }
121
+ if (step === 'two-step' && origin.purpose === 'setup' && answer.backupCodes !== null) {
122
+ const userId = answer.access.principal.userId;
123
+ const codes = answer.backupCodes;
124
+ // A refused write leaves the pinned record intact. A successful supersession
125
+ // observed a newer generation, so an older record cannot establish its recipient.
126
+ const minimumGeneration = decided.ran ? pinned + 1 : pinned;
127
+ return {
128
+ kind: 'superseded',
129
+ codesAtDelivery: () => canDeliverCodes(userId, minimumGeneration) ? codes : null,
130
+ };
131
+ }
132
+ return { kind: 'superseded' };
133
+ }
134
+ case 'two-step':
135
+ case 'two-step-setup': {
136
+ if (step === 'two-step') {
137
+ report({ kind: 'answer', path: PATH[step], status: 200 });
138
+ return { kind: 'unknown', status: 200 };
139
+ }
140
+ switch (adoption(pinned, press)) {
141
+ // A challenge overtaken by a sign-out or sign-in must not reopen the code screen (#481).
142
+ case 'superseded':
143
+ return { kind: 'superseded' };
144
+ // Every caller left: the challenge is dropped, what was pending stays (SDK-D1975).
145
+ case 'nobody':
146
+ return { kind: 'unavailable' };
147
+ case 'adopt':
148
+ break;
149
+ }
150
+ overtake(origin, 'challenge');
151
+ // No cookie yet, so a pending logout stays through the code screen. A set-up challenge
152
+ // is kept as a code's is, for /mfa/enroll and then the code (#465).
153
+ const pending = { attemptId: origin.attemptId, email: origin.email, challenge: answer.challenge, generation: pinned };
154
+ cell.twoStepSuperseded = false;
155
+ cell.twoStepEndedByTab = false;
156
+ if (answer.kind === 'two-step-setup') {
157
+ const setup = Object.freeze({ kind: 'sign_in_setup', expires_at: answer.challenge.expiresAt });
158
+ cell.setupSteps.set(setup, 'issued');
159
+ cell.twoStep = { ...pending, purpose: 'setup', setup };
160
+ return { kind: 'two-step-setup', setup };
161
+ }
162
+ cell.twoStep = { ...pending, purpose: 'verify' };
163
+ return { kind: 'two-step' };
164
+ }
165
+ case 'unsettled': {
166
+ report({ kind: 'answer', path: PATH[step], status: answer.status });
167
+ // A code answered with a 2xx that can't be read spent its challenge, whatever happens to
168
+ // the logout below: the next code reads `expired` and is not sent (SDK-D1978).
169
+ if (step === 'two-step')
170
+ cell.twoStep = null;
171
+ // pulse-auth answered 2xx, so it may have set its cookie, but this tab can't use
172
+ // the answer. That session ends rather than live on behind a failure screen: it is
173
+ // recorded as the current generation's pending logout, so a reload or another tab
174
+ // sends the logout too and never restores it, and sent now, inside the cookie lock. The
175
+ // generation stays, so the serial moves: a flag a tab read as paid at this generation is
176
+ // another record, and this one is never read as paid (sdk#506).
177
+ const recorded = await withState((state) => {
178
+ const owed = state.generation === pinned && !state.signedIn;
179
+ return {
180
+ write: owed
181
+ ? { ...state, serial: state.serial + 1, logoutPending: { generation: state.generation } }
182
+ : null,
183
+ result: owed ? state.serial + 1 : null,
184
+ };
185
+ });
186
+ cell.logoutBackoffStep = 0;
187
+ if (recorded.ran && recorded.value !== null) {
188
+ const answer = await sendLogout(pinned);
189
+ if (answer.kind !== 'confirmed')
190
+ notConfirmed(answer, pinned, recorded.value);
191
+ }
192
+ else {
193
+ // Storage refused, or a sign-out elsewhere moved the state: owed as a sign-out the
194
+ // shared record missed, and sent now.
195
+ const owed = await oweRevoke();
196
+ const answer = await sendLogout(owed.generation);
197
+ if (answer.kind !== 'confirmed')
198
+ notConfirmed(answer, owed.generation, owed.serial);
199
+ }
200
+ return { kind: 'unknown', status: answer.status };
201
+ }
202
+ case 'unknown':
203
+ report({ kind: 'answer', path: PATH[step], status: answer.status });
204
+ return { kind: 'unknown', status: answer.status };
205
+ case 'proxy-refused':
206
+ report({ kind: 'answer', path: PATH[step], status: 403 });
207
+ return { kind: 'proxy-refused' };
208
+ case 'dead-link':
209
+ case 'wrong-code':
210
+ case 'locked':
211
+ case 'expired':
212
+ case 'no-account':
213
+ case 'inactive':
214
+ case 'unavailable':
215
+ return { kind: answer.kind };
216
+ default: {
217
+ const unhandled = answer;
218
+ throw unhandledError('sign-in answer', unhandled);
219
+ }
220
+ }
221
+ };
222
+ // The press pins the generation. The cookie lock covers the send and all settlement, revoke
223
+ // included. The caller's signal cuts only a wait before the send.
224
+ async function underCookie(pinned, signal, onSend, send,
225
+ // A shared caller list's: every caller left in the turn of the press (SDK-D1932).
226
+ gone) {
227
+ const locked = await withCookie(async () => {
228
+ // Closed while this waited for the lock: nothing is sent (SDK-D1930).
229
+ if (cell.closed)
230
+ return { kind: 'unavailable' };
231
+ const state = shared();
232
+ if (state.generation !== pinned)
233
+ return { kind: 'superseded' };
234
+ if (state.logoutPending?.generation === state.generation) {
235
+ // Not confirmed: a successful sign-in replaces the cookie (and clears the flag);
236
+ // any other answer leaves it owed (SDK-D1963).
237
+ const answer = await sendLogout(state.generation);
238
+ if (answer.kind !== 'confirmed')
239
+ notConfirmed(answer, state.generation, state.serial);
240
+ }
241
+ // The caller may have left once the lock was had (its abort no longer ends the wait), or the
242
+ // session closed while the owed logout was out: look again right before the send
243
+ // (SDK-D1902, SDK-D1930). Nothing sent.
244
+ if (cell.closed || signal?.aborted === true || gone?.() === true)
245
+ return { kind: 'unavailable' };
246
+ // A sign-out elsewhere while the owed logout was out (SDK-D1925). A confirmed logout keeps
247
+ // the generation, so only a sign-out moves it here.
248
+ if (shared().generation !== pinned)
249
+ return { kind: 'superseded' };
250
+ // From here on the caller's abort can't stop the request (SDK-D1931).
251
+ onSend?.();
252
+ return send();
253
+ }, signal);
254
+ return locked.ran ? locked.value : { kind: 'unavailable' };
255
+ }
256
+ const signIn = (call, origin, step, pinnedAt, signal, onSend, localSignOuts) => {
257
+ // A redeem is pinned to the click; a code, to the redeem that asked for it.
258
+ const pinned = pinnedAt ?? shared().generation;
259
+ return underCookie(pinned, signal, onSend, async () => settleSignIn(await call(), pinned, origin, step, { localSignOuts }));
260
+ };
261
+ const through = (how, body, pinned, localSignOuts, callers, received) => underCookie(pinned, callers.signal, callers.sent, async () => {
262
+ const exchange = await http.sendSignIn(routes[how.step], body);
263
+ if (exchange.kind === 'proxy-refused') {
264
+ report({ kind: 'answer', path: routes[how.step], status: exchange.status });
265
+ return { kind: 'proxy_refused' };
266
+ }
267
+ // Once sent, a refusal, no answer or a proxy's is answered as it is, whatever the generation:
268
+ // it sets no cookie, so a sign-out elsewhere changes nothing about it (SDK-D1946).
269
+ if (exchange.kind !== 'received')
270
+ return exchange;
271
+ // Every 2xx of pulse-auth's, the one the session can't use included, before it is settled
272
+ // (sdk#472). An HTML page is the app's own, never pulse-auth's: the accept didn't reach it.
273
+ if (!exchange.page)
274
+ received();
275
+ const token = exchange.token;
276
+ const settled = await settleSignIn(token, pinned, { attemptId: null, email: how.email }, how.step, { localSignOuts, gone: callers.gone });
277
+ // A dropped challenge answers nobody: every caller has its `aborted` already.
278
+ if (settled.kind === 'superseded' || settled.kind === 'unavailable')
279
+ return { kind: settled.kind };
280
+ if (settled.kind === 'signed-in') {
281
+ return { kind: 'step', step: { kind: 'signed_in' }, extra: signedInExtra[how.step](exchange.body) };
282
+ }
283
+ if (settled.kind === 'two-step-setup') {
284
+ return { kind: 'step', step: { kind: 'two_step_setup', expires_at: settled.setup.expires_at, setup: settled.setup }, extra: {} };
285
+ }
286
+ if (settled.kind === 'two-step' && token.kind === 'two-step') {
287
+ return { kind: 'step', step: { kind: 'two_step', expires_at: token.challenge.expiresAt }, extra: {} };
288
+ }
289
+ return { kind: 'invalid' };
290
+ }, callers.gone);
291
+ const endTwoStep = () => endCodeScreen(cell);
292
+ return { signIn, through, endTwoStep };
293
+ }
294
+ //# sourceMappingURL=signIn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signIn.js","sourceRoot":"","sources":["../../../src/first-party/session/signIn.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,SAAS,IAAI,cAAc,EAAE,MAAM,cAAc,CAAA;AAG1D,OAAO,EAAE,UAAU,IAAI,aAAa,EAAE,SAAS,EAAyC,MAAM,WAAW,CAAA;AAuBzG,MAAM,MAAM,GAAG,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,qBAAqB,EAAW,CAAA;AAC7E,wFAAwF;AACxF,MAAM,aAAa,GAAG;IACpB,QAAQ,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC;IACpB,MAAM,EAAE,CAAC,IAAa,EAAE,EAAE,CACxB,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,eAAe,IAAI,IAAI;QAClE,CAAC,CAAC,EAAE,aAAa,EAAE,IAAI,CAAC,aAAa,EAAE,CAAC,CAAC,CAAC,EAAE;CACxC,CAAA;AAuCV,kGAAkG;AAClG,MAAM,IAAI,GAAG,EAAE,GAAG,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,aAAa,EAAW,CAAA;AAErF,MAAM,UAAU,YAAY,CAC1B,IAAiB,EACjB,IAAiB,EACjB,EAAE,MAAM,EAAE,eAAe,EAAE,SAAS,EAAE,UAAU,EAAe,EAC/D,EAAE,IAAI,EAAU,EAChB,EAAE,UAAU,EAAE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;YAAE,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,CAAA;QAClG,IAAI,OAAO,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;YACjC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,EAAE,KAAK,EAAE,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAA;QACrG,CAAC;QACD,IAAI,OAAO,CAAC,IAAI,KAAK,gBAAgB,EAAE,CAAC;YACtC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,gBAAgB,EAAE,UAAU,EAAE,OAAO,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,CAAA;QAClI,CAAC;QACD,IAAI,OAAO,CAAC,IAAI,KAAK,UAAU,IAAI,KAAK,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;YAC7D,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE,UAAU,EAAE,KAAK,CAAC,SAAS,CAAC,SAAS,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,CAAA;QACvG,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAA;IAC5B,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;IAElB,MAAM,UAAU,GAAG,GAAS,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,CAAA;IAElD,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,CAAA;AACxC,CAAC"}
@@ -0,0 +1,5 @@
1
+ import type { SignInHooks } from '../signInHooks.js';
2
+ import type { SessionCell } from './cell.js';
3
+ import type { SharedState } from './shared.js';
4
+ import type { SignIn } from './signIn.js';
5
+ export declare function createSignInThrough(cell: Pick<SessionCell, 'supported' | 'localSignOuts'>, shared: SharedState['shared'], signIn: SignIn): SignInHooks['signInThrough'];
@@ -0,0 +1,95 @@
1
+ import { stopListening } from '../../core/signal.js';
2
+ import { sharedSignal } from './sharedSignal.js';
3
+ /**
4
+ * The same request: the same fields, each with the same value. Compared field by field, so a
5
+ * password is never part of a key or of any string built from the request (SDK-D1947).
6
+ */
7
+ function sameRequest(a, b) {
8
+ const fields = Object.keys(a);
9
+ return fields.length === Object.keys(b).length &&
10
+ fields.every((field) => Object.hasOwn(b, field) && Object.is(a[field], b[field]));
11
+ }
12
+ /**
13
+ * One caller's answer, once it has joined the flight's callers. Its own abort answers it `aborted`
14
+ * at once, for it alone; the request is given up only once every caller has left before the send
15
+ * (SDK-D1932).
16
+ */
17
+ function answerOf(flight, signal) {
18
+ if (signal === undefined)
19
+ return flight.result;
20
+ if (signal.aborted)
21
+ return Promise.resolve({ kind: 'aborted' });
22
+ let leave = () => undefined;
23
+ const left = new Promise((resolve) => {
24
+ leave = () => {
25
+ resolve({ kind: 'aborted' });
26
+ };
27
+ signal.addEventListener('abort', leave, { once: true });
28
+ });
29
+ return Promise.race([flight.result, left]).finally(() => {
30
+ stopListening(signal, leave);
31
+ });
32
+ }
33
+ export function createSignInThrough(cell, shared, signIn) {
34
+ /**
35
+ * The attempts in flight, by generation, step and address (SDK-D1947). The same request joins
36
+ * the attempt in flight; a different one is an attempt of its own and joins nothing (SDK-D1908);
37
+ * an earlier attempt keeps running for the callers it has. So p1, p2, then p1 again while both
38
+ * are out: the third joins the first, and two requests are sent. An attempt every caller left
39
+ * before the send is given up (SDK-D1932): nobody joins it, and a new press starts its own.
40
+ */
41
+ const flights = new Map();
42
+ return (request, how, options) => {
43
+ // A family's programming error comes first, whatever the signal or the browser (SDK-D1929).
44
+ if (request.method !== 'POST' || request.query !== undefined ||
45
+ request.path !== (how.step === 'accept' ? '/invitations/accept' : '/login')) {
46
+ return Promise.reject(new TypeError('a sign-in hook requires the POST route for its step'));
47
+ }
48
+ if (options?.signal?.aborted)
49
+ return Promise.resolve({ kind: 'aborted' });
50
+ if (!cell.supported)
51
+ return Promise.resolve({ kind: 'unsupported' });
52
+ const pinned = shared().generation;
53
+ const body = request.body ?? {};
54
+ const key = JSON.stringify([pinned, how.step, how.email]);
55
+ const inFlight = flights.get(key) ?? [];
56
+ const earlier = inFlight.find((flight) => !flight.callers.givenUp() && sameRequest(flight.body, body));
57
+ if (earlier !== undefined) {
58
+ earlier.callers.join(options?.signal);
59
+ return answerOf(earlier, options?.signal);
60
+ }
61
+ // The first caller is counted before the flight starts: a lock granted in the same turn makes
62
+ // its last look inside `through`, and must find that caller there.
63
+ const callers = sharedSignal();
64
+ callers.join(options?.signal);
65
+ // Whether pulse-auth answered this request a 2xx, whatever the session then made of it.
66
+ let received = false;
67
+ const flight = {
68
+ body,
69
+ callers,
70
+ result: signIn.through(how, body, pinned, cell.localSignOuts, callers, () => {
71
+ received = true;
72
+ }),
73
+ };
74
+ flights.set(key, [...inFlight, flight]);
75
+ const forget = () => {
76
+ callers.settled();
77
+ const rest = (flights.get(key) ?? []).filter((other) => other !== flight);
78
+ if (rest.length === 0)
79
+ flights.delete(key);
80
+ else
81
+ flights.set(key, rest);
82
+ };
83
+ flight.result.then(forget, forget);
84
+ // One request, one report: read once, on the request's own result, whatever its callers did
85
+ // (SDK-D1976). A caller that left was answered `aborted` already and never reads it. Both
86
+ // hooks run before any caller is answered; a result that rejects calls neither.
87
+ flight.result.then((answer) => {
88
+ if (answer.kind === 'refused')
89
+ how.onRefused(answer.answer);
90
+ how.onSettled(answer, { received });
91
+ }, () => undefined);
92
+ return answerOf(flight, options?.signal);
93
+ };
94
+ }
95
+ //# sourceMappingURL=signInThrough.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signInThrough.js","sourceRoot":"","sources":["../../../src/first-party/session/signInThrough.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AA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AAM,CAAC,CAAA;QAClC,4FAA4F;QAC5F,0FAA0F;QAC1F,gFAAgF;QAChF,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE;YAC5B,IAAI,MAAM,CAAC,IAAI,KAAK,SAAS;gBAAE,GAAG,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;YAC3D,GAAG,CAAC,SAAS,CAAC,MAAM,EAAE,EAAE,QAAQ,EAAE,CAAC,CAAA;QACrC,CAAC,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;QACnB,OAAO,QAAQ,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;IAC1C,CAAC,CAAA;AACH,CAAC"}
@@ -0,0 +1,17 @@
1
+ import { type SessionCell } from './cell.js';
2
+ import type { Logout } from './logout.js';
3
+ import type { Refresher } from './refresh.js';
4
+ import { type SharedState } from './shared.js';
5
+ import type { SignIn } from './signIn.js';
6
+ import type { Tokens } from './tokens.js';
7
+ import type { SessionDeps } from './types.js';
8
+ /** Keeping this tab in step with the others: their messages, and the shared state. */
9
+ export interface Sync {
10
+ /** Another tab's message on the channel. */
11
+ readonly onMessage: (data: unknown) => void;
12
+ /** Checks the shared state again: a tab coming back, a token asked for. */
13
+ readonly sync: () => Promise<void>;
14
+ /** Another tab wrote the shared state (the `storage` event for its key): checks it again. */
15
+ readonly stateWritten: () => Promise<void>;
16
+ }
17
+ export declare function createSync(deps: SessionDeps, cell: SessionCell, { shared }: SharedState, { hold, release }: Tokens, { refresh, restore }: Refresher, { tryLogout, writeDown, writeDownPaid }: Logout, { endTwoStep }: SignIn): Sync;
@@ -0,0 +1,143 @@
1
+ import { readAccessToken } from '../claims.js';
2
+ import { readState } from '../state.js';
3
+ import { adoptNewer, isKeptFor, Message } from './cell.js';
4
+ import { isNewer } from './shared.js';
5
+ export function createSync(deps, cell, { shared }, { hold, release }, { refresh, restore }, { tryLogout, writeDown, writeDownPaid }, { endTwoStep }) {
6
+ const { report } = deps;
7
+ const onMessage = (data) => {
8
+ // No storage or no locks: nothing this tab can sign in with. The listener is not added
9
+ // either. A message that arrived anyway must not show a peer's sign-in or schedule a
10
+ // renewal for a token this tab cannot keep.
11
+ if (!cell.supported)
12
+ return;
13
+ const parsed = Message.safeParse(data);
14
+ if (!parsed.success)
15
+ return;
16
+ const message = parsed.data;
17
+ if (message.type === 'signed-out') {
18
+ // Read storage first: a record written since this tab's own unrecorded sign-out ends that
19
+ // one, so the heard sign-out is compared with, and stamped against, what storage reads now.
20
+ const latest = shared();
21
+ const learned = {
22
+ generation: message.generation,
23
+ signedIn: false,
24
+ serial: latest.serial,
25
+ logoutPending: { generation: message.generation },
26
+ principal: null,
27
+ person: null,
28
+ };
29
+ if (isNewer(learned, latest)) {
30
+ // A sign-out storage never took: this tab holds its logout as the tab that made it does,
31
+ // so a closed session leaves it in this page's slot too, under what storage reads now
32
+ // (SDK-D1977; the first one wins). A recorded one is in storage already.
33
+ const stored = deps.storage === null ? null : readState(deps.storage).generation;
34
+ if (stored !== null && message.generation > stored)
35
+ cell.owedStored ??= stored;
36
+ cell.known = learned;
37
+ }
38
+ if (cell.twoStep !== null && message.generation > cell.twoStep.generation)
39
+ endTwoStep();
40
+ if (cell.held !== null && message.generation > cell.held.generation) {
41
+ release({ status: 'signed-out' });
42
+ }
43
+ return;
44
+ }
45
+ // Made under an older state than this tab knows (generation, then serial): a late
46
+ // answer, not the current session's latest token. (generation, serial) is one version.
47
+ const latest = shared();
48
+ if (message.generation < latest.generation ||
49
+ (message.generation === latest.generation && message.serial < latest.serial)) {
50
+ return;
51
+ }
52
+ if (cell.held !== null &&
53
+ cell.held.generation === message.generation &&
54
+ message.serial <= cell.held.serial) {
55
+ return;
56
+ }
57
+ const read = readAccessToken(message.accessToken);
58
+ if (read.kind === 'invalid') {
59
+ // Never adopted: this tab asks pulse-auth itself, under the lock.
60
+ // Its issues name what was wrong with the token: never reported (SDK-D1903).
61
+ report({ kind: 'message' });
62
+ void refresh(cell.held?.access.token ?? null);
63
+ return;
64
+ }
65
+ const learned = {
66
+ generation: message.generation,
67
+ signedIn: true,
68
+ serial: message.serial,
69
+ logoutPending: null,
70
+ principal: read.access.fingerprint,
71
+ person: read.access.person,
72
+ };
73
+ // A sign-in heard replaces whatever sign-out this tab owed: its cookie replaced that one (SDK-D2005).
74
+ if (isNewer(learned, cell.known))
75
+ adoptNewer(cell, learned);
76
+ // Someone signed in elsewhere: a code this tab waits for belongs to an older generation.
77
+ if (cell.twoStep !== null && message.generation > cell.twoStep.generation)
78
+ endTwoStep();
79
+ hold(read.access, message.generation, message.serial, 'another-tab');
80
+ };
81
+ const sync = async () => {
82
+ if (!cell.supported)
83
+ return;
84
+ // A logout paid while storage refused to clear its flag: cleared in storage once it takes it.
85
+ await writeDownPaid();
86
+ const owing = shared();
87
+ // A logout still owed (this tab's or another's that never got through): send it now,
88
+ // rather than wait for the next page load. Not while it is on the timer, nor when pulse-auth
89
+ // refused it here or answered outside the contract: that waits for a load, a sign-in or a
90
+ // restore this tab runs (SDK-D1963, SDK-D2008). Kept, it is still written down once storage
91
+ // takes it, with no request, so a reload owes it too (SDK-D2012).
92
+ if (!owing.signedIn &&
93
+ owing.logoutPending !== null &&
94
+ owing.logoutPending.generation === owing.generation &&
95
+ cell.logoutRetryTimer === undefined) {
96
+ if (isKeptFor(cell.logoutKept, owing))
97
+ await writeDown();
98
+ else
99
+ void tryLogout();
100
+ }
101
+ // Read again: the write-down waits for the state lock, and another tab may sign in under it
102
+ // and post a token this tab adopts meanwhile. The state from before would sign that out.
103
+ const state = shared();
104
+ if (cell.twoStep !== null && state.generation !== cell.twoStep.generation)
105
+ endTwoStep();
106
+ if (cell.held !== null && cell.held.generation !== state.generation) {
107
+ // Another tab signed in or out and this one missed the message: drop the token,
108
+ // and pick up whoever is signed in now from the cookie.
109
+ cell.twoStep = null;
110
+ release(state.signedIn ? { status: 'restoring' } : { status: 'signed-out' });
111
+ if (state.signedIn)
112
+ await restore();
113
+ return;
114
+ }
115
+ if (cell.held === null && state.signedIn && cell.status.status === 'signed-out') {
116
+ await restore();
117
+ return;
118
+ }
119
+ // Another tab adopted a newer token for a different principal (a changed role), and
120
+ // this tab missed its message: ask for its own, under the cookie lock. The same
121
+ // principal at a newer serial changes nothing this tab may do, so no refresh.
122
+ if (cell.held !== null &&
123
+ state.signedIn &&
124
+ state.serial > cell.held.serial &&
125
+ state.principal !== null &&
126
+ state.principal !== cell.held.access.fingerprint) {
127
+ await refresh(cell.held.access.token);
128
+ }
129
+ };
130
+ /**
131
+ * Another tab wrote the shared state, and that write landed. The record this tab marked as paid
132
+ * is no longer known to be the same write: a build that doesn't move the serial can clear the
133
+ * flag and set it again at the same generation and serial before this tab reads, and reading
134
+ * can't tell. So the mark ends before the state is read (sdk#506). The price is at most one more
135
+ * `/logout` per such write, the mistake SDK-D1967 prefers to a swallowed sign-out.
136
+ */
137
+ const stateWritten = () => {
138
+ cell.paidLogout = null;
139
+ return sync();
140
+ };
141
+ return { onMessage, sync, stateWritten };
142
+ }
143
+ //# sourceMappingURL=sync.js.map