@coffre/ui 0.1.10 → 0.1.11
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.
- package/dist/client/_coffre/assets/account-BJAdDvZf.js +2 -0
- package/dist/client/_coffre/assets/account-D0jLpk5c.js +1 -0
- package/dist/client/_coffre/assets/affordances-Bq2-oUB5.js +1 -0
- package/dist/client/_coffre/assets/audit-C-pcBgKe.js +2 -0
- package/dist/client/_coffre/assets/audit-CHGlDeBB.js +1 -0
- package/dist/client/_coffre/assets/{auth.device-BP5FY0K1.js → auth.device-BndOUjse.js} +1 -1
- package/dist/client/_coffre/assets/auth.device-CWHNQn9W.js +2 -0
- package/dist/client/_coffre/assets/directory-nDExpkAz.js +1 -0
- package/dist/client/_coffre/assets/dist-QVHMLatb.js +1 -0
- package/dist/client/_coffre/assets/globals-CKBoTlyH.css +1 -0
- package/dist/client/_coffre/assets/grants-CjoOnxzz.js +1 -0
- package/dist/client/_coffre/assets/icons-BQ27Ds9u.js +1 -0
- package/dist/client/_coffre/assets/index-CUGytaO3.js +12 -0
- package/dist/client/_coffre/assets/link-DP8SVWon.js +1 -0
- package/dist/client/_coffre/assets/login-CeaxDMcr.js +2 -0
- package/dist/client/_coffre/assets/login-DrMgvqZn.js +1 -0
- package/dist/client/_coffre/assets/offboarding-CNr_1tnl.js +1 -0
- package/dist/client/_coffre/assets/page-CGmvjSy4.js +1 -0
- package/dist/client/_coffre/assets/preload-helper-ClBkJ19f.js +1 -0
- package/dist/client/_coffre/assets/principal-page-DNLb_q_e.js +1 -0
- package/dist/client/_coffre/assets/projects._project._environment-CHzbboGp.js +3 -0
- package/dist/client/_coffre/assets/projects._project._environment-DDxu_HHD.js +2 -0
- package/dist/client/_coffre/assets/projects._project.index-CIAsjvg6.js +2 -0
- package/dist/client/_coffre/assets/projects._project.index-DZ9l6d-1.js +1 -0
- package/dist/client/_coffre/assets/projects.index-4eUXreE_.js +1 -0
- package/dist/client/_coffre/assets/row-state-ByET1OKX.js +1 -0
- package/dist/client/_coffre/assets/settings-D2zp1Ssu.js +1 -0
- package/dist/client/_coffre/assets/suspense-C7KwSnTG.js +1 -0
- package/dist/client/_coffre/assets/tokens._token-DWmbqzNk.js +2 -0
- package/dist/client/_coffre/assets/tokens._token-Dui_4KfS.js +1 -0
- package/dist/client/_coffre/assets/tokens.index-CSi5Wxwf.js +1 -0
- package/dist/client/_coffre/assets/ui-BdipqGpP.js +46 -0
- package/dist/client/_coffre/assets/unregistered-CA7B4C1m.js +1 -0
- package/dist/client/_coffre/assets/use-action-DRm9yVhV.js +1 -0
- package/dist/client/_coffre/assets/use-change-BRRMJocl.js +1 -0
- package/dist/client/_coffre/assets/use-shell-BMGlJ25Q.js +1 -0
- package/dist/client/_coffre/assets/useQuery-D__2KBOs.js +1 -0
- package/dist/client/_coffre/assets/useSuspenseQuery-CSXyDALB.js +1 -0
- package/dist/client/_coffre/assets/users._user-BkNrX7Qf.js +1 -0
- package/dist/client/_coffre/assets/users._user-jbSvtdOg.js +2 -0
- package/dist/client/_coffre/assets/users.index-CKnQ1G76.js +1 -0
- package/dist/server/.vite/manifest.json +343 -246
- package/dist/server/_coffre/assets/_tanstack-start-manifest_v-Dx-c3XVk.js +162 -0
- package/dist/server/_coffre/assets/{account-CUQYGCYh.js → account-BocTPU1J.js} +150 -117
- package/dist/server/_coffre/assets/{account-DMlUoBxQ.js → account-CL8hOGhO.js} +10 -24
- package/dist/server/_coffre/assets/{affordances-BNaXjMOb.js → affordances-CKANXDTG.js} +2 -22
- package/dist/server/_coffre/assets/{async-local-storage-C0cwgkEr.js → async-local-storage-C10uQtMM.js} +3 -2
- package/dist/server/_coffre/assets/{audit-CCdSLmvK.js → audit-Bcka4vA6.js} +46 -16
- package/dist/server/_coffre/assets/audit-CSRQYIhu.js +31 -0
- package/dist/server/_coffre/assets/{auth.device-C05u5ROO.js → auth.device-CMcUISZY.js} +7 -5
- package/dist/server/_coffre/assets/{auth.device-BoFM1jcf.js → auth.device-DqO0T-rU.js} +5 -5
- package/dist/server/_coffre/assets/{directory-GsnYkse0.js → directory-CyYx6Jx1.js} +87 -112
- package/dist/server/_coffre/assets/{use-action-DwEmf58L.js → dist-CnnV3t2g.js} +3 -48
- package/dist/server/_coffre/assets/globals-CKBoTlyH.css +1 -0
- package/dist/server/_coffre/assets/grants-CRijabX0.js +158 -0
- package/dist/server/_coffre/assets/{page-BdnhpwvH.js → icons-Dy-5ccDF.js} +3 -196
- package/dist/server/_coffre/assets/lazyRouteComponent-CqBNCGuZ.js +480 -0
- package/dist/server/_coffre/assets/link-42ZWdjzZ.js +422 -0
- package/dist/server/_coffre/assets/{login-DUtCQju5.js → login-BI9tzJEy.js} +7 -5
- package/dist/server/_coffre/assets/{login-B2njIq_o.js → login-C-v88YwP.js} +2 -2
- package/dist/server/_coffre/assets/matchContext-qLfYbC6_.js +56 -0
- package/dist/server/_coffre/assets/{offboarding-8yMa7ViM.js → offboarding-1H0LavTd.js} +6 -4
- package/dist/server/_coffre/assets/page-D_mO0-sn.js +3592 -0
- package/dist/server/_coffre/assets/{principal-page-B8zPcS2b.js → principal-page-DQDoXute.js} +51 -42
- package/dist/server/_coffre/assets/{projects._project._environment-BZ5Z80V8.js → projects._project._environment-C-yVwC92.js} +168 -248
- package/dist/server/_coffre/assets/{projects._project._environment-DdIOc7wx.js → projects._project._environment-D_5R40of.js} +4 -11
- package/dist/server/_coffre/assets/{projects._project.index-CuEAUwGn.js → projects._project.index-A2BImFIT.js} +134 -96
- package/dist/server/_coffre/assets/{projects._project.index-DvbcU1kh.js → projects._project.index-BPPKxKrt.js} +5 -4
- package/dist/server/_coffre/assets/{projects.index-5HI0wOuG.js → projects.index-543IsPzY.js} +57 -55
- package/dist/server/_coffre/assets/{react-dom-qCTiwGGR.js → react-dom-bBoJXlK2.js} +417 -51
- package/dist/server/_coffre/assets/{router-CQDoql8s.js → router-CM3vK8QO.js} +376 -92
- package/dist/server/_coffre/assets/row-state-XBtDoIA4.js +117 -0
- package/dist/server/_coffre/assets/{settings-BVz78wjP.js → settings-BLVU74ym.js} +8 -6
- package/dist/server/_coffre/assets/suspense-CzYTwxOj.js +602 -0
- package/dist/server/_coffre/assets/tokens._token-B-hBgwBW.js +10 -0
- package/dist/server/_coffre/assets/{tokens._token-CWs3Mtki.js → tokens._token-aubAUonP.js} +97 -79
- package/dist/server/_coffre/assets/{tokens.index-Ck0LN7aC.js → tokens.index-4D3jwp5v.js} +9 -6
- package/dist/server/_coffre/assets/{ui-D_slKpSF.js → ui-BI3Bf72q.js} +7 -836
- package/dist/server/_coffre/assets/{unregistered-Z17da4Rc.js → unregistered-CagDxMyw.js} +5 -3
- package/dist/server/_coffre/assets/use-action-tzBZvjjQ.js +47 -0
- package/dist/server/_coffre/assets/use-change-Dggqxy5y.js +1212 -0
- package/dist/server/_coffre/assets/use-shell-CwStDfzX.js +462 -0
- package/dist/server/_coffre/assets/useQuery-C6_dBjem.js +8 -0
- package/dist/server/_coffre/assets/{useStore-DWGPqzXR.js → useRouter-CaUSUNsw.js} +1 -420
- package/dist/server/_coffre/assets/useSuspenseQuery-BuapQt_P.js +117 -0
- package/dist/server/_coffre/assets/{users._user-CMwj-VI9.js → users._user-137WNyxn.js} +4 -5
- package/dist/server/_coffre/assets/users._user-C252Iilj.js +10 -0
- package/dist/server/_coffre/assets/{users.index-fWHv6A30.js → users.index-DQHsfnal.js} +9 -6
- package/dist/server/index.js +6 -5
- package/package.json +5 -3
- package/src/components/directory.tsx +75 -104
- package/src/components/grants.tsx +60 -112
- package/src/components/principal-page.tsx +54 -67
- package/src/components/principal-picker.tsx +7 -16
- package/src/components/row-state.tsx +95 -0
- package/src/components/service-tokens.tsx +75 -64
- package/src/components/shell.tsx +1 -1
- package/src/components/syncs.tsx +42 -26
- package/src/lib/announce.ts +36 -0
- package/src/lib/changes.ts +498 -0
- package/src/lib/optimistic.ts +233 -0
- package/src/lib/queries.ts +393 -0
- package/src/lib/use-action.ts +20 -15
- package/src/lib/use-change.ts +42 -0
- package/src/lib/use-shell.ts +17 -0
- package/src/router.tsx +17 -9
- package/src/routes/__root.tsx +8 -38
- package/src/routes/account.tsx +163 -154
- package/src/routes/audit.tsx +51 -72
- package/src/routes/auth.device.tsx +4 -2
- package/src/routes/login.tsx +3 -2
- package/src/routes/projects.$project.$environment.tsx +226 -231
- package/src/routes/projects.$project.index.tsx +136 -118
- package/src/routes/projects.index.tsx +117 -145
- package/src/routes/settings.tsx +7 -7
- package/src/routes/tokens.$token.tsx +14 -17
- package/src/routes/tokens.index.tsx +8 -4
- package/src/routes/unregistered.tsx +3 -2
- package/src/routes/users.$user.tsx +4 -4
- package/src/routes/users.index.tsx +8 -4
- package/src/styles/globals.css +78 -1
- package/dist/client/_coffre/assets/account-B48wuSF8.js +0 -1
- package/dist/client/_coffre/assets/account-BkNyIDT7.js +0 -2
- package/dist/client/_coffre/assets/affordances-CmoY-8Xf.js +0 -1
- package/dist/client/_coffre/assets/audit-BvwKYb-5.js +0 -2
- package/dist/client/_coffre/assets/audit-D02H-zxS.js +0 -1
- package/dist/client/_coffre/assets/auth.device-DYOONgFF.js +0 -2
- package/dist/client/_coffre/assets/coffre-BBp7cKVa.js +0 -1
- package/dist/client/_coffre/assets/directory-Bu0EIxqI.js +0 -1
- package/dist/client/_coffre/assets/globals-Bh2x1gBM.css +0 -1
- package/dist/client/_coffre/assets/grants-DavcwofW.js +0 -1
- package/dist/client/_coffre/assets/index-CDPlq5rM.js +0 -12
- package/dist/client/_coffre/assets/login-C-wTm_t1.js +0 -2
- package/dist/client/_coffre/assets/login-CLLv1om9.js +0 -1
- package/dist/client/_coffre/assets/offboarding-DWsE2FSa.js +0 -1
- package/dist/client/_coffre/assets/page-DbSS7pYH.js +0 -1
- package/dist/client/_coffre/assets/preload-helper-DduCCcmc.js +0 -46
- package/dist/client/_coffre/assets/principal-page-Bp_iwuSd.js +0 -1
- package/dist/client/_coffre/assets/projects._project._environment-BMeX_B8l.js +0 -2
- package/dist/client/_coffre/assets/projects._project._environment-CT0A-hIU.js +0 -3
- package/dist/client/_coffre/assets/projects._project.index-C_xYOGLT.js +0 -2
- package/dist/client/_coffre/assets/projects._project.index-Chcf-cUd.js +0 -1
- package/dist/client/_coffre/assets/projects.index-CfOtb1X5.js +0 -2
- package/dist/client/_coffre/assets/projects.index-DSjOhLDh.js +0 -1
- package/dist/client/_coffre/assets/settings-BC9cUJNK.js +0 -1
- package/dist/client/_coffre/assets/settings-Bkj_E21y.js +0 -2
- package/dist/client/_coffre/assets/tokens._token-BA0KqvaW.js +0 -1
- package/dist/client/_coffre/assets/tokens._token-Dh9jLOkm.js +0 -2
- package/dist/client/_coffre/assets/tokens.index-BgAMWkHT.js +0 -2
- package/dist/client/_coffre/assets/tokens.index-CHueIAv1.js +0 -1
- package/dist/client/_coffre/assets/unregistered-D5go1AfR.js +0 -1
- package/dist/client/_coffre/assets/use-action-l__xZDYY.js +0 -1
- package/dist/client/_coffre/assets/users._user-Dwqa0gv5.js +0 -1
- package/dist/client/_coffre/assets/users._user-xJAb3T6Y.js +0 -2
- package/dist/client/_coffre/assets/users.index-BJoF-L37.js +0 -2
- package/dist/client/_coffre/assets/users.index-YUjtbTGy.js +0 -1
- package/dist/server/_coffre/assets/_tanstack-start-manifest_v-BKg2AlPu.js +0 -133
- package/dist/server/_coffre/assets/audit-CThXn2v7.js +0 -74
- package/dist/server/_coffre/assets/coffre-C2ANsTAz.js +0 -281
- package/dist/server/_coffre/assets/globals-Bh2x1gBM.css +0 -1
- package/dist/server/_coffre/assets/grants-B6k4Fu9z.js +0 -243
- package/dist/server/_coffre/assets/projects.index-uiomWX_l.js +0 -54
- package/dist/server/_coffre/assets/settings-4oRyDizv.js +0 -18
- package/dist/server/_coffre/assets/tokens._token-CF_deAwq.js +0 -18
- package/dist/server/_coffre/assets/tokens.index-DF_07KEe.js +0 -10
- package/dist/server/_coffre/assets/users._user-DUzwkSKT.js +0 -10
- package/dist/server/_coffre/assets/users.index-BbuweTwH.js +0 -10
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
import type { CoffreClient, IdentityRow, SecretKey, SessionRow } from '@coffre/client';
|
|
2
|
+
import type { Role } from '@coffre/core/access';
|
|
3
|
+
import type { QueryKey } from '@tanstack/react-query';
|
|
4
|
+
|
|
5
|
+
import { failureMessage, memberRef, Refusal } from './coffre.ts';
|
|
6
|
+
import { projectAccessLabel } from './project-access.ts';
|
|
7
|
+
import type { Change, ListShape, Target, Words } from './optimistic.ts';
|
|
8
|
+
import { affects, keys } from './queries.ts';
|
|
9
|
+
import { applySecretEditBatch, type SecretChange, type SecretDraft, type SecretEditBatchResult } from './secret-edit-batch.ts';
|
|
10
|
+
import type { DirectoryPrincipal, GrantRow, ProjectSummary } from '../shared/models.ts';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Every change the pages make before the server answers, one per kind, each
|
|
14
|
+
* with the list it shows in (`lib/optimistic.ts`). Issuing a token is not
|
|
15
|
+
* here: only the server can show what it issued.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
type Failure = { ok: false; error: string };
|
|
19
|
+
|
|
20
|
+
/** A list held in a read's `field`, when the read answered. */
|
|
21
|
+
function listOf<TItem, TData extends { ok: boolean } | null>(
|
|
22
|
+
queryKey: QueryKey,
|
|
23
|
+
field: string,
|
|
24
|
+
id: (item: TItem) => string,
|
|
25
|
+
): ListShape<TData, TItem> {
|
|
26
|
+
return {
|
|
27
|
+
queryKey,
|
|
28
|
+
items: (data) => (data?.ok === true ? ((data as unknown as Record<string, TItem[]>)[field] ?? []) : []),
|
|
29
|
+
withItems: (data, items) => (data?.ok === true ? { ...data, [field]: items } : data),
|
|
30
|
+
id,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const ARCHIVING: Words = { pending: 'Archiving…', done: 'archived', failed: 'Not archived.' };
|
|
35
|
+
const REVOKING: Words = { pending: 'Revoking…', done: 'revoked', failed: 'Not revoked.' };
|
|
36
|
+
|
|
37
|
+
const saving = (id: string): Target => ({ id, kind: 'saving' });
|
|
38
|
+
const removing = (id: string): Target => ({ id, kind: 'removing' });
|
|
39
|
+
|
|
40
|
+
// --- access --------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
type Grants = ({ ok: true; grants: GrantRow[] } | Failure);
|
|
43
|
+
|
|
44
|
+
/** A grant's identity, before the server has given it an id: who, and where. */
|
|
45
|
+
export function grantId(grant: Pick<GrantRow, 'principalType' | 'principalId' | 'environmentSlug'>): string {
|
|
46
|
+
return `${memberRef(grant.principalType, grant.principalId)}@${grant.environmentSlug ?? ''}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function grantsList(project: string) {
|
|
50
|
+
return listOf<GrantRow, Grants>(keys.grants(project), 'grants', grantId);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Where a grant applies, as the API names it: `market`, or `market/prod`. */
|
|
54
|
+
export function grantPlace(project: string, environmentSlug: string | null): string {
|
|
55
|
+
return environmentSlug === null ? project : `${project}/${environmentSlug}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type GrantVars = {
|
|
59
|
+
principalType: GrantRow['principalType'];
|
|
60
|
+
principalId: string;
|
|
61
|
+
role: string;
|
|
62
|
+
/** How the role is shown while it saves. */
|
|
63
|
+
roleName: string;
|
|
64
|
+
environmentSlug: string | null;
|
|
65
|
+
expiresAt: string | null;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Give someone a role at one place. Access is declarative, so asking for what
|
|
70
|
+
* they already hold is not an error, whoever asked first: `existed` says so.
|
|
71
|
+
*/
|
|
72
|
+
export function grantAccess(client: CoffreClient, project: string): Change<Grants, GrantRow, GrantVars, { existed: boolean }> {
|
|
73
|
+
const list = grantsList(project);
|
|
74
|
+
return {
|
|
75
|
+
list,
|
|
76
|
+
label: (vars) => `${vars.roleName} for ${vars.principalId}`,
|
|
77
|
+
targets: (vars) => [saving(grantId(vars))],
|
|
78
|
+
apply: (grants, vars) => {
|
|
79
|
+
const row: GrantRow = {
|
|
80
|
+
id: `pending:${grantId(vars)}`,
|
|
81
|
+
principalType: vars.principalType,
|
|
82
|
+
principalId: vars.principalId,
|
|
83
|
+
role: vars.role,
|
|
84
|
+
roleName: vars.roleName,
|
|
85
|
+
permissions: [],
|
|
86
|
+
scope: vars.environmentSlug === null ? 'project' : 'environment',
|
|
87
|
+
environmentSlug: vars.environmentSlug,
|
|
88
|
+
expiresAt: vars.expiresAt,
|
|
89
|
+
};
|
|
90
|
+
const id = grantId(vars);
|
|
91
|
+
return grants.some((grant) => grantId(grant) === id)
|
|
92
|
+
? grants.map((grant) => (grantId(grant) === id ? { ...row, id: grant.id } : grant))
|
|
93
|
+
: [...grants, row];
|
|
94
|
+
},
|
|
95
|
+
affects: (vars) => affects.access(project, memberRef(vars.principalType, vars.principalId)),
|
|
96
|
+
run: async (vars) => {
|
|
97
|
+
const place = grantPlace(project, vars.environmentSlug);
|
|
98
|
+
const role = vars.role as Role;
|
|
99
|
+
const { changes } = await client.access.set(memberRef(vars.principalType, vars.principalId), {
|
|
100
|
+
[place]: vars.expiresAt === null ? role : { role, until: vars.expiresAt },
|
|
101
|
+
});
|
|
102
|
+
return { existed: changes[place] === 'unchanged' };
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function revokeGrant(client: CoffreClient, project: string): Change<Grants, GrantRow, GrantRow, unknown> {
|
|
108
|
+
return {
|
|
109
|
+
list: grantsList(project),
|
|
110
|
+
label: (grant) => `${projectAccessLabel(grant)} for ${grant.principalId}`,
|
|
111
|
+
targets: (grant) => [removing(grantId(grant))],
|
|
112
|
+
removing: REVOKING,
|
|
113
|
+
affects: (grant) => affects.access(project, memberRef(grant.principalType, grant.principalId)),
|
|
114
|
+
run: (grant) =>
|
|
115
|
+
client.access.set(memberRef(grant.principalType, grant.principalId), {
|
|
116
|
+
[grantPlace(project, grant.environmentSlug)]: null,
|
|
117
|
+
}),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// --- the directory -------------------------------------------------------------
|
|
122
|
+
|
|
123
|
+
type Directory = ({ ok: true; principals: DirectoryPrincipal[] } | Failure);
|
|
124
|
+
|
|
125
|
+
const principalId = (principal: Pick<DirectoryPrincipal, 'principalType' | 'principalId'>) =>
|
|
126
|
+
memberRef(principal.principalType, principal.principalId);
|
|
127
|
+
|
|
128
|
+
/** The directory as owners read it: the only ones who change it. */
|
|
129
|
+
export const directoryList = listOf<DirectoryPrincipal, Directory>([...keys.directory, { owner: true }], 'principals', principalId);
|
|
130
|
+
|
|
131
|
+
export type InviteVars = { principalType: DirectoryPrincipal['principalType']; principalId: string; owner: boolean };
|
|
132
|
+
|
|
133
|
+
export function invite(client: CoffreClient): Change<Directory, DirectoryPrincipal, InviteVars, void> {
|
|
134
|
+
return {
|
|
135
|
+
list: directoryList,
|
|
136
|
+
label: (vars) => vars.principalId,
|
|
137
|
+
targets: (vars) => [saving(principalId(vars))],
|
|
138
|
+
// Someone already listed is not added twice: the server will say they exist.
|
|
139
|
+
apply: (principals, vars) =>
|
|
140
|
+
principals.some((principal) => principalId(principal) === principalId(vars)) ? principals : [
|
|
141
|
+
...principals,
|
|
142
|
+
{
|
|
143
|
+
principalType: vars.principalType,
|
|
144
|
+
principalId: vars.principalId,
|
|
145
|
+
instanceRole: vars.owner ? 'owner' : 'user',
|
|
146
|
+
isRootAdmin: false,
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
affects: () => affects.admission(),
|
|
150
|
+
run: async (vars) => {
|
|
151
|
+
// Adding is an idempotent PUT that would also set the role of someone
|
|
152
|
+
// already here; this is only for someone new.
|
|
153
|
+
const member = memberRef(vars.principalType, vars.principalId);
|
|
154
|
+
const { members } = await client.members.list();
|
|
155
|
+
if (members.some((entry) => entry.member === member)) throw new Refusal('That principal already exists.');
|
|
156
|
+
await client.members.add(member, { owner: vars.principalType === 'user' && vars.owner });
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function changeRole(client: CoffreClient): Change<Directory, DirectoryPrincipal, { principalId: string; owner: boolean }, unknown> {
|
|
162
|
+
const id = (vars: { principalId: string }) => memberRef('user', vars.principalId);
|
|
163
|
+
return {
|
|
164
|
+
list: directoryList,
|
|
165
|
+
label: (vars) => `the role of ${vars.principalId}`,
|
|
166
|
+
targets: (vars) => [saving(id(vars))],
|
|
167
|
+
apply: (principals, vars) =>
|
|
168
|
+
principals.map((principal) =>
|
|
169
|
+
principalId(principal) === id(vars) ? { ...principal, instanceRole: vars.owner ? 'owner' : 'user' } : principal,
|
|
170
|
+
),
|
|
171
|
+
affects: (vars) => affects.role(id(vars)),
|
|
172
|
+
run: (vars) => client.members.add(id(vars), { owner: vars.owner }),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function removeMember(client: CoffreClient): Change<Directory, DirectoryPrincipal, DirectoryPrincipal, unknown> {
|
|
177
|
+
return {
|
|
178
|
+
list: directoryList,
|
|
179
|
+
label: (principal) => principal.principalId,
|
|
180
|
+
targets: (principal) => [removing(principalId(principal))],
|
|
181
|
+
affects: (principal) => affects.removal(principalId(principal)),
|
|
182
|
+
run: (principal) => client.members.remove(principalId(principal)),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// --- credentials, sessions, linked accounts --------------------------------------
|
|
187
|
+
|
|
188
|
+
type Credential = { id: string; hint: string };
|
|
189
|
+
type Credentials = ({ ok: true; tokens: Credential[] } | Failure) | null;
|
|
190
|
+
|
|
191
|
+
export function revokeCredential(client: CoffreClient, serviceId: string): Change<Credentials, Credential, Credential, unknown> {
|
|
192
|
+
const member = memberRef('service', serviceId);
|
|
193
|
+
return {
|
|
194
|
+
list: listOf<Credential, Credentials>([...keys.credentials(member), { allowed: true }], 'tokens', (token) => token.id),
|
|
195
|
+
label: (token) => `token ${token.hint}`,
|
|
196
|
+
targets: (token) => [removing(token.id)],
|
|
197
|
+
removing: REVOKING,
|
|
198
|
+
affects: () => affects.credentials(member),
|
|
199
|
+
run: (token) => client.tokens.revoke(member, token.id),
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
type Sessions = ({ ok: true; sessions: SessionRow[] } | Failure);
|
|
204
|
+
|
|
205
|
+
export function endSession(client: CoffreClient): Change<Sessions, SessionRow, SessionRow, unknown> {
|
|
206
|
+
return {
|
|
207
|
+
list: listOf<SessionRow, Sessions>(keys.sessions, 'sessions', (session) => session.id),
|
|
208
|
+
label: () => 'the session',
|
|
209
|
+
targets: (session) => [removing(session.id)],
|
|
210
|
+
removing: { pending: 'Ending…', done: 'ended', failed: 'Not ended.' },
|
|
211
|
+
affects: () => affects.sessions(),
|
|
212
|
+
run: (session) => client.sessions.revoke(session.id),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
type Identities = ({ ok: true; identities: IdentityRow[] } | Failure);
|
|
217
|
+
|
|
218
|
+
export function unlinkIdentity(client: CoffreClient): Change<Identities, IdentityRow, IdentityRow & { label: string }, unknown> {
|
|
219
|
+
return {
|
|
220
|
+
list: listOf<IdentityRow, Identities>(keys.identities, 'identities', (identity) => identity.id),
|
|
221
|
+
label: (identity) => `the ${identity.label} account`,
|
|
222
|
+
targets: (identity) => [removing(identity.id)],
|
|
223
|
+
removing: { pending: 'Unlinking…', done: 'unlinked', failed: 'Not unlinked.' },
|
|
224
|
+
affects: () => affects.identities(),
|
|
225
|
+
run: (identity) => client.identities.unlink(identity.id),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// --- projects and environments ---------------------------------------------------
|
|
230
|
+
|
|
231
|
+
type Projects = ({ ok: true; projects: ProjectSummary[] } | Failure);
|
|
232
|
+
|
|
233
|
+
export const projectsList = listOf<ProjectSummary, Projects>(keys.projects, 'projects', (project) => project.slug);
|
|
234
|
+
|
|
235
|
+
export function createProject(client: CoffreClient): Change<Projects, ProjectSummary, { slug: string; name: string }, void> {
|
|
236
|
+
return {
|
|
237
|
+
list: projectsList,
|
|
238
|
+
label: (vars) => `project ${vars.slug}`,
|
|
239
|
+
targets: (vars) => [saving(vars.slug)],
|
|
240
|
+
apply: (projects, vars) => [
|
|
241
|
+
...projects,
|
|
242
|
+
{
|
|
243
|
+
slug: vars.slug,
|
|
244
|
+
name: vars.name,
|
|
245
|
+
archivedAt: null,
|
|
246
|
+
// You become its owner.
|
|
247
|
+
permissions: ['project.manage', 'environment.manage', 'grant.manage'],
|
|
248
|
+
environments: [],
|
|
249
|
+
secretCount: null,
|
|
250
|
+
},
|
|
251
|
+
],
|
|
252
|
+
affects: () => affects.places(),
|
|
253
|
+
run: async (vars) => {
|
|
254
|
+
// Creating is an idempotent PUT; a slug that is taken is refused here.
|
|
255
|
+
const { created } = await client.projects.create(vars.slug, { name: vars.name });
|
|
256
|
+
if (!created) throw new Refusal(`A project named "${vars.slug}" already exists.`);
|
|
257
|
+
},
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
type Environment = ProjectSummary['environments'][number];
|
|
262
|
+
|
|
263
|
+
/** One project's environments, held in the project list. */
|
|
264
|
+
export function environmentsList(project: string): ListShape<Projects, Environment> {
|
|
265
|
+
const environmentsOf = (data: Projects) =>
|
|
266
|
+
data.ok ? (data.projects.find((entry) => entry.slug === project)?.environments ?? []) : [];
|
|
267
|
+
return {
|
|
268
|
+
queryKey: keys.projects,
|
|
269
|
+
items: environmentsOf,
|
|
270
|
+
withItems: (data, environments) =>
|
|
271
|
+
data.ok
|
|
272
|
+
? { ...data, projects: data.projects.map((entry) => (entry.slug === project ? { ...entry, environments } : entry)) }
|
|
273
|
+
: data,
|
|
274
|
+
// Under the project's name: the projects list, in the same read, uses bare slugs.
|
|
275
|
+
id: (environment) => environmentId(project, environment.slug),
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function environmentId(project: string, environment: string): string {
|
|
280
|
+
return `${project}/${environment}`;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function createEnvironment(client: CoffreClient, project: string): Change<Projects, Environment, { slug: string; name: string }, void> {
|
|
284
|
+
return {
|
|
285
|
+
list: environmentsList(project),
|
|
286
|
+
label: (vars) => `environment ${vars.slug}`,
|
|
287
|
+
targets: (vars) => [saving(environmentId(project, vars.slug))],
|
|
288
|
+
apply: (environments, vars) => [
|
|
289
|
+
...environments,
|
|
290
|
+
{ slug: vars.slug, name: vars.name, accessible: false, details: { archivedAt: null, secretCount: 0 } },
|
|
291
|
+
],
|
|
292
|
+
affects: () => affects.places(),
|
|
293
|
+
run: async (vars) => {
|
|
294
|
+
const { created } = await client.environments.create(`${project}/${vars.slug}`, { name: vars.name });
|
|
295
|
+
if (!created) throw new Refusal(`An environment named "${vars.slug}" already exists.`);
|
|
296
|
+
},
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export type RenameVars = { from: string; slug: string; name: string };
|
|
301
|
+
|
|
302
|
+
export function renameEnvironment(client: CoffreClient, project: string): Change<Projects, Environment, RenameVars, unknown> {
|
|
303
|
+
return {
|
|
304
|
+
list: environmentsList(project),
|
|
305
|
+
label: (vars) => `environment ${vars.from}`,
|
|
306
|
+
targets: (vars) =>
|
|
307
|
+
vars.slug === vars.from
|
|
308
|
+
? [saving(environmentId(project, vars.from))]
|
|
309
|
+
: [saving(environmentId(project, vars.from)), saving(environmentId(project, vars.slug))],
|
|
310
|
+
apply: (environments, vars) =>
|
|
311
|
+
environments.map((environment) =>
|
|
312
|
+
environment.slug === vars.from ? { ...environment, slug: vars.slug, name: vars.name } : environment,
|
|
313
|
+
),
|
|
314
|
+
affects: () => affects.places(),
|
|
315
|
+
run: (vars) => client.environments.update(`${project}/${vars.from}`, { slug: vars.slug, name: vars.name }),
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Archive an environment or bring it back. Archiving ends every read of it,
|
|
321
|
+
* so it waits for the server, as a removal does; restoring shows at once.
|
|
322
|
+
*/
|
|
323
|
+
export function archiveEnvironment(client: CoffreClient, project: string): Change<Projects, Environment, { slug: string; archived: boolean }, unknown> {
|
|
324
|
+
const archivedAt = (archived: boolean) => (archived ? new Date().toISOString() : null);
|
|
325
|
+
const withArchived = (environment: Environment, archived: boolean): Environment =>
|
|
326
|
+
environment.details === null
|
|
327
|
+
? environment
|
|
328
|
+
: { ...environment, details: { ...environment.details, archivedAt: archivedAt(archived) } };
|
|
329
|
+
return {
|
|
330
|
+
list: environmentsList(project),
|
|
331
|
+
label: (vars) => `environment ${vars.slug}`,
|
|
332
|
+
targets: (vars) => [
|
|
333
|
+
vars.archived ? removing(environmentId(project, vars.slug)) : saving(environmentId(project, vars.slug)),
|
|
334
|
+
],
|
|
335
|
+
removing: ARCHIVING,
|
|
336
|
+
apply: (environments, vars) =>
|
|
337
|
+
vars.archived
|
|
338
|
+
? environments
|
|
339
|
+
: environments.map((environment) => (environment.slug === vars.slug ? withArchived(environment, false) : environment)),
|
|
340
|
+
confirmed: (environments, _vars, archived) =>
|
|
341
|
+
environments.map((environment) =>
|
|
342
|
+
archived.has(environmentId(project, environment.slug)) ? withArchived(environment, true) : environment,
|
|
343
|
+
),
|
|
344
|
+
affects: () => affects.places(),
|
|
345
|
+
run: (vars) => client.environments.update(`${project}/${vars.slug}`, { archived: vars.archived }),
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// --- secrets ---------------------------------------------------------------------
|
|
350
|
+
|
|
351
|
+
type Secrets = ({ ok: true; keys: SecretKey[] } | Failure);
|
|
352
|
+
|
|
353
|
+
export function secretsList(place: { project: string; environment: string }) {
|
|
354
|
+
return listOf<SecretKey, Secrets>(keys.secrets(place), 'keys', (entry) => entry.key);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** A save that stopped partway: what is left to retry, as the ledger holds it. */
|
|
358
|
+
export class UnsavedEdits extends Refusal {
|
|
359
|
+
readonly outcome: SecretEditBatchResult;
|
|
360
|
+
|
|
361
|
+
constructor(outcome: SecretEditBatchResult) {
|
|
362
|
+
const message = outcome.error instanceof Error ? outcome.error.message : failureMessage(outcome.error);
|
|
363
|
+
// Only renames can have landed before a failure: the patch is all or nothing.
|
|
364
|
+
super(
|
|
365
|
+
outcome.applied === 0
|
|
366
|
+
? message
|
|
367
|
+
: `${message} The renames before it were saved; nothing else was, and the rest is ready to retry.`,
|
|
368
|
+
);
|
|
369
|
+
this.outcome = outcome;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export type SaveSecretsVars = {
|
|
374
|
+
active: SecretKey[];
|
|
375
|
+
drafts: SecretDraft[];
|
|
376
|
+
changes: Record<string, SecretChange>;
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Write the ledger's pending edits, as `applySecretEditBatch` does: renames
|
|
381
|
+
* one by one, then the rest as one patch that lands whole or not at all.
|
|
382
|
+
* New and edited secrets show at once; archived ones stay, struck through,
|
|
383
|
+
* until the server confirms.
|
|
384
|
+
*/
|
|
385
|
+
export function saveSecrets(
|
|
386
|
+
client: CoffreClient,
|
|
387
|
+
place: { project: string; environment: string },
|
|
388
|
+
/** Who is saving, shown as the author until the list is read back. */
|
|
389
|
+
author: string | null,
|
|
390
|
+
): Change<Secrets, SecretKey, SaveSecretsVars, number> {
|
|
391
|
+
const path = `${place.project}/${place.environment}`;
|
|
392
|
+
const edits = (vars: SaveSecretsVars) =>
|
|
393
|
+
vars.active.flatMap((entry) => {
|
|
394
|
+
const change = vars.changes[entry.key];
|
|
395
|
+
return change === undefined ? [] : [{ entry, change }];
|
|
396
|
+
});
|
|
397
|
+
return {
|
|
398
|
+
list: secretsList(place),
|
|
399
|
+
label: (vars) => {
|
|
400
|
+
const named = [...vars.drafts.map((draft) => draft.key.trim()), ...edits(vars).map(({ entry }) => entry.key)];
|
|
401
|
+
return named.length === 1 ? named[0]! : `${named.length} changes`;
|
|
402
|
+
},
|
|
403
|
+
targets: (vars) => [
|
|
404
|
+
...vars.drafts.map((draft) => saving(draft.key.trim())),
|
|
405
|
+
...edits(vars).flatMap(({ entry, change }) =>
|
|
406
|
+
change.archived
|
|
407
|
+
? [removing(entry.key)]
|
|
408
|
+
: change.key.trim() === entry.key
|
|
409
|
+
? [saving(entry.key)]
|
|
410
|
+
: [saving(entry.key), saving(change.key.trim())],
|
|
411
|
+
),
|
|
412
|
+
],
|
|
413
|
+
removing: ARCHIVING,
|
|
414
|
+
apply: (entries, vars) => {
|
|
415
|
+
const now = new Date().toISOString();
|
|
416
|
+
const bumped = (entry: SecretKey | undefined, key: string): SecretKey => ({
|
|
417
|
+
key,
|
|
418
|
+
archived: false,
|
|
419
|
+
version: (entry?.version ?? 0) + 1,
|
|
420
|
+
updatedAt: now,
|
|
421
|
+
updatedBy: author,
|
|
422
|
+
});
|
|
423
|
+
let next = entries.map((entry) => {
|
|
424
|
+
const change = vars.changes[entry.key];
|
|
425
|
+
if (change === undefined || change.archived) return entry;
|
|
426
|
+
const key = change.key.trim();
|
|
427
|
+
return change.value === null ? { ...entry, key } : bumped(entry, key);
|
|
428
|
+
});
|
|
429
|
+
for (const draft of vars.drafts) {
|
|
430
|
+
const key = draft.key.trim();
|
|
431
|
+
const existing = next.find((entry) => entry.key === key);
|
|
432
|
+
next = existing === undefined ? [bumped(undefined, key), ...next] : next.map((entry) => (entry.key === key ? bumped(entry, key) : entry));
|
|
433
|
+
}
|
|
434
|
+
return next;
|
|
435
|
+
},
|
|
436
|
+
confirmed: (entries, _vars, archived) =>
|
|
437
|
+
entries.map((entry) => (archived.has(entry.key) ? { ...entry, archived: true } : entry)),
|
|
438
|
+
affects: () => affects.secrets(place),
|
|
439
|
+
run: async (vars) => {
|
|
440
|
+
const outcome = await applySecretEditBatch({
|
|
441
|
+
active: vars.active,
|
|
442
|
+
drafts: vars.drafts,
|
|
443
|
+
changes: vars.changes,
|
|
444
|
+
operations: {
|
|
445
|
+
rename: async (key, nextKey) => {
|
|
446
|
+
try {
|
|
447
|
+
await client.secrets.rename(`${path}/${key}`, nextKey);
|
|
448
|
+
} catch (error) {
|
|
449
|
+
// A refused rename names the key it stopped at.
|
|
450
|
+
throw new Refusal(`${key}: ${failureMessage(error)}`);
|
|
451
|
+
}
|
|
452
|
+
},
|
|
453
|
+
write: async (patch) => {
|
|
454
|
+
try {
|
|
455
|
+
await client.secrets.set(path, patch);
|
|
456
|
+
} catch (error) {
|
|
457
|
+
throw new Refusal(failureMessage(error));
|
|
458
|
+
}
|
|
459
|
+
},
|
|
460
|
+
},
|
|
461
|
+
});
|
|
462
|
+
if (outcome.error !== null) throw new UnsavedEdits(outcome);
|
|
463
|
+
return outcome.applied;
|
|
464
|
+
},
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** Restore an archived secret, from the Archived list. */
|
|
469
|
+
export function restoreSecret(
|
|
470
|
+
client: CoffreClient,
|
|
471
|
+
place: { project: string; environment: string },
|
|
472
|
+
): Change<Secrets, SecretKey, { key: string }, unknown> {
|
|
473
|
+
return {
|
|
474
|
+
list: secretsList(place),
|
|
475
|
+
label: (vars) => vars.key,
|
|
476
|
+
targets: (vars) => [saving(vars.key)],
|
|
477
|
+
apply: (entries, vars) => entries.map((entry) => (entry.key === vars.key ? { ...entry, archived: false } : entry)),
|
|
478
|
+
affects: () => affects.secrets(place),
|
|
479
|
+
run: (vars) => client.secrets.update(`${place.project}/${place.environment}/${vars.key}`, { archived: false }),
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** Archive one secret, as Undo after a restore does: it stays, struck through, until confirmed. */
|
|
484
|
+
export function archiveSecret(
|
|
485
|
+
client: CoffreClient,
|
|
486
|
+
place: { project: string; environment: string },
|
|
487
|
+
): Change<Secrets, SecretKey, { key: string }, unknown> {
|
|
488
|
+
return {
|
|
489
|
+
list: secretsList(place),
|
|
490
|
+
label: (vars) => vars.key,
|
|
491
|
+
targets: (vars) => [removing(vars.key)],
|
|
492
|
+
removing: ARCHIVING,
|
|
493
|
+
confirmed: (entries, _vars, archived) =>
|
|
494
|
+
entries.map((entry) => (archived.has(entry.key) ? { ...entry, archived: true } : entry)),
|
|
495
|
+
affects: () => affects.secrets(place),
|
|
496
|
+
run: (vars) => client.secrets.update(`${place.project}/${place.environment}/${vars.key}`, { archived: true }),
|
|
497
|
+
};
|
|
498
|
+
}
|