@coffre/ui 0.0.0 → 0.1.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 (168) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +7 -3
  3. package/dist/client/.assetsignore +2 -0
  4. package/dist/client/_coffre/assets/account-C3Zo1l5A.js +1 -0
  5. package/dist/client/_coffre/assets/account-C4bNbeO7.js +2 -0
  6. package/dist/client/_coffre/assets/affordances-DiuJIiR2.js +1 -0
  7. package/dist/client/_coffre/assets/audit-1xLyuxGz.js +2 -0
  8. package/dist/client/_coffre/assets/audit-Dab2GuRj.js +1 -0
  9. package/dist/client/_coffre/assets/auth.device-BfCfeBWe.js +2 -0
  10. package/dist/client/_coffre/assets/auth.device-D2pCLF-X.js +1 -0
  11. package/dist/client/_coffre/assets/coffre-C7qL3HGR.js +1 -0
  12. package/dist/client/_coffre/assets/directory-C2Ou4SQv.js +1 -0
  13. package/dist/client/_coffre/assets/globals-Dgq4Jy0x.css +1 -0
  14. package/dist/client/_coffre/assets/grants-fqTtJ-aW.js +1 -0
  15. package/dist/client/_coffre/assets/index-BpGwbjmM.js +12 -0
  16. package/dist/client/_coffre/assets/inter-cyrillic-ext-opsz-normal-CfYZpDVp.woff2 +0 -0
  17. package/dist/client/_coffre/assets/inter-cyrillic-opsz-normal-BFQXbQ-f.woff2 +0 -0
  18. package/dist/client/_coffre/assets/inter-greek-ext-opsz-normal-BmPQvyvo.woff2 +0 -0
  19. package/dist/client/_coffre/assets/inter-greek-opsz-normal-BM0NlUE_.woff2 +0 -0
  20. package/dist/client/_coffre/assets/inter-latin-ext-opsz-normal-DpK-iCPk.woff2 +0 -0
  21. package/dist/client/_coffre/assets/inter-latin-opsz-normal-BwkfbSeq.woff2 +0 -0
  22. package/dist/client/_coffre/assets/inter-vietnamese-opsz-normal-Ci9T9ieq.woff2 +0 -0
  23. package/dist/client/_coffre/assets/jetbrains-mono-cyrillic-ext-wght-normal-EocZY2iu.woff2 +0 -0
  24. package/dist/client/_coffre/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
  25. package/dist/client/_coffre/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
  26. package/dist/client/_coffre/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
  27. package/dist/client/_coffre/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
  28. package/dist/client/_coffre/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
  29. package/dist/client/_coffre/assets/login-CR9CBXlw.js +2 -0
  30. package/dist/client/_coffre/assets/login-DZizFxa4.js +1 -0
  31. package/dist/client/_coffre/assets/offboarding-DOLJQcXU.js +1 -0
  32. package/dist/client/_coffre/assets/page-BxW2MwaX.js +1 -0
  33. package/dist/client/_coffre/assets/preload-helper-BHuP8_FL.js +46 -0
  34. package/dist/client/_coffre/assets/principal-page-xnBZVURr.js +1 -0
  35. package/dist/client/_coffre/assets/project-environments-CfIH5kFM.js +1 -0
  36. package/dist/client/_coffre/assets/projects._project._environment-CrWjQmIA.js +2 -0
  37. package/dist/client/_coffre/assets/projects._project._environment-CwRVG9Ne.js +3 -0
  38. package/dist/client/_coffre/assets/projects._project.index-CCDEuE-z.js +2 -0
  39. package/dist/client/_coffre/assets/projects._project.index-DCJsmnD7.js +1 -0
  40. package/dist/client/_coffre/assets/projects.index-B8eV8JIn.js +1 -0
  41. package/dist/client/_coffre/assets/projects.index-BNEC7Oz8.js +2 -0
  42. package/dist/client/_coffre/assets/settings-2BPsenNM.js +2 -0
  43. package/dist/client/_coffre/assets/settings-BrS0-wEt.js +1 -0
  44. package/dist/client/_coffre/assets/signin-errors-CroZWZgm.js +1 -0
  45. package/dist/client/_coffre/assets/tokens._token-B3-YwuKp.js +2 -0
  46. package/dist/client/_coffre/assets/tokens._token-D_hsbHx6.js +1 -0
  47. package/dist/client/_coffre/assets/tokens.index-BdKE2Jv8.js +2 -0
  48. package/dist/client/_coffre/assets/tokens.index-XvA4XRxh.js +1 -0
  49. package/dist/client/_coffre/assets/unregistered-CTRMtBIy.js +1 -0
  50. package/dist/client/_coffre/assets/use-action-DgAf-tiX.js +1 -0
  51. package/dist/client/_coffre/assets/users._user-BFPXcJ4y.js +2 -0
  52. package/dist/client/_coffre/assets/users._user-DEtMeSoh.js +1 -0
  53. package/dist/client/_coffre/assets/users.index-B03beUHq.js +1 -0
  54. package/dist/client/_coffre/assets/users.index-D8hwzM6P.js +2 -0
  55. package/dist/client/_coffre/assets/validation-9Cl_XS-l.js +1 -0
  56. package/dist/index.d.ts +19 -0
  57. package/dist/server/.vite/manifest.json +572 -0
  58. package/dist/server/_coffre/assets/_tanstack-start-manifest_v-B6CZDfrB.js +133 -0
  59. package/dist/server/_coffre/assets/account-B71_b5xc.js +296 -0
  60. package/dist/server/_coffre/assets/account-BoO6ZqMc.js +176 -0
  61. package/dist/server/_coffre/assets/affordances-BNaXjMOb.js +46 -0
  62. package/dist/server/_coffre/assets/async-local-storage-C0cwgkEr.js +3107 -0
  63. package/dist/server/_coffre/assets/audit-CwRFnSAY.js +74 -0
  64. package/dist/server/_coffre/assets/audit-t1sYjtrf.js +964 -0
  65. package/dist/server/_coffre/assets/auth.device-1IcJ475s.js +208 -0
  66. package/dist/server/_coffre/assets/auth.device-CJwZYYtn.js +29 -0
  67. package/dist/server/_coffre/assets/coffre-1hxT2E_U.js +278 -0
  68. package/dist/server/_coffre/assets/directory-wm_JQUfk.js +396 -0
  69. package/dist/server/_coffre/assets/empty-plugin-adapters-Cq4--YW-.js +5 -0
  70. package/dist/server/_coffre/assets/globals-Dgq4Jy0x.css +1 -0
  71. package/dist/server/_coffre/assets/grants-BmhEY1vz.js +243 -0
  72. package/dist/server/_coffre/assets/inter-cyrillic-ext-opsz-normal-CfYZpDVp.woff2 +0 -0
  73. package/dist/server/_coffre/assets/inter-cyrillic-opsz-normal-BFQXbQ-f.woff2 +0 -0
  74. package/dist/server/_coffre/assets/inter-greek-ext-opsz-normal-BmPQvyvo.woff2 +0 -0
  75. package/dist/server/_coffre/assets/inter-greek-opsz-normal-BM0NlUE_.woff2 +0 -0
  76. package/dist/server/_coffre/assets/inter-latin-ext-opsz-normal-DpK-iCPk.woff2 +0 -0
  77. package/dist/server/_coffre/assets/inter-latin-opsz-normal-BwkfbSeq.woff2 +0 -0
  78. package/dist/server/_coffre/assets/inter-vietnamese-opsz-normal-Ci9T9ieq.woff2 +0 -0
  79. package/dist/server/_coffre/assets/jetbrains-mono-cyrillic-ext-wght-normal-EocZY2iu.woff2 +0 -0
  80. package/dist/server/_coffre/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
  81. package/dist/server/_coffre/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
  82. package/dist/server/_coffre/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
  83. package/dist/server/_coffre/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
  84. package/dist/server/_coffre/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
  85. package/dist/server/_coffre/assets/login-B5O5LPXV.js +100 -0
  86. package/dist/server/_coffre/assets/login-Bvc4gp0X.js +22 -0
  87. package/dist/server/_coffre/assets/offboarding-CvSilnSm.js +330 -0
  88. package/dist/server/_coffre/assets/page-BjXk0Ehu.js +2003 -0
  89. package/dist/server/_coffre/assets/principal-page-DkwNGzM2.js +617 -0
  90. package/dist/server/_coffre/assets/project-environments-lEzFHUt4.js +9 -0
  91. package/dist/server/_coffre/assets/projects._project._environment-7grhqlgq.js +2227 -0
  92. package/dist/server/_coffre/assets/projects._project._environment-ByEf6pmz.js +55 -0
  93. package/dist/server/_coffre/assets/projects._project.index-B1bjowP-.js +932 -0
  94. package/dist/server/_coffre/assets/projects._project.index-DU8wtoHD.js +22 -0
  95. package/dist/server/_coffre/assets/projects.index-B0DoHbCz.js +280 -0
  96. package/dist/server/_coffre/assets/projects.index-kMDA6pMy.js +54 -0
  97. package/dist/server/_coffre/assets/react-dom-qCTiwGGR.js +1352 -0
  98. package/dist/server/_coffre/assets/router-ChU_CwCX.js +2028 -0
  99. package/dist/server/_coffre/assets/settings-CM9-h7in.js +18 -0
  100. package/dist/server/_coffre/assets/settings-Cg5qOGij.js +60 -0
  101. package/dist/server/_coffre/assets/signin-errors-BEFlVDbQ.js +28 -0
  102. package/dist/server/_coffre/assets/start-DR3rN0St.js +4 -0
  103. package/dist/server/_coffre/assets/tokens._token-CCpaPMAk.js +268 -0
  104. package/dist/server/_coffre/assets/tokens._token-Di4OZdAA.js +18 -0
  105. package/dist/server/_coffre/assets/tokens.index-Do3OHR31.js +34 -0
  106. package/dist/server/_coffre/assets/tokens.index-tDKq4AFh.js +10 -0
  107. package/dist/server/_coffre/assets/ui-BNzhIAQP.js +15710 -0
  108. package/dist/server/_coffre/assets/unregistered-BqaWYFJK.js +51 -0
  109. package/dist/server/_coffre/assets/use-action-BkJXC5JM.js +953 -0
  110. package/dist/server/_coffre/assets/useStore-DWGPqzXR.js +892 -0
  111. package/dist/server/_coffre/assets/users._user-BenYva93.js +15 -0
  112. package/dist/server/_coffre/assets/users._user-Cp039Va-.js +10 -0
  113. package/dist/server/_coffre/assets/users.index-BrzG1Ecq.js +10 -0
  114. package/dist/server/_coffre/assets/users.index-DQ_45DQl.js +34 -0
  115. package/dist/server/_coffre/assets/validation-BQtqPe2V.js +27 -0
  116. package/dist/server/index.js +13910 -0
  117. package/package.json +58 -5
  118. package/src/components/affordances.ts +80 -0
  119. package/src/components/agentation.tsx +34 -0
  120. package/src/components/command-palette.tsx +271 -0
  121. package/src/components/directory.tsx +448 -0
  122. package/src/components/grants.tsx +204 -0
  123. package/src/components/icons.tsx +283 -0
  124. package/src/components/offboarding.tsx +286 -0
  125. package/src/components/page.tsx +128 -0
  126. package/src/components/principal-page.tsx +549 -0
  127. package/src/components/principal-picker.tsx +168 -0
  128. package/src/components/principal.tsx +53 -0
  129. package/src/components/route-states.tsx +60 -0
  130. package/src/components/service-tokens.tsx +212 -0
  131. package/src/components/shell.tsx +641 -0
  132. package/src/components/syncs.tsx +581 -0
  133. package/src/components/theme.tsx +141 -0
  134. package/src/components/tile.tsx +22 -0
  135. package/src/components/ui.tsx +440 -0
  136. package/src/entry.ts +19 -0
  137. package/src/lib/access-plan.ts +201 -0
  138. package/src/lib/audit-sentences.ts +468 -0
  139. package/src/lib/capabilities.ts +45 -0
  140. package/src/lib/coffre.ts +57 -0
  141. package/src/lib/project-access.ts +95 -0
  142. package/src/lib/project-environments.ts +23 -0
  143. package/src/lib/reveal.ts +21 -0
  144. package/src/lib/secret-edit-batch.ts +181 -0
  145. package/src/lib/signin-errors.ts +33 -0
  146. package/src/lib/use-action.ts +50 -0
  147. package/src/lib/validation.ts +27 -0
  148. package/src/routeTree.gen.ts +367 -0
  149. package/src/router.tsx +84 -0
  150. package/src/routes/__root.tsx +192 -0
  151. package/src/routes/access.tsx +11 -0
  152. package/src/routes/account.tsx +359 -0
  153. package/src/routes/audit.tsx +545 -0
  154. package/src/routes/auth.device.tsx +200 -0
  155. package/src/routes/index.tsx +15 -0
  156. package/src/routes/login.tsx +129 -0
  157. package/src/routes/projects.$project.$environment.tsx +1659 -0
  158. package/src/routes/projects.$project.index.tsx +917 -0
  159. package/src/routes/projects.index.tsx +353 -0
  160. package/src/routes/settings.tsx +72 -0
  161. package/src/routes/tokens.$token.tsx +35 -0
  162. package/src/routes/tokens.index.tsx +37 -0
  163. package/src/routes/unregistered.tsx +59 -0
  164. package/src/routes/users.$user.tsx +13 -0
  165. package/src/routes/users.index.tsx +37 -0
  166. package/src/shared/models.ts +55 -0
  167. package/src/styles/globals.css +4118 -0
  168. package/src/types.ts +21 -0
@@ -0,0 +1,448 @@
1
+ import type { CoffreClient } from '@coffre/client';
2
+ import { useEffect, useState } from 'react';
3
+ import { DropdownMenu } from 'radix-ui';
4
+ import { toast } from 'sonner';
5
+ import { memberRef, Refusal, uiResult, useCoffre } from '../lib/coffre';
6
+ import { useAction } from '../lib/use-action';
7
+ import type { DirectoryPrincipal } from '../shared/models';
8
+ import { ConfirmDialog, EmptyState, ErrorLine, Modal, Spinner, Toggletip } from './ui';
9
+ import { PrincipalLink } from './principal';
10
+ import { Key, Lock, MoreHorizontal, Pencil, Plus, ShieldCheck, User, X } from './icons';
11
+
12
+ /**
13
+ * The instance directory, shared by the Users and Tokens pages.
14
+ *
15
+ * Both are rows in the same table on the server (principals, typed user or
16
+ * service); the pages split them because people and machines are looked after
17
+ * differently, not because the model does.
18
+ */
19
+
20
+ type PrincipalType = DirectoryPrincipal['principalType'];
21
+
22
+ /**
23
+ * Everyone in the directory, and who was removed. The API shows grant
24
+ * managers the members of their projects; the directory pages stay the
25
+ * owners' own, and asking for anyone else would only log a refusal.
26
+ */
27
+ export async function loadDirectory(client: CoffreClient, canManage: boolean) {
28
+ if (!canManage) return { ok: false as const, error: 'Only owners can manage users and service accounts.' };
29
+ return uiResult(async () => {
30
+ const { members, removed } = await client.members.list();
31
+ const principals: DirectoryPrincipal[] = members.map(
32
+ ({ principalType, principalId, instanceRole, isRootAdmin, tampered }) => ({ principalType, principalId, instanceRole, isRootAdmin, tampered }),
33
+ );
34
+ return { principals, removed };
35
+ });
36
+ }
37
+
38
+ export const ROLE_LABEL: Record<DirectoryPrincipal['instanceRole'], string> = {
39
+ 'root-admin': 'Root admin',
40
+ owner: 'Owner',
41
+ user: 'Member',
42
+ };
43
+
44
+ /** What each kind of principal is called in the interface. */
45
+ export const KIND: Record<PrincipalType, string> = {
46
+ user: 'user',
47
+ service: 'token',
48
+ };
49
+
50
+ export function DirectoryTable({
51
+ principalType,
52
+ principals,
53
+ hasRemoved = false,
54
+ }: {
55
+ principalType: PrincipalType;
56
+ principals: DirectoryPrincipal[];
57
+ /** Whether some were removed, listed beneath, so an empty table is not "yet". */
58
+ hasRemoved?: boolean;
59
+ }) {
60
+ const users = principalType === 'user';
61
+ return (
62
+ <section className="card" aria-label={users ? 'Users' : 'Tokens'}>
63
+ {principals.length === 0 ? (
64
+ <EmptyState
65
+ title={
66
+ hasRemoved
67
+ ? `No active ${KIND[principalType]}s`
68
+ : users
69
+ ? 'Nobody is registered'
70
+ : 'No tokens yet'
71
+ }
72
+ >
73
+ {hasRemoved ? `Add a ${KIND[principalType]}` : `Add the first ${KIND[principalType]}`} to let
74
+ it through the door. Project access is a separate step, granted from each project's page.
75
+ </EmptyState>
76
+ ) : (
77
+ <div className="dt-wrap">
78
+ <table className={`dt directory directory-${principalType} stacks`}>
79
+ <thead>
80
+ <tr>
81
+ <th className="n">#</th>
82
+ <th className="col-principal">
83
+ <span className="th">
84
+ {users ? <User size={14} /> : <Key size={14} />}
85
+ {users ? 'Email' : 'Name'}
86
+ </span>
87
+ </th>
88
+ {users && (
89
+ <th className="col-role">
90
+ <span className="th">
91
+ <ShieldCheck size={14} />
92
+ Instance role
93
+ </span>
94
+ </th>
95
+ )}
96
+ <th className="col-actions">
97
+ <span className="visually-hidden">Actions</span>
98
+ </th>
99
+ </tr>
100
+ </thead>
101
+ <tbody>
102
+ {principals.map((principal, index) => (
103
+ <tr key={principal.principalId} className="row-link">
104
+ <td className="n">{index + 1}</td>
105
+ <td
106
+ className="col-lead"
107
+ data-label={principal.principalType === 'user' ? 'Email' : 'Name'}
108
+ >
109
+ <PrincipalLink
110
+ type={principal.principalType}
111
+ id={principal.principalId}
112
+ stretch
113
+ />
114
+ </td>
115
+ {users && (
116
+ <td className="col-role" data-label="Instance role">
117
+ <InstanceRole principal={principal} />
118
+ </td>
119
+ )}
120
+ <td className="col-actions">
121
+ <PrincipalActions principal={principal} />
122
+ </td>
123
+ </tr>
124
+ ))}
125
+ </tbody>
126
+ </table>
127
+ </div>
128
+ )}
129
+ </section>
130
+ );
131
+ }
132
+
133
+ /**
134
+ * A user's instance role. Tokens have none worth showing: every one is a
135
+ * plain member. A root admin's role comes from the deployment, so its tag says
136
+ * so on hover or tap instead of offering a menu that could not work.
137
+ */
138
+ export function InstanceRole({ principal }: { principal: DirectoryPrincipal }) {
139
+ if (principal.tampered === true) {
140
+ return (
141
+ <Toggletip
142
+ label={
143
+ <>
144
+ Their record was changed outside coffre, so the vault refuses them
145
+ everything. Remove them to start them over, then add them again.
146
+ </>
147
+ }
148
+ >
149
+ <button type="button" className="tag tag-red tag-button">
150
+ <Lock size={11} />
151
+ Integrity check failed
152
+ </button>
153
+ </Toggletip>
154
+ );
155
+ }
156
+ if (principal.isRootAdmin) {
157
+ return (
158
+ <Toggletip
159
+ label={
160
+ <>
161
+ Set by <code>rootAdmins</code> in the vault's configuration, so it
162
+ cannot be changed or removed here.
163
+ </>
164
+ }
165
+ >
166
+ <button type="button" className="tag tag-violet tag-button">
167
+ <Lock size={11} />
168
+ Root admin
169
+ </button>
170
+ </Toggletip>
171
+ );
172
+ }
173
+ return (
174
+ <span className={`tag${principal.instanceRole === 'user' ? '' : ' tag-violet'}`}>
175
+ {ROLE_LABEL[principal.instanceRole]}
176
+ </span>
177
+ );
178
+ }
179
+
180
+ /**
181
+ * Change role and Remove, behind one menu. Root admins get none: the
182
+ * deployment's configuration owns them.
183
+ */
184
+ export function PrincipalActions({
185
+ principal,
186
+ trigger = 'act act-quiet act-menu',
187
+ onRemoved,
188
+ }: {
189
+ principal: DirectoryPrincipal;
190
+ /** The menu button's classes: a row action in tables, a button in a page head. */
191
+ trigger?: string;
192
+ onRemoved?: () => unknown;
193
+ }) {
194
+ const [editing, setEditing] = useState(false);
195
+ const [confirming, setConfirming] = useState(false);
196
+ const [instanceRole, setInstanceRole] = useState<'user' | 'owner'>(
197
+ principal.instanceRole === 'owner' ? 'owner' : 'user',
198
+ );
199
+ const coffre = useCoffre();
200
+ const { pending, error, setError, run } = useAction();
201
+ const kind = KIND[principal.principalType];
202
+
203
+ useEffect(() => {
204
+ if (error !== null && !editing) toast.error(error);
205
+ }, [editing, error]);
206
+
207
+ if (principal.isRootAdmin) return null;
208
+
209
+ return (
210
+ <>
211
+ <DropdownMenu.Root>
212
+ <DropdownMenu.Trigger asChild>
213
+ <button
214
+ className={trigger}
215
+ aria-label={`Actions for ${principal.principalId}`}
216
+ disabled={pending}
217
+ >
218
+ {pending ? <Spinner size={13} /> : <MoreHorizontal size={16} />}
219
+ </button>
220
+ </DropdownMenu.Trigger>
221
+ <DropdownMenu.Portal>
222
+ <DropdownMenu.Content className="menu" sideOffset={6} align="end">
223
+ {principal.principalType === 'user' && (
224
+ <>
225
+ <DropdownMenu.Item
226
+ className="menu-item"
227
+ onSelect={() => {
228
+ setInstanceRole(principal.instanceRole === 'owner' ? 'owner' : 'user');
229
+ setEditing(true);
230
+ }}
231
+ >
232
+ <Pencil size={14} />
233
+ Change role
234
+ </DropdownMenu.Item>
235
+ <DropdownMenu.Separator className="menu-sep" />
236
+ </>
237
+ )}
238
+ <DropdownMenu.Item
239
+ className="menu-item menu-item-danger"
240
+ onSelect={() => setConfirming(true)}
241
+ >
242
+ <X size={14} />
243
+ Remove {kind}…
244
+ </DropdownMenu.Item>
245
+ </DropdownMenu.Content>
246
+ </DropdownMenu.Portal>
247
+ </DropdownMenu.Root>
248
+
249
+ {principal.principalType === 'user' && (
250
+ <Modal
251
+ open={editing}
252
+ onOpenChange={(open) => {
253
+ setEditing(open);
254
+ if (!open) setError(null);
255
+ }}
256
+ title={
257
+ <>
258
+ Role of <span className="mono">{principal.principalId}</span>
259
+ </>
260
+ }
261
+ >
262
+ <form
263
+ className="form"
264
+ onSubmit={(event) => {
265
+ event.preventDefault();
266
+ void run(
267
+ () =>
268
+ coffre.members.add(memberRef('user', principal.principalId), {
269
+ owner: instanceRole === 'owner',
270
+ }),
271
+ () => {
272
+ toast.success(
273
+ `${principal.principalId} is now ${ROLE_LABEL[instanceRole].toLowerCase()}`,
274
+ );
275
+ setEditing(false);
276
+ },
277
+ );
278
+ }}
279
+ >
280
+ <RoleField value={instanceRole} onChange={setInstanceRole} />
281
+ <ErrorLine error={error} />
282
+ <div className="dialog-actions">
283
+ <button className="btn" type="button" onClick={() => setEditing(false)}>
284
+ Cancel
285
+ </button>
286
+ <button className="btn btn-primary" type="submit" disabled={pending}>
287
+ {pending && <Spinner />}
288
+ Save
289
+ </button>
290
+ </div>
291
+ </form>
292
+ </Modal>
293
+ )}
294
+
295
+ <ConfirmDialog
296
+ open={confirming}
297
+ onOpenChange={setConfirming}
298
+ title={
299
+ <>
300
+ Remove <span className="mono">{principal.principalId}</span>?
301
+ </>
302
+ }
303
+ body={
304
+ principal.principalType === 'user' ? (
305
+ <>
306
+ They are signed out everywhere, and their CLI logins, linked sign-in accounts and
307
+ project permissions are revoked at once. Their past actions stay in the audit log,
308
+ and their page then lists the values they saw, to rotate.
309
+ </>
310
+ ) : (
311
+ <>
312
+ Every token issued to it stops working and its project permissions are revoked at
313
+ once, including for anything running with it right now. Its past actions stay in
314
+ the audit log, and its page then lists the values it read, to rotate.
315
+ </>
316
+ )
317
+ }
318
+ confirmLabel={`Remove ${kind}`}
319
+ onConfirm={() =>
320
+ void run(
321
+ () => coffre.members.remove(memberRef(principal.principalType, principal.principalId)),
322
+ async () => {
323
+ toast.success(`${principal.principalId} removed`);
324
+ await onRemoved?.();
325
+ },
326
+ )
327
+ }
328
+ />
329
+ </>
330
+ );
331
+ }
332
+
333
+ export function AddPrincipal({ principalType }: { principalType: PrincipalType }) {
334
+ const [open, setOpen] = useState(false);
335
+ const [principalId, setPrincipalId] = useState('');
336
+ const [instanceRole, setInstanceRole] = useState<'user' | 'owner'>('user');
337
+ const coffre = useCoffre();
338
+ const { pending, error, setError, run } = useAction();
339
+ const kind = KIND[principalType];
340
+
341
+ function close() {
342
+ setOpen(false);
343
+ setPrincipalId('');
344
+ setInstanceRole('user');
345
+ setError(null);
346
+ }
347
+
348
+ return (
349
+ <>
350
+ <button className="btn btn-primary" onClick={() => setOpen(true)}>
351
+ <Plus size={14} />
352
+ Add {kind}
353
+ </button>
354
+
355
+ <Modal
356
+ open={open}
357
+ onOpenChange={(next) => (next ? setOpen(true) : close())}
358
+ title={`Add a ${kind}`}
359
+ description={`This lets the ${kind} through the door and nothing more. Grant project access from its page or from each project's.`}
360
+ >
361
+ <form
362
+ className="form"
363
+ onSubmit={(event) => {
364
+ event.preventDefault();
365
+ void run(
366
+ async () => {
367
+ // Adding is an idempotent PUT that would also set the role of
368
+ // someone already here; this form is only for someone new.
369
+ const member = memberRef(principalType, principalId.trim());
370
+ const { members } = await coffre.members.list();
371
+ if (members.some((entry) => entry.member === member)) {
372
+ throw new Refusal('That principal already exists.');
373
+ }
374
+ await coffre.members.add(member, { owner: principalType === 'user' && instanceRole === 'owner' });
375
+ },
376
+ () => {
377
+ toast.success(`${principalId.trim()} added`);
378
+ close();
379
+ },
380
+ );
381
+ }}
382
+ >
383
+ <label className="field">
384
+ <span className="label">
385
+ {principalType === 'user' ? 'Cloudflare Access email' : 'Service token common name'}
386
+ </span>
387
+ <input
388
+ className="input input-mono"
389
+ autoFocus
390
+ spellCheck={false}
391
+ autoComplete="off"
392
+ value={principalId}
393
+ placeholder={
394
+ principalType === 'user' ? 'someone@acme.example' : 'ci-deploy'
395
+ }
396
+ onChange={(event) => setPrincipalId(event.target.value)}
397
+ />
398
+ </label>
399
+
400
+ {principalType === 'user' && (
401
+ <RoleField value={instanceRole} onChange={setInstanceRole} />
402
+ )}
403
+
404
+ <ErrorLine error={error} />
405
+ <div className="dialog-actions">
406
+ <button className="btn" type="button" onClick={close}>
407
+ Cancel
408
+ </button>
409
+ <button
410
+ className="btn btn-primary"
411
+ type="submit"
412
+ disabled={pending || principalId.trim() === ''}
413
+ >
414
+ {pending && <Spinner />}
415
+ Add {kind}
416
+ </button>
417
+ </div>
418
+ </form>
419
+ </Modal>
420
+ </>
421
+ );
422
+ }
423
+
424
+ function RoleField({
425
+ value,
426
+ onChange,
427
+ }: {
428
+ value: 'user' | 'owner';
429
+ onChange: (value: 'user' | 'owner') => void;
430
+ }) {
431
+ return (
432
+ <label className="field">
433
+ <span className="label">Instance role</span>
434
+ <select
435
+ className="select"
436
+ value={value}
437
+ onChange={(event) => onChange(event.target.value as 'user' | 'owner')}
438
+ >
439
+ <option value="user">Member</option>
440
+ <option value="owner">Owner</option>
441
+ </select>
442
+ <span className="hint">
443
+ Owners manage users and tokens, can create projects, and read the whole audit log.
444
+ Neither role reads a secret without a project grant.
445
+ </span>
446
+ </label>
447
+ );
448
+ }
@@ -0,0 +1,204 @@
1
+ import type { CoffreClient } from '@coffre/client';
2
+ import type { Role } from '@coffre/core/access';
3
+ import type { ReactNode } from 'react';
4
+ import { toast } from 'sonner';
5
+ import { failureMessage, memberRef, uiFailure, useCoffre } from '../lib/coffre';
6
+ import { useAction } from '../lib/use-action';
7
+ import { projectAccessLabel } from '../lib/project-access';
8
+ import type { GrantRow, ProjectSummary } from '../shared/models';
9
+ import { ConfirmButton, ErrorLine, Spinner } from './ui';
10
+ import { Clock, ShieldCheck } from './icons';
11
+
12
+ /**
13
+ * One project and, when you manage its access, its grants. A project that is
14
+ * not there for you fails with no message: the page words that itself.
15
+ */
16
+ export async function loadProject(client: CoffreClient, slug: string) {
17
+ let project: ProjectSummary | undefined;
18
+ try {
19
+ project = (await client.projects.list()).projects.find((entry) => entry.slug === slug);
20
+ } catch (error) {
21
+ return uiFailure(error);
22
+ }
23
+ if (project === undefined) return { ok: false as const, error: null };
24
+ if (!project.permissions.includes('grant.manage')) {
25
+ return { ok: true as const, project, grants: [] as GrantRow[], grantsError: null };
26
+ }
27
+
28
+ try {
29
+ const { members } = await client.members.list(slug);
30
+ const grants: GrantRow[] = members.flatMap((member) =>
31
+ member.grants.map((grant) => ({
32
+ id: grant.id,
33
+ principalType: member.principalType,
34
+ principalId: member.principalId,
35
+ role: grant.role,
36
+ roleName: grant.roleName,
37
+ permissions: grant.permissions,
38
+ scope: grant.environment === null ? ('project' as const) : ('environment' as const),
39
+ environmentSlug: grant.environment,
40
+ expiresAt: grant.expiresAt,
41
+ })),
42
+ );
43
+ return { ok: true as const, project, grants, grantsError: null };
44
+ } catch (error) {
45
+ return { ok: true as const, project, grants: [] as GrantRow[], grantsError: failureMessage(error) };
46
+ }
47
+ }
48
+
49
+ /** Where a grant applies, as the API names it: `market`, or `market/prod`. */
50
+ export function grantPlace(project: string, environmentSlug: string | null): string {
51
+ return environmentSlug === null ? project : `${project}/${environmentSlug}`;
52
+ }
53
+
54
+ /**
55
+ * Give someone a role at one place. Access is declarative, so asking for what
56
+ * they already hold is not an error, whoever asked first: `existed` says so.
57
+ */
58
+ export async function ensureGrant(
59
+ coffre: CoffreClient,
60
+ data: {
61
+ project: string;
62
+ principalType: GrantRow['principalType'];
63
+ principalId: string;
64
+ role: string;
65
+ environmentSlug: string | null;
66
+ expiresAt: string | null;
67
+ },
68
+ ): Promise<{ existed: boolean }> {
69
+ const place = grantPlace(data.project, data.environmentSlug);
70
+ const role = data.role as Role;
71
+ const { changes } = await coffre.access.set(memberRef(data.principalType, data.principalId), {
72
+ [place]: data.expiresAt === null ? role : { role, until: data.expiresAt },
73
+ });
74
+ return { existed: changes[place] === 'unchanged' };
75
+ }
76
+
77
+ /**
78
+ * Grants as a table: who or where in the first column, then the access, when
79
+ * it lapses, and a revoke.
80
+ *
81
+ * A project's page leads each row with the principal; a user's page leads with
82
+ * the project. Everything after the first column is the same grant either way.
83
+ */
84
+ export function GrantsTable({ lead, children }: { lead: ReactNode; children: ReactNode }) {
85
+ return (
86
+ <div className="dt-wrap">
87
+ <table className="dt grants stacks">
88
+ <thead>
89
+ <tr>
90
+ <th className="n">#</th>
91
+ <th className="col-principal">{lead}</th>
92
+ <th>
93
+ <span className="th">
94
+ <ShieldCheck size={14} />
95
+ Permissions
96
+ </span>
97
+ </th>
98
+ <th className="col-expires">
99
+ <span className="th">
100
+ <Clock size={14} />
101
+ Expires
102
+ </span>
103
+ </th>
104
+ <th className="col-actions">
105
+ <span className="visually-hidden">Actions</span>
106
+ </th>
107
+ </tr>
108
+ </thead>
109
+ <tbody>{children}</tbody>
110
+ </table>
111
+ </div>
112
+ );
113
+ }
114
+
115
+ export function GrantRowView({
116
+ number,
117
+ project,
118
+ grant,
119
+ lead,
120
+ leadLabel,
121
+ }: {
122
+ number: number;
123
+ project: string;
124
+ grant: GrantRow;
125
+ lead: ReactNode;
126
+ leadLabel: string;
127
+ }) {
128
+ const coffre = useCoffre();
129
+ const { pending, error, run } = useAction();
130
+ const label = projectAccessLabel(grant);
131
+ const expired = grant.expiresAt !== null && new Date(grant.expiresAt).getTime() < Date.now();
132
+
133
+ return (
134
+ <>
135
+ <tr>
136
+ <td className="n">{number}</td>
137
+ <td className="col-lead" data-label={leadLabel}>
138
+ {lead}
139
+ </td>
140
+ <td className="col-access" data-label="Permissions">
141
+ <span className={`tag${grant.role === 'owner' ? ' tag-violet' : ''}`}>{label}</span>
142
+ </td>
143
+ <td
144
+ className={`col-expires cell-mono cell-muted${grant.expiresAt === null ? ' is-never' : ''}`}
145
+ data-label="Expires"
146
+ >
147
+ {grant.expiresAt === null ? (
148
+ 'never'
149
+ ) : (
150
+ <>
151
+ <span className="narrow-only">expires </span>
152
+ {grant.expiresAt.slice(0, 10)}
153
+ {expired && (
154
+ <>
155
+ {' '}
156
+ <span className="tag tag-red">expired</span>
157
+ </>
158
+ )}
159
+ </>
160
+ )}
161
+ </td>
162
+ <td className="col-actions">
163
+ <ConfirmButton
164
+ trigger={
165
+ <button className="act act-danger" disabled={pending}>
166
+ {pending && <Spinner size={13} />}
167
+ Revoke
168
+ </button>
169
+ }
170
+ title={
171
+ <>
172
+ Revoke {label} from <span className="mono">{grant.principalId}</span>?
173
+ </>
174
+ }
175
+ body={
176
+ <>
177
+ They lose <strong>{label}</strong> on <span className="mono">{project}</span>{' '}
178
+ immediately, including any process using it right now. Other grants they hold
179
+ still apply.
180
+ </>
181
+ }
182
+ confirmLabel="Revoke access"
183
+ onConfirm={() =>
184
+ run(
185
+ () =>
186
+ coffre.access.set(memberRef(grant.principalType, grant.principalId), {
187
+ [grantPlace(project, grant.environmentSlug)]: null,
188
+ }),
189
+ () => toast.success(`Revoked ${label} on ${project} from ${grant.principalId}`),
190
+ )
191
+ }
192
+ />
193
+ </td>
194
+ </tr>
195
+ {error !== null && (
196
+ <tr className="row-error">
197
+ <td colSpan={5}>
198
+ <ErrorLine error={error} />
199
+ </td>
200
+ </tr>
201
+ )}
202
+ </>
203
+ );
204
+ }