@x12i/keyrox-store 0.0.0-stage → 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.
- package/dist/access.d.ts +22 -0
- package/dist/access.d.ts.map +1 -0
- package/dist/access.js +28 -0
- package/dist/access.js.map +1 -0
- package/dist/directory-memory.d.ts +23 -0
- package/dist/directory-memory.d.ts.map +1 -0
- package/dist/directory-memory.js +163 -0
- package/dist/directory-memory.js.map +1 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +12 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/memory.d.ts +3 -0
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +882 -0
- package/dist/memory.js.map +1 -0
- package/dist/repository.d.ts +236 -0
- package/dist/repository.d.ts.map +1 -0
- package/dist/repository.js +2 -0
- package/dist/repository.js.map +1 -0
- package/dist/time.d.ts +5 -0
- package/dist/time.d.ts.map +1 -0
- package/dist/time.js +17 -0
- package/dist/time.js.map +1 -0
- package/package.json +32 -4
- package/README.md +0 -3
package/dist/memory.js
ADDED
|
@@ -0,0 +1,882 @@
|
|
|
1
|
+
import { decideLongLived, effectiveActions } from "./access.js";
|
|
2
|
+
import { fail } from "./errors.js";
|
|
3
|
+
import { bindDirectoryMemory, emptyDirectory } from "./directory-memory.js";
|
|
4
|
+
import { asTime, asTimeOrNull, clone, isExpired } from "./time.js";
|
|
5
|
+
const roles = new Set(["owner", "admin", "member"]);
|
|
6
|
+
function empty() {
|
|
7
|
+
return {
|
|
8
|
+
users: new Map(), orgs: new Map(), memberships: new Map(), challenges: new Map(), invites: new Map(),
|
|
9
|
+
sessions: new Map(), clients: new Map(), agents: new Map(), authorizations: new Map(), grants: new Map(),
|
|
10
|
+
instances: new Map(), executions: new Map(), longTokens: new Map(), refreshes: new Map(), providers: new Map(),
|
|
11
|
+
trusts: new Map(), audits: new Map(), deniedJtis: new Map(), idempotency: new Map(),
|
|
12
|
+
applications: new Map(), environments: new Map(), appTokens: new Map(),
|
|
13
|
+
signIn: new Map(), appSignIn: new Map(), redirects: new Map(), ssoLogins: new Map(), ssoExchanges: new Map(),
|
|
14
|
+
serviceCredentials: new Map(), principals: new Map(), workloadCredentials: new Map(), audiences: new Map(),
|
|
15
|
+
roles: new Map(), assignments: new Map(), gatewayRoutes: new Map(),
|
|
16
|
+
directory: emptyDirectory(),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function stampUser(user) {
|
|
20
|
+
return { ...user, lastOrgId: user.lastOrgId ?? null, emailVerifiedAt: asTimeOrNull(user.emailVerifiedAt), disabledAt: asTimeOrNull(user.disabledAt), createdAt: asTime(user.createdAt) };
|
|
21
|
+
}
|
|
22
|
+
function stampOrg(org) {
|
|
23
|
+
return { ...org, logoUrl: org.logoUrl ?? null, policy: { ...org.policy }, createdAt: asTime(org.createdAt) };
|
|
24
|
+
}
|
|
25
|
+
function stampApplication(row) {
|
|
26
|
+
return {
|
|
27
|
+
...row, allowedScopes: [...row.allowedScopes], allowedFeatures: [...row.allowedFeatures],
|
|
28
|
+
productName: row.productName || row.title, logoUrl: row.logoUrl ?? null, accentColor: row.accentColor ?? null,
|
|
29
|
+
signupMode: row.signupMode ?? "open", allowRegistrationWithoutPersonalOrg: row.allowRegistrationWithoutPersonalOrg ?? false,
|
|
30
|
+
createdAt: asTime(row.createdAt), updatedAt: asTime(row.updatedAt),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function stampEnvironment(row) {
|
|
34
|
+
return {
|
|
35
|
+
...row, previousKeyExpiresAt: asTimeOrNull(row.previousKeyExpiresAt), createdAt: asTime(row.createdAt), updatedAt: asTime(row.updatedAt),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function stampAppToken(row) {
|
|
39
|
+
return { ...row, payload: clone(row.payload), revokedAt: asTimeOrNull(row.revokedAt), expiresAt: asTimeOrNull(row.expiresAt), createdAt: asTime(row.createdAt) };
|
|
40
|
+
}
|
|
41
|
+
function stampMembership(membership) {
|
|
42
|
+
if (!roles.has(membership.role))
|
|
43
|
+
fail("conflict", "Invalid role.");
|
|
44
|
+
return {
|
|
45
|
+
...membership,
|
|
46
|
+
productionAccess: membership.role === "owner" || membership.productionAccess === true,
|
|
47
|
+
actions: [...membership.actions],
|
|
48
|
+
createdAt: asTime(membership.createdAt),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function auditMatches(row, query) {
|
|
52
|
+
if (row.orgId !== query.orgId)
|
|
53
|
+
return false;
|
|
54
|
+
if (query.action && row.action !== query.action)
|
|
55
|
+
return false;
|
|
56
|
+
if (query.subject && row.actorUserId !== query.subject && row.target !== query.subject)
|
|
57
|
+
return false;
|
|
58
|
+
if (query.source && !row.detail.includes(`source=${query.source}`))
|
|
59
|
+
return false;
|
|
60
|
+
if (query.from && row.at < query.from)
|
|
61
|
+
return false;
|
|
62
|
+
if (query.to && row.at > query.to)
|
|
63
|
+
return false;
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
function takeExpiring(map, match, at) {
|
|
67
|
+
const row = [...map.values()].find(match);
|
|
68
|
+
if (!row)
|
|
69
|
+
return null;
|
|
70
|
+
map.delete(row.id);
|
|
71
|
+
if (isExpired(row.expiresAt, asTime(at)))
|
|
72
|
+
return null;
|
|
73
|
+
return clone(row);
|
|
74
|
+
}
|
|
75
|
+
export function createMemoryStore() {
|
|
76
|
+
let tables = empty();
|
|
77
|
+
const tx = (run) => {
|
|
78
|
+
const saved = clone(tables);
|
|
79
|
+
try {
|
|
80
|
+
return run();
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
tables = saved;
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const one = (map, id) => {
|
|
88
|
+
const row = map.get(id);
|
|
89
|
+
return row ? clone(row) : null;
|
|
90
|
+
};
|
|
91
|
+
const rows = (map, pred = () => true) => [...map.values()].filter(pred).map((row) => clone(row));
|
|
92
|
+
const put = (map, row) => {
|
|
93
|
+
if (map.has(row.id))
|
|
94
|
+
fail("duplicate", "Duplicate key.");
|
|
95
|
+
map.set(row.id, clone(row));
|
|
96
|
+
};
|
|
97
|
+
const replace = (map, row) => {
|
|
98
|
+
if (!map.has(row.id))
|
|
99
|
+
fail("not_found", "Record not found.");
|
|
100
|
+
map.set(row.id, clone(row));
|
|
101
|
+
};
|
|
102
|
+
const user = (id) => tables.users.get(id) ?? fail("conflict", "User is missing.");
|
|
103
|
+
const org = (id) => tables.orgs.get(id) ?? fail("conflict", "Organization is missing.");
|
|
104
|
+
const client = (id) => tables.clients.get(id) ?? fail("conflict", "Client is missing.");
|
|
105
|
+
const agent = (id) => tables.agents.get(id) ?? fail("conflict", "Agent is missing.");
|
|
106
|
+
const grantRow = (id) => tables.grants.get(id) ?? fail("conflict", "Grant is missing.");
|
|
107
|
+
const membershipsForUser = (userId) => rows(tables.memberships, (row) => row.userId === userId);
|
|
108
|
+
const revokeGrant = (id, at) => {
|
|
109
|
+
const grant = tables.grants.get(id);
|
|
110
|
+
if (!grant)
|
|
111
|
+
return null;
|
|
112
|
+
const changed = grant.revokedAt === null;
|
|
113
|
+
if (changed)
|
|
114
|
+
grant.revokedAt = asTime(at);
|
|
115
|
+
for (const token of tables.longTokens.values()) {
|
|
116
|
+
if (token.grantId === id && token.revokedAt === null)
|
|
117
|
+
token.revokedAt = grant.revokedAt;
|
|
118
|
+
}
|
|
119
|
+
for (const refresh of tables.refreshes.values()) {
|
|
120
|
+
if (refresh.grantId === id && refresh.revokedAt === null)
|
|
121
|
+
refresh.revokedAt = grant.revokedAt;
|
|
122
|
+
}
|
|
123
|
+
return { grant: clone(grant), changed };
|
|
124
|
+
};
|
|
125
|
+
const store = {
|
|
126
|
+
async signup(input) {
|
|
127
|
+
tx(() => {
|
|
128
|
+
const nextUser = stampUser(input.user);
|
|
129
|
+
const nextOrg = stampOrg(input.org);
|
|
130
|
+
const nextMembership = stampMembership(input.membership);
|
|
131
|
+
if (nextMembership.userId !== nextUser.id || nextMembership.orgId !== nextOrg.id || nextMembership.role !== "owner") {
|
|
132
|
+
fail("conflict", "Signup requires the new organization and its owner membership.");
|
|
133
|
+
}
|
|
134
|
+
if ([...tables.users.values()].some((row) => row.email.toLowerCase() === nextUser.email.toLowerCase()))
|
|
135
|
+
fail("duplicate_email", "Email already exists.");
|
|
136
|
+
if ([...tables.orgs.values()].some((row) => row.slug === nextOrg.slug))
|
|
137
|
+
fail("duplicate_slug", "Slug already exists.");
|
|
138
|
+
put(tables.users, nextUser);
|
|
139
|
+
put(tables.orgs, nextOrg);
|
|
140
|
+
put(tables.memberships, nextMembership);
|
|
141
|
+
});
|
|
142
|
+
},
|
|
143
|
+
async createOrg(input) {
|
|
144
|
+
tx(() => {
|
|
145
|
+
const nextOrg = stampOrg(input.org);
|
|
146
|
+
const nextMembership = stampMembership(input.membership);
|
|
147
|
+
if (nextMembership.orgId !== nextOrg.id || nextMembership.role !== "owner")
|
|
148
|
+
fail("conflict", "A new organization needs an owner membership.");
|
|
149
|
+
user(nextMembership.userId);
|
|
150
|
+
if ([...tables.orgs.values()].some((row) => row.slug === nextOrg.slug))
|
|
151
|
+
fail("duplicate_slug", "Slug already exists.");
|
|
152
|
+
put(tables.orgs, nextOrg);
|
|
153
|
+
put(tables.memberships, nextMembership);
|
|
154
|
+
});
|
|
155
|
+
},
|
|
156
|
+
async getUser(id) { return one(tables.users, id); },
|
|
157
|
+
async findUserByEmail(email) {
|
|
158
|
+
return rows(tables.users, (row) => row.email.toLowerCase() === email.toLowerCase())[0] ?? null;
|
|
159
|
+
},
|
|
160
|
+
async updateUser(next) { tx(() => replace(tables.users, stampUser(next))); },
|
|
161
|
+
async getOrg(id) { return one(tables.orgs, id); },
|
|
162
|
+
async findOrgBySlug(slug) { return rows(tables.orgs, (row) => row.slug === slug)[0] ?? null; },
|
|
163
|
+
async updateOrg(next) {
|
|
164
|
+
tx(() => {
|
|
165
|
+
org(next.id);
|
|
166
|
+
replace(tables.orgs, stampOrg(next));
|
|
167
|
+
});
|
|
168
|
+
},
|
|
169
|
+
async findMembership(userId, orgId) {
|
|
170
|
+
return rows(tables.memberships, (row) => row.userId === userId && row.orgId === orgId)[0] ?? null;
|
|
171
|
+
},
|
|
172
|
+
async listMembershipsByUser(userId) { return membershipsForUser(userId); },
|
|
173
|
+
async listMembershipsByOrg(orgId) { return rows(tables.memberships, (row) => row.orgId === orgId); },
|
|
174
|
+
async saveMembership(membership) {
|
|
175
|
+
tx(() => {
|
|
176
|
+
const next = stampMembership(membership);
|
|
177
|
+
const existing = tables.memberships.get(next.id);
|
|
178
|
+
if (!existing || existing.orgId !== next.orgId || existing.userId !== next.userId)
|
|
179
|
+
fail("not_found", "Member not found.");
|
|
180
|
+
if (existing.role === "owner" && next.role !== "owner") {
|
|
181
|
+
const owners = rows(tables.memberships, (row) => row.orgId === existing.orgId && row.role === "owner");
|
|
182
|
+
if (owners.length < 2)
|
|
183
|
+
fail("last_owner", "An organization must keep an owner.");
|
|
184
|
+
}
|
|
185
|
+
replace(tables.memberships, next);
|
|
186
|
+
});
|
|
187
|
+
},
|
|
188
|
+
async removeMembership(input) {
|
|
189
|
+
tx(() => {
|
|
190
|
+
const at = asTime(input.at);
|
|
191
|
+
const existing = [...tables.memberships.values()].find((row) => row.userId === input.userId && row.orgId === input.orgId);
|
|
192
|
+
if (!existing)
|
|
193
|
+
fail("not_found", "Member not found.");
|
|
194
|
+
user(input.userId);
|
|
195
|
+
if (membershipsForUser(input.userId).length < 2)
|
|
196
|
+
fail("last_membership", "An account must belong to at least one organization.");
|
|
197
|
+
if (existing.role === "owner" && rows(tables.memberships, (row) => row.orgId === input.orgId && row.role === "owner").length < 2) {
|
|
198
|
+
fail("last_owner", "An organization must keep an owner.");
|
|
199
|
+
}
|
|
200
|
+
tables.memberships.delete(existing.id);
|
|
201
|
+
for (const grant of [...tables.grants.values()].filter((row) => row.userId === input.userId && row.orgId === input.orgId))
|
|
202
|
+
revokeGrant(grant.id, at);
|
|
203
|
+
for (const token of tables.longTokens.values()) {
|
|
204
|
+
if (token.userId === input.userId && token.orgId === input.orgId && token.revokedAt === null)
|
|
205
|
+
token.revokedAt = at;
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
},
|
|
209
|
+
async insertChallenge(row) {
|
|
210
|
+
tx(() => {
|
|
211
|
+
user(row.userId);
|
|
212
|
+
if (row.purpose !== "verify" && row.purpose !== "reset")
|
|
213
|
+
fail("conflict", "Invalid challenge.");
|
|
214
|
+
if ([...tables.challenges.values()].some((item) => item.tokenHash === row.tokenHash))
|
|
215
|
+
fail("duplicate", "Duplicate key.");
|
|
216
|
+
put(tables.challenges, { ...row, expiresAt: asTime(row.expiresAt), usedAt: asTimeOrNull(row.usedAt), createdAt: asTime(row.createdAt) });
|
|
217
|
+
});
|
|
218
|
+
},
|
|
219
|
+
async verifyEmail(tokenHash, at) {
|
|
220
|
+
return tx(() => {
|
|
221
|
+
const stamp = asTime(at);
|
|
222
|
+
const challenge = [...tables.challenges.values()].find((row) => row.tokenHash === tokenHash && row.purpose === "verify");
|
|
223
|
+
if (!challenge || challenge.usedAt || isExpired(challenge.expiresAt, stamp))
|
|
224
|
+
fail("challenge_invalid", "Verification link is no longer valid.");
|
|
225
|
+
const account = tables.users.get(challenge.userId);
|
|
226
|
+
if (!account)
|
|
227
|
+
fail("challenge_invalid", "Verification link is no longer valid.");
|
|
228
|
+
account.emailVerifiedAt = stamp;
|
|
229
|
+
challenge.usedAt = stamp;
|
|
230
|
+
return clone(account);
|
|
231
|
+
});
|
|
232
|
+
},
|
|
233
|
+
async resetPassword(input) {
|
|
234
|
+
return tx(() => {
|
|
235
|
+
const stamp = asTime(input.at);
|
|
236
|
+
const challenge = [...tables.challenges.values()].find((row) => row.tokenHash === input.tokenHash && row.purpose === "reset");
|
|
237
|
+
if (!challenge || challenge.usedAt || isExpired(challenge.expiresAt, stamp))
|
|
238
|
+
fail("challenge_invalid", "Reset link is no longer valid.");
|
|
239
|
+
const account = tables.users.get(challenge.userId);
|
|
240
|
+
if (!account)
|
|
241
|
+
fail("challenge_invalid", "Reset link is no longer valid.");
|
|
242
|
+
account.passwordHash = input.passwordHash;
|
|
243
|
+
if (!account.emailVerifiedAt)
|
|
244
|
+
account.emailVerifiedAt = stamp;
|
|
245
|
+
challenge.usedAt = stamp;
|
|
246
|
+
for (const session of [...tables.sessions.values()]) {
|
|
247
|
+
if (session.userId === account.id)
|
|
248
|
+
tables.sessions.delete(session.id);
|
|
249
|
+
}
|
|
250
|
+
return clone(account);
|
|
251
|
+
});
|
|
252
|
+
},
|
|
253
|
+
async insertInvite(row) {
|
|
254
|
+
tx(() => {
|
|
255
|
+
org(row.orgId);
|
|
256
|
+
user(row.invitedBy);
|
|
257
|
+
if (!roles.has(row.role))
|
|
258
|
+
fail("conflict", "Invalid role.");
|
|
259
|
+
if ([...tables.invites.values()].some((item) => item.tokenHash === row.tokenHash))
|
|
260
|
+
fail("duplicate", "Duplicate key.");
|
|
261
|
+
put(tables.invites, { ...row, actions: [...row.actions], expiresAt: asTime(row.expiresAt), acceptedAt: asTimeOrNull(row.acceptedAt), createdAt: asTime(row.createdAt) });
|
|
262
|
+
});
|
|
263
|
+
},
|
|
264
|
+
async acceptInvite(input) {
|
|
265
|
+
return tx(() => {
|
|
266
|
+
const at = asTime(input.at);
|
|
267
|
+
const invite = [...tables.invites.values()].find((row) => row.tokenHash === input.tokenHash);
|
|
268
|
+
if (!invite || invite.acceptedAt || isExpired(invite.expiresAt, at))
|
|
269
|
+
fail("invite_invalid", "Invite is no longer valid.");
|
|
270
|
+
const account = tables.users.get(input.userId);
|
|
271
|
+
if (!account)
|
|
272
|
+
fail("not_found", "User not found.");
|
|
273
|
+
if (invite.email.toLowerCase() !== input.email.toLowerCase() || invite.email.toLowerCase() !== account.email.toLowerCase()) {
|
|
274
|
+
fail("invite_email", "Invite email does not match the account.");
|
|
275
|
+
}
|
|
276
|
+
org(invite.orgId);
|
|
277
|
+
const existing = [...tables.memberships.values()].find((row) => row.userId === account.id && row.orgId === invite.orgId);
|
|
278
|
+
if (!existing) {
|
|
279
|
+
put(tables.memberships, stampMembership({
|
|
280
|
+
id: input.membershipId, orgId: invite.orgId, userId: account.id, role: invite.role, actions: [...invite.actions],
|
|
281
|
+
productionAccess: invite.productionAccess === true, createdAt: at,
|
|
282
|
+
}));
|
|
283
|
+
}
|
|
284
|
+
invite.acceptedAt = at;
|
|
285
|
+
return clone(invite);
|
|
286
|
+
});
|
|
287
|
+
},
|
|
288
|
+
async insertSession(row) {
|
|
289
|
+
tx(() => {
|
|
290
|
+
user(row.userId);
|
|
291
|
+
org(row.orgId);
|
|
292
|
+
if ([...tables.sessions.values()].some((item) => item.secretHash === row.secretHash))
|
|
293
|
+
fail("duplicate", "Duplicate key.");
|
|
294
|
+
put(tables.sessions, { ...row, expiresAt: asTime(row.expiresAt), createdAt: asTime(row.createdAt) });
|
|
295
|
+
});
|
|
296
|
+
},
|
|
297
|
+
async findSessionByHash(secretHash) { return rows(tables.sessions, (row) => row.secretHash === secretHash)[0] ?? null; },
|
|
298
|
+
async updateSession(session) {
|
|
299
|
+
tx(() => {
|
|
300
|
+
const existing = tables.sessions.get(session.id);
|
|
301
|
+
if (!existing)
|
|
302
|
+
fail("not_found", "Session not found.");
|
|
303
|
+
user(session.userId);
|
|
304
|
+
org(session.orgId);
|
|
305
|
+
replace(tables.sessions, { ...session, expiresAt: asTime(session.expiresAt), createdAt: asTime(session.createdAt) });
|
|
306
|
+
});
|
|
307
|
+
},
|
|
308
|
+
async deleteSession(id) { tx(() => { tables.sessions.delete(id); }); },
|
|
309
|
+
async insertClient(row) {
|
|
310
|
+
tx(() => {
|
|
311
|
+
if (row.authMethod !== "none" && row.authMethod !== "client_secret_basic")
|
|
312
|
+
fail("conflict", "Invalid client.");
|
|
313
|
+
put(tables.clients, { ...row, redirectUris: [...row.redirectUris], createdAt: asTime(row.createdAt) });
|
|
314
|
+
});
|
|
315
|
+
},
|
|
316
|
+
async getClient(id) { return one(tables.clients, id); },
|
|
317
|
+
async insertAgent(row) {
|
|
318
|
+
tx(() => {
|
|
319
|
+
client(row.clientId);
|
|
320
|
+
user(row.ownerUserId);
|
|
321
|
+
org(row.orgId);
|
|
322
|
+
if ([...tables.agents.values()].some((item) => item.clientId === row.clientId && item.ownerUserId === row.ownerUserId && item.orgId === row.orgId)) {
|
|
323
|
+
fail("duplicate", "Duplicate key.");
|
|
324
|
+
}
|
|
325
|
+
put(tables.agents, { ...row, suspendedAt: asTimeOrNull(row.suspendedAt), createdAt: asTime(row.createdAt) });
|
|
326
|
+
});
|
|
327
|
+
},
|
|
328
|
+
async getAgent(id) { return one(tables.agents, id); },
|
|
329
|
+
async findAgentByClientOwner(clientId, ownerUserId, orgId) {
|
|
330
|
+
return rows(tables.agents, (row) => row.clientId === clientId && row.ownerUserId === ownerUserId && row.orgId === orgId)[0] ?? null;
|
|
331
|
+
},
|
|
332
|
+
async listAgentsByOrg(orgId) { return rows(tables.agents, (row) => row.orgId === orgId); },
|
|
333
|
+
async suspendAgent(id, at) {
|
|
334
|
+
return tx(() => {
|
|
335
|
+
const stamp = asTime(at);
|
|
336
|
+
const existing = tables.agents.get(id);
|
|
337
|
+
if (!existing)
|
|
338
|
+
return null;
|
|
339
|
+
existing.suspendedAt = stamp;
|
|
340
|
+
const revoked = [...tables.grants.values()]
|
|
341
|
+
.filter((row) => row.agentId === id)
|
|
342
|
+
.map((row) => revokeGrant(row.id, stamp))
|
|
343
|
+
.filter((row) => row !== null && row.changed)
|
|
344
|
+
.map((row) => row.grant);
|
|
345
|
+
return { agent: clone(existing), revoked };
|
|
346
|
+
});
|
|
347
|
+
},
|
|
348
|
+
async insertAuthorization(row) {
|
|
349
|
+
tx(() => {
|
|
350
|
+
client(row.clientId);
|
|
351
|
+
if (row.orgId)
|
|
352
|
+
org(row.orgId);
|
|
353
|
+
if (row.userId)
|
|
354
|
+
user(row.userId);
|
|
355
|
+
if (row.agentId)
|
|
356
|
+
agent(row.agentId);
|
|
357
|
+
if (row.grantId)
|
|
358
|
+
grantRow(row.grantId);
|
|
359
|
+
put(tables.authorizations, {
|
|
360
|
+
...row, actions: [...row.actions], resources: [...row.resources], lastPollAt: asTimeOrNull(row.lastPollAt),
|
|
361
|
+
expiresAt: asTime(row.expiresAt), createdAt: asTime(row.createdAt),
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
},
|
|
365
|
+
async getAuthorization(id) { return one(tables.authorizations, id); },
|
|
366
|
+
async findAuthorization(filter) {
|
|
367
|
+
return rows(tables.authorizations, (row) => (filter.codeHash === undefined || row.codeHash === filter.codeHash)
|
|
368
|
+
&& (filter.deviceCodeHash === undefined || row.deviceCodeHash === filter.deviceCodeHash)
|
|
369
|
+
&& (filter.userCodeHash === undefined || row.userCodeHash === filter.userCodeHash))[0] ?? null;
|
|
370
|
+
},
|
|
371
|
+
async updateAuthorization(row) {
|
|
372
|
+
tx(() => {
|
|
373
|
+
if (!tables.authorizations.has(row.id))
|
|
374
|
+
fail("not_found", "Authorization not found.");
|
|
375
|
+
client(row.clientId);
|
|
376
|
+
if (row.orgId)
|
|
377
|
+
org(row.orgId);
|
|
378
|
+
if (row.userId)
|
|
379
|
+
user(row.userId);
|
|
380
|
+
if (row.agentId)
|
|
381
|
+
agent(row.agentId);
|
|
382
|
+
if (row.grantId)
|
|
383
|
+
grantRow(row.grantId);
|
|
384
|
+
replace(tables.authorizations, {
|
|
385
|
+
...row, actions: [...row.actions], resources: [...row.resources], lastPollAt: asTimeOrNull(row.lastPollAt),
|
|
386
|
+
expiresAt: asTime(row.expiresAt), createdAt: asTime(row.createdAt),
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
},
|
|
390
|
+
async listAuthorizationsByStatus(status) { return rows(tables.authorizations, (row) => row.status === status); },
|
|
391
|
+
async insertGrant(row) {
|
|
392
|
+
tx(() => {
|
|
393
|
+
user(row.userId);
|
|
394
|
+
org(row.orgId);
|
|
395
|
+
client(row.clientId);
|
|
396
|
+
agent(row.agentId);
|
|
397
|
+
if (!tables.authorizations.has(row.approvalRequestId))
|
|
398
|
+
fail("conflict", "Authorization request is missing.");
|
|
399
|
+
put(tables.grants, {
|
|
400
|
+
...row, actions: [...row.actions], resources: [...row.resources], createdAt: asTime(row.createdAt),
|
|
401
|
+
expiresAt: asTime(row.expiresAt), revokedAt: asTimeOrNull(row.revokedAt),
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
},
|
|
405
|
+
async getGrant(id) { return one(tables.grants, id); },
|
|
406
|
+
async updateGrant(row) {
|
|
407
|
+
tx(() => {
|
|
408
|
+
if (!tables.grants.has(row.id))
|
|
409
|
+
fail("not_found", "Grant not found.");
|
|
410
|
+
user(row.userId);
|
|
411
|
+
org(row.orgId);
|
|
412
|
+
replace(tables.grants, {
|
|
413
|
+
...row, actions: [...row.actions], resources: [...row.resources], createdAt: asTime(row.createdAt),
|
|
414
|
+
expiresAt: asTime(row.expiresAt), revokedAt: asTimeOrNull(row.revokedAt),
|
|
415
|
+
});
|
|
416
|
+
});
|
|
417
|
+
},
|
|
418
|
+
async listGrants(filter) {
|
|
419
|
+
return rows(tables.grants, (row) => (filter.orgId === undefined || row.orgId === filter.orgId)
|
|
420
|
+
&& (filter.userId === undefined || row.userId === filter.userId)
|
|
421
|
+
&& (filter.agentId === undefined || row.agentId === filter.agentId));
|
|
422
|
+
},
|
|
423
|
+
async revokeGrant(id, at) { return tx(() => revokeGrant(id, at)); },
|
|
424
|
+
async insertLongToken(row) {
|
|
425
|
+
tx(() => {
|
|
426
|
+
user(row.userId);
|
|
427
|
+
org(row.orgId);
|
|
428
|
+
if (row.grantId)
|
|
429
|
+
grantRow(row.grantId);
|
|
430
|
+
if (row.agentId)
|
|
431
|
+
agent(row.agentId);
|
|
432
|
+
if (row.kind !== "personal" && row.kind !== "agent")
|
|
433
|
+
fail("conflict", "Invalid token.");
|
|
434
|
+
if ([...tables.longTokens.values()].some((item) => item.hash === row.hash))
|
|
435
|
+
fail("duplicate", "Duplicate key.");
|
|
436
|
+
put(tables.longTokens, {
|
|
437
|
+
...row, actions: [...row.actions], resources: [...row.resources], expiresAt: asTimeOrNull(row.expiresAt),
|
|
438
|
+
revokedAt: asTimeOrNull(row.revokedAt), createdAt: asTime(row.createdAt), lastUsedAt: asTimeOrNull(row.lastUsedAt),
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
},
|
|
442
|
+
async getLongToken(id) { return one(tables.longTokens, id); },
|
|
443
|
+
async findLongTokenByHash(hash) { return rows(tables.longTokens, (row) => row.hash === hash)[0] ?? null; },
|
|
444
|
+
async updateLongToken(row) {
|
|
445
|
+
tx(() => {
|
|
446
|
+
if (!tables.longTokens.has(row.id))
|
|
447
|
+
fail("not_found", "Token not found.");
|
|
448
|
+
replace(tables.longTokens, {
|
|
449
|
+
...row, actions: [...row.actions], resources: [...row.resources], expiresAt: asTimeOrNull(row.expiresAt),
|
|
450
|
+
revokedAt: asTimeOrNull(row.revokedAt), createdAt: asTime(row.createdAt), lastUsedAt: asTimeOrNull(row.lastUsedAt),
|
|
451
|
+
});
|
|
452
|
+
});
|
|
453
|
+
},
|
|
454
|
+
async listLongTokens(filter) {
|
|
455
|
+
return rows(tables.longTokens, (row) => (filter.orgId === undefined || row.orgId === filter.orgId)
|
|
456
|
+
&& (filter.userId === undefined || row.userId === filter.userId)
|
|
457
|
+
&& (filter.grantId === undefined || row.grantId === filter.grantId));
|
|
458
|
+
},
|
|
459
|
+
async revokeLongToken(id, at) {
|
|
460
|
+
return tx(() => {
|
|
461
|
+
const token = tables.longTokens.get(id);
|
|
462
|
+
if (!token)
|
|
463
|
+
return null;
|
|
464
|
+
const stamp = asTime(at);
|
|
465
|
+
const already = token.revokedAt !== null;
|
|
466
|
+
if (!already)
|
|
467
|
+
token.revokedAt = stamp;
|
|
468
|
+
const revocation = token.grantId && !already ? revokeGrant(token.grantId, stamp) : null;
|
|
469
|
+
return { token: clone(token), grant: revocation?.grant ?? null, grantChanged: revocation?.changed ?? false };
|
|
470
|
+
});
|
|
471
|
+
},
|
|
472
|
+
async longLivedAccess(hash, at) {
|
|
473
|
+
const stamp = asTime(at);
|
|
474
|
+
const token = rows(tables.longTokens, (row) => row.hash === hash)[0] ?? null;
|
|
475
|
+
return decideLongLived({
|
|
476
|
+
token,
|
|
477
|
+
user: token ? one(tables.users, token.userId) : null,
|
|
478
|
+
membership: token ? rows(tables.memberships, (row) => row.userId === token.userId && row.orgId === token.orgId)[0] ?? null : null,
|
|
479
|
+
grant: token?.grantId ? one(tables.grants, token.grantId) : null,
|
|
480
|
+
agent: token?.agentId ? one(tables.agents, token.agentId) : null,
|
|
481
|
+
at: stamp,
|
|
482
|
+
});
|
|
483
|
+
},
|
|
484
|
+
async insertRefresh(row) {
|
|
485
|
+
tx(() => {
|
|
486
|
+
user(row.userId);
|
|
487
|
+
org(row.orgId);
|
|
488
|
+
client(row.clientId);
|
|
489
|
+
if (row.grantId)
|
|
490
|
+
grantRow(row.grantId);
|
|
491
|
+
if (row.agentId)
|
|
492
|
+
agent(row.agentId);
|
|
493
|
+
if ([...tables.refreshes.values()].some((item) => item.hash === row.hash))
|
|
494
|
+
fail("duplicate", "Duplicate key.");
|
|
495
|
+
put(tables.refreshes, {
|
|
496
|
+
...row, actions: [...row.actions], resources: [...row.resources], expiresAt: asTime(row.expiresAt),
|
|
497
|
+
rotatedAt: asTimeOrNull(row.rotatedAt), revokedAt: asTimeOrNull(row.revokedAt), createdAt: asTime(row.createdAt),
|
|
498
|
+
});
|
|
499
|
+
});
|
|
500
|
+
},
|
|
501
|
+
async findRefreshByHash(hash) { return rows(tables.refreshes, (row) => row.hash === hash)[0] ?? null; },
|
|
502
|
+
async rotateRefresh(input) {
|
|
503
|
+
return tx(() => {
|
|
504
|
+
const at = asTime(input.at);
|
|
505
|
+
const current = [...tables.refreshes.values()].find((row) => row.hash === input.hash);
|
|
506
|
+
if (!current || current.clientId !== input.clientId)
|
|
507
|
+
return { status: "missing" };
|
|
508
|
+
if (current.rotatedAt || current.revokedAt) {
|
|
509
|
+
for (const refresh of tables.refreshes.values()) {
|
|
510
|
+
if (refresh.familyId === current.familyId && refresh.revokedAt === null)
|
|
511
|
+
refresh.revokedAt = at;
|
|
512
|
+
}
|
|
513
|
+
const revocation = current.grantId ? revokeGrant(current.grantId, at) : null;
|
|
514
|
+
return { status: "reuse", grant: revocation?.grant ?? null, grantChanged: revocation?.changed ?? false };
|
|
515
|
+
}
|
|
516
|
+
if (isExpired(current.expiresAt, at))
|
|
517
|
+
return { status: "expired" };
|
|
518
|
+
const account = tables.users.get(current.userId);
|
|
519
|
+
if (!account || account.disabledAt)
|
|
520
|
+
return { status: "inactive", reason: "user" };
|
|
521
|
+
const membership = [...tables.memberships.values()].find((row) => row.userId === current.userId && row.orgId === current.orgId) ?? null;
|
|
522
|
+
if (!membership)
|
|
523
|
+
return { status: "inactive", reason: "membership" };
|
|
524
|
+
const grant = current.grantId ? tables.grants.get(current.grantId) ?? null : null;
|
|
525
|
+
if (current.grantId && (!grant || grant.revokedAt || isExpired(grant.expiresAt, at)))
|
|
526
|
+
return { status: "inactive", reason: "grant" };
|
|
527
|
+
const actions = effectiveActions(membership, current.actions, grant);
|
|
528
|
+
if (!actions.length)
|
|
529
|
+
return { status: "inactive", reason: "actions" };
|
|
530
|
+
if (input.next.hash === current.hash || tables.refreshes.has(input.next.id) || [...tables.refreshes.values()].some((row) => row.hash === input.next.hash)) {
|
|
531
|
+
fail("duplicate", "Duplicate key.");
|
|
532
|
+
}
|
|
533
|
+
current.rotatedAt = at;
|
|
534
|
+
const next = {
|
|
535
|
+
id: input.next.id, hash: input.next.hash, familyId: current.familyId, grantId: current.grantId, clientId: current.clientId,
|
|
536
|
+
userId: current.userId, orgId: current.orgId, audience: current.audience, actions, resources: [...current.resources],
|
|
537
|
+
agentId: current.agentId, expiresAt: asTime(input.next.expiresAt), rotatedAt: null, revokedAt: null, createdAt: asTime(input.next.createdAt),
|
|
538
|
+
};
|
|
539
|
+
put(tables.refreshes, next);
|
|
540
|
+
return { status: "rotated", token: clone(next), actions: [...actions], grant: grant ? clone(grant) : null };
|
|
541
|
+
});
|
|
542
|
+
},
|
|
543
|
+
async revokeRefreshFamily(hash, at) {
|
|
544
|
+
tx(() => {
|
|
545
|
+
const stamp = asTime(at);
|
|
546
|
+
const current = [...tables.refreshes.values()].find((row) => row.hash === hash);
|
|
547
|
+
if (!current)
|
|
548
|
+
return;
|
|
549
|
+
for (const refresh of tables.refreshes.values()) {
|
|
550
|
+
if (refresh.familyId === current.familyId && refresh.revokedAt === null)
|
|
551
|
+
refresh.revokedAt = stamp;
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
},
|
|
555
|
+
async insertInstance(row) {
|
|
556
|
+
tx(() => {
|
|
557
|
+
agent(row.agentId);
|
|
558
|
+
grantRow(row.grantId);
|
|
559
|
+
put(tables.instances, { ...row, createdAt: asTime(row.createdAt), lastSeenAt: asTime(row.lastSeenAt) });
|
|
560
|
+
});
|
|
561
|
+
},
|
|
562
|
+
async insertExecution(row) {
|
|
563
|
+
tx(() => {
|
|
564
|
+
agent(row.agentId);
|
|
565
|
+
if (row.instanceId && !tables.instances.has(row.instanceId))
|
|
566
|
+
fail("conflict", "Instance is missing.");
|
|
567
|
+
if (row.grantId && !tables.grants.has(row.grantId))
|
|
568
|
+
fail("conflict", "Grant is missing.");
|
|
569
|
+
put(tables.executions, { ...row, createdAt: asTime(row.createdAt) });
|
|
570
|
+
});
|
|
571
|
+
},
|
|
572
|
+
async insertProvider(row) {
|
|
573
|
+
tx(() => {
|
|
574
|
+
user(row.userId);
|
|
575
|
+
org(row.orgId);
|
|
576
|
+
put(tables.providers, { ...row, scopes: [...row.scopes], revokedAt: asTimeOrNull(row.revokedAt), createdAt: asTime(row.createdAt) });
|
|
577
|
+
});
|
|
578
|
+
},
|
|
579
|
+
async getProvider(id) { return one(tables.providers, id); },
|
|
580
|
+
async listProviders(orgId, userId) {
|
|
581
|
+
return rows(tables.providers, (row) => row.orgId === orgId && row.userId === userId);
|
|
582
|
+
},
|
|
583
|
+
async insertTrust(row) {
|
|
584
|
+
tx(() => {
|
|
585
|
+
org(row.orgId);
|
|
586
|
+
put(tables.trusts, { ...row, actions: [...row.actions], jwks: clone(row.jwks), disabledAt: asTimeOrNull(row.disabledAt), createdAt: asTime(row.createdAt) });
|
|
587
|
+
});
|
|
588
|
+
},
|
|
589
|
+
async getTrust(id) { return one(tables.trusts, id); },
|
|
590
|
+
async listTrusts(issuer, audience) {
|
|
591
|
+
return rows(tables.trusts, (row) => row.issuer === issuer && row.audience === audience);
|
|
592
|
+
},
|
|
593
|
+
async insertAudit(row) {
|
|
594
|
+
tx(() => {
|
|
595
|
+
if (row.orgId)
|
|
596
|
+
org(row.orgId);
|
|
597
|
+
if (row.actorUserId)
|
|
598
|
+
user(row.actorUserId);
|
|
599
|
+
if (row.agentId)
|
|
600
|
+
agent(row.agentId);
|
|
601
|
+
if (row.decision !== "allow" && row.decision !== "deny")
|
|
602
|
+
fail("conflict", "Invalid audit decision.");
|
|
603
|
+
put(tables.audits, { ...row, at: asTime(row.at) });
|
|
604
|
+
});
|
|
605
|
+
},
|
|
606
|
+
async listAudit(query) {
|
|
607
|
+
return rows(tables.audits, (row) => auditMatches(row, query))
|
|
608
|
+
.sort((left, right) => right.at.localeCompare(left.at) || right.id.localeCompare(left.id))
|
|
609
|
+
.slice(query.offset, query.offset + query.limit);
|
|
610
|
+
},
|
|
611
|
+
async getDeniedJti(id) { return one(tables.deniedJtis, id); },
|
|
612
|
+
async denyJti(row) { tx(() => { if (!tables.deniedJtis.has(row.id))
|
|
613
|
+
tables.deniedJtis.set(row.id, { ...row }); }); },
|
|
614
|
+
async getIdempotency(id) { return one(tables.idempotency, id); },
|
|
615
|
+
async insertIdempotency(row) {
|
|
616
|
+
tx(() => put(tables.idempotency, { ...row, createdAt: asTime(row.createdAt) }));
|
|
617
|
+
},
|
|
618
|
+
async insertApplication(row) {
|
|
619
|
+
tx(() => {
|
|
620
|
+
org(row.orgId);
|
|
621
|
+
if (row.status !== "active" && row.status !== "disabled")
|
|
622
|
+
fail("conflict", "Invalid application status.");
|
|
623
|
+
put(tables.applications, stampApplication(row));
|
|
624
|
+
});
|
|
625
|
+
},
|
|
626
|
+
async getApplication(id) { return one(tables.applications, id); },
|
|
627
|
+
async listApplications(orgId) {
|
|
628
|
+
return rows(tables.applications, (row) => row.orgId === orgId).sort((left, right) => right.createdAt.localeCompare(left.createdAt) || right.id.localeCompare(left.id));
|
|
629
|
+
},
|
|
630
|
+
async updateApplication(row) {
|
|
631
|
+
tx(() => {
|
|
632
|
+
const current = tables.applications.get(row.id);
|
|
633
|
+
if (!current)
|
|
634
|
+
fail("not_found", "Application not found.");
|
|
635
|
+
if (current.orgId !== row.orgId)
|
|
636
|
+
fail("conflict", "An application cannot change organization.");
|
|
637
|
+
if (row.status !== "active" && row.status !== "disabled")
|
|
638
|
+
fail("conflict", "Invalid application status.");
|
|
639
|
+
replace(tables.applications, stampApplication(row));
|
|
640
|
+
});
|
|
641
|
+
},
|
|
642
|
+
async insertEnvironment(row) {
|
|
643
|
+
tx(() => {
|
|
644
|
+
const app = tables.applications.get(row.appId);
|
|
645
|
+
if (!app)
|
|
646
|
+
fail("conflict", "Application is missing.");
|
|
647
|
+
if (row.orgId !== app.orgId)
|
|
648
|
+
fail("conflict", "Environment tenant does not match the application.");
|
|
649
|
+
if (row.status !== "active" && row.status !== "disabled")
|
|
650
|
+
fail("conflict", "Invalid environment status.");
|
|
651
|
+
if ([...tables.environments.values()].some((item) => item.appId === row.appId && item.name === row.name))
|
|
652
|
+
fail("duplicate", "Environment already exists.");
|
|
653
|
+
put(tables.environments, stampEnvironment(row));
|
|
654
|
+
});
|
|
655
|
+
},
|
|
656
|
+
async getEnvironment(id) { return one(tables.environments, id); },
|
|
657
|
+
async listEnvironments(appId) {
|
|
658
|
+
return rows(tables.environments, (row) => row.appId === appId).sort((left, right) => left.createdAt.localeCompare(right.createdAt) || left.name.localeCompare(right.name));
|
|
659
|
+
},
|
|
660
|
+
async updateEnvironment(row) {
|
|
661
|
+
tx(() => {
|
|
662
|
+
const current = tables.environments.get(row.id);
|
|
663
|
+
if (!current)
|
|
664
|
+
fail("not_found", "Environment not found.");
|
|
665
|
+
if (current.appId !== row.appId || current.orgId !== row.orgId || current.name !== row.name)
|
|
666
|
+
fail("conflict", "An environment cannot change application or name.");
|
|
667
|
+
if (row.status !== "active" && row.status !== "disabled")
|
|
668
|
+
fail("conflict", "Invalid environment status.");
|
|
669
|
+
replace(tables.environments, stampEnvironment(row));
|
|
670
|
+
});
|
|
671
|
+
},
|
|
672
|
+
async insertAppToken(row) {
|
|
673
|
+
tx(() => {
|
|
674
|
+
const app = tables.applications.get(row.appId);
|
|
675
|
+
if (!app)
|
|
676
|
+
fail("conflict", "Application is missing.");
|
|
677
|
+
const environment = tables.environments.get(row.environmentId);
|
|
678
|
+
if (!environment || environment.appId !== row.appId)
|
|
679
|
+
fail("conflict", "Environment is missing.");
|
|
680
|
+
if (row.orgId !== app.orgId)
|
|
681
|
+
fail("conflict", "Token tenant does not match the application.");
|
|
682
|
+
put(tables.appTokens, stampAppToken(row));
|
|
683
|
+
});
|
|
684
|
+
},
|
|
685
|
+
async getAppToken(id) { return one(tables.appTokens, id); },
|
|
686
|
+
async listAppTokens(filter) {
|
|
687
|
+
return rows(tables.appTokens, (row) => (!filter.appId || row.appId === filter.appId) && (!filter.environmentId || row.environmentId === filter.environmentId) && (!filter.identityId || row.ownerIdentityId === filter.identityId) && (!filter.orgId || row.orgId === filter.orgId))
|
|
688
|
+
.sort((left, right) => right.createdAt.localeCompare(left.createdAt) || right.id.localeCompare(left.id));
|
|
689
|
+
},
|
|
690
|
+
async revokeAppToken(id, at) {
|
|
691
|
+
return tx(() => {
|
|
692
|
+
const row = tables.appTokens.get(id);
|
|
693
|
+
if (!row)
|
|
694
|
+
return null;
|
|
695
|
+
if (!row.revokedAt)
|
|
696
|
+
row.revokedAt = asTime(at);
|
|
697
|
+
return clone(row);
|
|
698
|
+
});
|
|
699
|
+
},
|
|
700
|
+
async revokeAppTokens(filter, at) {
|
|
701
|
+
return tx(() => {
|
|
702
|
+
const stamp = asTime(at);
|
|
703
|
+
let count = 0;
|
|
704
|
+
for (const row of tables.appTokens.values()) {
|
|
705
|
+
if (row.revokedAt)
|
|
706
|
+
continue;
|
|
707
|
+
if (filter.appId && row.appId !== filter.appId)
|
|
708
|
+
continue;
|
|
709
|
+
if (filter.environmentId && row.environmentId !== filter.environmentId)
|
|
710
|
+
continue;
|
|
711
|
+
if (filter.identityId && row.ownerIdentityId !== filter.identityId)
|
|
712
|
+
continue;
|
|
713
|
+
if (filter.orgId && row.orgId !== filter.orgId)
|
|
714
|
+
continue;
|
|
715
|
+
row.revokedAt = stamp;
|
|
716
|
+
count += 1;
|
|
717
|
+
}
|
|
718
|
+
return count;
|
|
719
|
+
});
|
|
720
|
+
},
|
|
721
|
+
async saveSignInConnection(row) {
|
|
722
|
+
tx(() => {
|
|
723
|
+
org(row.orgId);
|
|
724
|
+
const existing = [...tables.signIn.values()].find((item) => item.orgId === row.orgId && item.provider === row.provider);
|
|
725
|
+
tables.signIn.set(existing?.id ?? row.id, { ...row, id: existing?.id ?? row.id, createdAt: asTime(existing?.createdAt ?? row.createdAt) });
|
|
726
|
+
});
|
|
727
|
+
},
|
|
728
|
+
async listSignInConnections(orgId) {
|
|
729
|
+
return rows(tables.signIn, (row) => row.orgId === orgId).sort((left, right) => left.provider.localeCompare(right.provider));
|
|
730
|
+
},
|
|
731
|
+
async deleteSignInConnection(orgId, provider) {
|
|
732
|
+
tx(() => { for (const row of [...tables.signIn.values()])
|
|
733
|
+
if (row.orgId === orgId && row.provider === provider)
|
|
734
|
+
tables.signIn.delete(row.id); });
|
|
735
|
+
},
|
|
736
|
+
async saveAppSignIn(appId, providers) {
|
|
737
|
+
tx(() => {
|
|
738
|
+
if (!tables.applications.has(appId))
|
|
739
|
+
fail("conflict", "Application is missing.");
|
|
740
|
+
tables.appSignIn.set(appId, [...providers]);
|
|
741
|
+
});
|
|
742
|
+
},
|
|
743
|
+
async getAppSignIn(appId) { return [...(tables.appSignIn.get(appId) ?? [])]; },
|
|
744
|
+
async saveRedirects(environmentId, uris) {
|
|
745
|
+
tx(() => {
|
|
746
|
+
if (!tables.environments.has(environmentId))
|
|
747
|
+
fail("conflict", "Environment is missing.");
|
|
748
|
+
tables.redirects.set(environmentId, [...uris]);
|
|
749
|
+
});
|
|
750
|
+
},
|
|
751
|
+
async getRedirects(environmentId) { return [...(tables.redirects.get(environmentId) ?? [])]; },
|
|
752
|
+
async insertSsoLogin(row) { tx(() => put(tables.ssoLogins, { ...row, expiresAt: asTime(row.expiresAt), createdAt: asTime(row.createdAt) })); },
|
|
753
|
+
async takeSsoLogin(stateHash, at) {
|
|
754
|
+
return tx(() => takeExpiring(tables.ssoLogins, (row) => row.stateHash === stateHash, at));
|
|
755
|
+
},
|
|
756
|
+
async insertSsoExchange(row) { tx(() => put(tables.ssoExchanges, { ...row, expiresAt: asTime(row.expiresAt), createdAt: asTime(row.createdAt) })); },
|
|
757
|
+
async takeSsoExchange(codeHash, at) {
|
|
758
|
+
return tx(() => takeExpiring(tables.ssoExchanges, (row) => row.codeHash === codeHash, at));
|
|
759
|
+
},
|
|
760
|
+
async insertServiceCredential(row) {
|
|
761
|
+
tx(() => {
|
|
762
|
+
org(row.orgId);
|
|
763
|
+
if ([...tables.serviceCredentials.values()].some((item) => item.secretHash === row.secretHash))
|
|
764
|
+
fail("duplicate", "Duplicate key.");
|
|
765
|
+
put(tables.serviceCredentials, { ...row, appIds: [...row.appIds], permissions: [...row.permissions], revokedAt: asTimeOrNull(row.revokedAt), createdAt: asTime(row.createdAt) });
|
|
766
|
+
});
|
|
767
|
+
},
|
|
768
|
+
async listServiceCredentials(orgId) {
|
|
769
|
+
return rows(tables.serviceCredentials, (row) => row.orgId === orgId).sort((left, right) => right.createdAt.localeCompare(left.createdAt));
|
|
770
|
+
},
|
|
771
|
+
async findServiceCredentialByHash(hash) {
|
|
772
|
+
return rows(tables.serviceCredentials, (row) => row.secretHash === hash)[0] ?? null;
|
|
773
|
+
},
|
|
774
|
+
async revokeServiceCredential(id, at) {
|
|
775
|
+
return tx(() => {
|
|
776
|
+
const row = tables.serviceCredentials.get(id);
|
|
777
|
+
if (!row)
|
|
778
|
+
return null;
|
|
779
|
+
if (!row.revokedAt)
|
|
780
|
+
row.revokedAt = asTime(at);
|
|
781
|
+
return clone(row);
|
|
782
|
+
});
|
|
783
|
+
},
|
|
784
|
+
async insertPrincipal(row) {
|
|
785
|
+
tx(() => {
|
|
786
|
+
org(row.orgId);
|
|
787
|
+
if ([...tables.principals.values()].some((item) => item.clientId === row.clientId))
|
|
788
|
+
fail("duplicate", "Duplicate key.");
|
|
789
|
+
put(tables.principals, { ...row, createdAt: asTime(row.createdAt) });
|
|
790
|
+
});
|
|
791
|
+
},
|
|
792
|
+
async listPrincipals(orgId) {
|
|
793
|
+
return rows(tables.principals, (row) => row.orgId === orgId).sort((left, right) => right.createdAt.localeCompare(left.createdAt));
|
|
794
|
+
},
|
|
795
|
+
async findPrincipalByClientId(clientId) {
|
|
796
|
+
return rows(tables.principals, (row) => row.clientId === clientId)[0] ?? null;
|
|
797
|
+
},
|
|
798
|
+
async insertWorkloadCredential(row) {
|
|
799
|
+
tx(() => {
|
|
800
|
+
const principal = tables.principals.get(row.principalId);
|
|
801
|
+
if (!principal || principal.orgId !== row.orgId)
|
|
802
|
+
fail("conflict", "Workload principal is missing.");
|
|
803
|
+
put(tables.workloadCredentials, { ...row, jwks: clone(row.jwks), createdAt: asTime(row.createdAt) });
|
|
804
|
+
});
|
|
805
|
+
},
|
|
806
|
+
async listWorkloadCredentials(principalId) {
|
|
807
|
+
return rows(tables.workloadCredentials, (row) => row.principalId === principalId);
|
|
808
|
+
},
|
|
809
|
+
async insertAudience(row) {
|
|
810
|
+
tx(() => {
|
|
811
|
+
org(row.orgId);
|
|
812
|
+
if ([...tables.audiences.values()].some((item) => item.orgId === row.orgId && item.identifier === row.identifier))
|
|
813
|
+
fail("duplicate", "Duplicate key.");
|
|
814
|
+
put(tables.audiences, { ...row, createdAt: asTime(row.createdAt) });
|
|
815
|
+
});
|
|
816
|
+
},
|
|
817
|
+
async listAudiences(orgId) { return rows(tables.audiences, (row) => row.orgId === orgId); },
|
|
818
|
+
async findAudience(orgId, identifier) {
|
|
819
|
+
return rows(tables.audiences, (row) => row.orgId === orgId && row.identifier === identifier)[0] ?? null;
|
|
820
|
+
},
|
|
821
|
+
async insertRole(row) {
|
|
822
|
+
tx(() => { org(row.orgId); put(tables.roles, { ...row, actions: [...row.actions], createdAt: asTime(row.createdAt) }); });
|
|
823
|
+
},
|
|
824
|
+
async listRoles(orgId) { return rows(tables.roles, (row) => row.orgId === orgId); },
|
|
825
|
+
async insertAssignment(row) {
|
|
826
|
+
tx(() => {
|
|
827
|
+
org(row.orgId);
|
|
828
|
+
if (!tables.principals.has(row.principalId) || !tables.roles.has(row.roleId))
|
|
829
|
+
fail("conflict", "Assignment target is missing.");
|
|
830
|
+
put(tables.assignments, { ...row, createdAt: asTime(row.createdAt) });
|
|
831
|
+
});
|
|
832
|
+
},
|
|
833
|
+
async listAssignments(orgId, principalId) {
|
|
834
|
+
return rows(tables.assignments, (row) => row.orgId === orgId && (!principalId || row.principalId === principalId));
|
|
835
|
+
},
|
|
836
|
+
async insertGatewayRoute(row) {
|
|
837
|
+
tx(() => {
|
|
838
|
+
org(row.orgId);
|
|
839
|
+
if (!tables.audiences.has(row.audienceId))
|
|
840
|
+
fail("conflict", "Audience is missing.");
|
|
841
|
+
put(tables.gatewayRoutes, { ...row, createdAt: asTime(row.createdAt) });
|
|
842
|
+
});
|
|
843
|
+
},
|
|
844
|
+
async listGatewayRoutes(orgId) { return rows(tables.gatewayRoutes, (row) => !orgId || row.orgId === orgId); },
|
|
845
|
+
async deleteGatewayRoute(id, orgId) {
|
|
846
|
+
tx(() => {
|
|
847
|
+
const row = tables.gatewayRoutes.get(id);
|
|
848
|
+
if (!row || row.orgId !== orgId)
|
|
849
|
+
fail("not_found", "Route not found.");
|
|
850
|
+
tables.gatewayRoutes.delete(id);
|
|
851
|
+
});
|
|
852
|
+
},
|
|
853
|
+
...bindDirectoryMemory(() => tables.directory, () => tables.users.size, tx),
|
|
854
|
+
async ping() { },
|
|
855
|
+
async purgeExpired(at) {
|
|
856
|
+
tx(() => {
|
|
857
|
+
const stamp = asTime(at);
|
|
858
|
+
const seconds = Math.floor(Date.parse(stamp) / 1000);
|
|
859
|
+
for (const row of [...tables.sessions.values()])
|
|
860
|
+
if (isExpired(row.expiresAt, stamp))
|
|
861
|
+
tables.sessions.delete(row.id);
|
|
862
|
+
for (const row of [...tables.challenges.values()])
|
|
863
|
+
if (isExpired(row.expiresAt, stamp))
|
|
864
|
+
tables.challenges.delete(row.id);
|
|
865
|
+
for (const row of [...tables.authorizations.values()])
|
|
866
|
+
if (isExpired(row.expiresAt, stamp))
|
|
867
|
+
tables.authorizations.delete(row.id);
|
|
868
|
+
for (const row of [...tables.deniedJtis.values()])
|
|
869
|
+
if (row.exp <= seconds)
|
|
870
|
+
tables.deniedJtis.delete(row.id);
|
|
871
|
+
for (const row of [...tables.ssoLogins.values()])
|
|
872
|
+
if (isExpired(row.expiresAt, stamp))
|
|
873
|
+
tables.ssoLogins.delete(row.id);
|
|
874
|
+
for (const row of [...tables.ssoExchanges.values()])
|
|
875
|
+
if (isExpired(row.expiresAt, stamp))
|
|
876
|
+
tables.ssoExchanges.delete(row.id);
|
|
877
|
+
});
|
|
878
|
+
},
|
|
879
|
+
};
|
|
880
|
+
return store;
|
|
881
|
+
}
|
|
882
|
+
//# sourceMappingURL=memory.js.map
|