@flipstream/sdk 0.14.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. package/CHANGELOG.md +125 -0
  2. package/README.md +95 -1
  3. package/dist/api/datasets.js +8 -8
  4. package/dist/api/datasets.js.map +1 -1
  5. package/dist/api/log.js +6 -10
  6. package/dist/api/log.js.map +1 -1
  7. package/dist/api/pathArgument.d.ts +7 -0
  8. package/dist/api/pathArgument.js +12 -0
  9. package/dist/api/pathArgument.js.map +1 -0
  10. package/dist/auth/transport.d.ts +6 -2
  11. package/dist/auth/transport.js +5 -3
  12. package/dist/auth/transport.js.map +1 -1
  13. package/dist/core/call.d.ts +4 -0
  14. package/dist/core/call.js +7 -1
  15. package/dist/core/call.js.map +1 -1
  16. package/dist/core/fetch.d.ts +20 -1
  17. package/dist/core/fetch.js +50 -3
  18. package/dist/core/fetch.js.map +1 -1
  19. package/dist/core/hosts.d.ts +11 -0
  20. package/dist/core/hosts.js +13 -0
  21. package/dist/core/hosts.js.map +1 -0
  22. package/dist/core/identify.d.ts +6 -2
  23. package/dist/core/identify.js +16 -12
  24. package/dist/core/identify.js.map +1 -1
  25. package/dist/core/result.d.ts +1 -1
  26. package/dist/core/shared.d.ts +2 -0
  27. package/dist/core/shared.js +1 -0
  28. package/dist/core/shared.js.map +1 -1
  29. package/dist/core/signal.d.ts +7 -0
  30. package/dist/core/signal.js +9 -0
  31. package/dist/core/signal.js.map +1 -1
  32. package/dist/first-party/asPerson.d.ts +13 -0
  33. package/dist/first-party/asPerson.js +144 -0
  34. package/dist/first-party/asPerson.js.map +1 -0
  35. package/dist/first-party/attempts.d.ts +71 -0
  36. package/dist/first-party/attempts.js +132 -0
  37. package/dist/first-party/attempts.js.map +1 -0
  38. package/dist/first-party/authProxyMark.d.ts +10 -0
  39. package/dist/first-party/authProxyMark.js +11 -0
  40. package/dist/first-party/authProxyMark.js.map +1 -0
  41. package/dist/first-party/browser.d.ts +55 -0
  42. package/dist/first-party/browser.js +137 -0
  43. package/dist/first-party/browser.js.map +1 -0
  44. package/dist/first-party/browserSession.d.ts +95 -0
  45. package/dist/first-party/browserSession.js +242 -0
  46. package/dist/first-party/browserSession.js.map +1 -0
  47. package/dist/first-party/calls/consent.d.ts +89 -0
  48. package/dist/first-party/calls/consent.js +174 -0
  49. package/dist/first-party/calls/consent.js.map +1 -0
  50. package/dist/first-party/calls/device.d.ts +97 -0
  51. package/dist/first-party/calls/device.js +133 -0
  52. package/dist/first-party/calls/device.js.map +1 -0
  53. package/dist/first-party/calls/invitation.d.ts +244 -0
  54. package/dist/first-party/calls/invitation.js +258 -0
  55. package/dist/first-party/calls/invitation.js.map +1 -0
  56. package/dist/first-party/calls/logIn.d.ts +61 -0
  57. package/dist/first-party/calls/logIn.js +48 -0
  58. package/dist/first-party/calls/logIn.js.map +1 -0
  59. package/dist/first-party/calls/reset.d.ts +61 -0
  60. package/dist/first-party/calls/reset.js +163 -0
  61. package/dist/first-party/calls/reset.js.map +1 -0
  62. package/dist/first-party/calls/signUp.d.ts +21 -0
  63. package/dist/first-party/calls/signUp.js +72 -0
  64. package/dist/first-party/calls/signUp.js.map +1 -0
  65. package/dist/first-party/calls/twoStepSetup.d.ts +78 -0
  66. package/dist/first-party/calls/twoStepSetup.js +168 -0
  67. package/dist/first-party/calls/twoStepSetup.js.map +1 -0
  68. package/dist/first-party/claims.d.ts +88 -0
  69. package/dist/first-party/claims.js +181 -0
  70. package/dist/first-party/claims.js.map +1 -0
  71. package/dist/first-party/enrollment.d.ts +10 -0
  72. package/dist/first-party/enrollment.js +44 -0
  73. package/dist/first-party/enrollment.js.map +1 -0
  74. package/dist/first-party/entry.d.ts +573 -0
  75. package/dist/first-party/entry.js +582 -0
  76. package/dist/first-party/entry.js.map +1 -0
  77. package/dist/first-party/hooks.d.ts +81 -0
  78. package/dist/first-party/hooks.js +19 -0
  79. package/dist/first-party/hooks.js.map +1 -0
  80. package/dist/first-party/http.d.ts +191 -0
  81. package/dist/first-party/http.js +364 -0
  82. package/dist/first-party/http.js.map +1 -0
  83. package/dist/first-party/index.d.ts +14 -0
  84. package/dist/first-party/index.js +3 -0
  85. package/dist/first-party/index.js.map +1 -0
  86. package/dist/first-party/landed.d.ts +32 -0
  87. package/dist/first-party/landed.js +50 -0
  88. package/dist/first-party/landed.js.map +1 -0
  89. package/dist/first-party/landedProof.d.ts +12 -0
  90. package/dist/first-party/landedProof.js +20 -0
  91. package/dist/first-party/landedProof.js.map +1 -0
  92. package/dist/first-party/linkToken.d.ts +15 -0
  93. package/dist/first-party/linkToken.js +28 -0
  94. package/dist/first-party/linkToken.js.map +1 -0
  95. package/dist/first-party/links.d.ts +100 -0
  96. package/dist/first-party/links.js +84 -0
  97. package/dist/first-party/links.js.map +1 -0
  98. package/dist/first-party/locks.d.ts +36 -0
  99. package/dist/first-party/locks.js +64 -0
  100. package/dist/first-party/locks.js.map +1 -0
  101. package/dist/first-party/personTransport.d.ts +38 -0
  102. package/dist/first-party/personTransport.js +177 -0
  103. package/dist/first-party/personTransport.js.map +1 -0
  104. package/dist/first-party/proxy/app.d.ts +32 -0
  105. package/dist/first-party/proxy/app.js +94 -0
  106. package/dist/first-party/proxy/app.js.map +1 -0
  107. package/dist/first-party/proxy/bound.d.ts +5 -0
  108. package/dist/first-party/proxy/bound.js +13 -0
  109. package/dist/first-party/proxy/bound.js.map +1 -0
  110. package/dist/first-party/proxy/cases.d.ts +71 -0
  111. package/dist/first-party/proxy/cases.js +411 -0
  112. package/dist/first-party/proxy/cases.js.map +1 -0
  113. package/dist/first-party/proxy/decide.d.ts +31 -0
  114. package/dist/first-party/proxy/decide.js +139 -0
  115. package/dist/first-party/proxy/decide.js.map +1 -0
  116. package/dist/first-party/proxy/freeze.d.ts +2 -0
  117. package/dist/first-party/proxy/freeze.js +10 -0
  118. package/dist/first-party/proxy/freeze.js.map +1 -0
  119. package/dist/first-party/proxy/handler.d.ts +23 -0
  120. package/dist/first-party/proxy/handler.js +190 -0
  121. package/dist/first-party/proxy/handler.js.map +1 -0
  122. package/dist/first-party/proxy/headers.d.ts +7 -0
  123. package/dist/first-party/proxy/headers.js +20 -0
  124. package/dist/first-party/proxy/headers.js.map +1 -0
  125. package/dist/first-party/proxy/hop.d.ts +9 -0
  126. package/dist/first-party/proxy/hop.js +37 -0
  127. package/dist/first-party/proxy/hop.js.map +1 -0
  128. package/dist/first-party/proxy/host.d.ts +5 -0
  129. package/dist/first-party/proxy/host.js +11 -0
  130. package/dist/first-party/proxy/host.js.map +1 -0
  131. package/dist/first-party/proxy/index.d.ts +7 -0
  132. package/dist/first-party/proxy/index.js +6 -0
  133. package/dist/first-party/proxy/index.js.map +1 -0
  134. package/dist/first-party/proxy/pass-back.d.ts +26 -0
  135. package/dist/first-party/proxy/pass-back.js +47 -0
  136. package/dist/first-party/proxy/pass-back.js.map +1 -0
  137. package/dist/first-party/proxy/paths.d.ts +15 -0
  138. package/dist/first-party/proxy/paths.js +34 -0
  139. package/dist/first-party/proxy/paths.js.map +1 -0
  140. package/dist/first-party/proxy/shown.d.ts +5 -0
  141. package/dist/first-party/proxy/shown.js +16 -0
  142. package/dist/first-party/proxy/shown.js.map +1 -0
  143. package/dist/first-party/proxy/stand-in.d.ts +49 -0
  144. package/dist/first-party/proxy/stand-in.js +341 -0
  145. package/dist/first-party/proxy/stand-in.js.map +1 -0
  146. package/dist/first-party/report.d.ts +56 -0
  147. package/dist/first-party/report.js +38 -0
  148. package/dist/first-party/report.js.map +1 -0
  149. package/dist/first-party/result.d.ts +150 -0
  150. package/dist/first-party/result.js +40 -0
  151. package/dist/first-party/result.js.map +1 -0
  152. package/dist/first-party/session/cell.d.ts +201 -0
  153. package/dist/first-party/session/cell.js +103 -0
  154. package/dist/first-party/session/cell.js.map +1 -0
  155. package/dist/first-party/session/closed.d.ts +11 -0
  156. package/dist/first-party/session/closed.js +15 -0
  157. package/dist/first-party/session/closed.js.map +1 -0
  158. package/dist/first-party/session/index.d.ts +3 -0
  159. package/dist/first-party/session/index.js +3 -0
  160. package/dist/first-party/session/index.js.map +1 -0
  161. package/dist/first-party/session/logout.d.ts +102 -0
  162. package/dist/first-party/session/logout.js +526 -0
  163. package/dist/first-party/session/logout.js.map +1 -0
  164. package/dist/first-party/session/owed.d.ts +105 -0
  165. package/dist/first-party/session/owed.js +128 -0
  166. package/dist/first-party/session/owed.js.map +1 -0
  167. package/dist/first-party/session/refresh.d.ts +19 -0
  168. package/dist/first-party/session/refresh.js +236 -0
  169. package/dist/first-party/session/refresh.js.map +1 -0
  170. package/dist/first-party/session/results.d.ts +38 -0
  171. package/dist/first-party/session/results.js +74 -0
  172. package/dist/first-party/session/results.js.map +1 -0
  173. package/dist/first-party/session/session.d.ts +2 -0
  174. package/dist/first-party/session/session.js +376 -0
  175. package/dist/first-party/session/session.js.map +1 -0
  176. package/dist/first-party/session/shared.d.ts +34 -0
  177. package/dist/first-party/session/shared.js +145 -0
  178. package/dist/first-party/session/shared.js.map +1 -0
  179. package/dist/first-party/session/sharedSignal.d.ts +27 -0
  180. package/dist/first-party/session/sharedSignal.js +55 -0
  181. package/dist/first-party/session/sharedSignal.js.map +1 -0
  182. package/dist/first-party/session/signIn.d.ts +48 -0
  183. package/dist/first-party/session/signIn.js +340 -0
  184. package/dist/first-party/session/signIn.js.map +1 -0
  185. package/dist/first-party/session/signInThrough.d.ts +6 -0
  186. package/dist/first-party/session/signInThrough.js +144 -0
  187. package/dist/first-party/session/signInThrough.js.map +1 -0
  188. package/dist/first-party/session/sync.d.ts +17 -0
  189. package/dist/first-party/session/sync.js +174 -0
  190. package/dist/first-party/session/sync.js.map +1 -0
  191. package/dist/first-party/session/tokens.d.ts +18 -0
  192. package/dist/first-party/session/tokens.js +64 -0
  193. package/dist/first-party/session/tokens.js.map +1 -0
  194. package/dist/first-party/session/types.d.ts +270 -0
  195. package/dist/first-party/session/types.js +3 -0
  196. package/dist/first-party/session/types.js.map +1 -0
  197. package/dist/first-party/sessionWire.d.ts +31 -0
  198. package/dist/first-party/sessionWire.js +91 -0
  199. package/dist/first-party/sessionWire.js.map +1 -0
  200. package/dist/first-party/signInHooks.d.ts +85 -0
  201. package/dist/first-party/signInHooks.js +2 -0
  202. package/dist/first-party/signInHooks.js.map +1 -0
  203. package/dist/first-party/state.d.ts +71 -0
  204. package/dist/first-party/state.js +55 -0
  205. package/dist/first-party/state.js.map +1 -0
  206. package/dist/first-party/strip.d.ts +1 -0
  207. package/dist/first-party/strip.js +8 -0
  208. package/dist/first-party/strip.js.map +1 -0
  209. package/dist/first-party/twoStepLock.d.ts +26 -0
  210. package/dist/first-party/twoStepLock.js +58 -0
  211. package/dist/first-party/twoStepLock.js.map +1 -0
  212. package/dist/first-party/twoStepSession.d.ts +21 -0
  213. package/dist/first-party/twoStepSession.js +31 -0
  214. package/dist/first-party/twoStepSession.js.map +1 -0
  215. package/dist/first-party/wire.d.ts +63 -0
  216. package/dist/first-party/wire.js +5 -0
  217. package/dist/first-party/wire.js.map +1 -0
  218. package/dist/flipstream.d.ts +80 -19
  219. package/dist/flipstream.js +121 -36
  220. package/dist/flipstream.js.map +1 -1
  221. package/dist/node/session.js +3 -1
  222. package/dist/node/session.js.map +1 -1
  223. package/dist/version.gen.js +1 -1
  224. package/docs/first-party.md +300 -0
  225. package/package.json +34 -5
@@ -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,174 @@
1
+ import { readAccessToken } from '../claims.js';
2
+ import { readState } from '../state.js';
3
+ import { adoptNewer, isKeptFor, owedLogout, 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
+ madeBy: 'sign-out',
29
+ };
30
+ if (isNewer(learned, latest)) {
31
+ // A sign-out storage never took: this tab holds its logout as the tab that made it does,
32
+ // so a closed session leaves it in this page's slot too, under what storage reads now
33
+ // (SDK-D1977; the first one wins). A recorded one is in storage already. The message (the
34
+ // console's format) doesn't say whose cookie it was owed for, so a refresh's record ends it
35
+ // here as before (sdk#552).
36
+ const record = deps.storage === null ? null : readState(deps.storage);
37
+ // A paid entry is not owed, so this heard sign-out starts its own (SDK-D2047). An unpaid
38
+ // one already set is the first and stays. The serial and the base person are the record's.
39
+ // The message names no owed person. The write matches the record anyway (SDK-D2053).
40
+ if (record !== null && message.generation > record.generation && owedLogout(cell) === null) {
41
+ cell.owedFor = {
42
+ stored: record.generation,
43
+ serial: record.serial,
44
+ basePerson: record.person,
45
+ person: null,
46
+ revoke: null,
47
+ };
48
+ }
49
+ cell.known = learned;
50
+ // `known` is the heard sign-out now, not the slot's this session took (sdk#567).
51
+ cell.slotFor = null;
52
+ }
53
+ if (cell.twoStep !== null && message.generation > cell.twoStep.generation)
54
+ endTwoStep();
55
+ if (cell.held !== null && message.generation > cell.held.generation) {
56
+ release({ status: 'signed-out' });
57
+ }
58
+ return;
59
+ }
60
+ // Made under an older state than this tab knows (generation, then serial): a late
61
+ // answer, not the current session's latest token. (generation, serial) is one version.
62
+ const latest = shared();
63
+ if (message.generation < latest.generation ||
64
+ (message.generation === latest.generation && message.serial < latest.serial)) {
65
+ return;
66
+ }
67
+ if (cell.held !== null &&
68
+ cell.held.generation === message.generation &&
69
+ message.serial <= cell.held.serial) {
70
+ return;
71
+ }
72
+ const read = readAccessToken(message.accessToken);
73
+ // Owed only while the logout is still pending. A paid `owedFor` is read by the give-way
74
+ // check and the write-down (sdk#586, SDK-D2050); every other reader treats it as null, so a
75
+ // token may decide and an unreadable one starts a refresh (sdk#572, SDK-D2047).
76
+ // While one is still owed, a token decides nothing: it can arrive before its record, and it
77
+ // doesn't say what made its generation, so the record decides through `standingAt` (PR 558 R1).
78
+ // A session holding the slot owes it the same way: `shared()` just dropped a slot a record has
79
+ // spoken past, and a sign-in in this page empties it, so no session here is signed in under it
80
+ // (sdk#559). A token that doesn't parse is still reported; only the refresh waits (PR 558 round 2 S6).
81
+ const owed = owedLogout(cell) !== null || (deps.owed?.left ?? null) !== null;
82
+ if (read.kind === 'invalid') {
83
+ // Never adopted: this tab asks pulse-auth itself, under the lock.
84
+ // Its issues name what was wrong with the token: never reported (SDK-D1903).
85
+ report({ kind: 'message' });
86
+ if (!owed)
87
+ void refresh(cell.held?.access.token ?? null);
88
+ return;
89
+ }
90
+ if (owed)
91
+ return;
92
+ const learned = {
93
+ generation: message.generation,
94
+ signedIn: true,
95
+ serial: message.serial,
96
+ logoutPending: null,
97
+ principal: read.access.fingerprint,
98
+ person: read.access.person,
99
+ // A token message doesn't say what made its generation; the record does, and is read next.
100
+ madeBy: null,
101
+ };
102
+ // A sign-in heard replaces a recorded sign-out this tab knew: its cookie replaced that one
103
+ // (SDK-D2005). An unrecorded one is left to the record, above.
104
+ if (isNewer(learned, cell.known))
105
+ adoptNewer(cell, learned);
106
+ // Someone signed in elsewhere: a code this tab waits for belongs to an older generation.
107
+ if (cell.twoStep !== null && message.generation > cell.twoStep.generation)
108
+ endTwoStep();
109
+ hold(read.access, message.generation, message.serial, 'another-tab');
110
+ };
111
+ const sync = async () => {
112
+ if (!cell.supported)
113
+ return;
114
+ // A logout paid while storage refused to clear its flag: cleared in storage once it takes it.
115
+ await writeDownPaid();
116
+ const owing = shared();
117
+ // A logout still owed (this tab's or another's that never got through): send it now,
118
+ // rather than wait for the next page load. Not while it is on the timer, nor when pulse-auth
119
+ // refused it here or answered outside the contract: that waits for a load, a sign-in or a
120
+ // restore this tab runs (SDK-D1963, SDK-D2008). Kept, it is still written down once storage
121
+ // takes it, with no request, so a reload owes it too (SDK-D2012). A copy another session has
122
+ // confirmed since is paid first, so that write does not put the flag back (sdk#586).
123
+ if (!owing.signedIn &&
124
+ owing.logoutPending !== null &&
125
+ owing.logoutPending.generation === owing.generation &&
126
+ cell.logoutRetryTimer === undefined) {
127
+ if (isKeptFor(cell.logoutKept, owing))
128
+ await writeDown();
129
+ else
130
+ void tryLogout();
131
+ }
132
+ // Read again: the write-down waits for the state lock, and another tab may sign in under it
133
+ // and post a token this tab adopts meanwhile. The state from before would sign that out.
134
+ const state = shared();
135
+ if (cell.twoStep !== null && state.generation !== cell.twoStep.generation)
136
+ endTwoStep();
137
+ if (cell.held !== null && cell.held.generation !== state.generation) {
138
+ // Another tab signed in or out and this one missed the message: drop the token,
139
+ // and pick up whoever is signed in now from the cookie.
140
+ cell.twoStep = null;
141
+ release(state.signedIn ? { status: 'restoring' } : { status: 'signed-out' });
142
+ if (state.signedIn)
143
+ await restore();
144
+ return;
145
+ }
146
+ if (cell.held === null && state.signedIn && cell.status.status === 'signed-out') {
147
+ await restore();
148
+ return;
149
+ }
150
+ // Another tab adopted a newer token for a different principal (a changed role), and
151
+ // this tab missed its message: ask for its own, under the cookie lock. The same
152
+ // principal at a newer serial changes nothing this tab may do, so no refresh.
153
+ if (cell.held !== null &&
154
+ state.signedIn &&
155
+ state.serial > cell.held.serial &&
156
+ state.principal !== null &&
157
+ state.principal !== cell.held.access.fingerprint) {
158
+ await refresh(cell.held.access.token);
159
+ }
160
+ };
161
+ /**
162
+ * Another tab wrote the shared state, and that write landed. The record this tab marked as paid
163
+ * is no longer known to be the same write: a build that doesn't move the serial can clear the
164
+ * flag and set it again at the same generation and serial before this tab reads, and reading
165
+ * can't tell. So the mark ends before the state is read (sdk#506). The price is at most one more
166
+ * `/logout` per such write, the mistake SDK-D1967 prefers to a swallowed sign-out.
167
+ */
168
+ const stateWritten = () => {
169
+ cell.paidLogout = null;
170
+ return sync();
171
+ };
172
+ return { onMessage, sync, stateWritten };
173
+ }
174
+ //# sourceMappingURL=sync.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync.js","sourceRoot":"","sources":["../../../src/first-party/session/sync.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAC9C,OAAO,EAAE,SAAS,EAAkB,MAAM,aAAa,CAAA;AACvD,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,OAAO,EAAoB,MAAM,WAAW,CAAA;AAGxF,OAAO,EAAE,OAAO,EAAoB,MAAM,aAAa,CAAA;AAevD,MAAM,UAAU,UAAU,CACxB,IAAiB,EACjB,IAAiB,EACjB,EAAE,MAAM,EAAe,EACvB,EAAE,IAAI,EAAE,OAAO,EAAU,EACzB,EAAE,OAAO,EAAE,OAAO,EAAa,EAC/B,EAAE,SAAS,EAAE,SAAS,EAAE,aAAa,EAAU,E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,CAAC,IAAI,KAAK,IAAI,IAAI,KAAK,CAAC,QAAQ,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,YAAY,EAAE,CAAC;YAChF,MAAM,OAAO,EAAE,CAAA;YACf,OAAM;QACR,CAAC;QACD,oFAAoF;QACpF,gFAAgF;QAChF,8EAA8E;QAC9E,IACE,IAAI,CAAC,IAAI,KAAK,IAAI;YAClB,KAAK,CAAC,QAAQ;YACd,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM;YAC/B,KAAK,CAAC,SAAS,KAAK,IAAI;YACxB,KAAK,CAAC,SAAS,KAAK,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,EAChD,CAAC;YACD,MAAM,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;QACvC,CAAC;IACH,CAAC,CAAA;IAED;;;;;;OAMG;IACH,MAAM,YAAY,GAAG,GAAkB,EAAE;QACvC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAA;QACtB,OAAO,IAAI,EAAE,CAAA;IACf,CAAC,CAAA;IAED,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,YAAY,EAAE,CAAA;AAC1C,CAAC"}
@@ -0,0 +1,18 @@
1
+ import type { AccessToken } from '../claims.js';
2
+ import { type RefreshOutcome, type SessionCell } from './cell.js';
3
+ import type { SharedState } from './shared.js';
4
+ import { type Session, type SessionDeps } from './types.js';
5
+ /** The token this tab holds: taking one, dropping it, and refreshing it before it expires. */
6
+ export interface Tokens {
7
+ readonly publish: (next: Session) => void;
8
+ /** Holds `access` as the token of `generation`, and tells listeners (and, from here, the other tabs). */
9
+ readonly hold: (access: AccessToken, generation: number, serial: number, from: 'here' | 'another-tab') => void;
10
+ /** Drops the token: nobody is signed in here now, or nobody can tell. */
11
+ readonly release: (next: Session) => void;
12
+ readonly nextBackoff: () => number;
13
+ }
14
+ /**
15
+ * The held token's part of the session. Its timer refreshes the token through `refresh`,
16
+ * which is the refresher's: the session hands it in, since a refresh holds its answer here.
17
+ */
18
+ export declare function createTokens(deps: SessionDeps, cell: SessionCell, { broadcast }: SharedState, refresh: (stale: string | null) => Promise<RefreshOutcome>): Tokens;
@@ -0,0 +1,64 @@
1
+ import { BACKOFF_MS, MIN_REFRESH_DELAY_MS, } from './cell.js';
2
+ import { REFRESH_MARGIN_MS } from './types.js';
3
+ /**
4
+ * The held token's part of the session. Its timer refreshes the token through `refresh`,
5
+ * which is the refresher's: the session hands it in, since a refresh holds its answer here.
6
+ */
7
+ export function createTokens(deps, cell, { broadcast }, refresh) {
8
+ const { now } = deps;
9
+ const publish = (next) => {
10
+ cell.status = next;
11
+ for (const listener of cell.listeners)
12
+ listener();
13
+ };
14
+ const hold = (access, generation, serial, from) => {
15
+ // Any generation change is a new session, even for the same person: work begun under
16
+ // the old one must not carry on (§2.3).
17
+ if (access.principalKey !== cell.principalKey ||
18
+ cell.held?.generation !== generation) {
19
+ cell.revision += 1;
20
+ }
21
+ cell.principalKey = access.principalKey;
22
+ const held = { access, generation, serial, expiresAt: now() + access.lifetimeMs };
23
+ cell.held = held;
24
+ cell.backoffStep = 0;
25
+ clearTimeout(cell.restoreTimer);
26
+ scheduleRefresh(Math.max(held.expiresAt - REFRESH_MARGIN_MS - now(), MIN_REFRESH_DELAY_MS));
27
+ if (from === 'here') {
28
+ broadcast({ type: 'token', generation, serial, accessToken: access.token });
29
+ }
30
+ publish({ status: 'signed-in', principal: access.principal, revision: cell.revision });
31
+ };
32
+ const release = (next) => {
33
+ cell.held = null;
34
+ clearTimeout(cell.refreshTimer);
35
+ if (cell.principalKey !== null) {
36
+ cell.principalKey = null;
37
+ cell.revision += 1;
38
+ }
39
+ publish(next);
40
+ };
41
+ const scheduleRefresh = (delay) => {
42
+ clearTimeout(cell.refreshTimer);
43
+ if (cell.closed)
44
+ return;
45
+ cell.refreshTimer = setTimeout(() => {
46
+ const token = cell.held?.access.token ?? null;
47
+ if (token === null)
48
+ return;
49
+ void refresh(token).then((outcome) => {
50
+ // Pulse-auth couldn't be reached: the token may still be good for a while. Try
51
+ // again, backing off, without telling anyone it is signed out.
52
+ if (outcome === 'unavailable' && cell.held !== null)
53
+ scheduleRefresh(nextBackoff());
54
+ });
55
+ }, delay);
56
+ };
57
+ const nextBackoff = () => {
58
+ const delay = BACKOFF_MS[Math.min(cell.backoffStep, BACKOFF_MS.length - 1)] ?? 30_000;
59
+ cell.backoffStep += 1;
60
+ return delay;
61
+ };
62
+ return { publish, hold, release, nextBackoff };
63
+ }
64
+ //# sourceMappingURL=tokens.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tokens.js","sourceRoot":"","sources":["../../../src/first-party/session/tokens.ts"],"names":[],"mappings":"AACA,OAAO,EACL,UAAU,EACV,oBAAoB,GAGrB,MAAM,WAAW,CAAA;AAElB,OAAO,EAAE,iBAAiB,EAAkC,MAAM,YAAY,CAAA;AAiB9E;;;GAGG;AACH,MAAM,UAAU,YAAY,CAC1B,IAAiB,EACjB,IAAiB,EACjB,EAAE,SAAS,EAAe,EAC1B,OAA0D;IAE1D,MAAM,EAAE,GAAG,EAAE,GAAG,IAAI,CAAA;IAEpB,MAAM,OAAO,GAAG,CAAC,IAAa,EAAQ,EAAE;QACtC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAA;QAClB,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,SAAS;YAAE,QAAQ,EAAE,CAAA;IACnD,CAAC,CAAA;IAED,MAAM,IAAI,GAAG,CACX,MAAmB,EACnB,UAAkB,EAClB,MAAc,EACd,IAA4B,EACtB,EAAE;QACR,qFAAqF;QACrF,wCAAwC;QACxC,IACE,MAAM,CAAC,YAAY,KAAK,IAAI,CAAC,YAAY;YACzC,IAAI,CAAC,IAAI,EAAE,UAAU,KAAK,UAAU,EACpC,CAAC;YACD,IAAI,CAAC,QAAQ,IAAI,CAAC,CAAA;QACpB,CAAC;QACD,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,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GAAG,UAAU,CAAC,GAAG,EAAE;YAClC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,IAAI,IAAI,CAAA;YAC7C,IAAI,KAAK,KAAK,IAAI;gBAAE,OAAM;YAC1B,KAAK,OAAO,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE;gBACnC,+EAA+E;gBAC/E,+DAA+D;gBAC/D,IAAI,OAAO,KAAK,aAAa,IAAI,IAAI,CAAC,IAAI,KAAK,IAAI;oBACjD,eAAe,CAAC,WAAW,EAAE,CAAC,CAAA;YAClC,CAAC,CAAC,CAAA;QACJ,CAAC,EAAE,KAAK,CAAC,CAAA;IACX,CAAC,CAAA;IAED,MAAM,WAAW,GAAG,GAAW,EAAE;QAC/B,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,IAAI,MAAM,CAAA;QACrF,IAAI,CAAC,WAAW,IAAI,CAAC,CAAA;QACrB,OAAO,KAAK,CAAA;IACd,CAAC,CAAA;IAED,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,WAAW,EAAE,CAAA;AAChD,CAAC"}
@@ -0,0 +1,270 @@
1
+ import type { SignInSetup } from '../hooks.js';
2
+ import type { SignInHooks } from '../signInHooks.js';
3
+ import type { ChannelLike, LockManagerLike, StorageLike } from '../browser.js';
4
+ import type { Principal } from '../claims.js';
5
+ import type { AuthHttp } from '../http.js';
6
+ import type { SessionReport } from '../report.js';
7
+ import type { OwedSlot } from './owed.js';
8
+ /** What the console knows about the session, one meaning per case (§2.6). */
9
+ export type Session = {
10
+ readonly status: 'restoring';
11
+ } | {
12
+ readonly status: 'unavailable';
13
+ readonly retryInMs: number;
14
+ }
15
+ /** pulse-auth answered outside its contract, or our own proxy refused: reported. */
16
+ | {
17
+ readonly status: 'broken';
18
+ readonly httpStatus: number;
19
+ }
20
+ /** No Web Locks or localStorage: tabs can't agree, so sign-in is refused. */
21
+ | {
22
+ readonly status: 'unsupported';
23
+ } | {
24
+ readonly status: 'signed-out';
25
+ } | {
26
+ readonly status: 'signed-in';
27
+ readonly principal: Principal;
28
+ /** Moves whenever who is signed in, or what they may do, changes. */
29
+ readonly revision: number;
30
+ };
31
+ /** A redeemed login link (§2.2): a union, so each case gets its own screen. */
32
+ export type RedeemResult = {
33
+ readonly kind: 'signed-in';
34
+ } | {
35
+ readonly kind: 'two-step';
36
+ } | {
37
+ readonly kind: 'two-step-setup';
38
+ readonly setup: SignInSetup;
39
+ } | {
40
+ readonly kind: 'dead-link';
41
+ } | {
42
+ readonly kind: 'no-account';
43
+ } | {
44
+ readonly kind: 'inactive';
45
+ } | {
46
+ readonly kind: 'proxy-refused';
47
+ } | {
48
+ readonly kind: 'unavailable';
49
+ } | {
50
+ readonly kind: 'unsupported';
51
+ }
52
+ /** A sign-out happened while this was in flight: the sign-out wins. */
53
+ | {
54
+ readonly kind: 'superseded';
55
+ } | {
56
+ readonly kind: 'unknown';
57
+ readonly status: number;
58
+ };
59
+ /** The private reader is called only as the entry hands the verify result to the page. */
60
+ export interface SupersededVerify {
61
+ readonly kind: 'superseded';
62
+ readonly codesAtDelivery?: () => readonly string[] | null;
63
+ }
64
+ /** A two-step code, checked (A4): the same rules as a redeem. */
65
+ export type TwoStepResult =
66
+ /** `backupCodes`: the ten codes, once, when this sign-in also set two-step up; otherwise null. */
67
+ {
68
+ readonly kind: 'signed-in';
69
+ readonly backupCodes: readonly string[] | null;
70
+ } | {
71
+ readonly kind: 'wrong-code';
72
+ }
73
+ /**
74
+ * Too many wrong codes: none is taken for `minutes`, so not before `until` (epoch ms), the
75
+ * latest the lock can end.
76
+ */
77
+ | {
78
+ readonly kind: 'locked';
79
+ readonly minutes: number;
80
+ readonly until: number;
81
+ } | {
82
+ readonly kind: 'expired';
83
+ } | {
84
+ readonly kind: 'no-account';
85
+ } | {
86
+ readonly kind: 'inactive';
87
+ } | {
88
+ readonly kind: 'proxy-refused';
89
+ } | {
90
+ readonly kind: 'unavailable';
91
+ } | {
92
+ readonly kind: 'unsupported';
93
+ } | SupersededVerify | {
94
+ readonly kind: 'unknown';
95
+ readonly status: number;
96
+ };
97
+ /** The token to send now, or why there is none. */
98
+ export type TokenFor = {
99
+ readonly kind: 'token';
100
+ readonly token: string;
101
+ readonly revision: number;
102
+ } | {
103
+ readonly kind: 'signed-out';
104
+ } | {
105
+ readonly kind: 'unavailable';
106
+ };
107
+ /** What became of a sign-out's first try to tell pulse-auth (#465, SDK-D1904). */
108
+ export type Told =
109
+ /** pulse-auth confirmed it: to this tab, or to another tab first (the flag gone, same generation). */
110
+ {
111
+ readonly kind: 'confirmed';
112
+ }
113
+ /**
114
+ * Not confirmed: it stays pending and is sent again, backing off. `unavailable`: no answer
115
+ * (offline, a 5xx or 429, the cookie lock not had in time); `refused`: a 401 or 403, which
116
+ * proves nothing about the cookie; `invalid`: an answer outside the contract, the proxy's
117
+ * refusal included, reported.
118
+ */
119
+ | {
120
+ readonly kind: 'unconfirmed';
121
+ readonly reason: 'unavailable' | 'refused' | 'invalid';
122
+ }
123
+ /**
124
+ * The record moved on since this sign-out (its serial, which a sign-out never moves): a sign-in,
125
+ * in any tab, or rarely a renewal by a tab that hadn't heard of it. That tab's cookie replaced
126
+ * the old one; it sent this logout first when it knew it was owed, and this call isn't told
127
+ * whether pulse-auth confirmed it. Nothing is pending for this sign-out; a sign-out made since
128
+ * is owed and sent on its own. Read `current()` (SDK-D1933, SDK-D2022).
129
+ */
130
+ | {
131
+ readonly kind: 'superseded';
132
+ }
133
+ /** No locks or storage: nobody could sign in here, so nothing was sent and nothing is pending. */
134
+ | {
135
+ readonly kind: 'unsupported';
136
+ };
137
+ /** A sign-out that is done here; `told` settles what pulse-auth said to the first try. */
138
+ export interface SignedOut {
139
+ readonly told: Promise<Told>;
140
+ }
141
+ /** After a 401: send again with a new token, or not at all. */
142
+ export type AfterUnauthorized = 'retry' | 'signed-out' | 'unavailable';
143
+ export interface SessionDeps {
144
+ readonly http: AuthHttp;
145
+ /** `navigator.locks`; null makes the browser unsupported. */
146
+ readonly locks: LockManagerLike | null;
147
+ /** `localStorage`; null makes the browser unsupported. */
148
+ readonly storage: StorageLike | null;
149
+ readonly channel: ChannelLike | null;
150
+ /** What the app's operator should know (report.ts): never a body, a token or a message's content. */
151
+ readonly report: (report: SessionReport) => void;
152
+ /**
153
+ * A subscriber's exception. It is the page's own bug, so it goes where the page's uncaught
154
+ * errors go, and never into `report` (SDK-D1910, SDK-D1915).
155
+ */
156
+ readonly reportError: (cause: unknown) => void;
157
+ readonly now: () => number;
158
+ /**
159
+ * The page's slot for a logout a closed session owed, which storage never took (owed.ts,
160
+ * sdk#483). Null where nothing can sign in (no locks or storage): never read or written there.
161
+ */
162
+ readonly owed: OwedSlot | null;
163
+ }
164
+ /** Refresh this long before the access token expires (pulse-auth issues 1-hour tokens). */
165
+ export declare const REFRESH_MARGIN_MS = 60000;
166
+ /**
167
+ * The session as the rest of the console sees it (`Auth.session`): its state and the
168
+ * actions a page takes. No credential: the token, the refreshes and the raw redeem stay in
169
+ * `AuthSession`, inside packages/platform/auth (A2).
170
+ */
171
+ export interface SessionView {
172
+ /** What is known now. The same object until something changes (useSyncExternalStore). */
173
+ current(): Session;
174
+ subscribe(listener: () => void): () => void;
175
+ /** Resolves once the first answer is in: anything but `restoring`. */
176
+ ready(): Promise<Session>;
177
+ /** On page load: the access token is gone, the refresh cookie decides. */
178
+ start(): void;
179
+ /** Try again, after `broken` or `unavailable`. */
180
+ retry(): void;
181
+ /** Whether a two-step code is being waited for, and until when: never the challenge. */
182
+ twoStepPending(): {
183
+ readonly expiresAt: number;
184
+ } | null;
185
+ /**
186
+ * With no code waited for: whether a sign-out or sign-in elsewhere ended the last challenge
187
+ * (true), or its five minutes or a lock did (false).
188
+ */
189
+ twoStepEndedByTab(): boolean;
190
+ /**
191
+ * `signal`, the caller's own: aborted while this waits for the cookie lock, nothing is sent
192
+ * (`unavailable`, and the caller reads its signal); once sent, the code is settled here.
193
+ * `onSend`: called right before the code goes out; from then on the call answers what
194
+ * pulse-auth said, whatever the signal does (SDK-D1931).
195
+ */
196
+ verifyTwoStep(code: string, signal?: AbortSignal, onSend?: () => void): Promise<TwoStepResult>;
197
+ /**
198
+ * Local first: resolves once this browser is signed out, every tab of it. pulse-auth is
199
+ * told after, under the cookie lock; `told` settles once it was asked (a logout it never
200
+ * confirmed stays pending and is sent again).
201
+ */
202
+ signOut(): Promise<SignedOut>;
203
+ /** Checks the shared state again: a tab coming back, a token asked for. */
204
+ sync(): Promise<void>;
205
+ /**
206
+ * Another tab wrote the shared state (the `storage` event for its key, or for every key): ends
207
+ * this tab's paid mark, then checks the shared state again (sdk#506).
208
+ */
209
+ stateWritten(): Promise<void>;
210
+ /**
211
+ * Ends this tab's session object: its timers stop and are never set again, it stops hearing the
212
+ * other tabs, closes its channel and tells its subscribers nothing more. What it holds stays
213
+ * readable (`current()`, `twoStepPending()`). A request already sent settles with its real
214
+ * outcome and still writes the shared state the other tabs read; a sign-in already sent that then
215
+ * loses (a sign-out won, or its 2xx can't be read) is still revoked with a `/logout` (SDK-D1941).
216
+ * Nothing new is sent (SDK-D1930): a call still waiting for the cookie lock answers
217
+ * `unavailable`, and a sign-out's try that hadn't gone out is `unconfirmed`, its pending logout
218
+ * left recorded. A call made after it sends nothing and throws, or rejects, with a
219
+ * `SessionClosedError`: a closed session is not used again. Safe to call twice. The shared state
220
+ * and the cookie are untouched: this is no sign-out.
221
+ *
222
+ * A logout owed only in this object's memory (one storage refused to record, a sign-in's revoke
223
+ * included) is not lost with it: it is left for the next session in this page that looks, which
224
+ * sends it before it restores. It is lost when it was never written down: the page closed while
225
+ * storage still refused, another tab that never heard of it restored the cookie first, or storage
226
+ * came back too shortly before to have taken it (a logout on the back-off is written at its next
227
+ * try, one kept without a timer at the next `sync()`; the `close()` row in `docs/first-party.md`).
228
+ *
229
+ * A `ready()` already waiting gets the answer of the restore under way; with none under
230
+ * way it rejects with a `SessionClosedError`. An `unavailable` it gets after the close isn't
231
+ * tried again.
232
+ */
233
+ close(): void;
234
+ }
235
+ /** The whole session, its credentials included: packages/platform/auth's own code only (A2). */
236
+ export interface AuthSession extends SessionView, SignInHooks {
237
+ /** Whether `close()` was called: what else sends on this session's behalf (the wire) reads it. */
238
+ isClosed(): boolean;
239
+ getAccessToken(): Promise<TokenFor>;
240
+ /** After a 401 on a request sent with `sent`. */
241
+ refreshAfterUnauthorized(sent: string): Promise<AfterUnauthorized>;
242
+ /** After a 403: the role comes from the token's claims, so ask for fresh ones, once a minute at most. */
243
+ refreshAfterForbidden(): Promise<void>;
244
+ /** `signal`: as for `verifyTwoStep`. */
245
+ redeem(linkToken: string, attemptId: string, signal?: AbortSignal): Promise<RedeemResult>;
246
+ /**
247
+ * Reads the current setup challenge while it is live. Clears it on expiry or marks it
248
+ * superseded on a generation change. Returns null for a plain verify or an ended challenge.
249
+ */
250
+ setupChallenge(): {
251
+ readonly challenge: string;
252
+ readonly expiresAt: number;
253
+ } | null;
254
+ /** Checks the original sign-in setup from this session before its secret can be requested or delivered. */
255
+ signInSetupChallenge(setup: SignInSetup): {
256
+ readonly kind: 'pending';
257
+ readonly challenge: string;
258
+ } | {
259
+ readonly kind: 'expired' | 'superseded';
260
+ };
261
+ /** After a password reset, ask pulse-auth whether the current browser session still stands. */
262
+ readonly askPulseAuthAgain: () => Promise<void>;
263
+ /**
264
+ * Whether `revision` is still who is signed in, by the shared state too: a sign-out or a
265
+ * changed principal another tab committed counts even before its message arrives.
266
+ */
267
+ isCurrent(revision: number): boolean;
268
+ /** Whether issued codes may reach the caller in the shared browser now. */
269
+ canDeliverCodes(userId: string): boolean;
270
+ }
@@ -0,0 +1,3 @@
1
+ /** Refresh this long before the access token expires (pulse-auth issues 1-hour tokens). */
2
+ export const REFRESH_MARGIN_MS = 60_000;
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/first-party/session/types.ts"],"names":[],"mappings":"AA0HA,2FAA2F;AAC3F,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAA"}
@@ -0,0 +1,31 @@
1
+ import type { Wire } from './wire.js';
2
+ /** A call straight to a host gives up after this long, as the main entry's calls do (30 s). */
3
+ export declare const OPEN_DEADLINE_MS = 30000;
4
+ export interface WireDeps {
5
+ /** The page's fetch: the one network edge. */
6
+ readonly fetch: typeof fetch;
7
+ /** The hosts' base URLs, checked when the session was made, with no trailing slash. */
8
+ readonly hosts: {
9
+ readonly auth: string;
10
+ readonly admin: string;
11
+ };
12
+ /** An answer outside a family's contract: the path and the status, never a body or a query. */
13
+ readonly report: (answer: {
14
+ readonly path: string;
15
+ readonly status: number;
16
+ }) => void;
17
+ readonly now: () => number;
18
+ /**
19
+ * Whether the session was closed: then `open` sends nothing and answers `unavailable`, as the
20
+ * session's own calls send nothing new after `close()` (SDK-D1930). A family's call made after
21
+ * the close rejecting comes with the first family's attachment (SDK-D1938).
22
+ */
23
+ readonly closed: () => boolean;
24
+ }
25
+ /** The shared clock, reporter and open sender used to compose a session's family wire. */
26
+ export type SessionWire = Pick<Wire, 'open' | 'report' | 'now'>;
27
+ /** A path a family sends to, on its host: '/oauth/device/verify'. Anything else is a programming error. */
28
+ export declare function checkPath(path: string): void;
29
+ /** The JSON a body holds, or undefined when there is none, or it isn't JSON. */
30
+ export declare function parsed(text: string): unknown;
31
+ export declare function createSessionWire(deps: WireDeps): SessionWire;