@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,66 @@
1
+ import type { Wire } from './wire.js';
2
+ /** The frozen setup returned for one sign-in. Pass this object unchanged to startSignInTwoStepSetup. */
3
+ export interface SignInSetup {
4
+ /** Identifies the handle accepted by startSignInTwoStepSetup. */
5
+ readonly kind: 'sign_in_setup';
6
+ /** When this sign-in challenge ends, in epoch milliseconds. */
7
+ readonly expires_at: number;
8
+ }
9
+ /** Where a sign-in stands after an answer that didn't refuse it. Epoch ms. (sdk#465) */
10
+ export type SignInStep = {
11
+ readonly kind: 'signed_in';
12
+ }
13
+ /** A code is wanted: `verifyTwoStep`. `expires_at`: when the challenge ends. */
14
+ | {
15
+ readonly kind: 'two_step';
16
+ readonly expires_at: number;
17
+ }
18
+ /**
19
+ * Two-step must be set up before this sign-in can finish, then `verifyTwoStep`. `expires_at`:
20
+ * when the challenge ends.
21
+ */
22
+ | {
23
+ readonly kind: 'two_step_setup';
24
+ readonly expires_at: number;
25
+ readonly setup: SignInSetup;
26
+ };
27
+ /**
28
+ * sdk#465 (the export): the landed tokens, the signed-in address and the set-up challenge.
29
+ * None of them touches the cookie lock, the stored record or the tabs' messages.
30
+ */
31
+ export interface SessionHooks {
32
+ /**
33
+ * The token the page was opened with, when its path is exactly that landing
34
+ * (`invitationLanding`, `resetLanding`); null otherwise. Taken once, when the session is made,
35
+ * and kept in memory: never in a result, a report or the app's hands.
36
+ */
37
+ readonly landedToken: (landing: 'invitation' | 'reset') => string | null;
38
+ /**
39
+ * The family is done with it: spent (accepted, password set) or dead (`dead_link`). From now on
40
+ * `landedToken` gives null and `openedInvitation()` / `openedReset()` say `no_token`. Not after
41
+ * `unavailable` or `aborted`: trying again may work.
42
+ */
43
+ readonly forgetLanded: (landing: 'invitation' | 'reset') => void;
44
+ /** The signed-in person's address (the token's `email`); null when nobody is signed in. */
45
+ readonly email: () => string | null;
46
+ /**
47
+ * Reads this tab's current setup challenge and its expiry. Returns null when none is live.
48
+ * Reading an expired challenge clears it. A changed shared generation ends the challenge as superseded.
49
+ */
50
+ readonly setupChallenge: () => {
51
+ readonly challenge: string;
52
+ readonly expires_at: number;
53
+ } | null;
54
+ }
55
+ /** What a family is handed for a session the entry made: its wire and its hooks. */
56
+ export interface SessionParts {
57
+ readonly wire: Wire;
58
+ readonly hooks: SessionHooks;
59
+ }
60
+ /** The entry, as it makes a session. */
61
+ export declare function registerParts(session: object, given: SessionParts): void;
62
+ /**
63
+ * A family's way in: the wire and hooks of `session`. A session the entry didn't make (a copy,
64
+ * a fake, a spread) is a programming error, a TypeError.
65
+ */
66
+ export declare function partsOf(session: object): SessionParts;
@@ -0,0 +1,19 @@
1
+ // Keyed by the object the entry handed the app, so only a session the entry made has parts, and
2
+ // the app, which holds the object, can't reach them: nothing it is given leads here.
3
+ const parts = new WeakMap();
4
+ /** The entry, as it makes a session. */
5
+ export function registerParts(session, given) {
6
+ parts.set(session, given);
7
+ }
8
+ /**
9
+ * A family's way in: the wire and hooks of `session`. A session the entry didn't make (a copy,
10
+ * a fake, a spread) is a programming error, a TypeError.
11
+ */
12
+ export function partsOf(session) {
13
+ const found = parts.get(session);
14
+ if (found === undefined) {
15
+ throw new TypeError('This is not a session createBrowserSession made: pass the session it returned, as it is.');
16
+ }
17
+ return found;
18
+ }
19
+ //# sourceMappingURL=hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../src/first-party/hooks.ts"],"names":[],"mappings":"AA2DA,gGAAgG;AAChG,qFAAqF;AACrF,MAAM,KAAK,GAAG,IAAI,OAAO,EAAwB,CAAA;AAEjD,wCAAwC;AACxC,MAAM,UAAU,aAAa,CAAC,OAAe,EAAE,KAAmB;IAChE,KAAK,CAAC,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;AAC3B,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,OAAO,CAAC,OAAe;IACrC,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;IAChC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,MAAM,IAAI,SAAS,CAAC,0FAA0F,CAAC,CAAA;IACjH,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC"}
@@ -0,0 +1,191 @@
1
+ import { z } from 'zod';
2
+ import { AUTH_PROXY_REFUSAL as PROXY_REFUSAL_HEADER, AUTH_PROXY_REFUSAL_VALUE as PROXY_REFUSAL_VALUE } from './authProxyMark.js';
3
+ import type { WireAnswer } from './wire.js';
4
+ import { type AccessToken } from './claims.js';
5
+ /**
6
+ * The same-origin path the console's own server forwards to pulse-auth (Vite, nginx): the
7
+ * default. A site of ours names its own (`authPath`, #465).
8
+ */
9
+ export declare const AUTH_PATH = "/auth";
10
+ /** The proxy marks its own refusals, so one is never read as pulse-auth's answer (§2.10). */
11
+ export { PROXY_REFUSAL_HEADER, PROXY_REFUSAL_VALUE };
12
+ /**
13
+ * How long a caller waits for a refresh before it hears `unavailable` (pulse-fe's 15 s). The
14
+ * refresh itself goes on: see LAST_RESORT_MS.
15
+ */
16
+ export declare const ABANDON_AFTER_MS = 15000;
17
+ /**
18
+ * pulse-auth's `TokenResponse`, the fields this console reads (checked against the spec).
19
+ * In cookie mode the refresh token is only ever in the httpOnly cookie: the body says
20
+ * "cookie" (or "" for an answer that set none), and carries no id token. An answer with a
21
+ * real token in its body breaks the contract the console's security rests on (§2.1: a
22
+ * long-lived credential script can read), so it is refused as outside the contract and
23
+ * reported, never used.
24
+ */
25
+ export declare const TokenResponseSchema: z.ZodObject<{
26
+ access_token: z.ZodString;
27
+ refresh_token: z.ZodOptional<z.ZodEnum<{
28
+ "": "";
29
+ cookie: "cookie";
30
+ }>>;
31
+ id_token: z.ZodOptional<z.ZodLiteral<"">>;
32
+ mfa_required: z.ZodOptional<z.ZodBoolean>;
33
+ mfa_enrollment_required: z.ZodOptional<z.ZodBoolean>;
34
+ mfa_challenge: z.ZodOptional<z.ZodString>;
35
+ mfa_backup_codes: z.ZodOptional<z.ZodArray<z.ZodString>>;
36
+ }, z.core.$loose>;
37
+ export type LinkRequestAnswer = {
38
+ readonly kind: 'sent';
39
+ } | {
40
+ readonly kind: 'unavailable';
41
+ }
42
+ /** A 2xx without "ok": the mail may have gone out all the same, so its attempt stays. */
43
+ | {
44
+ readonly kind: 'unsettled';
45
+ readonly httpStatus: number;
46
+ readonly issues: string;
47
+ }
48
+ /** The app's own server refused it (the proxy's own refusal mark): a deployment fault, no mail sent. */
49
+ | {
50
+ readonly kind: 'proxy-refused';
51
+ readonly httpStatus: number;
52
+ }
53
+ /** Outside pulse-auth's contract (a refused `continue_url`, HTML): no mail went out. */
54
+ | {
55
+ readonly kind: 'broken';
56
+ readonly httpStatus: number;
57
+ readonly issues: string;
58
+ };
59
+ export type RefreshAnswer = {
60
+ readonly kind: 'token';
61
+ readonly access: AccessToken;
62
+ }
63
+ /** A 401, or a 403 from pulse-auth itself: no session behind this cookie. */
64
+ | {
65
+ readonly kind: 'signed-out';
66
+ } | {
67
+ readonly kind: 'unavailable';
68
+ } | {
69
+ readonly kind: 'broken';
70
+ readonly httpStatus: number;
71
+ readonly issues: string;
72
+ };
73
+ /** A two-step challenge: memory only, never a URL or storage (A4). */
74
+ export interface TwoStepChallenge {
75
+ readonly challenge: string;
76
+ /** On this tab's clock: arrival + 5 minutes (pulse-auth's CHALLENGE_TTL). */
77
+ readonly expiresAt: number;
78
+ }
79
+ /** pulse-auth's two-step challenge lifetime (`mfa.py` CHALLENGE_TTL, 300 s). */
80
+ export declare const TWO_STEP_TTL_MS = 300000;
81
+ /** What a sign-in call (a redeem, a two-step verify) answered, before the session decides. */
82
+ export type SignInAnswer = {
83
+ readonly kind: 'signed-in';
84
+ readonly access: AccessToken;
85
+ /** The ten backup codes when this sign-in also set two-step up; otherwise null (#465). */
86
+ readonly backupCodes: readonly string[] | null;
87
+ } | {
88
+ readonly kind: 'two-step';
89
+ readonly challenge: TwoStepChallenge;
90
+ }
91
+ /** Two-step must be set up first, with this challenge: /mfa/enroll, then /mfa/verify (#465). */
92
+ | {
93
+ readonly kind: 'two-step-setup';
94
+ readonly challenge: TwoStepChallenge;
95
+ }
96
+ /** Redeem only: 400, the link is expired, used or malformed. */
97
+ | {
98
+ readonly kind: 'dead-link';
99
+ }
100
+ /** Two-step only: "That code is not right." */
101
+ | {
102
+ readonly kind: 'wrong-code';
103
+ }
104
+ /** Two-step only: five wrong codes lock two-step for five minutes. */
105
+ | {
106
+ readonly kind: 'locked';
107
+ }
108
+ /** Two-step only: the challenge is past its five minutes. */
109
+ | {
110
+ readonly kind: 'expired';
111
+ } | {
112
+ readonly kind: 'no-account';
113
+ } | {
114
+ readonly kind: 'inactive';
115
+ } | {
116
+ readonly kind: 'proxy-refused';
117
+ } | {
118
+ readonly kind: 'unavailable';
119
+ }
120
+ /**
121
+ * A 2xx this console can't use (not a token answer, or a body that broke off): pulse-auth
122
+ * may have set its cookie with it, so the session settles it before saying anything.
123
+ */
124
+ | {
125
+ readonly kind: 'unsettled';
126
+ readonly status: number;
127
+ readonly issues: string;
128
+ } | {
129
+ readonly kind: 'unknown';
130
+ readonly status: number;
131
+ readonly issues: string;
132
+ };
133
+ export type LogoutAnswer = {
134
+ readonly kind: 'confirmed';
135
+ }
136
+ /** Not confirmed, so the pending logout stays: try again later. */
137
+ | {
138
+ readonly kind: 'unconfirmed';
139
+ readonly reason: 'unavailable' | 'refused';
140
+ }
141
+ /** A 2xx outside the contract, or a proxy refusal: reported, and the flag stays. */
142
+ | {
143
+ readonly kind: 'broken';
144
+ readonly httpStatus: number;
145
+ readonly issues: string;
146
+ };
147
+ export type ProxySignInAnswer = {
148
+ readonly kind: 'refused';
149
+ readonly answer: WireAnswer;
150
+ }
151
+ /** `page`: an HTML page, the console's own from a misrouted address, never pulse-auth's. */
152
+ | {
153
+ readonly kind: 'received';
154
+ readonly token: SignInAnswer;
155
+ readonly body: unknown;
156
+ readonly page: boolean;
157
+ } | {
158
+ readonly kind: 'proxy-refused';
159
+ readonly status: number;
160
+ } | {
161
+ readonly kind: 'unavailable';
162
+ };
163
+ export interface AuthHttp {
164
+ sendSignIn(path: '/login' | '/invitations/accept', body: Readonly<Record<string, unknown>>): Promise<ProxySignInAnswer>;
165
+ /** `signal`, the caller's own, cuts the request (#465): no cookie rides on a link request. */
166
+ requestLink(email: string, continueUrl: string, signal?: AbortSignal): Promise<LinkRequestAnswer>;
167
+ refresh(): Promise<RefreshAnswer>;
168
+ redeem(linkToken: string): Promise<SignInAnswer>;
169
+ verifyTwoStep(challenge: string, code: string): Promise<SignInAnswer>;
170
+ logout(): Promise<LogoutAnswer>;
171
+ }
172
+ /**
173
+ * A status the call refused to follow. It matches any 3xx, or a browser's opaque redirect
174
+ * (status 0). In a browser a manual redirect arrives as status 0 and nothing else can,
175
+ * because any other status-0 answer makes `fetch` reject (SDK-D1962).
176
+ */
177
+ export declare function refusedRedirect(status: number): boolean;
178
+ /**
179
+ * The paths `send` uses, proved before a call. An origin that does not parse, or a path that
180
+ * would leave it, throws the same TypeError as a cookie call. The session runs this before
181
+ * it opens a channel, so that throw leaves nothing to close (SDK-D1934).
182
+ */
183
+ export declare function checkCookieCalls(authPath: string, origin: string): void;
184
+ /**
185
+ * `fetchFn` is the one network edge: tests hand in a fetch that serves recorded answers.
186
+ * `now` reads this tab's clock, for a challenge's arrival. `authPath`: the same-origin path the
187
+ * page's own server forwards to pulse-auth, checked by the caller (#465). `origin`, when the
188
+ * session has the page's, makes each call absolute there (#480). An origin that does not parse,
189
+ * or a path that would leave it, throws here.
190
+ */
191
+ export declare function createAuthHttp(fetchFn: typeof fetch, now: () => number, authPath?: string, origin?: string): AuthHttp;
@@ -0,0 +1,364 @@
1
+ import { z } from 'zod';
2
+ import { AUTH_PROXY_REFUSAL as PROXY_REFUSAL_HEADER, AUTH_PROXY_REFUSAL_VALUE as PROXY_REFUSAL_VALUE } from './authProxyMark.js';
3
+ import { readAccessToken } from './claims.js';
4
+ /*
5
+ * The calls the browser makes for its own session, through the console's same-origin
6
+ * `/auth` proxy (docs/architecture.md 2.10), which makes the host-only refresh cookie
7
+ * first-party. Each answer is read into one union with one meaning per case: a session
8
+ * that is merely unreachable is never told apart from one that ended by guessing.
9
+ */
10
+ /**
11
+ * The same-origin path the console's own server forwards to pulse-auth (Vite, nginx): the
12
+ * default. A site of ours names its own (`authPath`, #465).
13
+ */
14
+ export const AUTH_PATH = '/auth';
15
+ /** The proxy marks its own refusals, so one is never read as pulse-auth's answer (§2.10). */
16
+ export { PROXY_REFUSAL_HEADER, PROXY_REFUSAL_VALUE };
17
+ /**
18
+ * How long a caller waits for a refresh before it hears `unavailable` (pulse-fe's 15 s). The
19
+ * refresh itself goes on: see LAST_RESORT_MS.
20
+ */
21
+ export const ABANDON_AFTER_MS = 15_000;
22
+ /**
23
+ * No call to pulse-auth is abandoned before the proxy has answered: it bounds every `/auth`
24
+ * call at 30 s (504, `unavailable`), and pulse-auth may commit a call the browser gave up
25
+ * on (a spent link, a rotated or newly set cookie, a mail sent). So the cookie lock is held
26
+ * until a definite answer, and only this last resort aborts a request nothing answers, so
27
+ * the lock can't be held forever (§2.3).
28
+ */
29
+ const LAST_RESORT_MS = 60_000;
30
+ /**
31
+ * pulse-auth's `TokenResponse`, the fields this console reads (checked against the spec).
32
+ * In cookie mode the refresh token is only ever in the httpOnly cookie: the body says
33
+ * "cookie" (or "" for an answer that set none), and carries no id token. An answer with a
34
+ * real token in its body breaks the contract the console's security rests on (§2.1: a
35
+ * long-lived credential script can read), so it is refused as outside the contract and
36
+ * reported, never used.
37
+ */
38
+ export const TokenResponseSchema = z.looseObject({
39
+ access_token: z.string(),
40
+ refresh_token: z.enum(['cookie', '']).optional(),
41
+ id_token: z.literal('').optional(),
42
+ mfa_required: z.boolean().optional(),
43
+ mfa_enrollment_required: z.boolean().optional(),
44
+ mfa_challenge: z.string().optional(),
45
+ /** The ten backup codes, once: a verify that finished setting two-step up (#465). */
46
+ mfa_backup_codes: z.array(z.string()).optional(),
47
+ });
48
+ /** FastAPI's error body, `{ detail }`: the only words /mfa/verify refuses with. */
49
+ const DetailBody = z.object({ detail: z.string() });
50
+ /** pulse-auth's `/logout` contract: a 2xx with this body is the only proof (§2.3). */
51
+ const LogoutBody = z.object({ message: z.string() });
52
+ /** pulse-auth's two-step challenge lifetime (`mfa.py` CHALLENGE_TTL, 300 s). */
53
+ export const TWO_STEP_TTL_MS = 300_000;
54
+ /**
55
+ * The cookie call's URL. With the page's origin it is absolute on that origin, so a document
56
+ * base on another origin cannot move it (`<base href>` is resolved before a relative fetch).
57
+ * No origin given is the one relative case: the caller already resolves the path (the tests
58
+ * #464 moved). An origin that does not parse, or a path that would leave it, is a TypeError
59
+ * naming the value, thrown when the client is made.
60
+ */
61
+ function cookieCallUrl(authPath, path, origin) {
62
+ const relative = `${authPath}${path}`;
63
+ if (origin === undefined)
64
+ return relative;
65
+ let page;
66
+ try {
67
+ page = new URL(origin);
68
+ }
69
+ catch {
70
+ throw new TypeError(`a cookie call’s origin does not parse: ${origin}`);
71
+ }
72
+ const absolute = new URL(relative, origin);
73
+ if (absolute.origin !== page.origin) {
74
+ throw new TypeError(`a cookie call’s path would leave ${origin}: ${relative}`);
75
+ }
76
+ return absolute.href;
77
+ }
78
+ /**
79
+ * A status the call refused to follow. It matches any 3xx, or a browser's opaque redirect
80
+ * (status 0). In a browser a manual redirect arrives as status 0 and nothing else can,
81
+ * because any other status-0 answer makes `fetch` reject (SDK-D1962).
82
+ */
83
+ export function refusedRedirect(status) {
84
+ return status === 0 || (status >= 300 && status < 400);
85
+ }
86
+ /**
87
+ * The paths `send` uses, proved before a call. An origin that does not parse, or a path that
88
+ * would leave it, throws the same TypeError as a cookie call. The session runs this before
89
+ * it opens a channel, so that throw leaves nothing to close (SDK-D1934).
90
+ */
91
+ export function checkCookieCalls(authPath, origin) {
92
+ for (const path of ['/login-link', '/refresh', '/mfa/verify', '/logout', '/login']) {
93
+ cookieCallUrl(authPath, path, origin);
94
+ }
95
+ }
96
+ /**
97
+ * `fetchFn` is the one network edge: tests hand in a fetch that serves recorded answers.
98
+ * `now` reads this tab's clock, for a challenge's arrival. `authPath`: the same-origin path the
99
+ * page's own server forwards to pulse-auth, checked by the caller (#465). `origin`, when the
100
+ * session has the page's, makes each call absolute there (#480). An origin that does not parse,
101
+ * or a path that would leave it, throws here.
102
+ */
103
+ export function createAuthHttp(fetchFn, now, authPath = AUTH_PATH, origin) {
104
+ if (origin !== undefined)
105
+ checkCookieCalls(authPath, origin);
106
+ const send = async (path, body, abandonAfterMs, signal) => {
107
+ const giveUp = new AbortController();
108
+ const timer = setTimeout(() => {
109
+ giveUp.abort();
110
+ }, abandonAfterMs);
111
+ const cut = () => {
112
+ giveUp.abort();
113
+ };
114
+ if (signal?.aborted === true)
115
+ cut();
116
+ signal?.addEventListener('abort', cut, { once: true });
117
+ try {
118
+ const response = await fetchFn(cookieCallUrl(authPath, path, origin), {
119
+ method: 'POST',
120
+ credentials: 'same-origin',
121
+ // A redirect is a misconfigured proxy, not an outage (SDK-D1953): never followed,
122
+ // because a 307 would send the body again. The status it had is the answer.
123
+ redirect: 'manual',
124
+ signal: giveUp.signal,
125
+ // Always JSON, an empty object for the calls without a body: the proxy refuses
126
+ // anything else, which a plain cross-site form can't send (§2.10).
127
+ headers: { 'Content-Type': 'application/json' },
128
+ body: JSON.stringify(body),
129
+ });
130
+ if (response.headers.get(PROXY_REFUSAL_HEADER) === PROXY_REFUSAL_VALUE) {
131
+ return { kind: 'proxy-refused', status: response.status };
132
+ }
133
+ // Nothing to parse, and Node's 3xx may carry a body, so it is cancelled at once.
134
+ // A same-origin cookie call always sees this: status 0 in a browser, the 3xx in Node.
135
+ if (refusedRedirect(response.status)) {
136
+ await response.body?.cancel().catch(() => undefined);
137
+ return { kind: 'answer', status: response.status, body: { json: false }, page: false };
138
+ }
139
+ // The headers are in, a cookie with them: a body that breaks off is still an answer,
140
+ // one that can't be read, never "unreachable" (a click pulse-auth may not have seen).
141
+ // Unless the caller cut it: its call has answered `aborted`, and an unread body it cut is
142
+ // no answer outside the contract to report (#475's round 1, finding 10). Only a link
143
+ // request takes the caller's signal; no cookie rides on it.
144
+ const text = await response.text().catch(() => null);
145
+ if (text === null && signal?.aborted === true)
146
+ return { kind: 'unreachable' };
147
+ return {
148
+ kind: 'answer',
149
+ status: response.status,
150
+ body: text === null ? { json: false } : parseBody(text),
151
+ page: isPage(response),
152
+ };
153
+ }
154
+ catch {
155
+ // fetch rejects only when no answer came: offline, DNS, reset, or aborted.
156
+ return { kind: 'unreachable' };
157
+ }
158
+ finally {
159
+ clearTimeout(timer);
160
+ signal?.removeEventListener('abort', cut);
161
+ }
162
+ };
163
+ const signInAnswer = (exchange) => {
164
+ if (exchange.kind === 'unreachable')
165
+ return { kind: 'unavailable' };
166
+ if (exchange.kind === 'proxy-refused')
167
+ return { kind: 'proxy-refused' };
168
+ const { status, body } = exchange;
169
+ if (status >= 200 && status <= 299) {
170
+ const answer = status === 200 ? tokenAnswer(body, status) : null;
171
+ if (answer !== null && answer.kind !== 'unknown')
172
+ return answer;
173
+ return {
174
+ kind: 'unsettled',
175
+ status,
176
+ issues: answer?.issues ?? `HTTP ${String(status)}`,
177
+ };
178
+ }
179
+ if (status === 401)
180
+ return { kind: 'no-account' };
181
+ if (status === 403)
182
+ return { kind: 'inactive' };
183
+ if (status === 429 || (status >= 500 && status <= 599))
184
+ return { kind: 'unavailable' };
185
+ return { kind: 'unknown', status, issues: `HTTP ${String(status)}` };
186
+ };
187
+ const tokenAnswer = (body, status) => {
188
+ const parsed = body.json ? TokenResponseSchema.safeParse(body.value) : null;
189
+ if (parsed === null || !parsed.success) {
190
+ return { kind: 'unknown', status, issues: 'not a token response' };
191
+ }
192
+ const answer = parsed.data;
193
+ if (answer.mfa_required === true || answer.mfa_enrollment_required === true) {
194
+ // A set-up answer carries its challenge as a code's does (pulse-auth P:264-265): kept, it
195
+ // is what /mfa/enroll and then /mfa/verify are sent with (#465).
196
+ const setup = answer.mfa_enrollment_required === true;
197
+ const challenge = answer.mfa_challenge ?? '';
198
+ if (challenge === '') {
199
+ return {
200
+ kind: 'unknown',
201
+ status,
202
+ issues: setup ? 'two-step setup without a challenge' : 'two-step without a challenge',
203
+ };
204
+ }
205
+ return {
206
+ kind: setup ? 'two-step-setup' : 'two-step',
207
+ challenge: { challenge, expiresAt: now() + TWO_STEP_TTL_MS },
208
+ };
209
+ }
210
+ const read = readAccessToken(answer.access_token);
211
+ if (read.kind === 'invalid')
212
+ return { kind: 'unknown', status, issues: read.issues };
213
+ // pulse-auth sends `mfa_backup_codes: []` with every sign-in (the recorded redeem): only a
214
+ // verify that finished setting two-step up has codes in it.
215
+ const codes = answer.mfa_backup_codes ?? [];
216
+ return { kind: 'signed-in', access: read.access, backupCodes: codes.length > 0 ? codes : null };
217
+ };
218
+ return {
219
+ async requestLink(email, continueUrl, signal) {
220
+ const exchange = await send('/login-link', { email, continue_url: continueUrl }, LAST_RESORT_MS, signal);
221
+ if (exchange.kind === 'unreachable')
222
+ return { kind: 'unavailable' };
223
+ if (exchange.kind === 'proxy-refused') {
224
+ return { kind: 'proxy-refused', httpStatus: exchange.status };
225
+ }
226
+ const { status, body } = exchange;
227
+ if (status === 429 || (status >= 500 && status <= 599))
228
+ return { kind: 'unavailable' };
229
+ // pulse-auth answers the JSON string "ok", whether or not the address has an
230
+ // account. A 2xx without it, from pulse-auth, may still have mailed the link; the
231
+ // console's own page (a misrouted proxy) or a refusal means no mail went out.
232
+ if (status === 200 && body.json && body.value === 'ok')
233
+ return { kind: 'sent' };
234
+ if (status >= 200 && status <= 299 && !exchange.page) {
235
+ return {
236
+ kind: 'unsettled',
237
+ httpStatus: status,
238
+ issues: 'no "ok" from login-link',
239
+ };
240
+ }
241
+ return { kind: 'broken', httpStatus: status, issues: 'no "ok" from login-link' };
242
+ },
243
+ async refresh() {
244
+ const exchange = await send('/refresh', {}, LAST_RESORT_MS);
245
+ if (exchange.kind === 'unreachable')
246
+ return { kind: 'unavailable' };
247
+ if (exchange.kind === 'proxy-refused') {
248
+ return { kind: 'broken', httpStatus: exchange.status, issues: 'proxy refused' };
249
+ }
250
+ const { status, body } = exchange;
251
+ if (status === 401 || status === 403)
252
+ return { kind: 'signed-out' };
253
+ if (status === 429 || (status >= 500 && status <= 599))
254
+ return { kind: 'unavailable' };
255
+ if (status !== 200) {
256
+ return { kind: 'broken', httpStatus: status, issues: `HTTP ${String(status)}` };
257
+ }
258
+ const parsed = body.json ? TokenResponseSchema.safeParse(body.value) : null;
259
+ if (parsed === null || !parsed.success) {
260
+ return { kind: 'broken', httpStatus: status, issues: 'not a token response' };
261
+ }
262
+ const read = readAccessToken(parsed.data.access_token);
263
+ if (read.kind === 'invalid') {
264
+ return { kind: 'broken', httpStatus: status, issues: read.issues };
265
+ }
266
+ return { kind: 'token', access: read.access };
267
+ },
268
+ async sendSignIn(path, body) {
269
+ // Each path is its own literal, so the proxy list can read what this sends.
270
+ // Exhaustive, so a path added to the union is a compile error, not a send of the other path.
271
+ let exchange;
272
+ switch (path) {
273
+ case '/login':
274
+ exchange = await send('/login', body, LAST_RESORT_MS);
275
+ break;
276
+ case '/invitations/accept':
277
+ exchange = await send('/invitations/accept', body, LAST_RESORT_MS);
278
+ break;
279
+ default: {
280
+ const exhaustive = path;
281
+ return exhaustive;
282
+ }
283
+ }
284
+ if (exchange.kind === 'unreachable')
285
+ return { kind: 'unavailable' };
286
+ if (exchange.kind === 'proxy-refused')
287
+ return exchange;
288
+ const value = exchange.body.json ? exchange.body.value : undefined;
289
+ if (exchange.status < 200 || exchange.status >= 300) {
290
+ return { kind: 'refused', answer: { kind: 'answer', status: exchange.status, body: value } };
291
+ }
292
+ return { kind: 'received', token: signInAnswer(exchange), body: value, page: exchange.page };
293
+ },
294
+ async redeem(linkToken) {
295
+ const exchange = await send('/login-link', { token: linkToken }, LAST_RESORT_MS);
296
+ // A 400 is pulse-auth's "Invalid or expired token!": expired, used or malformed,
297
+ // one message and one fix (pulse-fe's dead link).
298
+ if (exchange.kind === 'answer' && exchange.status === 400)
299
+ return { kind: 'dead-link' };
300
+ return signInAnswer(exchange);
301
+ },
302
+ async verifyTwoStep(challenge, code) {
303
+ const exchange = await send('/mfa/verify', { challenge, code }, LAST_RESORT_MS);
304
+ if (exchange.kind === 'answer' && exchange.status === 400) {
305
+ return twoStepRefusal(exchange.body);
306
+ }
307
+ return signInAnswer(exchange);
308
+ },
309
+ async logout() {
310
+ const exchange = await send('/logout', {}, LAST_RESORT_MS);
311
+ if (exchange.kind === 'unreachable') {
312
+ return { kind: 'unconfirmed', reason: 'unavailable' };
313
+ }
314
+ if (exchange.kind === 'proxy-refused') {
315
+ return { kind: 'broken', httpStatus: exchange.status, issues: 'proxy refused' };
316
+ }
317
+ const { status, body } = exchange;
318
+ if (status >= 200 && status <= 299) {
319
+ const parsed = body.json ? LogoutBody.safeParse(body.value) : null;
320
+ if (parsed?.success === true)
321
+ return { kind: 'confirmed' };
322
+ return { kind: 'broken', httpStatus: status, issues: 'not a logout answer' };
323
+ }
324
+ // A 401 or 403 proves nothing about the cookie (§2.3): the flag stays.
325
+ if (status === 401 || status === 403)
326
+ return { kind: 'unconfirmed', reason: 'refused' };
327
+ if (status === 429 || (status >= 500 && status <= 599)) {
328
+ return { kind: 'unconfirmed', reason: 'unavailable' };
329
+ }
330
+ return { kind: 'broken', httpStatus: status, issues: `HTTP ${String(status)}` };
331
+ },
332
+ };
333
+ }
334
+ /**
335
+ * /mfa/verify refuses every failure with a 400 and words (`mfa.py`): the words are the
336
+ * only way to tell them apart. Unknown words are not guessed at.
337
+ */
338
+ function twoStepRefusal(body) {
339
+ const parsed = body.json ? DetailBody.safeParse(body.value) : null;
340
+ const detail = parsed?.success === true ? parsed.data.detail : '';
341
+ if (detail === 'That code is not right.')
342
+ return { kind: 'wrong-code' };
343
+ if (detail.startsWith('Too many wrong codes'))
344
+ return { kind: 'locked' };
345
+ if (detail.startsWith('The sign-in took too long'))
346
+ return { kind: 'expired' };
347
+ return { kind: 'unknown', status: 400, issues: 'an unknown two-step refusal' };
348
+ }
349
+ /** Whether an answer is an HTML page, which pulse-auth never sends. */
350
+ function isPage(response) {
351
+ return (response.headers.get('content-type') ?? '')
352
+ .toLowerCase()
353
+ .startsWith('text/html');
354
+ }
355
+ function parseBody(text) {
356
+ try {
357
+ const value = JSON.parse(text);
358
+ return { json: true, value };
359
+ }
360
+ catch {
361
+ return { json: false };
362
+ }
363
+ }
364
+ //# sourceMappingURL=http.js.map