@x12i/keyrox-store 0.1.0 → 0.2.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.
- package/dist/application-lifecycle.d.ts +6 -0
- package/dist/application-lifecycle.d.ts.map +1 -0
- package/dist/application-lifecycle.js +28 -0
- package/dist/application-lifecycle.js.map +1 -0
- package/dist/directory-memory.d.ts +19 -4
- package/dist/directory-memory.d.ts.map +1 -1
- package/dist/directory-memory.js +666 -21
- package/dist/directory-memory.js.map +1 -1
- package/dist/email-code-policy.d.ts +7 -0
- package/dist/email-code-policy.d.ts.map +1 -0
- package/dist/email-code-policy.js +7 -0
- package/dist/email-code-policy.js.map +1 -0
- package/dist/errors.d.ts +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/hosted-human-sessions.d.ts +47 -0
- package/dist/hosted-human-sessions.d.ts.map +1 -0
- package/dist/hosted-human-sessions.js +2 -0
- package/dist/hosted-human-sessions.js.map +1 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +1108 -85
- package/dist/memory.js.map +1 -1
- package/dist/notification-delivery.d.ts +44 -0
- package/dist/notification-delivery.d.ts.map +1 -0
- package/dist/notification-delivery.js +58 -0
- package/dist/notification-delivery.js.map +1 -0
- package/dist/notification-webhook-jobs.d.ts +43 -0
- package/dist/notification-webhook-jobs.d.ts.map +1 -0
- package/dist/notification-webhook-jobs.js +59 -0
- package/dist/notification-webhook-jobs.js.map +1 -0
- package/dist/repository.d.ts +162 -12
- package/dist/repository.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/directory-memory.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { directoryHasOrganizations, directoryHasWorkspaces } from "@x12i/keyrox-types";
|
|
1
3
|
import { fail } from "./errors.js";
|
|
2
4
|
import { asTime, asTimeOrNull, clone, isExpired } from "./time.js";
|
|
3
5
|
export function emptyDirectory() {
|
|
@@ -8,22 +10,177 @@ export function emptyDirectory() {
|
|
|
8
10
|
directoryMemberships: new Map(),
|
|
9
11
|
permissions: new Map(),
|
|
10
12
|
productRoles: new Map(),
|
|
13
|
+
tenantProductRoles: new Map(),
|
|
14
|
+
signupRoles: new Map(),
|
|
11
15
|
groups: new Map(),
|
|
12
16
|
groupMembers: new Map(),
|
|
13
17
|
bindings: new Map(),
|
|
14
18
|
features: new Map(),
|
|
15
19
|
featureAssignments: new Map(),
|
|
16
20
|
appWaitlist: new Map(),
|
|
21
|
+
waitlistConfirmations: new Map(),
|
|
17
22
|
directoryChallenges: new Map(),
|
|
23
|
+
hostedSsoIdentities: new Map(),
|
|
24
|
+
workspaces: new Map(),
|
|
25
|
+
workspaceMemberships: new Map(),
|
|
26
|
+
onboardingResponses: new Map(),
|
|
18
27
|
};
|
|
19
28
|
}
|
|
20
|
-
export function bindDirectoryMemory(source, users, tx) {
|
|
29
|
+
export function bindDirectoryMemory(source, users, tx, activeApplicationForEnvironment, insertNotification) {
|
|
21
30
|
const rows = (map, pred = () => true) => [...map.values()].filter(pred).map((row) => clone(row));
|
|
22
31
|
const save = (map, row) => { map.set(row.id, clone(row)); };
|
|
23
32
|
const remove = (map, id) => {
|
|
24
33
|
if (!map.delete(id))
|
|
25
34
|
fail("not_found", "Record not found.");
|
|
26
35
|
};
|
|
36
|
+
function assertAvailableCatalogLink(role) {
|
|
37
|
+
if (!role.catalogRoleId)
|
|
38
|
+
return;
|
|
39
|
+
const maps = source();
|
|
40
|
+
const catalog = maps.tenantProductRoles.get(role.catalogRoleId);
|
|
41
|
+
if (!catalog || catalog.orgId !== activeApplicationForEnvironment(role.environmentId, true)?.orgId) {
|
|
42
|
+
fail("conflict", "Catalog role must belong to the application tenant.");
|
|
43
|
+
}
|
|
44
|
+
const existing = [...maps.productRoles.values()].some((item) => item.id !== role.id
|
|
45
|
+
&& item.environmentId === role.environmentId && item.catalogRoleId === role.catalogRoleId);
|
|
46
|
+
if (existing)
|
|
47
|
+
fail("duplicate", "The catalog role already has a link in this environment.");
|
|
48
|
+
}
|
|
49
|
+
function saveTenantRole(row) {
|
|
50
|
+
const catalog = source().tenantProductRoles;
|
|
51
|
+
if ([...catalog.values()].some((item) => item.id !== row.id && item.orgId === row.orgId && item.name.toLowerCase() === row.name.toLowerCase())) {
|
|
52
|
+
fail("duplicate", "A catalog role already uses this name.");
|
|
53
|
+
}
|
|
54
|
+
save(catalog, row);
|
|
55
|
+
}
|
|
56
|
+
function saveIdentity(maps, row) {
|
|
57
|
+
if (row.email && [...maps.identities.values()].some((item) => item.environmentId === row.environmentId && item.email?.toLowerCase() === row.email?.toLowerCase() && item.id !== row.id)) {
|
|
58
|
+
fail("duplicate_email", "Email already exists.");
|
|
59
|
+
}
|
|
60
|
+
const { emailProofAt, ...identity } = row;
|
|
61
|
+
save(maps.identities, { ...identity, metadata: clone(row.metadata), emailVerifiedAt: asTimeOrNull(row.emailVerifiedAt),
|
|
62
|
+
...(emailProofAt && { emailProofAt: asTime(emailProofAt) }), createdAt: asTime(row.createdAt), updatedAt: asTime(row.updatedAt) });
|
|
63
|
+
}
|
|
64
|
+
function recordEmailProof(identity, stamp) {
|
|
65
|
+
identity.emailVerifiedAt ??= stamp;
|
|
66
|
+
if (identity.kind === "user" && (!identity.emailProofAt || identity.emailProofAt < stamp))
|
|
67
|
+
identity.emailProofAt = stamp;
|
|
68
|
+
if (identity.updatedAt < stamp)
|
|
69
|
+
identity.updatedAt = stamp;
|
|
70
|
+
}
|
|
71
|
+
function addChallenge(maps, row) {
|
|
72
|
+
if (maps.directoryChallenges.has(row.id) || [...maps.directoryChallenges.values()].some((item) => item.tokenHash === row.tokenHash))
|
|
73
|
+
fail("duplicate", "Duplicate key.");
|
|
74
|
+
save(maps.directoryChallenges, { ...row, expiresAt: asTime(row.expiresAt), usedAt: asTimeOrNull(row.usedAt), createdAt: asTime(row.createdAt) });
|
|
75
|
+
}
|
|
76
|
+
function saveHostedSsoLink(maps, row) {
|
|
77
|
+
const identity = maps.identities.get(row.identityId);
|
|
78
|
+
if (identity?.environmentId !== row.environmentId || identity.kind !== "user")
|
|
79
|
+
fail("conflict", "Hosted sign-in identity is missing.");
|
|
80
|
+
const key = JSON.stringify([row.environmentId, row.authority, row.subject]);
|
|
81
|
+
const existing = maps.hostedSsoIdentities.get(key);
|
|
82
|
+
if (existing)
|
|
83
|
+
return existing.identityId === row.identityId;
|
|
84
|
+
maps.hostedSsoIdentities.set(key, clone(row));
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
function assertWorkspaceScope(workspace) {
|
|
88
|
+
const app = activeApplicationForEnvironment(workspace.environmentId);
|
|
89
|
+
const model = app?.directoryModel ?? "organizations";
|
|
90
|
+
if (!app || app.id !== workspace.appId || !directoryHasWorkspaces(model) ||
|
|
91
|
+
(model === "organizations-and-workspaces") !== (workspace.organizationId !== null)) {
|
|
92
|
+
fail("conflict", "This workspace is incompatible with the application directory model.");
|
|
93
|
+
}
|
|
94
|
+
if (workspace.organizationId) {
|
|
95
|
+
const organization = source().identities.get(workspace.organizationId);
|
|
96
|
+
if (organization?.kind !== "organization" || organization.status !== "active" ||
|
|
97
|
+
organization.appId !== workspace.appId || organization.environmentId !== workspace.environmentId) {
|
|
98
|
+
fail("conflict", "The workspace organization is unavailable in this environment.");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function requireOrganizationScope(environmentId) {
|
|
103
|
+
if (activeApplicationForEnvironment(environmentId, true)?.directoryModel === "people-only") {
|
|
104
|
+
fail("conflict", "Organizations are unavailable for this application.");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function addWorkspaceMember(workspace, row) {
|
|
108
|
+
const maps = source();
|
|
109
|
+
const identity = maps.identities.get(row.identityId);
|
|
110
|
+
if (row.workspaceId !== workspace.id || row.environmentId !== workspace.environmentId || identity?.kind === "organization" ||
|
|
111
|
+
identity?.status !== "active" || identity.environmentId !== workspace.environmentId || identity.appId !== workspace.appId) {
|
|
112
|
+
fail("conflict", "The workspace member is unavailable in this environment.");
|
|
113
|
+
}
|
|
114
|
+
if (workspace.organizationId && ![...maps.directoryMemberships.values()].some((member) => member.environmentId === row.environmentId && member.identityId === row.identityId && member.organizationId === workspace.organizationId)) {
|
|
115
|
+
fail("conflict", "The workspace member must belong to its organization.");
|
|
116
|
+
}
|
|
117
|
+
if (maps.workspaceMemberships.has(row.id) || [...maps.workspaceMemberships.values()]
|
|
118
|
+
.some((item) => item.workspaceId === row.workspaceId && item.identityId === row.identityId))
|
|
119
|
+
fail("duplicate", "Duplicate workspace membership.");
|
|
120
|
+
if (row.relation !== "owner" && row.relation !== "member")
|
|
121
|
+
fail("conflict", "Invalid workspace membership relation.");
|
|
122
|
+
save(maps.workspaceMemberships, { ...row, createdAt: asTime(row.createdAt) });
|
|
123
|
+
}
|
|
124
|
+
function workspaceInvitationTarget(row) {
|
|
125
|
+
if (!row.invitation)
|
|
126
|
+
return null;
|
|
127
|
+
const maps = source();
|
|
128
|
+
const app = activeApplicationForEnvironment(row.environmentId);
|
|
129
|
+
const workspace = maps.workspaces.get(row.invitation.workspaceId);
|
|
130
|
+
const role = maps.productRoles.get(row.invitation.roleId);
|
|
131
|
+
if (app?.id !== row.appId || workspace?.appId !== row.appId || workspace.environmentId !== row.environmentId ||
|
|
132
|
+
workspace.status !== "active" || role?.environmentId !== row.environmentId || !directoryHasWorkspaces(app.directoryModel) ||
|
|
133
|
+
(app.directoryModel === "organizations-and-workspaces") !== (workspace.organizationId !== null))
|
|
134
|
+
return null;
|
|
135
|
+
if (workspace.organizationId) {
|
|
136
|
+
const parent = maps.identities.get(workspace.organizationId);
|
|
137
|
+
if (parent?.kind !== "organization" || parent.status !== "active" || parent.environmentId !== row.environmentId)
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
return { workspace, role };
|
|
141
|
+
}
|
|
142
|
+
function applyWorkspaceInvitation(row, challenge, identity, at) {
|
|
143
|
+
const target = workspaceInvitationTarget(row);
|
|
144
|
+
if (!target)
|
|
145
|
+
return false;
|
|
146
|
+
const maps = source();
|
|
147
|
+
const { workspace, role } = target;
|
|
148
|
+
const parent = workspace.organizationId;
|
|
149
|
+
if (parent && ![...maps.directoryMemberships.values()].some((member) => member.organizationId === parent && member.identityId === identity.id)) {
|
|
150
|
+
save(maps.directoryMemberships, {
|
|
151
|
+
id: `dms_${challenge.id}`, environmentId: row.environmentId, identityId: identity.id, organizationId: parent,
|
|
152
|
+
relation: "member", createdAt: at,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
const existing = [...maps.workspaceMemberships.values()].find((member) => member.workspaceId === workspace.id && member.identityId === identity.id);
|
|
156
|
+
if (existing)
|
|
157
|
+
existing.roleId = role.id;
|
|
158
|
+
else
|
|
159
|
+
addWorkspaceMember(workspace, {
|
|
160
|
+
id: `wsm_${challenge.id}`, environmentId: row.environmentId, workspaceId: workspace.id, identityId: identity.id,
|
|
161
|
+
roleId: role.id, relation: "member", createdAt: at,
|
|
162
|
+
});
|
|
163
|
+
challenge.usedAt ??= at;
|
|
164
|
+
row.status = "admitted";
|
|
165
|
+
delete row.pendingWorkspaceIdentityId;
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
function acceptWorkspaceInvitation(identity, at, expectedTokenHash) {
|
|
169
|
+
if (!identity.emailVerifiedAt || identity.status !== "active" || identity.kind !== "user" || !identity.email)
|
|
170
|
+
return false;
|
|
171
|
+
const maps = source();
|
|
172
|
+
const invite = [...maps.appWaitlist.values()].find((row) => row.appId === identity.appId && row.environmentId === identity.environmentId &&
|
|
173
|
+
row.email.toLowerCase() === identity.email.toLowerCase() && row.status === "invited" && Boolean(row.invitation));
|
|
174
|
+
if (!invite || (expectedTokenHash && (invite.tokenHash !== expectedTokenHash || invite.pendingWorkspaceIdentityId)))
|
|
175
|
+
return false;
|
|
176
|
+
const challenge = [...maps.directoryChallenges.values()].find((row) => row.tokenHash === invite.tokenHash);
|
|
177
|
+
if (!challenge || challenge.purpose !== "admit" || challenge.environmentId !== invite.environmentId || challenge.email.toLowerCase() !== invite.email.toLowerCase())
|
|
178
|
+
return false;
|
|
179
|
+
if (isExpired(challenge.expiresAt, at) || (invite.pendingWorkspaceIdentityId
|
|
180
|
+
? invite.pendingWorkspaceIdentityId !== identity.id || !challenge.usedAt : Boolean(challenge.usedAt)))
|
|
181
|
+
return false;
|
|
182
|
+
return applyWorkspaceInvitation(invite, challenge, identity, at);
|
|
183
|
+
}
|
|
27
184
|
return {
|
|
28
185
|
async countUsers() { return users(); },
|
|
29
186
|
async getPlatformSignup() { return clone(source().settings); },
|
|
@@ -35,7 +192,17 @@ export function bindDirectoryMemory(source, users, tx) {
|
|
|
35
192
|
settings.operators = [...row.operators];
|
|
36
193
|
});
|
|
37
194
|
},
|
|
38
|
-
async
|
|
195
|
+
async insertPlatformWaitlist(row) {
|
|
196
|
+
return tx(() => {
|
|
197
|
+
const { platformWaitlist, waitlistConfirmations } = source();
|
|
198
|
+
if ([...platformWaitlist.values()].some((item) => item.email.toLowerCase() === row.email.toLowerCase()))
|
|
199
|
+
return false;
|
|
200
|
+
save(platformWaitlist, { ...row, createdAt: asTime(row.createdAt) });
|
|
201
|
+
waitlistConfirmations.set(`platform:${row.id}`, { claimedAt: null });
|
|
202
|
+
return true;
|
|
203
|
+
});
|
|
204
|
+
},
|
|
205
|
+
async savePlatformWaitlist(row, challenge) {
|
|
39
206
|
tx(() => {
|
|
40
207
|
const maps = source();
|
|
41
208
|
const email = row.email.toLowerCase();
|
|
@@ -43,6 +210,8 @@ export function bindDirectoryMemory(source, users, tx) {
|
|
|
43
210
|
if (clash)
|
|
44
211
|
fail("duplicate_email", "Email already exists.");
|
|
45
212
|
save(maps.platformWaitlist, { ...row, createdAt: asTime(row.createdAt) });
|
|
213
|
+
if (challenge)
|
|
214
|
+
addChallenge(maps, challenge);
|
|
46
215
|
});
|
|
47
216
|
},
|
|
48
217
|
async listPlatformWaitlist() { return rows(source().platformWaitlist); },
|
|
@@ -52,12 +221,109 @@ export function bindDirectoryMemory(source, users, tx) {
|
|
|
52
221
|
async saveDirectoryIdentity(row) {
|
|
53
222
|
tx(() => {
|
|
54
223
|
const maps = source();
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
224
|
+
const app = activeApplicationForEnvironment(row.environmentId, true);
|
|
225
|
+
if (app?.id !== row.appId || app.orgId !== row.orgId)
|
|
226
|
+
fail("conflict", "Identity must match its application environment and tenant.");
|
|
227
|
+
if (row.kind === "organization") {
|
|
228
|
+
requireOrganizationScope(row.environmentId);
|
|
229
|
+
if (!maps.identities.has(row.id) && !directoryHasOrganizations(activeApplicationForEnvironment(row.environmentId, true)?.directoryModel)) {
|
|
230
|
+
fail("conflict", "New organizations are unavailable for this application.");
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
const previous = maps.identities.get(row.id);
|
|
234
|
+
const proof = previous?.email?.toLowerCase() === row.email?.toLowerCase() && row.kind === "user" ? previous?.emailProofAt : null;
|
|
235
|
+
saveIdentity(maps, {
|
|
236
|
+
...row, emailProofAt: previous ? proof : row.emailProofAt,
|
|
237
|
+
emailVerifiedAt: row.emailVerifiedAt ?? (proof ? previous.emailVerifiedAt : null),
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
},
|
|
241
|
+
async proveDirectoryEmail(identityId, environmentId, email, at) {
|
|
242
|
+
return tx(() => {
|
|
243
|
+
const identity = source().identities.get(identityId);
|
|
244
|
+
const app = activeApplicationForEnvironment(environmentId);
|
|
245
|
+
if (!identity || !app || identity.environmentId !== environmentId || identity.appId !== app.id || identity.orgId !== app.orgId
|
|
246
|
+
|| identity.kind !== "user" || identity.status !== "active" || identity.email?.toLowerCase() !== email.toLowerCase())
|
|
247
|
+
return null;
|
|
248
|
+
recordEmailProof(identity, asTime(at));
|
|
249
|
+
return clone(identity);
|
|
250
|
+
});
|
|
251
|
+
},
|
|
252
|
+
async signupDirectoryPerson({ identity, organization, membership, bindingId, challenge, allowOrganizationless, admissionTokenHash, hostedSsoLink, notificationDelivery }) {
|
|
253
|
+
return tx(() => {
|
|
254
|
+
const maps = source();
|
|
255
|
+
const app = activeApplicationForEnvironment(identity.environmentId, true);
|
|
256
|
+
if (app?.id !== identity.appId || app.orgId !== identity.orgId)
|
|
257
|
+
fail("conflict", "Identity must match its application environment and tenant.");
|
|
258
|
+
let workspaceInvite;
|
|
259
|
+
if (admissionTokenHash) {
|
|
260
|
+
const stamp = asTime(identity.createdAt);
|
|
261
|
+
const email = identity.email?.toLowerCase();
|
|
262
|
+
const invite = [...maps.appWaitlist.values()].find((row) => row.appId === identity.appId &&
|
|
263
|
+
row.environmentId === identity.environmentId && row.email.toLowerCase() === email && row.status === "invited" && row.tokenHash === admissionTokenHash);
|
|
264
|
+
const admission = [...maps.directoryChallenges.values()].find((row) => row.tokenHash === admissionTokenHash);
|
|
265
|
+
if (!invite || !admission || admission.purpose !== "admit" || admission.environmentId !== identity.environmentId ||
|
|
266
|
+
admission.email.toLowerCase() !== email || admission.usedAt || isExpired(admission.expiresAt, stamp)) {
|
|
267
|
+
fail("challenge_invalid", "Waiting list invitation is no longer available.");
|
|
268
|
+
}
|
|
269
|
+
admission.usedAt = stamp;
|
|
270
|
+
if (invite.invitation) {
|
|
271
|
+
if (!workspaceInvitationTarget(invite))
|
|
272
|
+
fail("challenge_invalid", "The invited workspace or role is no longer available.");
|
|
273
|
+
workspaceInvite = { invite, admission };
|
|
274
|
+
invite.pendingWorkspaceIdentityId = identity.id;
|
|
275
|
+
}
|
|
276
|
+
else
|
|
277
|
+
invite.status = "admitted";
|
|
278
|
+
}
|
|
279
|
+
const roleId = workspaceInvite ? undefined : maps.signupRoles.get(identity.environmentId);
|
|
280
|
+
const role = roleId ? maps.productRoles.get(roleId) : undefined;
|
|
281
|
+
const model = activeApplicationForEnvironment(identity.environmentId, true)?.directoryModel;
|
|
282
|
+
const organizationCreated = !workspaceInvite && directoryHasOrganizations(model) && (!allowOrganizationless || role?.allowsOrganizationlessSignup !== true);
|
|
283
|
+
if (maps.identities.has(identity.id) || (organizationCreated && (maps.identities.has(organization.id) || maps.directoryMemberships.has(membership.id))))
|
|
284
|
+
fail("duplicate", "Duplicate key.");
|
|
285
|
+
saveIdentity(maps, identity);
|
|
286
|
+
if (workspaceInvite && identity.emailVerifiedAt && !applyWorkspaceInvitation(workspaceInvite.invite, workspaceInvite.admission, identity, asTime(identity.createdAt))) {
|
|
287
|
+
fail("challenge_invalid", "The invited workspace or role is no longer available.");
|
|
288
|
+
}
|
|
289
|
+
if (hostedSsoLink && (hostedSsoLink.identityId !== identity.id || hostedSsoLink.environmentId !== identity.environmentId ||
|
|
290
|
+
!saveHostedSsoLink(maps, hostedSsoLink))) {
|
|
291
|
+
fail("conflict", "This provider account is linked to another profile.");
|
|
292
|
+
}
|
|
293
|
+
if (organizationCreated) {
|
|
294
|
+
saveIdentity(maps, organization);
|
|
295
|
+
save(maps.directoryMemberships, { ...membership, createdAt: asTime(membership.createdAt) });
|
|
296
|
+
}
|
|
297
|
+
if (challenge)
|
|
298
|
+
addChallenge(maps, challenge);
|
|
299
|
+
if (roleId) {
|
|
300
|
+
if (maps.bindings.has(bindingId))
|
|
301
|
+
fail("duplicate", "Duplicate key.");
|
|
302
|
+
save(maps.bindings, {
|
|
303
|
+
id: bindingId, environmentId: identity.environmentId, roleId, subjectType: "identity",
|
|
304
|
+
subjectId: identity.id, organizationId: null, resource: null, createdAt: asTime(identity.createdAt),
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
insertNotification(identity, notificationDelivery);
|
|
308
|
+
return { organizationCreated };
|
|
309
|
+
});
|
|
310
|
+
},
|
|
311
|
+
async getDirectorySignupRole(environmentId) { return source().signupRoles.get(environmentId) ?? null; },
|
|
312
|
+
async findHostedSsoIdentity(environmentId, authority, subject) {
|
|
313
|
+
const row = source().hostedSsoIdentities.get(JSON.stringify([environmentId, authority, subject]));
|
|
314
|
+
return row ? clone(row) : null;
|
|
315
|
+
},
|
|
316
|
+
async saveHostedSsoIdentity(row) { return tx(() => saveHostedSsoLink(source(), row)); },
|
|
317
|
+
async setDirectorySignupRole(environmentId, roleId) {
|
|
318
|
+
tx(() => {
|
|
319
|
+
const maps = source();
|
|
320
|
+
if (roleId === null)
|
|
321
|
+
maps.signupRoles.delete(environmentId);
|
|
322
|
+
else {
|
|
323
|
+
if (maps.productRoles.get(roleId)?.environmentId !== environmentId)
|
|
324
|
+
fail("not_found", "The role is unavailable in this environment.");
|
|
325
|
+
maps.signupRoles.set(environmentId, roleId);
|
|
59
326
|
}
|
|
60
|
-
save(maps.identities, { ...row, metadata: clone(row.metadata), emailVerifiedAt: asTimeOrNull(row.emailVerifiedAt), createdAt: asTime(row.createdAt), updatedAt: asTime(row.updatedAt) });
|
|
61
327
|
});
|
|
62
328
|
},
|
|
63
329
|
async getDirectoryIdentity(id) {
|
|
@@ -70,6 +336,7 @@ export function bindDirectoryMemory(source, users, tx) {
|
|
|
70
336
|
},
|
|
71
337
|
async saveDirectoryMembership(row) {
|
|
72
338
|
tx(() => {
|
|
339
|
+
requireOrganizationScope(row.environmentId);
|
|
73
340
|
const maps = source();
|
|
74
341
|
const clash = [...maps.directoryMemberships.values()].find((item) => item.identityId === row.identityId && item.organizationId === row.organizationId && item.id !== row.id);
|
|
75
342
|
if (clash)
|
|
@@ -77,8 +344,104 @@ export function bindDirectoryMemory(source, users, tx) {
|
|
|
77
344
|
save(maps.directoryMemberships, { ...row, createdAt: asTime(row.createdAt) });
|
|
78
345
|
});
|
|
79
346
|
},
|
|
80
|
-
async deleteDirectoryMembership(id) {
|
|
347
|
+
async deleteDirectoryMembership(id) {
|
|
348
|
+
tx(() => {
|
|
349
|
+
const memberships = source().directoryMemberships;
|
|
350
|
+
const membership = memberships.get(id);
|
|
351
|
+
if (!membership)
|
|
352
|
+
fail("not_found", "Membership not found.");
|
|
353
|
+
if (membership.relation === "owner" && ![...memberships.values()].some((item) => item.id !== id && item.environmentId === membership.environmentId && item.organizationId === membership.organizationId && item.relation === "owner")) {
|
|
354
|
+
fail("last_owner", "An organization must keep an owner.");
|
|
355
|
+
}
|
|
356
|
+
memberships.delete(id);
|
|
357
|
+
});
|
|
358
|
+
},
|
|
81
359
|
async listDirectoryMemberships(environmentId) { return rows(source().directoryMemberships, (row) => row.environmentId === environmentId); },
|
|
360
|
+
async createDirectoryWorkspace(workspace, owner, organizationMembership) {
|
|
361
|
+
tx(() => {
|
|
362
|
+
const maps = source();
|
|
363
|
+
if (organizationMembership) {
|
|
364
|
+
requireOrganizationScope(organizationMembership.environmentId);
|
|
365
|
+
const clash = [...maps.directoryMemberships.values()].find((item) => item.id !== organizationMembership.id
|
|
366
|
+
&& item.identityId === organizationMembership.identityId && item.organizationId === organizationMembership.organizationId);
|
|
367
|
+
if (clash)
|
|
368
|
+
fail("duplicate", "Duplicate key.");
|
|
369
|
+
save(maps.directoryMemberships, { ...organizationMembership, createdAt: asTime(organizationMembership.createdAt) });
|
|
370
|
+
}
|
|
371
|
+
assertWorkspaceScope(workspace);
|
|
372
|
+
if (maps.workspaces.has(workspace.id))
|
|
373
|
+
fail("duplicate", "Duplicate workspace.");
|
|
374
|
+
if (owner.relation !== "owner" || !workspace.name.trim() ||
|
|
375
|
+
(workspace.status !== "active" && workspace.status !== "disabled"))
|
|
376
|
+
fail("conflict", "Invalid workspace or owner.");
|
|
377
|
+
save(maps.workspaces, { ...workspace, createdAt: asTime(workspace.createdAt), updatedAt: asTime(workspace.updatedAt) });
|
|
378
|
+
addWorkspaceMember(workspace, owner);
|
|
379
|
+
});
|
|
380
|
+
},
|
|
381
|
+
async getDirectoryWorkspace(environmentId, id) {
|
|
382
|
+
const workspace = source().workspaces.get(id);
|
|
383
|
+
return workspace?.environmentId === environmentId ? clone(workspace) : null;
|
|
384
|
+
},
|
|
385
|
+
async listDirectoryWorkspaces(environmentId) {
|
|
386
|
+
return rows(source().workspaces, (workspace) => workspace.environmentId === environmentId)
|
|
387
|
+
.sort((left, right) => left.createdAt.localeCompare(right.createdAt) || left.id.localeCompare(right.id));
|
|
388
|
+
},
|
|
389
|
+
async updateDirectoryWorkspace(environmentId, id, patch) {
|
|
390
|
+
return tx(() => {
|
|
391
|
+
const maps = source();
|
|
392
|
+
const workspace = maps.workspaces.get(id);
|
|
393
|
+
if (workspace?.environmentId !== environmentId)
|
|
394
|
+
return null;
|
|
395
|
+
const name = patch.name ?? workspace.name;
|
|
396
|
+
const status = patch.status ?? workspace.status;
|
|
397
|
+
if (!name.trim() || (status !== "active" && status !== "disabled"))
|
|
398
|
+
fail("conflict", "Invalid workspace name or status.");
|
|
399
|
+
if (patch.status === "active")
|
|
400
|
+
assertWorkspaceScope(workspace);
|
|
401
|
+
const updated = { ...workspace, name, status, updatedAt: asTime(patch.updatedAt) };
|
|
402
|
+
save(maps.workspaces, updated);
|
|
403
|
+
return updated;
|
|
404
|
+
});
|
|
405
|
+
},
|
|
406
|
+
async addDirectoryWorkspaceMembership(row) {
|
|
407
|
+
tx(() => {
|
|
408
|
+
const workspace = source().workspaces.get(row.workspaceId);
|
|
409
|
+
if (workspace?.environmentId !== row.environmentId || workspace.status !== "active")
|
|
410
|
+
fail("not_found", "Workspace not found.");
|
|
411
|
+
assertWorkspaceScope(workspace);
|
|
412
|
+
addWorkspaceMember(workspace, row);
|
|
413
|
+
});
|
|
414
|
+
},
|
|
415
|
+
async listDirectoryWorkspaceMemberships(environmentId) {
|
|
416
|
+
return rows(source().workspaceMemberships, (row) => row.environmentId === environmentId);
|
|
417
|
+
},
|
|
418
|
+
async removeDirectoryWorkspaceMembership(environmentId, workspaceId, membershipId) {
|
|
419
|
+
tx(() => {
|
|
420
|
+
const { workspaceMemberships } = source();
|
|
421
|
+
const membership = workspaceMemberships.get(membershipId);
|
|
422
|
+
if (membership?.environmentId !== environmentId || membership.workspaceId !== workspaceId)
|
|
423
|
+
fail("not_found", "Workspace membership not found.");
|
|
424
|
+
if (membership.relation === "owner" && ![...workspaceMemberships.values()]
|
|
425
|
+
.some((row) => row.workspaceId === workspaceId && row.relation === "owner" && row.id !== membershipId)) {
|
|
426
|
+
fail("last_owner", "A workspace must keep an owner.");
|
|
427
|
+
}
|
|
428
|
+
workspaceMemberships.delete(membershipId);
|
|
429
|
+
});
|
|
430
|
+
},
|
|
431
|
+
async getDirectoryOnboardingResponse(environmentId, target, subjectId) {
|
|
432
|
+
const response = source().onboardingResponses.get(JSON.stringify([environmentId, target, subjectId]));
|
|
433
|
+
return response ? clone(response) : null;
|
|
434
|
+
},
|
|
435
|
+
async saveDirectoryOnboardingResponse(row) {
|
|
436
|
+
tx(() => {
|
|
437
|
+
const maps = source();
|
|
438
|
+
const subject = row.target === "workspace" ? maps.workspaces.get(row.subjectId) : maps.identities.get(row.subjectId);
|
|
439
|
+
if (subject?.environmentId !== row.environmentId || (row.target !== "workspace" && (!("kind" in subject) || subject.kind !== row.target))) {
|
|
440
|
+
fail("not_found", "Onboarding subject not found.");
|
|
441
|
+
}
|
|
442
|
+
maps.onboardingResponses.set(JSON.stringify([row.environmentId, row.target, row.subjectId]), clone({ ...row, updatedAt: asTime(row.updatedAt) }));
|
|
443
|
+
});
|
|
444
|
+
},
|
|
82
445
|
async saveProductPermission(row) {
|
|
83
446
|
tx(() => {
|
|
84
447
|
const maps = source();
|
|
@@ -91,10 +454,71 @@ export function bindDirectoryMemory(source, users, tx) {
|
|
|
91
454
|
async deleteProductPermission(id) { tx(() => remove(source().permissions, id)); },
|
|
92
455
|
async listProductPermissions(environmentId) { return rows(source().permissions, (row) => row.environmentId === environmentId); },
|
|
93
456
|
async saveProductRole(row) {
|
|
94
|
-
tx(() =>
|
|
457
|
+
tx(() => {
|
|
458
|
+
const roles = source().productRoles;
|
|
459
|
+
const current = roles.get(row.id);
|
|
460
|
+
const next = { ...row, catalogRoleId: current ? current.catalogRoleId ?? null : row.catalogRoleId ?? null,
|
|
461
|
+
permissions: [...row.permissions], inherits: [...row.inherits], createdAt: asTime(row.createdAt) };
|
|
462
|
+
assertAvailableCatalogLink(next);
|
|
463
|
+
save(roles, next);
|
|
464
|
+
});
|
|
465
|
+
},
|
|
466
|
+
async deleteProductRole(id) {
|
|
467
|
+
tx(() => {
|
|
468
|
+
const maps = source();
|
|
469
|
+
const role = maps.productRoles.get(id);
|
|
470
|
+
if (role && maps.signupRoles.get(role.environmentId) === id)
|
|
471
|
+
maps.signupRoles.delete(role.environmentId);
|
|
472
|
+
remove(maps.productRoles, id);
|
|
473
|
+
for (const membership of maps.workspaceMemberships.values()) {
|
|
474
|
+
if (membership.roleId === id)
|
|
475
|
+
delete membership.roleId;
|
|
476
|
+
}
|
|
477
|
+
for (const link of maps.featureAssignments.values()) {
|
|
478
|
+
if (link.targetType === "role" && link.targetId === id)
|
|
479
|
+
maps.featureAssignments.delete(link.id);
|
|
480
|
+
}
|
|
481
|
+
});
|
|
95
482
|
},
|
|
96
|
-
async deleteProductRole(id) { tx(() => remove(source().productRoles, id)); },
|
|
97
483
|
async listProductRoles(environmentId) { return rows(source().productRoles, (row) => row.environmentId === environmentId); },
|
|
484
|
+
async insertTenantProductRole(row) {
|
|
485
|
+
tx(() => saveTenantRole({ ...row, createdAt: asTime(row.createdAt) }));
|
|
486
|
+
},
|
|
487
|
+
async listTenantProductRoles(orgId) { return rows(source().tenantProductRoles, (row) => row.orgId === orgId); },
|
|
488
|
+
async updateTenantProductRole(orgId, id, name, description) {
|
|
489
|
+
return tx(() => {
|
|
490
|
+
const current = source().tenantProductRoles.get(id);
|
|
491
|
+
if (!current || current.orgId !== orgId)
|
|
492
|
+
fail("not_found", "Catalog role not found.");
|
|
493
|
+
const updated = { ...current, name, description };
|
|
494
|
+
saveTenantRole(updated);
|
|
495
|
+
return clone(updated);
|
|
496
|
+
});
|
|
497
|
+
},
|
|
498
|
+
async deleteTenantProductRole(orgId, id) {
|
|
499
|
+
tx(() => {
|
|
500
|
+
const maps = source();
|
|
501
|
+
if (maps.tenantProductRoles.get(id)?.orgId !== orgId)
|
|
502
|
+
fail("not_found", "Catalog role not found.");
|
|
503
|
+
if ([...maps.productRoles.values()].some((item) => item.catalogRoleId === id))
|
|
504
|
+
fail("conflict", "This catalog role is still linked.");
|
|
505
|
+
remove(maps.tenantProductRoles, id);
|
|
506
|
+
});
|
|
507
|
+
},
|
|
508
|
+
async setProductRoleCatalogLink(environmentId, roleId, catalogRoleId, expectedCatalogRoleId) {
|
|
509
|
+
return tx(() => {
|
|
510
|
+
const roles = source().productRoles;
|
|
511
|
+
const current = roles.get(roleId);
|
|
512
|
+
if (!current || current.environmentId !== environmentId)
|
|
513
|
+
fail("not_found", "Role not found.");
|
|
514
|
+
if ((current.catalogRoleId ?? null) !== expectedCatalogRoleId)
|
|
515
|
+
fail("conflict", "The role link changed.");
|
|
516
|
+
const next = { ...current, catalogRoleId };
|
|
517
|
+
assertAvailableCatalogLink(next);
|
|
518
|
+
save(roles, next);
|
|
519
|
+
return clone(next);
|
|
520
|
+
});
|
|
521
|
+
},
|
|
98
522
|
async saveDirectoryGroup(row) { tx(() => save(source().groups, { ...row, createdAt: asTime(row.createdAt) })); },
|
|
99
523
|
async deleteDirectoryGroup(id) { tx(() => remove(source().groups, id)); },
|
|
100
524
|
async listDirectoryGroups(environmentId) { return rows(source().groups, (row) => row.environmentId === environmentId); },
|
|
@@ -109,7 +533,13 @@ export function bindDirectoryMemory(source, users, tx) {
|
|
|
109
533
|
},
|
|
110
534
|
async deleteGroupMember(id) { tx(() => remove(source().groupMembers, id)); },
|
|
111
535
|
async listGroupMembers(environmentId) { return rows(source().groupMembers, (row) => row.environmentId === environmentId); },
|
|
112
|
-
async saveRoleBinding(row) {
|
|
536
|
+
async saveRoleBinding(row) {
|
|
537
|
+
tx(() => {
|
|
538
|
+
if (row.subjectType === "organization" || row.organizationId !== null)
|
|
539
|
+
requireOrganizationScope(row.environmentId);
|
|
540
|
+
save(source().bindings, { ...row, createdAt: asTime(row.createdAt) });
|
|
541
|
+
});
|
|
542
|
+
},
|
|
113
543
|
async deleteRoleBinding(id) { tx(() => remove(source().bindings, id)); },
|
|
114
544
|
async listRoleBindings(environmentId) { return rows(source().bindings, (row) => row.environmentId === environmentId); },
|
|
115
545
|
async saveFeatureDefinition(row) {
|
|
@@ -118,46 +548,261 @@ export function bindDirectoryMemory(source, users, tx) {
|
|
|
118
548
|
const clash = [...maps.features.values()].find((item) => item.environmentId === row.environmentId && item.key === row.key && item.id !== row.id);
|
|
119
549
|
if (clash)
|
|
120
550
|
fail("duplicate", "Duplicate key.");
|
|
121
|
-
|
|
551
|
+
const kind = row.kind ?? maps.features.get(row.id)?.kind ?? "feature";
|
|
552
|
+
save(maps.features, { ...row, kind, config: clone(row.config), createdAt: asTime(row.createdAt) });
|
|
553
|
+
});
|
|
554
|
+
},
|
|
555
|
+
async updateFeatureDefinition(environmentId, id, changes) {
|
|
556
|
+
return tx(() => {
|
|
557
|
+
const features = source().features;
|
|
558
|
+
const current = features.get(id);
|
|
559
|
+
if (!current || current.environmentId !== environmentId)
|
|
560
|
+
fail("not_found", "Feature definition not found.");
|
|
561
|
+
const updated = { ...current, enabled: changes.enabled ?? current.enabled, config: changes.config ?? current.config };
|
|
562
|
+
save(features, updated);
|
|
563
|
+
return clone(updated);
|
|
564
|
+
});
|
|
565
|
+
},
|
|
566
|
+
async deleteFeatureDefinition(id) {
|
|
567
|
+
tx(() => {
|
|
568
|
+
const maps = source();
|
|
569
|
+
remove(maps.features, id);
|
|
570
|
+
for (const link of maps.featureAssignments.values()) {
|
|
571
|
+
if (link.featureId === id)
|
|
572
|
+
maps.featureAssignments.delete(link.id);
|
|
573
|
+
}
|
|
122
574
|
});
|
|
123
575
|
},
|
|
124
|
-
async deleteFeatureDefinition(id) { tx(() => remove(source().features, id)); },
|
|
125
576
|
async listFeatureDefinitions(environmentId) { return rows(source().features, (row) => row.environmentId === environmentId); },
|
|
126
|
-
async saveFeatureAssignment(row) {
|
|
577
|
+
async saveFeatureAssignment(row) {
|
|
578
|
+
tx(() => {
|
|
579
|
+
if (row.targetType === "organization")
|
|
580
|
+
requireOrganizationScope(row.environmentId);
|
|
581
|
+
const maps = source();
|
|
582
|
+
if (maps.features.get(row.featureId)?.environmentId !== row.environmentId)
|
|
583
|
+
fail("conflict", "Feature definition not found in this environment.");
|
|
584
|
+
if (row.targetType === "role") {
|
|
585
|
+
if (!row.targetId || maps.productRoles.get(row.targetId)?.environmentId !== row.environmentId)
|
|
586
|
+
fail("conflict", "Role not found in this environment.");
|
|
587
|
+
const duplicate = [...maps.featureAssignments.values()].some((item) => item.id !== row.id && item.targetType === "role"
|
|
588
|
+
&& item.featureId === row.featureId && item.targetId === row.targetId);
|
|
589
|
+
if (duplicate)
|
|
590
|
+
fail("duplicate", "This definition is already linked to the role.");
|
|
591
|
+
}
|
|
592
|
+
save(maps.featureAssignments, { ...row, config: clone(row.config), createdAt: asTime(row.createdAt) });
|
|
593
|
+
});
|
|
594
|
+
},
|
|
127
595
|
async deleteFeatureAssignment(id) { tx(() => remove(source().featureAssignments, id)); },
|
|
128
596
|
async listFeatureAssignments(environmentId) { return rows(source().featureAssignments, (row) => row.environmentId === environmentId); },
|
|
129
|
-
async
|
|
597
|
+
async insertAppWaitlist(row) {
|
|
598
|
+
return tx(() => {
|
|
599
|
+
const list = source().appWaitlist;
|
|
600
|
+
if ([...list.values()].some((item) => item.environmentId === row.environmentId && item.email.toLowerCase() === row.email.toLowerCase()))
|
|
601
|
+
return false;
|
|
602
|
+
save(list, { ...row, createdAt: asTime(row.createdAt) });
|
|
603
|
+
source().waitlistConfirmations.set(`app:${row.id}`, { claimedAt: null });
|
|
604
|
+
return true;
|
|
605
|
+
});
|
|
606
|
+
},
|
|
607
|
+
async saveAppWaitlist(row, challenge) {
|
|
130
608
|
tx(() => {
|
|
131
609
|
const maps = source();
|
|
132
610
|
const clash = [...maps.appWaitlist.values()].find((item) => item.environmentId === row.environmentId && item.email.toLowerCase() === row.email.toLowerCase() && item.id !== row.id);
|
|
133
611
|
if (clash)
|
|
134
612
|
fail("duplicate_email", "Email already exists.");
|
|
613
|
+
if (row.invitation && !workspaceInvitationTarget(row))
|
|
614
|
+
fail("conflict", "The invited workspace or role is unavailable.");
|
|
135
615
|
save(maps.appWaitlist, { ...row, createdAt: asTime(row.createdAt) });
|
|
616
|
+
if (challenge) {
|
|
617
|
+
if (!activeApplicationForEnvironment(row.environmentId, true))
|
|
618
|
+
fail("not_found", "Application environment not found.");
|
|
619
|
+
addChallenge(maps, challenge);
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
},
|
|
623
|
+
async acceptDirectoryWorkspaceInvitation(identityId, at) {
|
|
624
|
+
return tx(() => {
|
|
625
|
+
const identity = source().identities.get(identityId);
|
|
626
|
+
return identity ? acceptWorkspaceInvitation(identity, asTime(at)) : false;
|
|
627
|
+
});
|
|
628
|
+
},
|
|
629
|
+
async acceptDirectoryWorkspaceInvitationByToken(environmentId, tokenHash, at) {
|
|
630
|
+
return tx(() => {
|
|
631
|
+
const maps = source();
|
|
632
|
+
const challenge = [...maps.directoryChallenges.values()].find((row) => row.tokenHash === tokenHash && row.environmentId === environmentId && row.purpose === "admit");
|
|
633
|
+
const identity = challenge && [...maps.identities.values()].find((row) => row.environmentId === environmentId && row.email?.toLowerCase() === challenge.email.toLowerCase());
|
|
634
|
+
const stamp = asTime(at);
|
|
635
|
+
if (!identity || !acceptWorkspaceInvitation(identity, stamp, tokenHash))
|
|
636
|
+
return false;
|
|
637
|
+
identity.emailProofAt = stamp;
|
|
638
|
+
identity.updatedAt = stamp;
|
|
639
|
+
return true;
|
|
136
640
|
});
|
|
137
641
|
},
|
|
138
642
|
async listAppWaitlist(environmentId) { return rows(source().appWaitlist, (row) => row.environmentId === environmentId); },
|
|
139
643
|
async findAppWaitlist(environmentId, email) {
|
|
140
644
|
return rows(source().appWaitlist, (row) => row.environmentId === environmentId && row.email.toLowerCase() === email.toLowerCase())[0] ?? null;
|
|
141
645
|
},
|
|
142
|
-
async
|
|
646
|
+
async claimWaitlistConfirmation(list, entryId, at, staleAt) {
|
|
647
|
+
return tx(() => {
|
|
648
|
+
const maps = source();
|
|
649
|
+
const row = (list === "app" ? maps.appWaitlist : maps.platformWaitlist).get(entryId);
|
|
650
|
+
const key = `${list}:${entryId}`;
|
|
651
|
+
const pending = maps.waitlistConfirmations.get(key);
|
|
652
|
+
if (row?.status !== "waiting" || !pending || (pending.claimedAt !== null && pending.claimedAt > asTime(staleAt)))
|
|
653
|
+
return false;
|
|
654
|
+
maps.waitlistConfirmations.set(key, { claimedAt: asTime(at) });
|
|
655
|
+
return true;
|
|
656
|
+
});
|
|
657
|
+
},
|
|
658
|
+
async finishWaitlistConfirmation(list, entryId, claimedAt, accepted) {
|
|
143
659
|
tx(() => {
|
|
660
|
+
const pending = source().waitlistConfirmations;
|
|
661
|
+
const key = `${list}:${entryId}`;
|
|
662
|
+
if (pending.get(key)?.claimedAt !== asTime(claimedAt))
|
|
663
|
+
return;
|
|
664
|
+
if (accepted)
|
|
665
|
+
pending.delete(key);
|
|
666
|
+
else
|
|
667
|
+
pending.set(key, { claimedAt: null });
|
|
668
|
+
});
|
|
669
|
+
},
|
|
670
|
+
async insertDirectoryChallenge(row, delivery) {
|
|
671
|
+
tx(() => {
|
|
672
|
+
if (row.environmentId !== "platform" && !activeApplicationForEnvironment(row.environmentId, true)) {
|
|
673
|
+
fail("not_found", "Application environment not found.");
|
|
674
|
+
}
|
|
144
675
|
const maps = source();
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
676
|
+
addChallenge(maps, row);
|
|
677
|
+
if (delivery) {
|
|
678
|
+
const identity = maps.identities.get(row.identityId);
|
|
679
|
+
if (!identity || identity.environmentId !== row.environmentId)
|
|
680
|
+
fail("conflict", "Notification scope changed.");
|
|
681
|
+
insertNotification(identity, delivery);
|
|
682
|
+
}
|
|
148
683
|
});
|
|
149
684
|
},
|
|
150
|
-
async
|
|
685
|
+
async insertDirectoryChallengeWithCooldown(row, after, delivery) {
|
|
686
|
+
return tx(() => {
|
|
687
|
+
const maps = source();
|
|
688
|
+
if (!activeApplicationForEnvironment(row.environmentId))
|
|
689
|
+
return false;
|
|
690
|
+
const identity = maps.identities.get(row.identityId);
|
|
691
|
+
if (identity?.status !== "active" || identity.environmentId !== row.environmentId || identity.email !== row.email)
|
|
692
|
+
return false;
|
|
693
|
+
const cutoff = asTime(after);
|
|
694
|
+
const recent = [...maps.directoryChallenges.values()].some((item) => item.environmentId === row.environmentId && item.identityId === row.identityId && item.purpose === row.purpose && item.createdAt > cutoff);
|
|
695
|
+
if (recent)
|
|
696
|
+
return false;
|
|
697
|
+
addChallenge(maps, row);
|
|
698
|
+
insertNotification(identity, delivery);
|
|
699
|
+
return true;
|
|
700
|
+
});
|
|
701
|
+
},
|
|
702
|
+
async takeDirectoryChallenge(tokenHash, at, scope) {
|
|
151
703
|
return tx(() => {
|
|
152
704
|
const maps = source();
|
|
153
705
|
const stamp = asTime(at);
|
|
154
706
|
const row = [...maps.directoryChallenges.values()].find((item) => item.tokenHash === tokenHash);
|
|
155
|
-
if (!row || row.usedAt || isExpired(row.expiresAt, stamp)
|
|
707
|
+
if (!row || row.usedAt || isExpired(row.expiresAt, stamp)
|
|
708
|
+
|| (scope?.environmentId && row.environmentId !== scope.environmentId) || (scope?.purpose && row.purpose !== scope.purpose))
|
|
709
|
+
return null;
|
|
710
|
+
if (row.environmentId !== "platform" && !activeApplicationForEnvironment(row.environmentId, true))
|
|
156
711
|
return null;
|
|
157
712
|
row.usedAt = stamp;
|
|
158
713
|
return clone(row);
|
|
159
714
|
});
|
|
160
715
|
},
|
|
716
|
+
async verifyDirectoryEmail(tokenHash, at, environmentId) {
|
|
717
|
+
return tx(() => {
|
|
718
|
+
const maps = source();
|
|
719
|
+
const stamp = asTime(at);
|
|
720
|
+
const row = [...maps.directoryChallenges.values()].find((item) => item.tokenHash === tokenHash);
|
|
721
|
+
if (!row || row.purpose !== "verify" || row.usedAt || isExpired(row.expiresAt, stamp) || (environmentId && row.environmentId !== environmentId))
|
|
722
|
+
return null;
|
|
723
|
+
const identity = maps.identities.get(row.identityId);
|
|
724
|
+
if (!identity || !activeApplicationForEnvironment(row.environmentId, true) || identity.environmentId !== row.environmentId || identity.email?.toLowerCase() !== row.email.toLowerCase() || identity.status !== "active")
|
|
725
|
+
return null;
|
|
726
|
+
row.usedAt = stamp;
|
|
727
|
+
recordEmailProof(identity, stamp);
|
|
728
|
+
acceptWorkspaceInvitation(identity, stamp);
|
|
729
|
+
return clone(identity);
|
|
730
|
+
});
|
|
731
|
+
},
|
|
161
732
|
};
|
|
162
733
|
}
|
|
734
|
+
function directoryRecordId(prefix) {
|
|
735
|
+
return `${prefix}_${randomBytes(16).toString("base64url")}`;
|
|
736
|
+
}
|
|
737
|
+
export function repairDirectoryStructure(maps, environmentIds, app, model, at) {
|
|
738
|
+
const stamp = asTime(at);
|
|
739
|
+
const hasOrganizations = directoryHasOrganizations(model);
|
|
740
|
+
const hasWorkspaces = directoryHasWorkspaces(model);
|
|
741
|
+
const repair = { organizations: 0, workspaces: 0 };
|
|
742
|
+
const environments = [...environmentIds].sort();
|
|
743
|
+
for (const environmentId of environments) {
|
|
744
|
+
const people = [...maps.identities.values()].filter((identity) => identity.appId === app.id && identity.environmentId === environmentId
|
|
745
|
+
&& (identity.kind === "user" || identity.kind === "account"))
|
|
746
|
+
.sort((left, right) => left.createdAt.localeCompare(right.createdAt) || left.id.localeCompare(right.id));
|
|
747
|
+
if (hasOrganizations) {
|
|
748
|
+
for (const person of people) {
|
|
749
|
+
if ([...maps.directoryMemberships.values()].some((item) => item.environmentId === environmentId && item.identityId === person.id))
|
|
750
|
+
continue;
|
|
751
|
+
const organizationId = directoryRecordId("idn");
|
|
752
|
+
const membershipId = directoryRecordId("dms");
|
|
753
|
+
maps.identities.set(organizationId, {
|
|
754
|
+
id: organizationId, appId: app.id, environmentId, orgId: app.orgId, kind: "organization", email: null, passwordHash: null,
|
|
755
|
+
displayName: person.displayName, status: "active", emailVerifiedAt: null, metadata: {}, createdAt: stamp, updatedAt: stamp,
|
|
756
|
+
});
|
|
757
|
+
maps.directoryMemberships.set(membershipId, {
|
|
758
|
+
id: membershipId, environmentId, identityId: person.id, organizationId, relation: "owner", createdAt: stamp,
|
|
759
|
+
});
|
|
760
|
+
repair.organizations += 1;
|
|
761
|
+
}
|
|
762
|
+
if (model === "organizations-and-workspaces") {
|
|
763
|
+
const orphans = [...maps.workspaces.values()].filter((workspace) => workspace.appId === app.id && workspace.environmentId === environmentId
|
|
764
|
+
&& workspace.organizationId === null)
|
|
765
|
+
.sort((left, right) => left.createdAt.localeCompare(right.createdAt) || left.id.localeCompare(right.id));
|
|
766
|
+
for (const workspace of orphans) {
|
|
767
|
+
const organizationId = directoryRecordId("idn");
|
|
768
|
+
maps.identities.set(organizationId, {
|
|
769
|
+
id: organizationId, appId: app.id, environmentId, orgId: app.orgId, kind: "organization", email: null, passwordHash: null,
|
|
770
|
+
displayName: workspace.name, status: "active", emailVerifiedAt: null, metadata: {}, createdAt: stamp, updatedAt: stamp,
|
|
771
|
+
});
|
|
772
|
+
for (const owner of [...maps.workspaceMemberships.values()].filter((item) => item.workspaceId === workspace.id && item.relation === "owner")) {
|
|
773
|
+
if ([...maps.directoryMemberships.values()].some((item) => item.environmentId === environmentId && item.identityId === owner.identityId && item.organizationId === organizationId))
|
|
774
|
+
continue;
|
|
775
|
+
const membershipId = directoryRecordId("dms");
|
|
776
|
+
maps.directoryMemberships.set(membershipId, {
|
|
777
|
+
id: membershipId, environmentId, identityId: owner.identityId, organizationId, relation: "member", createdAt: stamp,
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
maps.workspaces.set(workspace.id, { ...workspace, organizationId, updatedAt: stamp });
|
|
781
|
+
repair.organizations += 1;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
if (!hasWorkspaces)
|
|
786
|
+
continue;
|
|
787
|
+
for (const person of people) {
|
|
788
|
+
if ([...maps.workspaceMemberships.values()].some((item) => item.environmentId === environmentId && item.identityId === person.id))
|
|
789
|
+
continue;
|
|
790
|
+
const parent = hasOrganizations ? [...maps.directoryMemberships.values()].filter((item) => item.environmentId === environmentId && item.identityId === person.id)
|
|
791
|
+
.sort((left, right) => left.createdAt.localeCompare(right.createdAt) || left.id.localeCompare(right.id))[0] : undefined;
|
|
792
|
+
if (hasOrganizations && !parent)
|
|
793
|
+
continue;
|
|
794
|
+
const workspaceId = directoryRecordId("wsp");
|
|
795
|
+
maps.workspaces.set(workspaceId, {
|
|
796
|
+
id: workspaceId, appId: app.id, environmentId, organizationId: parent?.organizationId ?? null, name: person.displayName,
|
|
797
|
+
status: "active", createdAt: stamp, updatedAt: stamp,
|
|
798
|
+
});
|
|
799
|
+
const membershipId = directoryRecordId("wsm");
|
|
800
|
+
maps.workspaceMemberships.set(membershipId, {
|
|
801
|
+
id: membershipId, environmentId, workspaceId, identityId: person.id, relation: "owner", createdAt: stamp,
|
|
802
|
+
});
|
|
803
|
+
repair.workspaces += 1;
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
return repair;
|
|
807
|
+
}
|
|
163
808
|
//# sourceMappingURL=directory-memory.js.map
|