@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,133 @@
1
+ // Device sign-in (sdk#471): a device shows a code; the person types it on an account page, sees the
2
+ // request, and approves or denies it. The look-up is open and the answer goes as the person, both
3
+ // straight to pulse-auth (SDK-D595). Rows written from source cite pulse-auth f3bf89c (SDK-D593).
4
+ import { z } from 'zod';
5
+ import { instant } from '../../api/time.js';
6
+ import { checkSignal } from '../../core/signal.js';
7
+ import { emptyFields, ok, stopped } from '../result.js';
8
+ import { isUnavailable } from '../wire.js';
9
+ const LOOKUP = '/oauth/device/verify';
10
+ const PRESS = '/oauth/device/approve';
11
+ export function deviceLookupRequest(input) {
12
+ const fields = emptyFields(input, ['user_code']);
13
+ if (fields.length > 0)
14
+ return { kind: 'refused', reason: 'empty', fields };
15
+ // oauth/ingress/fastapi/__init__.py:879-887. The service formats the code, so it goes as typed.
16
+ return { path: LOOKUP, method: 'GET', query: { user_code: input.user_code } };
17
+ }
18
+ export function deviceAnswerRequest(device, consent) {
19
+ // oauth/ingress/fastapi/__init__.py:307-312,889-903. A missing consent means true: always sent.
20
+ return { path: PRESS, method: 'POST', body: { user_code: device.user_code, consent } };
21
+ }
22
+ const ErrorBody = z.object({ error: z.string() });
23
+ const LookupBody = z.object({
24
+ user_code: z.string().min(1),
25
+ client_id: z.string().min(1),
26
+ client_name: z.string().transform((name) => (name === '' ? null : name)),
27
+ scopes: z.array(z.string()).readonly(),
28
+ // oauth_internal/__init__.py:782-783,834: the stored UTC time, microseconds included.
29
+ expires_at: instant.pipe(z.coerce.date()).transform((value) => value.getTime()),
30
+ }).readonly();
31
+ function errorCode(answer) {
32
+ if (answer.status !== 400)
33
+ return undefined;
34
+ return ErrorBody.safeParse(answer.body).data?.error;
35
+ }
36
+ export function readDeviceLookup(answer) {
37
+ if (answer.status === 200) {
38
+ // oauth_internal/__init__.py:819-835. An unresolved client has an empty name.
39
+ const parsed = LookupBody.safeParse(answer.body);
40
+ return parsed.success ? ok(parsed.data) : stopped({ kind: 'invalid' });
41
+ }
42
+ switch (errorCode(answer)) {
43
+ case 'invalid_grant':
44
+ return stopped({ kind: 'unknown_code' }); // oauth_internal/__init__.py:811-815
45
+ case 'access_denied':
46
+ return stopped({ kind: 'already_denied' }); // oauth_internal/__init__.py:816-817
47
+ default:
48
+ return stopped({ kind: isUnavailable(answer) ? 'unavailable' : 'invalid' });
49
+ }
50
+ }
51
+ export function readDeviceAnswer(answer, expected) {
52
+ // oauth_internal/__init__.py:859-874. Only the decision pressed confirms it.
53
+ if (answer.status === 200 && z.object({ status: z.literal(expected) }).safeParse(answer.body).success) {
54
+ return ok({ kind: expected });
55
+ }
56
+ if (errorCode(answer) === 'invalid_grant')
57
+ return stopped({ kind: 'unknown_code' }); // oauth_internal/__init__.py:852-856
58
+ // oauth_internal/__init__.py:848-851 answers access_denied on this route for a missing person: the
59
+ // wire sent one, so it is outside the contract.
60
+ return stopped({ kind: isUnavailable(answer) ? 'unavailable' : 'invalid' });
61
+ }
62
+ /**
63
+ * `signedIn` is the revision the page shows, or undefined when nobody is signed in: read at the
64
+ * press, as the wire pins the person there.
65
+ */
66
+ export function createDeviceCalls(wire, signedIn) {
67
+ // Only a request this session's look-up answered (frozen, SDK-D1922) can be answered: a copy, a
68
+ // forged one or another session's could approve a device the person never saw.
69
+ const shown = new WeakSet();
70
+ // One answer per code from this page, for one signed-in person (sdk#536 S2, K1, K2): pulse-auth
71
+ // f3bf89c's device_approve checks no earlier answer (oauth_internal/__init__.py:837-874), so a
72
+ // deny then an approve would answer `approved` while the device's poll is refused. A grant is
73
+ // its user_code, as pulse-auth formats it, so two look-ups of one code share one take. A press
74
+ // takes the code at once, for the revision shown then. It gives it back when it ended before its
75
+ // dispatch, or pulse-auth answered 429 or its unknown, expired or used code answer (sdk#539): each
76
+ // proves nothing was done. Any other press that went out may have acted (S1), so the code stays
77
+ // taken, and a press on it answers `answered`, until another person is signed in.
78
+ const taken = new Map();
79
+ async function answer(name, device, expected, options) {
80
+ if (!shown.has(device))
81
+ throw new TypeError(`${name}() takes the request lookUpDevice() answered on this session`);
82
+ checkSignal(name, options);
83
+ if (options?.signal?.aborted === true)
84
+ return stopped({ kind: 'aborted' });
85
+ const revision = signedIn();
86
+ const held = taken.get(device.user_code);
87
+ if (held !== undefined && held.revision === revision)
88
+ return stopped({ kind: 'answered' });
89
+ const take = { revision };
90
+ taken.set(device.user_code, take);
91
+ let doneNothing = true;
92
+ try {
93
+ const sent = await wire.asPerson(deviceAnswerRequest(device, expected === 'approved'), options, { onSend: () => { doneNothing = false; } });
94
+ if (sent.kind !== 'answer')
95
+ return stopped(sent);
96
+ if (sent.status === 429)
97
+ doneNothing = true;
98
+ const result = readDeviceAnswer(sent, expected);
99
+ if (!result.ok && result.error.kind === 'unknown_code')
100
+ doneNothing = true;
101
+ if (!result.ok && result.error.kind === 'invalid')
102
+ wire.report({ path: PRESS, status: sent.status });
103
+ return result;
104
+ }
105
+ finally {
106
+ // Only this press's own take: a press for a later person may have taken the code since.
107
+ if (doneNothing && taken.get(device.user_code) === take)
108
+ taken.delete(device.user_code);
109
+ }
110
+ }
111
+ return {
112
+ async lookUpDevice(input, options) {
113
+ checkSignal('lookUpDevice', options);
114
+ const request = deviceLookupRequest(input);
115
+ if ('kind' in request)
116
+ return stopped(request);
117
+ if (options?.signal?.aborted === true)
118
+ return stopped({ kind: 'aborted' });
119
+ const sent = await wire.open('auth', request, options);
120
+ if (sent.kind !== 'answer')
121
+ return stopped(sent);
122
+ const result = readDeviceLookup(sent);
123
+ if (result.ok)
124
+ shown.add(result.data);
125
+ else if (result.error.kind === 'invalid')
126
+ wire.report({ path: LOOKUP, status: sent.status });
127
+ return result;
128
+ },
129
+ approveDevice: (device, options) => answer('approveDevice', device, 'approved', options),
130
+ denyDevice: (device, options) => answer('denyDevice', device, 'denied', options),
131
+ };
132
+ }
133
+ //# sourceMappingURL=device.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"device.js","sourceRoot":"","sources":["../../../src/first-party/calls/device.ts"],"names":[],"mappings":"AAAA,oGAAoG;AACpG,kGAAkG;AAClG,kGAAkG;AAClG,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAClD,OAAO,EAAE,WAAW,EAAE,EAAE,EAAE,OAAO,EAAyE,MAAM,cAAc,CAAA;AAC9H,OAAO,EAAE,aAAa,EAAgD,MAAM,YAAY,CAAA;AAiFxF,MAAM,MAAM,GAAG,sBAAsB,CAAA;AACrC,MAAM,KAAK,GAAG,uBAAuB,CAAA;AAErC,MAAM,UAAU,mBAAmB,CAAC,KAAqC;IACvE,MAAM,MAAM,GA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C,YAAY,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,CAAC;KACjF,CAAA;AACH,CAAC"}
@@ -0,0 +1,244 @@
1
+ import type { SignInStep } from '../hooks.js';
2
+ import type { LandedTokens } from '../landed.js';
3
+ import { type CallOptions, type Refused, type Result, type Sent, type ThroughProxy } from '../result.js';
4
+ import type { SignInHooks } from '../signInHooks.js';
5
+ import { type Wire, type WireAnswer, type WireRequest } from '../wire.js';
6
+ /**
7
+ * Whether the page was opened with an invitation's token.
8
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#openedinvitation
9
+ */
10
+ export type OpenedInvitation = {
11
+ readonly kind: 'ready';
12
+ } | {
13
+ readonly kind: 'no_token';
14
+ };
15
+ /**
16
+ * What pulse-auth tells about a usable invitation.
17
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#previewinvitation
18
+ */
19
+ export interface InvitationShown {
20
+ /**
21
+ * The organisation the person joins; '' when pulse-auth could not read its name, or its deployment doesn't say (the hook that adds it is optional).
22
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#previewinvitation
23
+ */
24
+ readonly organization_name: string;
25
+ /**
26
+ * The deployment's own name, as pulse-auth calls itself.
27
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#previewinvitation
28
+ */
29
+ readonly app_name: string;
30
+ /**
31
+ * The roles the account is made with.
32
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#previewinvitation
33
+ */
34
+ readonly roles: readonly string[];
35
+ /**
36
+ * null when this deployment has no privacy policy to accept.
37
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#previewinvitation
38
+ */
39
+ readonly privacy_policy_url: string | null;
40
+ /**
41
+ * Where pulse-auth says this invitation goes on to after the accept; null when it names no app.
42
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#previewinvitation
43
+ */
44
+ readonly continue: {
45
+ readonly name: string;
46
+ readonly url: string;
47
+ readonly login_url: string;
48
+ } | null;
49
+ }
50
+ /**
51
+ * It names its recipient: mailed to that address, or handed over as a link made for it.
52
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#previewinvitation
53
+ */
54
+ export interface PersonalInvitation extends InvitationShown {
55
+ /**
56
+ * It names its recipient.
57
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#previewinvitation
58
+ */
59
+ readonly kind: 'personal';
60
+ /**
61
+ * The address it names, as pulse-auth holds it.
62
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#previewinvitation
63
+ */
64
+ readonly email: string;
65
+ }
66
+ /**
67
+ * It names nobody: whoever opens the link gives their own address and a password.
68
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#previewinvitation
69
+ */
70
+ export interface SharedInvitation extends InvitationShown {
71
+ /**
72
+ * It names nobody.
73
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#previewinvitation
74
+ */
75
+ readonly kind: 'shared';
76
+ }
77
+ /**
78
+ * What `previewInvitation` answers: narrow on `kind` before `acceptInvitation`.
79
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#previewinvitation
80
+ */
81
+ export type Invitation = PersonalInvitation | SharedInvitation;
82
+ /**
83
+ * What every accept gives.
84
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
85
+ */
86
+ export interface AcceptEntry {
87
+ /**
88
+ * Required: an empty one is `refused`.
89
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
90
+ */
91
+ readonly first_name: string;
92
+ /**
93
+ * Required: an empty one is `refused`.
94
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
95
+ */
96
+ readonly last_name: string;
97
+ /**
98
+ * Must be true when the invitation has a `privacy_policy_url`.
99
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
100
+ */
101
+ readonly privacy_policy_accepted: boolean;
102
+ }
103
+ /**
104
+ * In every arm the account exists, and this page is done with the invitation (spent, unless it allows more than one use).
105
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
106
+ */
107
+ export type Accepted = {
108
+ readonly kind: 'signed_in';
109
+ /**
110
+ * pulse-auth's fresh link into the app the invitation is for, or null.
111
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
112
+ */
113
+ readonly continue_to: string | null;
114
+ } | Exclude<SignInStep, {
115
+ readonly kind: 'signed_in';
116
+ }>;
117
+ /**
118
+ * Why `acceptInvitation` didn't sign the person in or ask for a code.
119
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
120
+ */
121
+ export type AcceptStop = ThroughProxy | Refused
122
+ /**
123
+ * The invitation is unknown, revoked, expired or spent: pulse-auth says one thing for all.
124
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
125
+ */
126
+ | {
127
+ readonly kind: 'dead_link';
128
+ } | {
129
+ readonly kind: 'privacy_policy_required';
130
+ }
131
+ /**
132
+ * The invitation was edited after it was previewed, and now needs an address or a password this call didn't carry.
133
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
134
+ */
135
+ | {
136
+ readonly kind: 'changed';
137
+ }
138
+ /**
139
+ * pulse-auth wants at least 8 characters, for any password given here.
140
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
141
+ */
142
+ | {
143
+ readonly kind: 'password_too_short';
144
+ }
145
+ /**
146
+ * The address already has an account: log in first.
147
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
148
+ */
149
+ | {
150
+ readonly kind: 'account_exists';
151
+ }
152
+ /**
153
+ * A sign-in or sign-out, in any tab, came after the press; read `current()`.
154
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
155
+ */
156
+ | {
157
+ readonly kind: 'superseded';
158
+ };
159
+ /**
160
+ * A personal accept: no address (the invitation's own wins), a password only if one is given.
161
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
162
+ */
163
+ export type PersonalAcceptEntry = AcceptEntry & {
164
+ readonly password?: string;
165
+ };
166
+ /**
167
+ * A shared accept: an address and a password, both required.
168
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
169
+ */
170
+ export type SharedAcceptEntry = AcceptEntry & {
171
+ readonly email: string;
172
+ readonly password: string;
173
+ };
174
+ /**
175
+ * Accepting signs the person in.
176
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
177
+ */
178
+ export interface AcceptInvitation {
179
+ /**
180
+ * A personal invitation: no address (its own wins); without a password, sign-in by link only.
181
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
182
+ */
183
+ (invitation: PersonalInvitation, entry: PersonalAcceptEntry, options?: CallOptions): Promise<Result<Accepted, AcceptStop>>;
184
+ /**
185
+ * A shared invitation: the person's own address and a password, both required.
186
+ * @see https://github.com/flipstream-io/sdk/blob/main/sdks/typescript/docs/first-party.md#acceptinvitation
187
+ */
188
+ (invitation: SharedInvitation, entry: SharedAcceptEntry, options?: CallOptions): Promise<Result<Accepted, AcceptStop>>;
189
+ }
190
+ /** Either form's entry, as the one implementation takes it. */
191
+ export type EitherAcceptEntry = AcceptEntry & {
192
+ readonly email?: string;
193
+ readonly password?: string;
194
+ };
195
+ /** What the calls need from the session beyond the wire. */
196
+ export interface InvitationSession {
197
+ readonly landedToken: LandedTokens['token'];
198
+ readonly forgetLanded: LandedTokens['forget'];
199
+ readonly signInThrough: SignInHooks['signInThrough'];
200
+ }
201
+ export declare function previewRequest(token: string): WireRequest;
202
+ export declare function readPreview(answer: WireAnswer): Result<Invitation, Sent | {
203
+ readonly kind: 'dead_link';
204
+ }>;
205
+ /**
206
+ * The accept's request, or why nothing is sent. Empty names, and a shared accept's empty address
207
+ * or password, are `refused`; a policy not accepted is `privacy_policy_required`.
208
+ */
209
+ export declare function acceptRequest(invitation: Invitation, entry: EitherAcceptEntry, token: string): Result<{
210
+ request: WireRequest;
211
+ email: string;
212
+ }, Refused | {
213
+ readonly kind: 'privacy_policy_required';
214
+ }>;
215
+ /**
216
+ * An accept's answer that is not a 2xx, for the invitation as it was previewed. pulse-auth reads its
217
+ * privacy setting live (hooks.py:98-99): its refusal to one previewed without a policy means the
218
+ * deployment changed since, so preview again.
219
+ */
220
+ export declare function acceptRefusal(answer: WireAnswer, previewed: Pick<InvitationShown, 'privacy_policy_url'>): AcceptStop;
221
+ /**
222
+ * The accept's `continue_link` (routes.py:47-53): '' or absent is null. One that is not an http or
223
+ * https link on the sent `continue_url`'s origin, with no username or password, is outside the
224
+ * contract: null, and reported.
225
+ */
226
+ export declare function continueTo(extra: Readonly<Record<string, unknown>>, sent: string | null): {
227
+ readonly kind: 'link';
228
+ readonly to: string | null;
229
+ } | {
230
+ readonly kind: 'outside';
231
+ };
232
+ export declare function createInvitationCalls(wire: Pick<Wire, 'open' | 'report'>, session: InvitationSession): {
233
+ openedInvitation: () => Readonly<{
234
+ readonly kind: "no_token";
235
+ }> | Readonly<{
236
+ readonly kind: "ready";
237
+ }>;
238
+ previewInvitation(opened: Extract<OpenedInvitation, {
239
+ readonly kind: "ready";
240
+ }>, options?: CallOptions): Promise<Result<Invitation, Sent | {
241
+ readonly kind: "dead_link";
242
+ }>>;
243
+ acceptInvitation: (invitation: Invitation, entry: EitherAcceptEntry, options?: CallOptions) => Promise<Result<Accepted, AcceptStop>>;
244
+ };
@@ -0,0 +1,258 @@
1
+ import { normalizeEmail } from '../claims.js';
2
+ import { landedProof } from '../landedProof.js';
3
+ import { z } from 'zod';
4
+ import { emptyFields, ok, stopped, } from '../result.js';
5
+ import { isUnavailable } from '../wire.js';
6
+ const PREVIEW = '/invitations/preview';
7
+ const ACCEPT = '/invitations/accept';
8
+ // accept.py:30, raised at 84-85, 212-213 and 242-263: one sentence for unknown, revoked, expired
9
+ // and spent.
10
+ const REFUSED = 'This invitation is not valid.';
11
+ // custom/domain/invitation/hooks.py:98-99.
12
+ const PRIVACY = 'Accept the privacy policy to continue.';
13
+ // accept.py:67-69 and 80-83: only when the record named nobody by the time of the accept.
14
+ const EMAIL_REQUIRED = 'An email address is required.';
15
+ const PASSWORD_REQUIRED = 'A password is required to accept this invitation.';
16
+ // custom/adapters/authentication_service/authentication/password.py:517-518, raised as
17
+ // domain/services.py:104-106's AUTHENTICATION_SERVICE_ERROR, a 400.
18
+ const TOO_SHORT = 'Password must be at least 8 characters!';
19
+ /**
20
+ * An absolute http or https URL with no username or password: what a page may link to or send
21
+ * back. Every link the SDK hands a page passes it.
22
+ */
23
+ function isWebUrl(value) {
24
+ try {
25
+ const url = new URL(value);
26
+ return (url.protocol === 'https:' || url.protocol === 'http:') && url.username === '' && url.password === '';
27
+ }
28
+ catch {
29
+ return false;
30
+ }
31
+ }
32
+ // queries/preview.py:24 and 31-52: every unusable invitation answers the same shape.
33
+ const Unusable = z.object({ usable: z.literal(false) });
34
+ // queries/preview.py:67-74 and custom/domain/invitation/hooks.py:60-71. An empty text and an
35
+ // absent one are read alike: pulse-auth writes '' where it has nothing.
36
+ const Usable = z.object({
37
+ usable: z.literal(true),
38
+ roles: z.array(z.string()),
39
+ email: z.string().optional(),
40
+ app_name: z.string(),
41
+ // extension.py:1-40: the previewing hook that adds it is optional.
42
+ organization_name: z.string().optional(),
43
+ privacy_policy_url: z.string().optional(),
44
+ continue_url: z.string().optional(),
45
+ continue_name: z.string().optional(),
46
+ login_url: z.string().optional(),
47
+ });
48
+ // The DomainException handler's shape: { code, message } with the exception's status.
49
+ const DomainError = z.object({ code: z.string(), message: z.string() });
50
+ export function previewRequest(token) {
51
+ return { method: 'GET', path: PREVIEW, query: { token } };
52
+ }
53
+ export function readPreview(answer) {
54
+ if (isUnavailable(answer))
55
+ return stopped({ kind: 'unavailable' });
56
+ if (answer.status !== 200)
57
+ return stopped({ kind: 'invalid' });
58
+ if (Unusable.safeParse(answer.body).success)
59
+ return stopped({ kind: 'dead_link' });
60
+ const parsed = Usable.safeParse(answer.body);
61
+ if (!parsed.success)
62
+ return stopped({ kind: 'invalid' });
63
+ const body = parsed.data;
64
+ const policy = body.privacy_policy_url ?? '';
65
+ if (policy !== '' && !isWebUrl(policy))
66
+ return stopped({ kind: 'invalid' });
67
+ // hooks.py:64-70 sets the three together or not at all.
68
+ const url = body.continue_url ?? '', name = body.continue_name ?? '', login = body.login_url ?? '';
69
+ const given = [url, name, login].filter((text) => text !== '').length;
70
+ if (given !== 0 && given !== 3)
71
+ return stopped({ kind: 'invalid' });
72
+ if (given === 3 && (!isWebUrl(url) || !isWebUrl(login)))
73
+ return stopped({ kind: 'invalid' });
74
+ const shown = {
75
+ organization_name: body.organization_name ?? '',
76
+ app_name: body.app_name,
77
+ roles: body.roles,
78
+ privacy_policy_url: policy === '' ? null : policy,
79
+ continue: given === 3 ? { name, url, login_url: login } : null,
80
+ };
81
+ // preview.py:71-73: a non-empty address names the recipient; '' is a link-style invitation.
82
+ const email = body.email ?? '';
83
+ return ok(email === '' ? { ...shown, kind: 'shared' } : { ...shown, kind: 'personal', email });
84
+ }
85
+ /**
86
+ * The accept's request, or why nothing is sent. Empty names, and a shared accept's empty address
87
+ * or password, are `refused`; a policy not accepted is `privacy_policy_required`.
88
+ */
89
+ export function acceptRequest(invitation, entry, token) {
90
+ const shared = invitation.kind === 'shared';
91
+ const required = shared ? ['first_name', 'last_name', 'email', 'password'] : ['first_name', 'last_name'];
92
+ const typed = { first_name: entry.first_name, last_name: entry.last_name, email: entry.email, password: entry.password };
93
+ const fields = [...emptyFields(typed, required, ['password'])];
94
+ // A personal invitation's password is optional, but one given at run time must be text.
95
+ const password = entry.password;
96
+ if (!shared && password !== undefined && typeof password !== 'string')
97
+ fields.push('password');
98
+ if (fields.length !== 0)
99
+ return stopped({ kind: 'refused', reason: 'empty', fields });
100
+ // hooks.py:98-99 refuses it; nothing is sent that pulse-auth would refuse for it.
101
+ if (invitation.privacy_policy_url !== null && entry.privacy_policy_accepted !== true) {
102
+ return stopped({ kind: 'privacy_policy_required' });
103
+ }
104
+ const body = {
105
+ token,
106
+ first_name: entry.first_name,
107
+ last_name: entry.last_name,
108
+ privacy_policy_accepted: entry.privacy_policy_accepted === true,
109
+ };
110
+ if (invitation.continue !== null)
111
+ body.continue_url = invitation.continue.url;
112
+ // accept.py:67: the record's own address wins, so a personal accept never sends one.
113
+ const email = normalizeEmail(invitation.kind === 'personal' ? invitation.email : entry.email ?? '');
114
+ if (shared)
115
+ body.email = email;
116
+ // accept.py:71-86: '' is no password, a sign-in by link only, for a personal invitation.
117
+ if (typeof entry.password === 'string' && entry.password !== '')
118
+ body.password = entry.password;
119
+ return ok({ request: { method: 'POST', path: ACCEPT, body }, email });
120
+ }
121
+ /**
122
+ * An accept's answer that is not a 2xx, for the invitation as it was previewed. pulse-auth reads its
123
+ * privacy setting live (hooks.py:98-99): its refusal to one previewed without a policy means the
124
+ * deployment changed since, so preview again.
125
+ */
126
+ export function acceptRefusal(answer, previewed) {
127
+ const parsed = DomainError.safeParse(answer.body);
128
+ const error = parsed.success ? parsed.data : null;
129
+ if (answer.status === 400 && error?.code === 'INVITATION_ERROR') {
130
+ switch (error.message) {
131
+ case REFUSED:
132
+ return { kind: 'dead_link' };
133
+ case PRIVACY:
134
+ return previewed.privacy_policy_url === null ? { kind: 'changed' } : { kind: 'privacy_policy_required' };
135
+ case EMAIL_REQUIRED:
136
+ case PASSWORD_REQUIRED:
137
+ return { kind: 'changed' };
138
+ }
139
+ }
140
+ if (answer.status === 400 && error?.code === 'AUTHENTICATION_SERVICE_ERROR' && error.message === TOO_SHORT) {
141
+ return { kind: 'password_too_short' };
142
+ }
143
+ // accept.py:33-40.
144
+ if (answer.status === 409 && error?.code === 'INVITATION_ACCOUNT_EXISTS')
145
+ return { kind: 'account_exists' };
146
+ return isUnavailable(answer) ? { kind: 'unavailable' } : { kind: 'invalid' };
147
+ }
148
+ /**
149
+ * The accept's `continue_link` (routes.py:47-53): '' or absent is null. One that is not an http or
150
+ * https link on the sent `continue_url`'s origin, with no username or password, is outside the
151
+ * contract: null, and reported.
152
+ */
153
+ export function continueTo(extra, sent) {
154
+ const link = extra.continue_link;
155
+ if (link === undefined || link === '')
156
+ return { kind: 'link', to: null };
157
+ // A blob: URL carries its page's origin, so the web URL check comes first.
158
+ if (typeof link !== 'string' || sent === null || !isWebUrl(link))
159
+ return { kind: 'outside' };
160
+ try {
161
+ return new URL(link).origin === new URL(sent).origin ? { kind: 'link', to: link } : { kind: 'outside' };
162
+ }
163
+ catch {
164
+ return { kind: 'outside' };
165
+ }
166
+ }
167
+ export function createInvitationCalls(wire, session) {
168
+ const proof = landedProof(() => session.landedToken('invitation'), 'previewInvitation() takes the ready invitation openedInvitation() returned, as it is');
169
+ // The invitations previewInvitation gave, so acceptInvitation reads only what pulse-auth said.
170
+ const shown = new WeakSet();
171
+ /**
172
+ * Why the token was forgotten, so an accept queued in the cookie lock answers why it was
173
+ * forgotten (sdk#549): used by an accept on this page (a 2xx), or dead (a dead refusal or a dead
174
+ * preview). A use is never overwritten: a 2xx proves it and always records, while a dead answer
175
+ * records only while the token is still landed (a preview sent before the use can be answered
176
+ * after it).
177
+ */
178
+ let gone = { kind: 'dead_link' };
179
+ const forget = (why) => {
180
+ if (why.kind === 'superseded' || session.landedToken('invitation') !== null)
181
+ gone = why;
182
+ session.forgetLanded('invitation');
183
+ };
184
+ async function acceptInvitation(invitation, entry, options) {
185
+ if (!shown.has(invitation)) {
186
+ throw new TypeError('acceptInvitation() takes an invitation previewInvitation() returned, as it is');
187
+ }
188
+ const token = session.landedToken('invitation');
189
+ const built = acceptRequest(invitation, entry, token ?? '');
190
+ if (!built.ok)
191
+ return built;
192
+ if (options?.signal?.aborted)
193
+ return stopped({ kind: 'aborted' });
194
+ // Spent or dead already: sending it again could only be refused.
195
+ if (token === null)
196
+ return stopped({ kind: 'dead_link' });
197
+ const { request, email } = built.data;
198
+ const sent = invitation.continue?.url ?? null;
199
+ // One request, one report, on the request's own result (SDK-D1949, SDK-D1976).
200
+ const onRefused = (answer) => {
201
+ if (acceptRefusal(answer, invitation).kind === 'invalid')
202
+ wire.report({ path: ACCEPT, status: answer.status });
203
+ };
204
+ // The token is the request's, not a caller's: read once, on the request's own result, whatever
205
+ // its callers did (an abort after the send, a joined second press). pulse-auth's answer shows
206
+ // the invitation used: any 2xx, whatever the session then made of it, or a dead refusal. Kept
207
+ // when that isn't known (no answer, a stop before the send) or pulse-auth gave the use back
208
+ // (accept.py:216-232).
209
+ const onSettled = (answer, heard) => {
210
+ if (heard.received)
211
+ forget({ kind: 'superseded' });
212
+ else if (answer.kind === 'refused' && acceptRefusal(answer.answer, invitation).kind === 'dead_link')
213
+ forget({ kind: 'dead_link' });
214
+ if (answer.kind === 'step' && answer.step.kind === 'signed_in' && continueTo(answer.extra, sent).kind === 'outside') {
215
+ wire.report({ path: ACCEPT, status: 200 });
216
+ }
217
+ };
218
+ // sdk#549: asked right before the send, inside the cookie lock. A token forgotten since the
219
+ // press means nothing is sent: `superseded` when an accept on this page used the invitation,
220
+ // `dead_link` when one, or a preview, found it dead.
221
+ const sendable = () => (session.landedToken('invitation') === token ? null : gone);
222
+ const result = await session.signInThrough(request, { step: 'accept', email, onRefused, onSettled, sendable }, options);
223
+ if (result.kind === 'step') {
224
+ if (result.step.kind !== 'signed_in')
225
+ return ok(result.step);
226
+ const link = continueTo(result.extra, sent);
227
+ return ok({ kind: 'signed_in', continue_to: link.kind === 'link' ? link.to : null });
228
+ }
229
+ if (result.kind !== 'refused')
230
+ return stopped(result);
231
+ return stopped(acceptRefusal(result.answer, invitation));
232
+ }
233
+ return {
234
+ openedInvitation: proof.opened,
235
+ async previewInvitation(opened, options) {
236
+ proof.assertIssued(opened);
237
+ if (options?.signal?.aborted)
238
+ return stopped({ kind: 'aborted' });
239
+ const token = session.landedToken('invitation');
240
+ if (token === null)
241
+ return stopped({ kind: 'dead_link' });
242
+ const answer = await wire.open('auth', previewRequest(token), options);
243
+ if (answer.kind !== 'answer')
244
+ return stopped(answer);
245
+ const result = readPreview(answer);
246
+ if (result.ok)
247
+ shown.add(result.data);
248
+ else if (result.error.kind === 'dead_link')
249
+ forget({ kind: 'dead_link' });
250
+ else if (result.error.kind === 'invalid')
251
+ wire.report({ path: PREVIEW, status: answer.status });
252
+ return result;
253
+ },
254
+ // One implementation answers both forms; the entry hands it out as `AcceptInvitation`.
255
+ acceptInvitation,
256
+ };
257
+ }
258
+ //# sourceMappingURL=invitation.js.map