@axiom-lattice/local-stores 5.0.2 → 5.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/.turbo/turbo-build.log +10 -10
- package/CHANGELOG.md +102 -0
- package/dist/index.d.mts +127 -2
- package/dist/index.d.ts +127 -2
- package/dist/index.js +947 -85
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +931 -62
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/LocalInviteLinkStore.test.ts +48 -0
- package/src/__tests__/LocalOidcIdentityClaimStore.test.ts +114 -0
- package/src/__tests__/LocalOidcProviderConfigStore.test.ts +203 -0
- package/src/__tests__/LocalPlatformInvitationStore.test.ts +207 -0
- package/src/__tests__/LocalPlatformMemberStore.test.ts +123 -0
- package/src/__tests__/LocalPlatformSettingsStore.test.ts +120 -0
- package/src/__tests__/LocalUserExternalIdentityStore.test.ts +104 -0
- package/src/__tests__/LocalUserStore.test.ts +44 -0
- package/src/createLocalStoreConfig.ts +14 -0
- package/src/index.ts +7 -0
- package/src/stores/LocalInviteLinkStore.ts +162 -0
- package/src/stores/LocalOidcIdentityClaimStore.ts +316 -0
- package/src/stores/LocalOidcProviderConfigStore.ts +260 -0
- package/src/stores/LocalPlatformInvitationStore.ts +139 -0
- package/src/stores/LocalPlatformMemberStore.ts +108 -0
- package/src/stores/LocalPlatformSettingsStore.ts +115 -0
- package/src/stores/LocalUserExternalIdentityStore.ts +120 -0
- package/src/stores/LocalUserStore.ts +3 -2
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import {
|
|
2
|
+
normalizeOidcIdentityEmail,
|
|
3
|
+
OidcIdentityClaimConflictError,
|
|
4
|
+
type CreateOidcIdentityClaimInput,
|
|
5
|
+
type OidcIdentityClaim,
|
|
6
|
+
type OidcIdentityClaimApproval,
|
|
7
|
+
type OidcIdentityClaimStatus,
|
|
8
|
+
type OidcIdentityClaimStore,
|
|
9
|
+
type UpdateOidcIdentityClaimInput,
|
|
10
|
+
} from "@axiom-lattice/protocols";
|
|
11
|
+
import { DatabaseWrapper, ensureTable, nowISO, parseISO } from "../database";
|
|
12
|
+
|
|
13
|
+
const DDL = `
|
|
14
|
+
CREATE TABLE IF NOT EXISTS lattice_oidc_identity_claims (
|
|
15
|
+
id TEXT NOT NULL PRIMARY KEY,
|
|
16
|
+
provider_id TEXT NOT NULL,
|
|
17
|
+
issuer TEXT NOT NULL,
|
|
18
|
+
subject TEXT NOT NULL,
|
|
19
|
+
email TEXT NOT NULL,
|
|
20
|
+
name TEXT,
|
|
21
|
+
picture TEXT,
|
|
22
|
+
status TEXT NOT NULL,
|
|
23
|
+
created_at TEXT NOT NULL,
|
|
24
|
+
updated_at TEXT NOT NULL,
|
|
25
|
+
expires_at TEXT NOT NULL,
|
|
26
|
+
reviewed_by TEXT,
|
|
27
|
+
reviewed_at TEXT,
|
|
28
|
+
linked_user_id TEXT,
|
|
29
|
+
source TEXT,
|
|
30
|
+
invite_link_id TEXT,
|
|
31
|
+
requested_user_id TEXT,
|
|
32
|
+
requested_platform_role TEXT,
|
|
33
|
+
requested_tenant_id TEXT,
|
|
34
|
+
requested_tenant_role TEXT
|
|
35
|
+
);
|
|
36
|
+
CREATE UNIQUE INDEX IF NOT EXISTS lattice_oidc_identity_claims_pending_subject
|
|
37
|
+
ON lattice_oidc_identity_claims (provider_id, issuer, subject)
|
|
38
|
+
WHERE status = 'pending';
|
|
39
|
+
`;
|
|
40
|
+
|
|
41
|
+
interface ClaimRow {
|
|
42
|
+
id: string;
|
|
43
|
+
provider_id: string;
|
|
44
|
+
issuer: string;
|
|
45
|
+
subject: string;
|
|
46
|
+
email: string;
|
|
47
|
+
name: string | null;
|
|
48
|
+
picture: string | null;
|
|
49
|
+
status: OidcIdentityClaimStatus;
|
|
50
|
+
created_at: string;
|
|
51
|
+
updated_at: string;
|
|
52
|
+
expires_at: string;
|
|
53
|
+
reviewed_by: string | null;
|
|
54
|
+
reviewed_at: string | null;
|
|
55
|
+
linked_user_id: string | null;
|
|
56
|
+
source: string | null;
|
|
57
|
+
invite_link_id: string | null;
|
|
58
|
+
requested_user_id: string | null;
|
|
59
|
+
requested_platform_role: string | null;
|
|
60
|
+
requested_tenant_id: string | null;
|
|
61
|
+
requested_tenant_role: string | null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function mapRow(row: ClaimRow): OidcIdentityClaim {
|
|
65
|
+
return {
|
|
66
|
+
id: row.id,
|
|
67
|
+
providerId: row.provider_id,
|
|
68
|
+
issuer: row.issuer,
|
|
69
|
+
subject: row.subject,
|
|
70
|
+
email: row.email,
|
|
71
|
+
...(row.name !== null ? { name: row.name } : {}),
|
|
72
|
+
...(row.picture !== null ? { picture: row.picture } : {}),
|
|
73
|
+
status: row.status,
|
|
74
|
+
createdAt: parseISO(row.created_at),
|
|
75
|
+
updatedAt: parseISO(row.updated_at),
|
|
76
|
+
expiresAt: parseISO(row.expires_at),
|
|
77
|
+
...(row.reviewed_by !== null ? { reviewedBy: row.reviewed_by } : {}),
|
|
78
|
+
...(row.reviewed_at !== null ? { reviewedAt: parseISO(row.reviewed_at) } : {}),
|
|
79
|
+
...(row.linked_user_id !== null
|
|
80
|
+
? { linkedUserId: row.linked_user_id }
|
|
81
|
+
: {}),
|
|
82
|
+
...(row.source !== null ? { source: row.source as never } : {}),
|
|
83
|
+
...(row.invite_link_id !== null
|
|
84
|
+
? { inviteLinkId: row.invite_link_id }
|
|
85
|
+
: {}),
|
|
86
|
+
...(row.requested_user_id !== null
|
|
87
|
+
? { requestedUserId: row.requested_user_id }
|
|
88
|
+
: {}),
|
|
89
|
+
...(row.requested_platform_role !== null
|
|
90
|
+
? { requestedPlatformRole: row.requested_platform_role as never }
|
|
91
|
+
: {}),
|
|
92
|
+
...(row.requested_tenant_id !== null
|
|
93
|
+
? { requestedTenantId: row.requested_tenant_id }
|
|
94
|
+
: {}),
|
|
95
|
+
...(row.requested_tenant_role !== null
|
|
96
|
+
? { requestedTenantRole: row.requested_tenant_role as never }
|
|
97
|
+
: {}),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** SQLite implementation of {@link OidcIdentityClaimStore}. */
|
|
102
|
+
export class LocalOidcIdentityClaimStore implements OidcIdentityClaimStore {
|
|
103
|
+
private readonly db: DatabaseWrapper;
|
|
104
|
+
|
|
105
|
+
constructor(db: DatabaseWrapper) {
|
|
106
|
+
this.db = db;
|
|
107
|
+
ensureTable(db, DDL);
|
|
108
|
+
this.ensureColumn("lattice_oidc_identity_claims", "source", "source TEXT");
|
|
109
|
+
this.ensureColumn("lattice_oidc_identity_claims", "invite_link_id", "invite_link_id TEXT");
|
|
110
|
+
this.ensureColumn("lattice_oidc_identity_claims", "requested_user_id", "requested_user_id TEXT");
|
|
111
|
+
this.ensureColumn("lattice_oidc_identity_claims", "requested_platform_role", "requested_platform_role TEXT");
|
|
112
|
+
this.ensureColumn("lattice_oidc_identity_claims", "requested_tenant_id", "requested_tenant_id TEXT");
|
|
113
|
+
this.ensureColumn("lattice_oidc_identity_claims", "requested_tenant_role", "requested_tenant_role TEXT");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private ensureColumn(table: string, column: string, ddl: string): void {
|
|
117
|
+
const columns = this.db
|
|
118
|
+
.prepare(`PRAGMA table_info(${table})`)
|
|
119
|
+
.all() as unknown as Array<{ name: string }>;
|
|
120
|
+
if (!columns.some((entry) => entry.name === column)) {
|
|
121
|
+
this.db.exec(`ALTER TABLE ${table} ADD COLUMN ${ddl}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async list(status?: OidcIdentityClaimStatus): Promise<OidcIdentityClaim[]> {
|
|
126
|
+
const rows = status
|
|
127
|
+
? (this.db
|
|
128
|
+
.prepare(
|
|
129
|
+
`SELECT * FROM lattice_oidc_identity_claims
|
|
130
|
+
WHERE status = ? ORDER BY created_at ASC, id ASC`,
|
|
131
|
+
)
|
|
132
|
+
.all(status) as unknown as ClaimRow[])
|
|
133
|
+
: (this.db
|
|
134
|
+
.prepare(
|
|
135
|
+
`SELECT * FROM lattice_oidc_identity_claims
|
|
136
|
+
ORDER BY created_at ASC, id ASC`,
|
|
137
|
+
)
|
|
138
|
+
.all() as unknown as ClaimRow[]);
|
|
139
|
+
return rows.map(mapRow);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async get(id: string): Promise<OidcIdentityClaim | null> {
|
|
143
|
+
const row = this.db
|
|
144
|
+
.prepare(`SELECT * FROM lattice_oidc_identity_claims WHERE id = ?`)
|
|
145
|
+
.get(id) as unknown as ClaimRow | undefined;
|
|
146
|
+
return row ? mapRow(row) : null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async getPendingBySubject(
|
|
150
|
+
providerId: string,
|
|
151
|
+
issuer: string,
|
|
152
|
+
subject: string,
|
|
153
|
+
now: Date,
|
|
154
|
+
): Promise<OidcIdentityClaim | null> {
|
|
155
|
+
const row = this.db
|
|
156
|
+
.prepare(
|
|
157
|
+
`SELECT * FROM lattice_oidc_identity_claims
|
|
158
|
+
WHERE provider_id = ? AND issuer = ? AND subject = ?
|
|
159
|
+
AND status = 'pending' AND expires_at > ?`,
|
|
160
|
+
)
|
|
161
|
+
.get(providerId, issuer, subject, now.toISOString()) as unknown as
|
|
162
|
+
| ClaimRow
|
|
163
|
+
| undefined;
|
|
164
|
+
return row ? mapRow(row) : null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async create(input: CreateOidcIdentityClaimInput): Promise<OidcIdentityClaim> {
|
|
168
|
+
const now = new Date();
|
|
169
|
+
const existing = this.db
|
|
170
|
+
.prepare(
|
|
171
|
+
`SELECT * FROM lattice_oidc_identity_claims
|
|
172
|
+
WHERE provider_id = ? AND issuer = ? AND subject = ?
|
|
173
|
+
AND status = 'pending'`,
|
|
174
|
+
)
|
|
175
|
+
.get(input.providerId, input.issuer, input.subject) as unknown as
|
|
176
|
+
| ClaimRow
|
|
177
|
+
| undefined;
|
|
178
|
+
if (existing) {
|
|
179
|
+
if (parseISO(existing.expires_at).getTime() > now.getTime()) {
|
|
180
|
+
throw new OidcIdentityClaimConflictError(
|
|
181
|
+
input.providerId,
|
|
182
|
+
input.issuer,
|
|
183
|
+
input.subject,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
this.db
|
|
187
|
+
.prepare(
|
|
188
|
+
`UPDATE lattice_oidc_identity_claims
|
|
189
|
+
SET status = 'expired', updated_at = ?
|
|
190
|
+
WHERE id = ?`,
|
|
191
|
+
)
|
|
192
|
+
.run(nowISO(), existing.id);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const timestamp = now.toISOString();
|
|
196
|
+
try {
|
|
197
|
+
this.db
|
|
198
|
+
.prepare(
|
|
199
|
+
`INSERT INTO lattice_oidc_identity_claims
|
|
200
|
+
(id, provider_id, issuer, subject, email, name, picture, status,
|
|
201
|
+
created_at, updated_at, expires_at, reviewed_by, reviewed_at,
|
|
202
|
+
linked_user_id, source, invite_link_id, requested_user_id,
|
|
203
|
+
requested_platform_role, requested_tenant_id, requested_tenant_role)
|
|
204
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, 'pending', ?, ?, ?, NULL, NULL, NULL,
|
|
205
|
+
?, ?, ?, ?, ?, ?)`,
|
|
206
|
+
)
|
|
207
|
+
.run(
|
|
208
|
+
input.id,
|
|
209
|
+
input.providerId,
|
|
210
|
+
input.issuer,
|
|
211
|
+
input.subject,
|
|
212
|
+
normalizeOidcIdentityEmail(input.email),
|
|
213
|
+
input.name ?? null,
|
|
214
|
+
input.picture ?? null,
|
|
215
|
+
timestamp,
|
|
216
|
+
timestamp,
|
|
217
|
+
input.expiresAt.toISOString(),
|
|
218
|
+
input.source ?? null,
|
|
219
|
+
input.inviteLinkId ?? null,
|
|
220
|
+
input.requestedUserId ?? null,
|
|
221
|
+
input.requestedPlatformRole ?? null,
|
|
222
|
+
input.requestedTenantId ?? null,
|
|
223
|
+
input.requestedTenantRole ?? null,
|
|
224
|
+
);
|
|
225
|
+
} catch (error) {
|
|
226
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
227
|
+
if (message.toLowerCase().includes("unique")) {
|
|
228
|
+
throw new OidcIdentityClaimConflictError(
|
|
229
|
+
input.providerId,
|
|
230
|
+
input.issuer,
|
|
231
|
+
input.subject,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
throw error;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const stored = await this.get(input.id);
|
|
238
|
+
if (!stored) throw new Error("OIDC identity claim was not persisted");
|
|
239
|
+
return stored;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async updatePending(
|
|
243
|
+
id: string,
|
|
244
|
+
updates: UpdateOidcIdentityClaimInput,
|
|
245
|
+
now: Date,
|
|
246
|
+
): Promise<OidcIdentityClaim | null> {
|
|
247
|
+
const sets: string[] = [];
|
|
248
|
+
const params: unknown[] = [];
|
|
249
|
+
if (updates.email !== undefined) {
|
|
250
|
+
sets.push("email = ?");
|
|
251
|
+
params.push(normalizeOidcIdentityEmail(updates.email));
|
|
252
|
+
}
|
|
253
|
+
if (updates.name !== undefined) {
|
|
254
|
+
sets.push("name = ?");
|
|
255
|
+
params.push(updates.name);
|
|
256
|
+
}
|
|
257
|
+
if (updates.picture !== undefined) {
|
|
258
|
+
sets.push("picture = ?");
|
|
259
|
+
params.push(updates.picture);
|
|
260
|
+
}
|
|
261
|
+
if (sets.length === 0) return this.get(id);
|
|
262
|
+
sets.push("updated_at = ?");
|
|
263
|
+
params.push(now.toISOString(), id);
|
|
264
|
+
const result = this.db
|
|
265
|
+
.prepare(
|
|
266
|
+
`UPDATE lattice_oidc_identity_claims SET ${sets.join(", ")}
|
|
267
|
+
WHERE id = ? AND status = 'pending'`,
|
|
268
|
+
)
|
|
269
|
+
.run(...params);
|
|
270
|
+
return result.changes > 0 ? this.get(id) : null;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async approve(
|
|
274
|
+
id: string,
|
|
275
|
+
approval: OidcIdentityClaimApproval,
|
|
276
|
+
now: Date,
|
|
277
|
+
): Promise<OidcIdentityClaim | null> {
|
|
278
|
+
const result = this.db
|
|
279
|
+
.prepare(
|
|
280
|
+
`UPDATE lattice_oidc_identity_claims
|
|
281
|
+
SET status = 'approved', reviewed_by = ?, reviewed_at = ?,
|
|
282
|
+
linked_user_id = ?, updated_at = ?
|
|
283
|
+
WHERE id = ? AND status = 'pending'`,
|
|
284
|
+
)
|
|
285
|
+
.run(
|
|
286
|
+
approval.reviewedBy,
|
|
287
|
+
now.toISOString(),
|
|
288
|
+
approval.linkedUserId,
|
|
289
|
+
now.toISOString(),
|
|
290
|
+
id,
|
|
291
|
+
);
|
|
292
|
+
return result.changes > 0 ? this.get(id) : null;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async reject(
|
|
296
|
+
id: string,
|
|
297
|
+
reviewedBy: string,
|
|
298
|
+
now: Date,
|
|
299
|
+
): Promise<OidcIdentityClaim | null> {
|
|
300
|
+
const result = this.db
|
|
301
|
+
.prepare(
|
|
302
|
+
`UPDATE lattice_oidc_identity_claims
|
|
303
|
+
SET status = 'rejected', reviewed_by = ?, reviewed_at = ?, updated_at = ?
|
|
304
|
+
WHERE id = ? AND status = 'pending'`,
|
|
305
|
+
)
|
|
306
|
+
.run(reviewedBy, now.toISOString(), now.toISOString(), id);
|
|
307
|
+
return result.changes > 0 ? this.get(id) : null;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
async delete(id: string): Promise<boolean> {
|
|
311
|
+
const result = this.db
|
|
312
|
+
.prepare(`DELETE FROM lattice_oidc_identity_claims WHERE id = ?`)
|
|
313
|
+
.run(id);
|
|
314
|
+
return result.changes > 0;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { safeDecrypt, encrypt } from "@axiom-lattice/core";
|
|
2
|
+
import {
|
|
3
|
+
OidcProviderConfigConflictError,
|
|
4
|
+
type CreateOidcProviderConfigInput,
|
|
5
|
+
type OidcProviderConfig,
|
|
6
|
+
type OidcProviderConfigStore,
|
|
7
|
+
type UpdateOidcProviderConfigInput,
|
|
8
|
+
} from "@axiom-lattice/protocols";
|
|
9
|
+
import { DatabaseWrapper, ensureTable, nowISO, parseISO } from "../database";
|
|
10
|
+
|
|
11
|
+
const DDL = `
|
|
12
|
+
CREATE TABLE IF NOT EXISTS lattice_oidc_provider_configs (
|
|
13
|
+
id TEXT NOT NULL PRIMARY KEY,
|
|
14
|
+
display_name TEXT NOT NULL,
|
|
15
|
+
issuer TEXT NOT NULL,
|
|
16
|
+
client_id TEXT NOT NULL,
|
|
17
|
+
client_secret TEXT NOT NULL,
|
|
18
|
+
scopes TEXT NOT NULL,
|
|
19
|
+
enabled INTEGER NOT NULL,
|
|
20
|
+
jit_provisioning INTEGER NOT NULL,
|
|
21
|
+
auto_link_email INTEGER NOT NULL,
|
|
22
|
+
default_tenant_id TEXT,
|
|
23
|
+
allowed_private_hosts TEXT,
|
|
24
|
+
created_at TEXT NOT NULL,
|
|
25
|
+
updated_at TEXT NOT NULL
|
|
26
|
+
);
|
|
27
|
+
`;
|
|
28
|
+
|
|
29
|
+
interface ProviderRow {
|
|
30
|
+
id: string;
|
|
31
|
+
display_name: string;
|
|
32
|
+
issuer: string;
|
|
33
|
+
client_id: string;
|
|
34
|
+
client_secret: string;
|
|
35
|
+
scopes: string;
|
|
36
|
+
enabled: number;
|
|
37
|
+
jit_provisioning: number;
|
|
38
|
+
auto_link_email: number;
|
|
39
|
+
default_tenant_id: string | null;
|
|
40
|
+
allowed_private_hosts: string | null;
|
|
41
|
+
created_at: string;
|
|
42
|
+
updated_at: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function parseAllowedPrivateHosts(value: string | null): string[] | undefined {
|
|
46
|
+
if (value === null) return undefined;
|
|
47
|
+
try {
|
|
48
|
+
const parsed = JSON.parse(value) as unknown;
|
|
49
|
+
return Array.isArray(parsed)
|
|
50
|
+
? parsed.filter((entry): entry is string => typeof entry === "string")
|
|
51
|
+
: undefined;
|
|
52
|
+
} catch {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function serializeAllowedPrivateHosts(
|
|
58
|
+
value: readonly string[] | undefined,
|
|
59
|
+
): string | null {
|
|
60
|
+
return value === undefined ? null : JSON.stringify(value);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function normalizeTenantId(value: string | null | undefined): string | undefined {
|
|
64
|
+
if (typeof value !== "string") return undefined;
|
|
65
|
+
const trimmed = value.trim();
|
|
66
|
+
return trimmed.length > 0 ? trimmed : undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function mapRow(row: ProviderRow): OidcProviderConfig {
|
|
70
|
+
const allowedPrivateHosts = parseAllowedPrivateHosts(
|
|
71
|
+
row.allowed_private_hosts,
|
|
72
|
+
);
|
|
73
|
+
return {
|
|
74
|
+
id: row.id,
|
|
75
|
+
displayName: row.display_name,
|
|
76
|
+
issuer: row.issuer,
|
|
77
|
+
clientId: row.client_id,
|
|
78
|
+
clientSecret: safeDecrypt(
|
|
79
|
+
row.client_secret,
|
|
80
|
+
`OIDC provider config "${row.id}" client secret`,
|
|
81
|
+
),
|
|
82
|
+
scopes: row.scopes,
|
|
83
|
+
enabled: Number(row.enabled) === 1,
|
|
84
|
+
jitProvisioning: Number(row.jit_provisioning) === 1,
|
|
85
|
+
autoLinkEmail: Number(row.auto_link_email) === 1,
|
|
86
|
+
...(row.default_tenant_id !== null
|
|
87
|
+
? { defaultTenantId: row.default_tenant_id }
|
|
88
|
+
: {}),
|
|
89
|
+
...(allowedPrivateHosts !== undefined ? { allowedPrivateHosts } : {}),
|
|
90
|
+
createdAt: parseISO(row.created_at),
|
|
91
|
+
updatedAt: parseISO(row.updated_at),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* SQLite implementation of {@link OidcProviderConfigStore}.
|
|
97
|
+
*
|
|
98
|
+
* Persists OIDC provider configurations in the local `lattice_oidc_provider_configs`
|
|
99
|
+
* table, encrypting the client secret at rest. Intended for single-process,
|
|
100
|
+
* file-backed deployments.
|
|
101
|
+
*/
|
|
102
|
+
export class LocalOidcProviderConfigStore implements OidcProviderConfigStore {
|
|
103
|
+
private readonly db: DatabaseWrapper;
|
|
104
|
+
|
|
105
|
+
constructor(db: DatabaseWrapper) {
|
|
106
|
+
this.db = db;
|
|
107
|
+
ensureTable(db, DDL);
|
|
108
|
+
this.ensureColumn(
|
|
109
|
+
"lattice_oidc_provider_configs",
|
|
110
|
+
"allowed_private_hosts",
|
|
111
|
+
"allowed_private_hosts TEXT",
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private ensureColumn(table: string, column: string, ddl: string): void {
|
|
116
|
+
const cols = this.db
|
|
117
|
+
.prepare(`PRAGMA table_info(${table})`)
|
|
118
|
+
.all() as Array<{ name: string }>;
|
|
119
|
+
if (!cols.some((entry) => entry.name === column)) {
|
|
120
|
+
this.db.exec(`ALTER TABLE ${table} ADD COLUMN ${ddl}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async list(): Promise<OidcProviderConfig[]> {
|
|
125
|
+
const rows = this.db
|
|
126
|
+
.prepare(
|
|
127
|
+
`SELECT * FROM lattice_oidc_provider_configs ORDER BY created_at ASC, id ASC`,
|
|
128
|
+
)
|
|
129
|
+
.all() as unknown as ProviderRow[];
|
|
130
|
+
return rows.map(mapRow);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async get(id: string): Promise<OidcProviderConfig | null> {
|
|
134
|
+
const row = this.db
|
|
135
|
+
.prepare(`SELECT * FROM lattice_oidc_provider_configs WHERE id = ?`)
|
|
136
|
+
.get(id) as unknown as ProviderRow | undefined;
|
|
137
|
+
return row ? mapRow(row) : null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async create(
|
|
141
|
+
id: string,
|
|
142
|
+
input: CreateOidcProviderConfigInput,
|
|
143
|
+
): Promise<OidcProviderConfig> {
|
|
144
|
+
if (this.db.prepare(`SELECT 1 FROM lattice_oidc_provider_configs WHERE id = ?`).get(id)) {
|
|
145
|
+
throw new OidcProviderConfigConflictError(id);
|
|
146
|
+
}
|
|
147
|
+
const now = nowISO();
|
|
148
|
+
const tenantId = normalizeTenantId(input.defaultTenantId);
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
this.db
|
|
152
|
+
.prepare(
|
|
153
|
+
`INSERT INTO lattice_oidc_provider_configs
|
|
154
|
+
(id, display_name, issuer, client_id, client_secret, scopes,
|
|
155
|
+
enabled, jit_provisioning, auto_link_email, default_tenant_id,
|
|
156
|
+
allowed_private_hosts, created_at, updated_at)
|
|
157
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
158
|
+
)
|
|
159
|
+
.run(
|
|
160
|
+
id,
|
|
161
|
+
input.displayName,
|
|
162
|
+
input.issuer,
|
|
163
|
+
input.clientId,
|
|
164
|
+
encrypt(input.clientSecret),
|
|
165
|
+
input.scopes,
|
|
166
|
+
input.enabled ? 1 : 0,
|
|
167
|
+
input.jitProvisioning ? 1 : 0,
|
|
168
|
+
input.autoLinkEmail ? 1 : 0,
|
|
169
|
+
tenantId ?? null,
|
|
170
|
+
serializeAllowedPrivateHosts(input.allowedPrivateHosts),
|
|
171
|
+
now,
|
|
172
|
+
now,
|
|
173
|
+
);
|
|
174
|
+
} catch (error) {
|
|
175
|
+
if (error instanceof Error && /UNIQUE constraint failed/.test(error.message)) {
|
|
176
|
+
throw new OidcProviderConfigConflictError(id);
|
|
177
|
+
}
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const created = await this.get(id);
|
|
182
|
+
if (!created) {
|
|
183
|
+
throw new Error(`OIDC provider config "${id}" was not persisted`);
|
|
184
|
+
}
|
|
185
|
+
return created;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async update(
|
|
189
|
+
id: string,
|
|
190
|
+
updates: UpdateOidcProviderConfigInput,
|
|
191
|
+
): Promise<OidcProviderConfig | null> {
|
|
192
|
+
const existing = await this.get(id);
|
|
193
|
+
if (!existing) return null;
|
|
194
|
+
|
|
195
|
+
const setClauses: string[] = [];
|
|
196
|
+
const values: unknown[] = [];
|
|
197
|
+
|
|
198
|
+
if (updates.displayName !== undefined) {
|
|
199
|
+
setClauses.push("display_name = ?");
|
|
200
|
+
values.push(updates.displayName);
|
|
201
|
+
}
|
|
202
|
+
if (updates.issuer !== undefined) {
|
|
203
|
+
setClauses.push("issuer = ?");
|
|
204
|
+
values.push(updates.issuer);
|
|
205
|
+
}
|
|
206
|
+
if (updates.clientId !== undefined) {
|
|
207
|
+
setClauses.push("client_id = ?");
|
|
208
|
+
values.push(updates.clientId);
|
|
209
|
+
}
|
|
210
|
+
if (updates.clientSecret !== undefined && updates.clientSecret !== "") {
|
|
211
|
+
setClauses.push("client_secret = ?");
|
|
212
|
+
values.push(encrypt(updates.clientSecret));
|
|
213
|
+
}
|
|
214
|
+
if (updates.scopes !== undefined) {
|
|
215
|
+
setClauses.push("scopes = ?");
|
|
216
|
+
values.push(updates.scopes);
|
|
217
|
+
}
|
|
218
|
+
if (updates.enabled !== undefined) {
|
|
219
|
+
setClauses.push("enabled = ?");
|
|
220
|
+
values.push(updates.enabled ? 1 : 0);
|
|
221
|
+
}
|
|
222
|
+
if (updates.jitProvisioning !== undefined) {
|
|
223
|
+
setClauses.push("jit_provisioning = ?");
|
|
224
|
+
values.push(updates.jitProvisioning ? 1 : 0);
|
|
225
|
+
}
|
|
226
|
+
if (updates.autoLinkEmail !== undefined) {
|
|
227
|
+
setClauses.push("auto_link_email = ?");
|
|
228
|
+
values.push(updates.autoLinkEmail ? 1 : 0);
|
|
229
|
+
}
|
|
230
|
+
if (updates.defaultTenantId !== undefined) {
|
|
231
|
+
setClauses.push("default_tenant_id = ?");
|
|
232
|
+
values.push(normalizeTenantId(updates.defaultTenantId) ?? null);
|
|
233
|
+
}
|
|
234
|
+
if (updates.allowedPrivateHosts !== undefined) {
|
|
235
|
+
setClauses.push("allowed_private_hosts = ?");
|
|
236
|
+
values.push(serializeAllowedPrivateHosts(updates.allowedPrivateHosts));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (setClauses.length === 0) return existing;
|
|
240
|
+
|
|
241
|
+
setClauses.push("updated_at = ?");
|
|
242
|
+
values.push(nowISO());
|
|
243
|
+
values.push(id);
|
|
244
|
+
|
|
245
|
+
this.db
|
|
246
|
+
.prepare(
|
|
247
|
+
`UPDATE lattice_oidc_provider_configs SET ${setClauses.join(", ")} WHERE id = ?`,
|
|
248
|
+
)
|
|
249
|
+
.run(...values);
|
|
250
|
+
|
|
251
|
+
return this.get(id);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async delete(id: string): Promise<boolean> {
|
|
255
|
+
const result = this.db
|
|
256
|
+
.prepare(`DELETE FROM lattice_oidc_provider_configs WHERE id = ?`)
|
|
257
|
+
.run(id);
|
|
258
|
+
return result.changes > 0;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import {
|
|
2
|
+
normalizePlatformInvitationEmail,
|
|
3
|
+
type PlatformInvitation,
|
|
4
|
+
type PlatformInvitationStore,
|
|
5
|
+
type PlatformRole,
|
|
6
|
+
} from "@axiom-lattice/protocols";
|
|
7
|
+
import { DatabaseWrapper, ensureTable, parseISO } from "../database";
|
|
8
|
+
|
|
9
|
+
const DDL = `
|
|
10
|
+
CREATE TABLE IF NOT EXISTS lattice_platform_invitations (
|
|
11
|
+
email TEXT NOT NULL PRIMARY KEY,
|
|
12
|
+
role TEXT NOT NULL,
|
|
13
|
+
invited_by TEXT,
|
|
14
|
+
created_at TEXT NOT NULL,
|
|
15
|
+
expires_at TEXT,
|
|
16
|
+
consumed_at TEXT
|
|
17
|
+
);
|
|
18
|
+
`;
|
|
19
|
+
|
|
20
|
+
interface InvitationRow {
|
|
21
|
+
email: string;
|
|
22
|
+
role: PlatformRole;
|
|
23
|
+
invited_by: string | null;
|
|
24
|
+
created_at: string;
|
|
25
|
+
expires_at: string | null;
|
|
26
|
+
consumed_at: string | null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function mapRow(row: InvitationRow): PlatformInvitation {
|
|
30
|
+
return {
|
|
31
|
+
email: row.email,
|
|
32
|
+
role: row.role,
|
|
33
|
+
...(row.invited_by !== null ? { invitedBy: row.invited_by } : {}),
|
|
34
|
+
createdAt: parseISO(row.created_at),
|
|
35
|
+
...(row.expires_at !== null ? { expiresAt: parseISO(row.expires_at) } : {}),
|
|
36
|
+
...(row.consumed_at !== null ? { consumedAt: parseISO(row.consumed_at) } : {}),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* SQLite implementation of {@link PlatformInvitationStore}.
|
|
42
|
+
*
|
|
43
|
+
* Persists invitations in the local `lattice_platform_invitations` table,
|
|
44
|
+
* keyed by normalized email. Intended for single-process, file-backed
|
|
45
|
+
* deployments.
|
|
46
|
+
*/
|
|
47
|
+
export class LocalPlatformInvitationStore implements PlatformInvitationStore {
|
|
48
|
+
private readonly db: DatabaseWrapper;
|
|
49
|
+
|
|
50
|
+
constructor(db: DatabaseWrapper) {
|
|
51
|
+
this.db = db;
|
|
52
|
+
ensureTable(db, DDL);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Lists every invitation ordered by normalized email. */
|
|
56
|
+
async list(): Promise<PlatformInvitation[]> {
|
|
57
|
+
const rows = this.db
|
|
58
|
+
.prepare(`SELECT * FROM lattice_platform_invitations ORDER BY email ASC`)
|
|
59
|
+
.all() as unknown as InvitationRow[];
|
|
60
|
+
return rows.map(mapRow);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Returns the invitation for an email, or null. */
|
|
64
|
+
async get(email: string): Promise<PlatformInvitation | null> {
|
|
65
|
+
const row = this.db
|
|
66
|
+
.prepare(`SELECT * FROM lattice_platform_invitations WHERE email = ?`)
|
|
67
|
+
.get(normalizePlatformInvitationEmail(email)) as
|
|
68
|
+
| InvitationRow
|
|
69
|
+
| undefined;
|
|
70
|
+
return row ? mapRow(row) : null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Inserts or replaces an invitation keyed by normalized email.
|
|
75
|
+
*
|
|
76
|
+
* The supplied invitation replaces any existing row in full (last write wins),
|
|
77
|
+
* including `expiresAt` and `consumedAt`.
|
|
78
|
+
*/
|
|
79
|
+
async upsert(invitation: PlatformInvitation): Promise<PlatformInvitation> {
|
|
80
|
+
const email = normalizePlatformInvitationEmail(invitation.email);
|
|
81
|
+
this.db
|
|
82
|
+
.prepare(
|
|
83
|
+
`INSERT INTO lattice_platform_invitations
|
|
84
|
+
(email, role, invited_by, created_at, expires_at, consumed_at)
|
|
85
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
86
|
+
ON CONFLICT(email) DO UPDATE SET
|
|
87
|
+
role = excluded.role,
|
|
88
|
+
invited_by = excluded.invited_by,
|
|
89
|
+
created_at = excluded.created_at,
|
|
90
|
+
expires_at = excluded.expires_at,
|
|
91
|
+
consumed_at = excluded.consumed_at`,
|
|
92
|
+
)
|
|
93
|
+
.run(
|
|
94
|
+
email,
|
|
95
|
+
invitation.role,
|
|
96
|
+
invitation.invitedBy ?? null,
|
|
97
|
+
invitation.createdAt.toISOString(),
|
|
98
|
+
invitation.expiresAt?.toISOString() ?? null,
|
|
99
|
+
invitation.consumedAt?.toISOString() ?? null,
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
const stored = await this.get(email);
|
|
103
|
+
if (!stored) {
|
|
104
|
+
throw new Error(`Platform invitation "${email}" was not persisted`);
|
|
105
|
+
}
|
|
106
|
+
return stored;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Deletes an invitation. Returns whether a row was removed. */
|
|
110
|
+
async delete(email: string): Promise<boolean> {
|
|
111
|
+
const result = this.db
|
|
112
|
+
.prepare(`DELETE FROM lattice_platform_invitations WHERE email = ?`)
|
|
113
|
+
.run(normalizePlatformInvitationEmail(email));
|
|
114
|
+
return result.changes > 0;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Atomically consumes a pending, unexpired invitation.
|
|
119
|
+
*
|
|
120
|
+
* Returns the consumed invitation, or null when the row is missing, already
|
|
121
|
+
* consumed, or expired.
|
|
122
|
+
*/
|
|
123
|
+
async consume(email: string, at: Date): Promise<PlatformInvitation | null> {
|
|
124
|
+
const normalized = normalizePlatformInvitationEmail(email);
|
|
125
|
+
const timestamp = at.toISOString();
|
|
126
|
+
const result = this.db
|
|
127
|
+
.prepare(
|
|
128
|
+
`UPDATE lattice_platform_invitations
|
|
129
|
+
SET consumed_at = ?
|
|
130
|
+
WHERE email = ?
|
|
131
|
+
AND consumed_at IS NULL
|
|
132
|
+
AND (expires_at IS NULL OR expires_at > ?)`,
|
|
133
|
+
)
|
|
134
|
+
.run(timestamp, normalized, timestamp);
|
|
135
|
+
|
|
136
|
+
if (result.changes === 0) return null;
|
|
137
|
+
return this.get(normalized);
|
|
138
|
+
}
|
|
139
|
+
}
|