@reventlessdev/reventless-registration 0.0.0-stage → 1.0.0-alpha.1

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 (40) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +50 -2
  3. package/package.json +38 -3
  4. package/rescript.json +28 -0
  5. package/src/ContactVerification.res +106 -0
  6. package/src/ContactVerification.res.mjs +60 -0
  7. package/src/ContactVerification_Guards.res +180 -0
  8. package/src/ContactVerification_Guards.res.mjs +97 -0
  9. package/src/Registration/ContactAddressId.res +6 -0
  10. package/src/Registration/ContactAddressId.res.mjs +29 -0
  11. package/src/Registration/InboundTranslation/CompleteRegistration.res +35 -0
  12. package/src/Registration/InboundTranslation/CompleteRegistration.res.mjs +58 -0
  13. package/src/Registration/InboundTranslation/CompleteRegistration_Translation.res +40 -0
  14. package/src/Registration/InboundTranslation/CompleteRegistration_Translation.res.mjs +67 -0
  15. package/src/Registration/InboundTranslation/SignUp.res +35 -0
  16. package/src/Registration/InboundTranslation/SignUp.res.mjs +57 -0
  17. package/src/Registration/InboundTranslation/SignUp_Translation.res +40 -0
  18. package/src/Registration/InboundTranslation/SignUp_Translation.res.mjs +66 -0
  19. package/src/Registration/OutboundTranslation/CreateRegisteredPrincipal.res +34 -0
  20. package/src/Registration/OutboundTranslation/CreateRegisteredPrincipal.res.mjs +61 -0
  21. package/src/Registration/OutboundTranslation/CreateRegisteredPrincipal_Translation.res +46 -0
  22. package/src/Registration/OutboundTranslation/CreateRegisteredPrincipal_Translation.res.mjs +99 -0
  23. package/src/Registration/OutboundTranslation/SendRegistrationChallenge.res +45 -0
  24. package/src/Registration/OutboundTranslation/SendRegistrationChallenge.res.mjs +78 -0
  25. package/src/Registration/OutboundTranslation/SendRegistrationChallenge_Translation.res +103 -0
  26. package/src/Registration/OutboundTranslation/SendRegistrationChallenge_Translation.res.mjs +150 -0
  27. package/src/Registration/StateChange/RegistrationPolicy.res +58 -0
  28. package/src/Registration/StateChange/RegistrationPolicy.res.mjs +86 -0
  29. package/src/Registration/StateChange/RegistrationPolicy_Decision.res +43 -0
  30. package/src/Registration/StateChange/RegistrationPolicy_Decision.res.mjs +88 -0
  31. package/src/Registration/StateChange/Registrations.res +157 -0
  32. package/src/Registration/StateChange/Registrations.res.mjs +224 -0
  33. package/src/Registration/StateChange/Registrations_Decision.res +281 -0
  34. package/src/Registration/StateChange/Registrations_Decision.res.mjs +541 -0
  35. package/src/Registration/StateViewStream/RegistrationWindows.res +27 -0
  36. package/src/Registration/StateViewStream/RegistrationWindows.res.mjs +77 -0
  37. package/src/Registration/StateViewStream/RegistrationWindows_Projection.res +7 -0
  38. package/src/Registration/StateViewStream/RegistrationWindows_Projection.res.mjs +38 -0
  39. package/src/RegistrationChallenge.res +20 -0
  40. package/src/RegistrationChallenge.res.mjs +45 -0
@@ -0,0 +1,281 @@
1
+ @@reventless.decision
2
+
3
+ // Claim, challenge, settle, complete. The challenge rules are the shared
4
+ // contact-verification ones, with no host to supersede the address: registration
5
+ // is the arm of those rules that `hostContact = None` exists for. A claim starts
6
+ // one of two ways — a visitor signing up, or a signed-in inviter — and ends the
7
+ // same way, at the one completion door.
8
+
9
+ module V = ContactVerification
10
+ module Guards = ContactVerification_Guards
11
+
12
+ type policyState =
13
+ /** Nothing recorded about the role: open if the app declares it
14
+ self-service, closed otherwise. */
15
+ | Unset
16
+ | Closed
17
+ | Open(option<Reventless.DateTime.t>)
18
+
19
+ type pending = {
20
+ /** When the claim was made, or its code last reissued: what a claim with no
21
+ challenge out lapses from. */
22
+ requestedAt: Reventless.DateTime.t,
23
+ /** What a settled proof grants, as the registration asked for it. */
24
+ roles: array<string>,
25
+ /** Who invited, for a claim an inviter made. */
26
+ invitedBy?: string,
27
+ challenge: option<Guards.challenge>,
28
+ proofHash: option<string>,
29
+ }
30
+
31
+ type stage =
32
+ | Unclaimed
33
+ | Pending(pending)
34
+ /** The proof settled and the account is being made. Still a claim: a second
35
+ registration must not start while the first is turning into an account. */
36
+ | Verified
37
+ | Completed
38
+
39
+ type state = {
40
+ admission: policyState,
41
+ stage: stage,
42
+ /** When a registered address was last told someone tried. */
43
+ lastNoticeAt: option<Reventless.DateTime.t>,
44
+ }
45
+
46
+ let initialState = {admission: Unset, stage: Unclaimed, lastNoticeAt: None}
47
+
48
+ /** At most one "someone tried" notice per address in this many seconds. The
49
+ caller is still answered as for a fresh address; only the mail is held back,
50
+ so the door cannot be used to flood a registered person's inbox. */
51
+ let noticeCooldown = 3600
52
+
53
+ let purposeOf = (p: pending) => RegistrationChallenge.purposeOf(~invitedBy=p.invitedBy)
54
+
55
+ /** The challenge rules a pending registration is held to: an invitation's code
56
+ holds longer than a sign-up's. */
57
+ let policyOf = (p: pending) => RegistrationChallenge.policyFor(purposeOf(p))
58
+
59
+ let evolve = (state, event) =>
60
+ switch event {
61
+ | RegistrationOpened({?until}) => {...state, admission: Open(until)}
62
+ | RegistrationClosed(_) => {...state, admission: Closed}
63
+ | RegistrationRequested({roles, requestedAt}) => {
64
+ ...state,
65
+ stage: Pending({requestedAt, roles, challenge: None, proofHash: None}),
66
+ }
67
+ | RegistrationInvited({roles, invitedBy, invitedAt}) => {
68
+ ...state,
69
+ stage: Pending({requestedAt: invitedAt, roles, invitedBy, challenge: None, proofHash: None}),
70
+ }
71
+ | RegistrationExpired(_) | RegistrationWithdrawn(_) => {...state, stage: Unclaimed}
72
+ | RegistrationAttemptedOnTakenAddress({attemptedAt}) => {
73
+ ...state,
74
+ lastNoticeAt: Some(attemptedAt),
75
+ }
76
+ | RegistrationChallengeIssued({email, proofHash, issuedAt}) =>
77
+ switch state.stage {
78
+ | Pending(p) => {
79
+ ...state,
80
+ stage: Pending({
81
+ ...p,
82
+ challenge: Some({
83
+ contact: ByEmail(Reventless.Email.unsafe(email->ContactAddressId.toString)),
84
+ purpose: purposeOf(p),
85
+ issuedAt,
86
+ attempts: 0,
87
+ settled: false,
88
+ }),
89
+ proofHash: Some(proofHash),
90
+ }),
91
+ }
92
+ | _ => state
93
+ }
94
+ // The code is void and its attempts with it; the claim waits for the next one
95
+ // from now.
96
+ | RegistrationChallengeReissued({reissuedAt}) =>
97
+ switch state.stage {
98
+ | Pending(p) => {
99
+ ...state,
100
+ stage: Pending({...p, requestedAt: reissuedAt, challenge: None, proofHash: None}),
101
+ }
102
+ | _ => state
103
+ }
104
+ // A refusal is a fact, so the attempt budget survives a replay.
105
+ | RegistrationProofRefused(_) =>
106
+ switch state.stage {
107
+ | Pending({challenge: Some(c)} as p) => {
108
+ ...state,
109
+ stage: Pending({...p, challenge: Some({...c, attempts: c.attempts + 1})}),
110
+ }
111
+ | _ => state
112
+ }
113
+ | RegistrationProofAccepted(_) => {...state, stage: Verified}
114
+ | RegistrationCompleted(_) => {...state, stage: Completed}
115
+ }
116
+
117
+ /** Whether the policy admits a registration for `role` that arrived at `at`.
118
+ A role nobody has recorded anything about is open exactly when it is declared
119
+ self-service; past an open window's end it refuses exactly as closed does,
120
+ with no event to close it. */
121
+ let admits = (admission, ~table, ~role, ~at) =>
122
+ switch admission {
123
+ | Unset => table->Reventless.Admission.isSelfService(role)
124
+ | Closed => false
125
+ | Open(None) => true
126
+ | Open(Some(until)) => Reventless.DateTime.isBefore(at, until)
127
+ }
128
+
129
+ /** Whether the app declares `role` as one a person is invited to. */
130
+ let isByInvitation = (table, role) =>
131
+ switch table->Reventless.Admission.strategyOf(role) {
132
+ | Invitation(_) => true
133
+ | SelfService | Provisioned => false
134
+ }
135
+
136
+ /** A pending registration nobody finished stops holding the address. Measured
137
+ from its challenge when one went out, else from the claim or the last reissue. */
138
+ let hasLapsed = (p: pending, ~now) =>
139
+ switch p.challenge {
140
+ | Some(c) => Guards.hasExpired(c, ~now, ~policy=policyOf(p))
141
+ | None => Guards.elapsedSeconds(~from=p.requestedAt, ~to_=now) >= policyOf(p).tokenTtl
142
+ }
143
+
144
+ let refusalReason = (r: Guards.refusal) =>
145
+ switch r {
146
+ | AddressSuperseded => "AddressSuperseded"
147
+ | ChallengeExpired => "ChallengeExpired"
148
+ | AlreadySettled => "AlreadySettled"
149
+ | AttemptsExhausted => "AttemptsExhausted"
150
+ | ProofMismatch => "ProofMismatch"
151
+ }
152
+
153
+ /** The decision over the app's table, which says which roles are self-service
154
+ and which are by invitation. */
155
+ module Make = (App: Reventless.Admission.Declaration) => {
156
+ module Spec = Spec
157
+ let moduleUrl = moduleUrl
158
+
159
+ type state = state
160
+ let initialState = initialState
161
+ let evolve = evolve
162
+
163
+ let decide = (state, command) =>
164
+ switch command {
165
+ // 🚨 Every arm a visitor reaches answers `Ok`, and the order is the security
166
+ // decision: the policy is consulted before the address, so a closed
167
+ // deployment sends nothing — not even the notice to an address that is
168
+ // already registered.
169
+ | Register({email, role, requestedAt}) =>
170
+ if !admits(state.admission, ~table=App.table, ~role, ~at=requestedAt) {
171
+ Ok([])
172
+ } else {
173
+ let roles = [(role :> string)]
174
+ switch state.stage {
175
+ // The caller is told nothing different; the address is told someone tried.
176
+ | Completed =>
177
+ switch state.lastNoticeAt {
178
+ | Some(last) if Guards.elapsedSeconds(~from=last, ~to_=requestedAt) < noticeCooldown =>
179
+ Ok([])
180
+ | _ => Ok([RegistrationAttemptedOnTakenAddress({email, attemptedAt: requestedAt})])
181
+ }
182
+ | Verified => Ok([])
183
+ // A visitor cannot take over a claim, an inviter's included, while it holds.
184
+ | Pending(p) if !hasLapsed(p, ~now=requestedAt) => Ok([])
185
+ | Pending(_) =>
186
+ Ok([
187
+ RegistrationExpired({email: email}),
188
+ RegistrationRequested({email, roles, requestedAt}),
189
+ ])
190
+ | Unclaimed => Ok([RegistrationRequested({email, roles, requestedAt})])
191
+ }
192
+ }
193
+
194
+ // The policy is not consulted: it says whether visitors may sign themselves
195
+ // up, and an invitation is not that. The table is, because it is the hard
196
+ // cap on how each role is admitted.
197
+ | Invite({email, roles, invitedBy, requestedAt}) =>
198
+ switch roles->Array.find(r => !isByInvitation(App.table, r)) {
199
+ | Some(role) => Error(NotByInvitation({role: (role :> string)}))
200
+ | None if roles == [] => Error(NoRoleToGrant)
201
+ | None =>
202
+ let invited = RegistrationInvited({
203
+ email,
204
+ roles: roles->Array.map(r => (r :> string)),
205
+ invitedBy,
206
+ invitedAt: requestedAt,
207
+ })
208
+ switch state.stage {
209
+ // An address that holds an account, or is about to, is not registered
210
+ // twice; granting it roles is not registration.
211
+ | Completed | Verified => Ok([])
212
+ | Pending(p) if !hasLapsed(p, ~now=requestedAt) => Ok([])
213
+ | Pending(_) => Ok([RegistrationExpired({email: email}), invited])
214
+ | Unclaimed => Ok([invited])
215
+ }
216
+ }
217
+
218
+ | IssueRegistrationChallenge({email, proofHash, issuedAt}) =>
219
+ switch state.stage {
220
+ | Pending(p) =>
221
+ switch Guards.onIssueRequested(
222
+ ~contact=ByEmail(Reventless.Email.unsafe(email->ContactAddressId.toString)),
223
+ ~existing=p.challenge,
224
+ ~now=issuedAt,
225
+ ~policy=policyOf(p),
226
+ ) {
227
+ | Issue => Ok([RegistrationChallengeIssued({email, proofHash, issuedAt})])
228
+ // A redelivered request mints nothing, and because nothing is emitted,
229
+ // sends nothing either.
230
+ | AlreadyOpen => Ok([])
231
+ }
232
+ | _ => Ok([])
233
+ }
234
+
235
+ // Only a code that went out can be voided — one still on its way would be
236
+ // replaced by a second in flight, and the first to arrive would be the one
237
+ // recorded. And not twice within the resend cooldown, so a door that
238
+ // reaches this cannot be used to flood an inbox.
239
+ | ReissueRegistrationChallenge({email, requestedAt}) =>
240
+ switch state.stage {
241
+ | Pending({challenge: Some(c)} as p)
242
+ if Guards.elapsedSeconds(~from=c.issuedAt, ~to_=requestedAt) >=
243
+ policyOf(p).resendCooldown =>
244
+ Ok([
245
+ RegistrationChallengeReissued({email, reissuedAt: requestedAt, invitedBy: ?p.invitedBy}),
246
+ ])
247
+ | _ => Ok([])
248
+ }
249
+
250
+ | SubmitRegistrationProof({email, proofHash, presentedAt}) =>
251
+ switch state.stage {
252
+ | Pending({challenge: Some(c), proofHash: Some(held), roles} as p) =>
253
+ switch Guards.onProofPresented(
254
+ c,
255
+ ~hostContact=None,
256
+ ~proofMatches=proofHash == held,
257
+ ~now=presentedAt,
258
+ ~policy=policyOf(p),
259
+ ) {
260
+ | Settle => Ok([RegistrationProofAccepted({email, roles, proofHash})])
261
+ | Refuse(reason) => Ok([RegistrationProofRefused({email, reason: refusalReason(reason)})])
262
+ }
263
+ // No challenge to answer: nothing to count it against, and nothing to say.
264
+ | _ => Ok([])
265
+ }
266
+
267
+ // Only a claim nobody has proved: once a proof settled, an account is on
268
+ // its way, and withdrawing the claim would not stop it.
269
+ | WithdrawRegistration({email, withdrawnAt}) =>
270
+ switch state.stage {
271
+ | Pending(_) => Ok([RegistrationWithdrawn({email, withdrawnAt})])
272
+ | Unclaimed | Verified | Completed => Ok([])
273
+ }
274
+
275
+ | RecordRegistrationCompleted({email}) =>
276
+ switch state.stage {
277
+ | Verified => Ok([RegistrationCompleted({email: email})])
278
+ | _ => Ok([])
279
+ }
280
+ }
281
+ }