@reventlessdev/reventless-seed 1.0.0-alpha.20 → 1.0.0-alpha.22

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 (154) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/lib/bs/.compiler.log +2 -2
  3. package/lib/bs/ReventlessSeed.cmi +0 -0
  4. package/lib/bs/ReventlessSeed.cmt +0 -0
  5. package/lib/bs/ReventlessSeed.mlmap +1 -0
  6. package/lib/bs/compiler-info.json +1 -1
  7. package/lib/bs/src/Seed-ReventlessSeed.cmi +0 -0
  8. package/lib/bs/src/Seed-ReventlessSeed.cmt +0 -0
  9. package/lib/bs/src/Seed.ast +0 -0
  10. package/lib/bs/src/Seed.res +8 -2
  11. package/lib/bs/src/Seed_Client-ReventlessSeed.cmi +0 -0
  12. package/lib/bs/src/Seed_Client-ReventlessSeed.cmj +0 -0
  13. package/lib/bs/src/Seed_Client-ReventlessSeed.cmt +0 -0
  14. package/lib/bs/src/Seed_Client.ast +0 -0
  15. package/lib/bs/src/Seed_Client.res +169 -21
  16. package/lib/bs/src/Seed_Client.res.mjs +142 -7
  17. package/lib/bs/src/Seed_Connect-ReventlessSeed.cmi +0 -0
  18. package/lib/bs/src/Seed_Connect-ReventlessSeed.cmj +0 -0
  19. package/lib/bs/src/Seed_Connect-ReventlessSeed.cmt +0 -0
  20. package/lib/bs/src/Seed_Connect.ast +0 -0
  21. package/lib/bs/src/Seed_Connect.res +258 -19
  22. package/lib/bs/src/Seed_Connect.res.mjs +158 -8
  23. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmi +0 -0
  24. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmj +0 -0
  25. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmt +0 -0
  26. package/lib/bs/src/Seed_Prompt.ast +0 -0
  27. package/lib/bs/src/Seed_Prompt.res +48 -23
  28. package/lib/bs/src/Seed_Prompt.res.mjs +33 -22
  29. package/lib/bs/src/Seed_Random-ReventlessSeed.cmi +0 -0
  30. package/lib/bs/src/Seed_Random-ReventlessSeed.cmt +0 -0
  31. package/lib/bs/src/Seed_Random.ast +0 -0
  32. package/lib/bs/src/Seed_Random.res +4 -2
  33. package/lib/bs/src/Seed_Runner-ReventlessSeed.cmi +0 -0
  34. package/lib/bs/src/Seed_Runner-ReventlessSeed.cmt +0 -0
  35. package/lib/bs/src/Seed_Runner.ast +0 -0
  36. package/lib/bs/src/Seed_Runner.res +7 -12
  37. package/lib/bs/src/Seed_Upload-ReventlessSeed.cmi +0 -0
  38. package/lib/bs/src/Seed_Upload-ReventlessSeed.cmt +0 -0
  39. package/lib/bs/src/Seed_Upload.ast +0 -0
  40. package/lib/bs/src/Seed_Upload.res +4 -2
  41. package/lib/bs/src/Seed_Users-ReventlessSeed.cmi +0 -0
  42. package/lib/bs/src/Seed_Users-ReventlessSeed.cmj +0 -0
  43. package/lib/bs/src/Seed_Users-ReventlessSeed.cmt +0 -0
  44. package/lib/bs/src/Seed_Users.ast +0 -0
  45. package/lib/bs/src/Seed_Users.res +30 -3
  46. package/lib/bs/src/Seed_Users.res.mjs +14 -1
  47. package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmi +0 -0
  48. package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmj +0 -0
  49. package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmt +0 -0
  50. package/lib/bs/tests/Seed_ClientTest.ast +0 -0
  51. package/lib/bs/tests/Seed_ClientTest.res +146 -16
  52. package/lib/bs/tests/Seed_ClientTest.res.mjs +102 -1
  53. package/lib/bs/tests/Seed_ConnectTest-ReventlessSeed.cmi +0 -0
  54. package/lib/bs/tests/Seed_ConnectTest-ReventlessSeed.cmj +0 -0
  55. package/lib/bs/tests/Seed_ConnectTest-ReventlessSeed.cmt +0 -0
  56. package/lib/bs/tests/Seed_ConnectTest.ast +0 -0
  57. package/lib/bs/tests/Seed_ConnectTest.res +64 -0
  58. package/lib/bs/tests/Seed_ConnectTest.res.mjs +56 -0
  59. package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmi +0 -0
  60. package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmt +0 -0
  61. package/lib/bs/tests/Seed_PromptTest.ast +0 -0
  62. package/lib/bs/tests/Seed_PromptTest.res +8 -12
  63. package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmi +0 -0
  64. package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmt +0 -0
  65. package/lib/bs/tests/Seed_RandomTest.ast +0 -0
  66. package/lib/bs/tests/Seed_RandomTest.res +130 -90
  67. package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmi +0 -0
  68. package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmt +0 -0
  69. package/lib/bs/tests/Seed_UsersTest.ast +0 -0
  70. package/lib/bs/tests/Seed_UsersTest.res +77 -2
  71. package/lib/bs/tests/Seed_UsersTest.res.mjs +45 -2
  72. package/lib/ocaml/.compiler.log +2 -2
  73. package/lib/ocaml/ReventlessSeed.cmi +0 -0
  74. package/lib/ocaml/ReventlessSeed.cmt +0 -0
  75. package/lib/ocaml/ReventlessSeed.mlmap +1 -0
  76. package/lib/ocaml/Seed-ReventlessSeed.cmi +0 -0
  77. package/lib/ocaml/Seed-ReventlessSeed.cmt +0 -0
  78. package/lib/ocaml/Seed.ast +0 -0
  79. package/lib/ocaml/Seed.res +8 -2
  80. package/lib/ocaml/Seed_Client-ReventlessSeed.cmi +0 -0
  81. package/lib/ocaml/Seed_Client-ReventlessSeed.cmj +0 -0
  82. package/lib/ocaml/Seed_Client-ReventlessSeed.cmt +0 -0
  83. package/lib/ocaml/Seed_Client.ast +0 -0
  84. package/lib/ocaml/Seed_Client.res +169 -21
  85. package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmi +0 -0
  86. package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmj +0 -0
  87. package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmt +0 -0
  88. package/lib/ocaml/Seed_ClientTest.ast +0 -0
  89. package/lib/ocaml/Seed_ClientTest.res +146 -16
  90. package/lib/ocaml/Seed_Connect-ReventlessSeed.cmi +0 -0
  91. package/lib/ocaml/Seed_Connect-ReventlessSeed.cmj +0 -0
  92. package/lib/ocaml/Seed_Connect-ReventlessSeed.cmt +0 -0
  93. package/lib/ocaml/Seed_Connect.ast +0 -0
  94. package/lib/ocaml/Seed_Connect.res +258 -19
  95. package/lib/ocaml/Seed_ConnectTest-ReventlessSeed.cmi +0 -0
  96. package/lib/ocaml/Seed_ConnectTest-ReventlessSeed.cmj +0 -0
  97. package/lib/ocaml/Seed_ConnectTest-ReventlessSeed.cmt +0 -0
  98. package/lib/ocaml/Seed_ConnectTest.ast +0 -0
  99. package/lib/ocaml/Seed_ConnectTest.res +64 -0
  100. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmi +0 -0
  101. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmj +0 -0
  102. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmt +0 -0
  103. package/lib/ocaml/Seed_Prompt.ast +0 -0
  104. package/lib/ocaml/Seed_Prompt.res +48 -23
  105. package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmi +0 -0
  106. package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmt +0 -0
  107. package/lib/ocaml/Seed_PromptTest.ast +0 -0
  108. package/lib/ocaml/Seed_PromptTest.res +8 -12
  109. package/lib/ocaml/Seed_Random-ReventlessSeed.cmi +0 -0
  110. package/lib/ocaml/Seed_Random-ReventlessSeed.cmt +0 -0
  111. package/lib/ocaml/Seed_Random.ast +0 -0
  112. package/lib/ocaml/Seed_Random.res +4 -2
  113. package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmi +0 -0
  114. package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmt +0 -0
  115. package/lib/ocaml/Seed_RandomTest.ast +0 -0
  116. package/lib/ocaml/Seed_RandomTest.res +130 -90
  117. package/lib/ocaml/Seed_Runner-ReventlessSeed.cmi +0 -0
  118. package/lib/ocaml/Seed_Runner-ReventlessSeed.cmt +0 -0
  119. package/lib/ocaml/Seed_Runner.ast +0 -0
  120. package/lib/ocaml/Seed_Runner.res +7 -12
  121. package/lib/ocaml/Seed_Upload-ReventlessSeed.cmi +0 -0
  122. package/lib/ocaml/Seed_Upload-ReventlessSeed.cmt +0 -0
  123. package/lib/ocaml/Seed_Upload.ast +0 -0
  124. package/lib/ocaml/Seed_Upload.res +4 -2
  125. package/lib/ocaml/Seed_Users-ReventlessSeed.cmi +0 -0
  126. package/lib/ocaml/Seed_Users-ReventlessSeed.cmj +0 -0
  127. package/lib/ocaml/Seed_Users-ReventlessSeed.cmt +0 -0
  128. package/lib/ocaml/Seed_Users.ast +0 -0
  129. package/lib/ocaml/Seed_Users.res +30 -3
  130. package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmi +0 -0
  131. package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmt +0 -0
  132. package/lib/ocaml/Seed_UsersTest.ast +0 -0
  133. package/lib/ocaml/Seed_UsersTest.res +77 -2
  134. package/package.json +2 -2
  135. package/src/Seed.res +8 -2
  136. package/src/Seed_Client.res +169 -21
  137. package/src/Seed_Client.res.mjs +142 -7
  138. package/src/Seed_Connect.res +258 -19
  139. package/src/Seed_Connect.res.mjs +158 -8
  140. package/src/Seed_Prompt.res +48 -23
  141. package/src/Seed_Prompt.res.mjs +33 -22
  142. package/src/Seed_Random.res +4 -2
  143. package/src/Seed_Runner.res +7 -12
  144. package/src/Seed_Upload.res +4 -2
  145. package/src/Seed_Users.res +30 -3
  146. package/src/Seed_Users.res.mjs +14 -1
  147. package/tests/Seed_ClientTest.res +146 -16
  148. package/tests/Seed_ClientTest.res.mjs +102 -1
  149. package/tests/Seed_ConnectTest.res +64 -0
  150. package/tests/Seed_ConnectTest.res.mjs +56 -0
  151. package/tests/Seed_PromptTest.res +8 -12
  152. package/tests/Seed_RandomTest.res +130 -90
  153. package/tests/Seed_UsersTest.res +77 -2
  154. package/tests/Seed_UsersTest.res.mjs +45 -2
@@ -15,15 +15,16 @@ open JestGlobals
15
15
 
16
16
  let errors = entries => JSON.Encode.array(entries->Array.map(JSON.Encode.object))
17
17
 
18
- let errorOfType = errorType =>
19
- Dict.fromArray([("errorType", JSON.Encode.string(errorType))])
18
+ let errorOfType = errorType => Dict.fromArray([("errorType", JSON.Encode.string(errorType))])
20
19
 
21
20
  describe("Seed_Client.isTransient:", () => {
22
21
  testSync("resends a fault on the endpoint's own side", () =>
23
- ["InternalFailure", "ServiceUnavailable", "Throttling", "TooManyRequestsException"]
24
- ->Array.forEach(t =>
25
- expect(Seed_Client.isTransient(errors([errorOfType(t)])))->toBe(true)
26
- )
22
+ [
23
+ "InternalFailure",
24
+ "ServiceUnavailable",
25
+ "Throttling",
26
+ "TooManyRequestsException",
27
+ ]->Array.forEach(t => expect(Seed_Client.isTransient(errors([errorOfType(t)])))->toBe(true))
27
28
  )
28
29
 
29
30
  // The exact payload the seed aborted on.
@@ -33,8 +34,8 @@ describe("Seed_Client.isTransient:", () => {
33
34
  })
34
35
 
35
36
  testSync("fails fast on an error that is an answer", () =>
36
- ["ValidationException", "Unauthorized", "FieldUndefined"]->Array.forEach(t =>
37
- expect(Seed_Client.isTransient(errors([errorOfType(t)])))->toBe(false)
37
+ ["ValidationException", "Unauthorized", "FieldUndefined"]->Array.forEach(
38
+ t => expect(Seed_Client.isTransient(errors([errorOfType(t)])))->toBe(false),
38
39
  )
39
40
  )
40
41
 
@@ -52,8 +53,9 @@ describe("Seed_Client.isTransient:", () => {
52
53
  // endpoint said errors but named none" into an endless resend.
53
54
  testSync("treats an empty or untyped error list as final", () => {
54
55
  expect(Seed_Client.isTransient(errors([])))->toBe(false)
55
- expect(Seed_Client.isTransient(errors([Dict.fromArray([("message", JSON.Encode.string("boom"))])])))
56
- ->toBe(false)
56
+ expect(
57
+ Seed_Client.isTransient(errors([Dict.fromArray([("message", JSON.Encode.string("boom"))])])),
58
+ )->toBe(false)
57
59
  expect(Seed_Client.isTransient(JSON.Encode.null))->toBe(false)
58
60
  })
59
61
  })
@@ -70,8 +72,15 @@ let b64url = payload =>
70
72
  ->NodeBuffer.fromStringUtf8
71
73
  ->NodeBuffer.toStringBase64Url
72
74
 
75
+ // A REAL JOSE header, not the literal string "header". The difference is the
76
+ // whole bug: `{"kid":…,"alg":"RS256"}` is base64url JSON and decodes exactly as
77
+ // the payload does, so `claims` took it as the claims set and found no groups
78
+ // and no id in any Cognito token ever issued. A fake header that happened not to
79
+ // decode is what let every assertion below pass against broken code.
80
+ let joseHeader = b64url(`{"kid":"abc123","alg":"RS256"}`)
81
+
73
82
  let jwt = claims =>
74
- `header.${b64url(JSON.stringify(JSON.Encode.object(Dict.fromArray(claims))))}.signature`
83
+ `${joseHeader}.${b64url(JSON.stringify(JSON.Encode.object(Dict.fromArray(claims))))}.signature`
75
84
 
76
85
  let localToken = claims =>
77
86
  `${b64url(JSON.stringify(JSON.Encode.object(Dict.fromArray(claims))))}.signature`
@@ -84,12 +93,41 @@ let clientWith = token => {
84
93
  c
85
94
  }
86
95
 
96
+ describe("Seed_Client.claims:", () => {
97
+ // The regression this file could not previously express. A JWT header is a
98
+ // decodable JSON object sitting in front of the payload, so "the first segment
99
+ // that parses" is the header on every real token — and the seed then reported
100
+ // no groups, printed no `Acting as:` line, and attached no identity to a
101
+ // refusal, which is exactly the diagnosis it exists to give.
102
+ testSync("skips the JOSE header and reads the payload behind it", () =>
103
+ expect(
104
+ clientWith(jwt([("sub", JSON.Encode.string("real-sub"))]))
105
+ ->Seed_Client.claims
106
+ ->Option.flatMap(c => c->Dict.get("sub"))
107
+ ->Option.flatMap(JSON.Decode.string),
108
+ )->toEqual(Some("real-sub"))
109
+ )
110
+
111
+ // The header is identified by `alg`, not by position — a two-segment local dev
112
+ // token has no header at all, and its payload must not be mistaken for one.
113
+ testSync("takes the first segment when there is no header", () =>
114
+ expect(
115
+ clientWith(localToken([("userId", JSON.Encode.string("local-shopper"))]))
116
+ ->Seed_Client.claims
117
+ ->Option.flatMap(c => c->Dict.get("userId"))
118
+ ->Option.flatMap(JSON.Decode.string),
119
+ )->toEqual(Some("local-shopper"))
120
+ )
121
+ })
122
+
87
123
  describe("Seed_Client.effectiveGroups:", () => {
88
124
  // A Cognito id token carries its payload in the second segment, a local dev
89
125
  // token in the first. Neither platform should have to announce which it is.
90
126
  testSync("reads the payload wherever the provider put it", () => {
91
127
  expect(
92
- clientWith(jwt([("cognito:groups", strings(["Admin", "Shopper"]))]))->Seed_Client.effectiveGroups,
128
+ clientWith(
129
+ jwt([("cognito:groups", strings(["Admin", "Shopper"]))]),
130
+ )->Seed_Client.effectiveGroups,
93
131
  )->toEqual(Some(["Admin", "Shopper"]))
94
132
  expect(
95
133
  clientWith(localToken([("groups", strings(["Merchandiser"]))]))->Seed_Client.effectiveGroups,
@@ -101,12 +139,64 @@ describe("Seed_Client.effectiveGroups:", () => {
101
139
  testSync("says nothing about a token it cannot read", () => {
102
140
  expect(clientWith("not-a-token")->Seed_Client.effectiveGroups)->toEqual(None)
103
141
  expect(
104
- Seed_Client.make(~config={endpoint: "http://example.invalid/graphql"})
105
- ->Seed_Client.effectiveGroups,
142
+ Seed_Client.make(
143
+ ~config={endpoint: "http://example.invalid/graphql"},
144
+ )->Seed_Client.effectiveGroups,
106
145
  )->toEqual(None)
107
146
  })
108
147
  })
109
148
 
149
+ // The id an `@owner` field is stamped with. A seed that cannot read it can only
150
+ // key owner-scoped rows to a literal, and a literal matches one platform's
151
+ // accounts at most — which is how a demo shopper came to be registered on a
152
+ // deployment where nobody can ever be that shopper.
153
+
154
+ describe("Seed_Client.callerId:", () => {
155
+ // A Cognito id token says `sub`; the local dev token is a base64url
156
+ // `Identity.t`, whose field is `userId`. Neither platform announces which.
157
+ testSync("reads the id under whichever name the provider gave it", () => {
158
+ expect(
159
+ clientWith(jwt([("sub", JSON.Encode.string("4275e4a4-00c1-70e1"))]))->Seed_Client.callerId,
160
+ )->toEqual(Some("4275e4a4-00c1-70e1"))
161
+ expect(
162
+ clientWith(
163
+ localToken([("userId", JSON.Encode.string("local-shopper"))]),
164
+ )->Seed_Client.callerId,
165
+ )->toEqual(Some("local-shopper"))
166
+ })
167
+
168
+ // `sub` first, so a token carrying both is read the way the pool that minted
169
+ // it means it.
170
+ testSync("prefers sub when a token carries both names", () =>
171
+ expect(
172
+ clientWith(
173
+ jwt([
174
+ ("sub", JSON.Encode.string("f215e4b4-30f1-700c")),
175
+ ("userId", JSON.Encode.string("local-shopper")),
176
+ ]),
177
+ )->Seed_Client.callerId,
178
+ )->toEqual(Some("f215e4b4-30f1-700c"))
179
+ )
180
+
181
+ // `None` is not an error — it means this cannot add anything, and the caller
182
+ // falls back to whatever the accounts file declared.
183
+ testSync("says nothing about a token it cannot read, or one naming no id", () => {
184
+ expect(clientWith("not-a-token")->Seed_Client.callerId)->toEqual(None)
185
+ expect(
186
+ clientWith(jwt([("cognito:groups", strings(["Admin"]))]))->Seed_Client.callerId,
187
+ )->toEqual(None)
188
+ expect(
189
+ Seed_Client.make(~config={endpoint: "http://example.invalid/graphql"})->Seed_Client.callerId,
190
+ )->toEqual(None)
191
+ })
192
+
193
+ // A claim of the wrong shape is not an id. Taking the array's first element
194
+ // would be a guess, and a guessed owner id seeds rows nobody can read.
195
+ testSync("refuses a claim that is not a single string", () =>
196
+ expect(clientWith(jwt([("sub", strings(["a", "b"]))]))->Seed_Client.callerId)->toEqual(None)
197
+ )
198
+ })
199
+
110
200
  describe("Seed_Client.identitySummary:", () => {
111
201
  // The case that cost the debugging: eligible in the user list, refused by the
112
202
  // token. Naming what it was narrowed FROM is what points at the role switch.
@@ -124,8 +214,9 @@ describe("Seed_Client.identitySummary:", () => {
124
214
  // An ordinary login is not narrowed and has nothing to explain.
125
215
  testSync("stays quiet about an unnarrowed token", () =>
126
216
  expect(
127
- clientWith(jwt([("cognito:groups", strings(["Admin", "Shopper"]))]))
128
- ->Seed_Client.identitySummary,
217
+ clientWith(
218
+ jwt([("cognito:groups", strings(["Admin", "Shopper"]))]),
219
+ )->Seed_Client.identitySummary,
129
220
  )->toEqual(Some("Admin, Shopper"))
130
221
  )
131
222
 
@@ -137,6 +228,45 @@ describe("Seed_Client.identitySummary:", () => {
137
228
  )
138
229
  })
139
230
 
231
+ // Whether a token is narrowed cannot be read off the group claim: a bearer
232
+ // carrying one group looks identical whether the account holds one role or five.
233
+ // `availableRoles` is written only when a role was chosen, so its presence IS
234
+ // the narrowing — and that is what decides whether the seed has anything to ask.
235
+
236
+ describe("Seed_Client.narrowedFrom:", () => {
237
+ testSync("names the membership a narrowed token was reduced from", () =>
238
+ expect(
239
+ clientWith(
240
+ jwt([
241
+ ("cognito:groups", strings(["Shopper"])),
242
+ ("availableRoles", JSON.Encode.string("Admin,Shopper")),
243
+ ]),
244
+ )->Seed_Client.narrowedFrom,
245
+ )->toEqual(Some(["Admin", "Shopper"]))
246
+ )
247
+
248
+ // The case that must NOT prompt: a single-role account is not narrowed, it is
249
+ // simply small. Asking it to choose would put a question in every run.
250
+ testSync("says nothing about an unnarrowed token", () => {
251
+ expect(
252
+ clientWith(jwt([("cognito:groups", strings(["Shopper"]))]))->Seed_Client.narrowedFrom,
253
+ )->toEqual(None)
254
+ expect(
255
+ clientWith(
256
+ jwt([("cognito:groups", strings(["Admin", "Shopper"]))]),
257
+ )->Seed_Client.narrowedFrom,
258
+ )->toEqual(None)
259
+ })
260
+
261
+ // An empty claim is not a narrowing to nothing — it is a claim carrying no
262
+ // information, and offering a menu of no roles would be a dead end.
263
+ testSync("treats an empty availableRoles as not narrowed", () =>
264
+ expect(
265
+ clientWith(jwt([("availableRoles", JSON.Encode.string(""))]))->Seed_Client.narrowedFrom,
266
+ )->toEqual(None)
267
+ )
268
+ })
269
+
140
270
  describe("Seed_Client.isDenied:", () => {
141
271
  // AppSync types it; the local server carries it in the message.
142
272
  testSync("recognises a refusal in either platform's vocabulary", () => {
Binary file
@@ -9,6 +9,45 @@
9
9
 
10
10
  open Seed_Types
11
11
 
12
+ // ── Active role ───────────────────────────────────────────────────────────────
13
+ //
14
+ // Both platforms narrow a token to the single role the caller last chose, and on
15
+ // Cognito that choice is STORED — so an operator who acted as one role in the
16
+ // host shell gets a narrowed token from the next seed login, days later, with
17
+ // nothing in the seed's own environment to explain it. That is a refusal on the
18
+ // first gated call and no way out of the terminal.
19
+
20
+ /**
21
+ * Applies an active-role choice, if the platform offers one. `None` clears the
22
+ * choice and widens back to the caller's full membership.
23
+ *
24
+ * Returns the fresh bearer when the platform mints one itself, and `None` when
25
+ * the choice is merely stored and a re-authentication is what picks it up. The
26
+ * two platforms genuinely differ here: the local server re-issues on the spot,
27
+ * while Cognito's pre-token-generation trigger reads the stored row on the next
28
+ * login, so nothing changes until the seed authenticates again.
29
+ *
30
+ * Supplied by the platform adapter for the reason `~login` is: this package must
31
+ * not learn which provider signed the token, and the routes are not alike — a
32
+ * GraphQL mutation on one, an HTTP endpoint on the other.
33
+ */
34
+ type roleSwitch = (~client: Seed_Client.t, ~role: option<string>) => promise<option<string>>
35
+
36
+ /** What a run should act as. `Full` is the default because seeding wants every
37
+ right the account has; `Narrowed` exists so a run can deliberately act as one
38
+ role — the only way to exercise what a restricted caller is refused. */
39
+ type roleChoice = Full | Narrowed(string)
40
+
41
+ // `SEED_ROLE`: a role name, or `full`/`all` to clear. Honoured whether or not the
42
+ // token arrived narrowed, so a deliberate narrowed run needs no TTY.
43
+ let roleFromEnv = (): option<roleChoice> =>
44
+ Seed_Prompt.envValue("SEED_ROLE")->Option.map(v =>
45
+ switch v->String.toLowerCase {
46
+ | "full" | "all" | "none" | "clear" => Full
47
+ | _ => Narrowed(v)
48
+ }
49
+ )
50
+
12
51
  // Under route B the client mints uploads through the domain API's `Upload_Presign`
13
52
  // mutation (authenticated by the same bearer as commands), so a connection needs no
14
53
  // upload endpoint — just the authenticated client and whether uploads are skipped this
@@ -17,35 +56,168 @@ type connection = {
17
56
  client: Seed_Client.t,
18
57
  uploadsSkipped: bool,
19
58
  label: string,
59
+ // Every account the platform's accounts file declares — `[]` when there was no
60
+ // file, since the `REVENTLESS_DEMO_USER`/`REVENTLESS_DEMO_PASSWORD` path
61
+ // bypasses it entirely. A data set resolves a demo owner through this, so the
62
+ // id it seeds is the id the platform stamps rather than a literal that only
63
+ // happens to match one platform's accounts.
64
+ accounts: array<Seed_Users.user>,
65
+ // The account this run authenticated as, and the id its bearer actually
66
+ // carries — which is the only id available when there is no accounts file.
67
+ caller: Seed_Users.user,
68
+ callerId: option<string>,
69
+ // The login this run authenticated with, kept so a data set can mint a second
70
+ // client for another account. An owner-scoped read is only ever verified by
71
+ // the account the rows belong to: the seeding client is elevated, and an
72
+ // elevated token answers for every owner at once.
73
+ login: (~username: string, ~password: string) => promise<string>,
74
+ // How this platform stores an active-role choice, when it offers one at all.
75
+ // `None` means the deployment has no such door (a unified-mode AWS API does
76
+ // not carry `Platform_SetActiveRole`), and a narrowing can then only be
77
+ // reported, not fixed from here.
78
+ roleSwitch: option<roleSwitch>,
20
79
  }
21
80
 
81
+ let announceIdentity = (client: Seed_Client.t, ~prefix: string="Acting as"): unit =>
82
+ switch Seed_Client.identitySummary(client) {
83
+ | Some(summary) => Console.log(`${prefix}: ${summary}`)
84
+ | None => ()
85
+ }
86
+
87
+ /**
88
+ * Applies a role choice and leaves `client` holding a bearer that reflects it.
89
+ *
90
+ * The re-authentication is not optional bookkeeping: on Cognito the stored row
91
+ * is read at token generation, so a run that set a role and kept its old bearer
92
+ * would carry on with exactly the token it just tried to change.
93
+ */
94
+ let applyRole = async (
95
+ ~client: Seed_Client.t,
96
+ ~roleSwitch: roleSwitch,
97
+ ~login: (~username: string, ~password: string) => promise<string>,
98
+ ~caller: Seed_Users.user,
99
+ ~role: option<string>,
100
+ ): unit => {
101
+ let minted = await roleSwitch(~client, ~role)
102
+ let token = switch minted {
103
+ | Some(fresh) => fresh
104
+ | None => await login(~username=caller.username, ~password=caller.password)
105
+ }
106
+ client->Seed_Client.useToken(token)
107
+ // A second line rather than a replacement, and worded so the pair reads as a
108
+ // change: the first says what the login gave, this says what the run proceeds
109
+ // under, and a reader needs both to see that the switch actually took.
110
+ client->announceIdentity(~prefix="Now acting as")
111
+ }
112
+
113
+ /**
114
+ * Chooses what the run acts as, when the platform lets it choose.
115
+ *
116
+ * Deliberately quiet on a healthy run: an unnarrowed token has nothing to ask
117
+ * about, so the common case gains no prompt. The menu appears only when the
118
+ * bearer arrived narrowed — the case where the account list says one thing and
119
+ * the token another, which is unreadable from the terminal without it.
120
+ *
121
+ * `SEED_ROLE` answers non-interactively and is honoured even when the token is
122
+ * NOT narrowed, which is what makes a deliberate restricted run possible: acting
123
+ * as one role is the only way to exercise what a restricted caller is refused.
124
+ *
125
+ * Without a TTY and without `SEED_ROLE` it proceeds untouched rather than
126
+ * throwing. A narrowing is not always fatal — narrowed to Admin is fine — so
127
+ * refusing to start would break runs that would have worked.
128
+ */
129
+ let resolveRole = async (
130
+ ~client: Seed_Client.t,
131
+ ~roleSwitch: option<roleSwitch>,
132
+ ~login: (~username: string, ~password: string) => promise<string>,
133
+ ~caller: Seed_Users.user,
134
+ ): unit =>
135
+ switch roleSwitch {
136
+ | None => ()
137
+ | Some(roleSwitch) =>
138
+ let narrowedFrom = Seed_Client.narrowedFrom(client)
139
+ let chosen = switch roleFromEnv() {
140
+ | Some(choice) => Some(choice)
141
+ | None =>
142
+ switch narrowedFrom {
143
+ | Some(available) if available->Array.length > 1 && Seed_Prompt.hasTty() =>
144
+ Console.log("")
145
+ Console.log(
146
+ "This token is narrowed to one role. Seeding needs every right the account has;",
147
+ )
148
+ Console.log("clearing also widens your host-shell session, which shares the stored choice.")
149
+ let options = Array.concat(
150
+ [(`full membership (${available->Array.join(", ")})`, Full)],
151
+ available->Array.map(r => (r, Narrowed(r))),
152
+ )
153
+ Some(await Seed_Prompt.select(~title="Act as:", ~options, ~defaultIndex=0))
154
+ | _ => None
155
+ }
156
+ }
157
+ switch chosen {
158
+ | None => ()
159
+ | Some(Full) => await applyRole(~client, ~roleSwitch, ~login, ~caller, ~role=None)
160
+ | Some(Narrowed(role)) =>
161
+ await applyRole(~client, ~roleSwitch, ~login, ~caller, ~role=Some(role))
162
+ }
163
+ }
164
+
22
165
  /**
23
166
  * Prompts for credentials, calls `login` to mint a bearer, and returns a
24
167
  * connection with that bearer already applied. `login` returns the bearer so a
25
168
  * single `useToken` path serves every provider.
169
+ *
170
+ * `roleSwitch` is the platform's active-role door, when it has one; supplying it
171
+ * is what lets a narrowed token be widened here instead of in a browser.
26
172
  */
27
173
  let make = async (
28
174
  ~label: string,
29
175
  ~endpoint: string,
30
176
  ~login: (~username: string, ~password: string) => promise<string>,
177
+ ~roleSwitch: option<roleSwitch>=?,
31
178
  ~localDefaults: bool=false,
32
179
  ): connection => {
33
180
  // `SEED_SKIP_UPLOADS` forces the upload phase to no-op — seed domain data fast, or
34
181
  // skip a broken/absent upload path without editing the data set. The data set reads
35
182
  // `connection.uploadsSkipped` and reports the skip.
36
183
  let uploadsSkipped = Seed_Upload.uploadsSkipped()
37
- let (username, password) = await Seed_Prompt.credentials(~localDefaults)
38
- let token = await login(~username, ~password)
184
+ let {caller, accounts} = await Seed_Prompt.credentials(~localDefaults)
185
+ let token = await login(~username=caller.username, ~password=caller.password)
39
186
  let client = Seed_Client.make(~config={endpoint: endpoint})
40
187
  client->Seed_Client.useToken(token)
41
188
  // What the bearer grants, which is not always what the account list showed: a
42
189
  // token narrowed to one role presents that role alone. Said at login so the
43
190
  // run starts from the identity it will actually be refused or served under.
44
- switch Seed_Client.identitySummary(client) {
45
- | Some(summary) => Console.log(`Acting as: ${summary}`)
46
- | None => ()
191
+ announceIdentity(client)
192
+ // Offered before anything is probed or written, so a narrowing is settled
193
+ // while the run can still be redirected rather than after a refusal.
194
+ await resolveRole(~client, ~roleSwitch, ~login, ~caller)
195
+ {
196
+ client,
197
+ uploadsSkipped,
198
+ label,
199
+ accounts,
200
+ caller,
201
+ callerId: Seed_Client.callerId(client),
202
+ login,
203
+ roleSwitch,
47
204
  }
48
- {client, uploadsSkipped, label}
205
+ }
206
+
207
+ /**
208
+ * A second authenticated client, for one of the accounts the platform's accounts
209
+ * file declares. Same endpoint, same login route, a different bearer.
210
+ *
211
+ * It exists so a data set can read back what it seeded as the account the rows
212
+ * belong to. The seeding client cannot answer that question: it is elevated, so
213
+ * an owner-scoped view counts every owner's rows for it and reads non-empty
214
+ * whether or not a single row is reachable by the person it was seeded for.
215
+ */
216
+ let clientFor = async (c: connection, ~account: Seed_Users.user): Seed_Client.t => {
217
+ let token = await c.login(~username=account.username, ~password=account.password)
218
+ let client = Seed_Client.make(~config={endpoint: Seed_Client.endpoint(c.client)})
219
+ client->Seed_Client.useToken(token)
220
+ client
49
221
  }
50
222
 
51
223
  /**
@@ -53,19 +225,77 @@ let make = async (
53
225
  * `/__inmemory/login` shape: POST `{username, password}` → `{token}`). Reuses
54
226
  * `Seed_Client.login` but hands the token back for `useToken`.
55
227
  */
56
- let viaLoginEndpoint = (~loginEndpoint: string) => async (
57
- ~username: string,
58
- ~password: string,
59
- ): string => {
60
- let client = Seed_Client.make(
61
- ~config={endpoint: loginEndpoint, loginEndpoint, username, password},
62
- )
63
- await Seed_Client.login(client)
64
- switch Seed_Client.currentToken(client) {
65
- | Some(token) => token
66
- | None => throw(Failed(`login at ${loginEndpoint} returned no token`))
228
+ let viaLoginEndpoint = (~loginEndpoint: string) =>
229
+ async (~username: string, ~password: string): string => {
230
+ let client = Seed_Client.make(
231
+ ~config={endpoint: loginEndpoint, loginEndpoint, username, password},
232
+ )
233
+ await Seed_Client.login(client)
234
+ switch Seed_Client.currentToken(client) {
235
+ | Some(token) => token
236
+ | None => throw(Failed(`login at ${loginEndpoint} returned no token`))
237
+ }
238
+ }
239
+
240
+ /**
241
+ * The local dev platform's active-role door: POST `/__inmemory/switch-role` with
242
+ * `{activeRole}` (null to clear) and the current bearer, which answers with a
243
+ * freshly minted one.
244
+ *
245
+ * The local server re-issues on the spot rather than storing a choice, so this
246
+ * returns the new token and no re-authentication follows. Its narrowing is also
247
+ * per-session, which is why a local seed never arrives narrowed on its own —
248
+ * this exists for a *deliberate* `SEED_ROLE` run, which is where a restricted
249
+ * caller's refusals can be exercised without a deployed stack.
250
+ */
251
+ let viaSwitchRoleEndpoint = (~switchRoleEndpoint: string): roleSwitch =>
252
+ async (~client: Seed_Client.t, ~role: option<string>) => {
253
+ let token = switch Seed_Client.currentToken(client) {
254
+ | Some(t) => t
255
+ | None => throw(Failed("switch-role: no bearer to present — log in first"))
256
+ }
257
+ // Clearing OMITS the key rather than sending `null`. The local handler reads
258
+ // its body through `Obj.magic` into an `option<string>`, and ReScript spells
259
+ // absence `undefined` — so a JSON `null` arrives as a present value and the
260
+ // server tries to act as a role literally named "null", refusing with
261
+ // `Cannot act as "null": not a group this user holds`. An absent key is the
262
+ // only thing it reads as "no role", which is what widens back to full.
263
+ let body = JSON.stringify(
264
+ JSON.Encode.object(
265
+ switch role {
266
+ | Some(r) => Dict.fromArray([("activeRole", JSON.Encode.string(r))])
267
+ | None => Dict.make()
268
+ },
269
+ ),
270
+ )
271
+ let res = try await Seed_Client.fetch(
272
+ switchRoleEndpoint,
273
+ {
274
+ method: "POST",
275
+ headers: Dict.fromArray([
276
+ ("content-type", "application/json"),
277
+ ("authorization", `Bearer ${token}`),
278
+ ]),
279
+ body,
280
+ },
281
+ ) catch {
282
+ | _ => throw(Failed(`switch-role: cannot reach ${switchRoleEndpoint}`))
283
+ }
284
+ let json = await res->Seed_Client.responseJson
285
+ if !(res->Seed_Client.responseOk) {
286
+ throw(
287
+ Failed(
288
+ `switch-role at ${switchRoleEndpoint} failed with HTTP ${res
289
+ ->Seed_Client.responseStatus
290
+ ->Int.toString}: ${JSON.stringify(json)}`,
291
+ ),
292
+ )
293
+ }
294
+ switch json->Seed_Client.field("token")->Option.flatMap(Seed_Client.asString) {
295
+ | Some(fresh) => Some(fresh)
296
+ | None => throw(Failed(`switch-role at ${switchRoleEndpoint} returned no token`))
297
+ }
67
298
  }
68
- }
69
299
 
70
300
  let envOr = (key: string, fallback: string): string =>
71
301
  Seed_Prompt.envValue(key)->Option.getOr(fallback)
@@ -82,12 +312,21 @@ let local = (~graphql=?, ~login=?, ()): (unit => promise<connection>) => {
82
312
  let endpoint =
83
313
  graphql->Option.getOr(envOr("REVENTLESS_GRAPHQL_ENDPOINT", "http://localhost:4000/graphql"))
84
314
  let loginEndpoint =
85
- login->Option.getOr(envOr("REVENTLESS_LOGIN_ENDPOINT", "http://localhost:4000/__inmemory/login"))
315
+ login->Option.getOr(
316
+ envOr("REVENTLESS_LOGIN_ENDPOINT", "http://localhost:4000/__inmemory/login"),
317
+ )
318
+ // The switch-role route sits beside the login one on the same server, so it is
319
+ // derived rather than configured — a second env var could name a different
320
+ // host from the login it is supposed to re-issue, which is the mismatch
321
+ // `LocalSeedTarget.loginFor` exists to prevent one level up.
322
+ let switchRoleEndpoint =
323
+ loginEndpoint->String.replace("/__inmemory/login", "/__inmemory/switch-role")
86
324
  () =>
87
325
  make(
88
326
  ~label="local",
89
327
  ~endpoint,
90
328
  ~login=viaLoginEndpoint(~loginEndpoint),
329
+ ~roleSwitch=viaSwitchRoleEndpoint(~switchRoleEndpoint),
91
330
  ~localDefaults=true,
92
331
  )
93
332
  }
Binary file
@@ -0,0 +1,64 @@
1
+ open JestGlobals
2
+
3
+ // `SEED_ROLE` is how a run says what to act as when nobody can answer a prompt —
4
+ // a CI run wedged by a stored narrowing, or a deliberate restricted run.
5
+ //
6
+ // It carries two different intentions in one variable, which is why the parsing
7
+ // is worth pinning: naming a role narrows to it, and the "give me everything"
8
+ // answer has several spellings because an operator setting it by hand will write
9
+ // whichever one comes to mind. Getting that wrong is not a typo — `SEED_ROLE=full`
10
+ // read as a role named "full" would ask the platform to act as a group nobody
11
+ // holds, and be refused.
12
+
13
+ let withEnv = (value: option<string>, f: unit => unit) => {
14
+ let key = "SEED_ROLE"
15
+ let previous = NodeProcess.env->Dict.get(key)
16
+ switch value {
17
+ | Some(v) => NodeProcess.env->Dict.set(key, v)
18
+ | None => NodeProcess.env->Dict.delete(key)
19
+ }
20
+ f()
21
+ switch previous {
22
+ | Some(v) => NodeProcess.env->Dict.set(key, v)
23
+ | None => NodeProcess.env->Dict.delete(key)
24
+ }
25
+ }
26
+
27
+ describe("Seed_Connect.roleFromEnv:", () => {
28
+ testSync("is absent when nothing asked for a role", () =>
29
+ withEnv(None, () => expect(Seed_Connect.roleFromEnv())->Expect.toEqual(None))
30
+ )
31
+
32
+ testSync("reads a named role as a narrowing to it", () =>
33
+ withEnv(
34
+ Some("Shopper"),
35
+ () =>
36
+ expect(Seed_Connect.roleFromEnv())->Expect.toEqual(Some(Seed_Connect.Narrowed("Shopper"))),
37
+ )
38
+ )
39
+
40
+ // Every spelling of "everything" an operator might reach for. They all mean
41
+ // clear the stored choice, which is what seeding wants.
42
+ testSync("takes each spelling of full membership as a clear", () =>
43
+ ["full", "all", "none", "clear", "FULL", "All"]->Array.forEach(
44
+ v =>
45
+ withEnv(
46
+ Some(v),
47
+ () => expect(Seed_Connect.roleFromEnv())->Expect.toEqual(Some(Seed_Connect.Full)),
48
+ ),
49
+ )
50
+ )
51
+
52
+ // Role names are case-sensitive to the platform (`mayActAs` compares exactly),
53
+ // so a named role is passed through untouched — only the clear vocabulary is
54
+ // case-folded.
55
+ testSync("passes a role name through with its case intact", () =>
56
+ withEnv(
57
+ Some("Merchandiser"),
58
+ () =>
59
+ expect(Seed_Connect.roleFromEnv())->Expect.toEqual(
60
+ Some(Seed_Connect.Narrowed("Merchandiser")),
61
+ ),
62
+ )
63
+ )
64
+ })
Binary file