@axiom-lattice/local-stores 5.0.3 → 5.1.1
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,108 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PlatformMember,
|
|
3
|
+
PlatformMemberStore,
|
|
4
|
+
PlatformRole,
|
|
5
|
+
} from "@axiom-lattice/protocols";
|
|
6
|
+
import { DatabaseWrapper, ensureTable, parseISO } from "../database";
|
|
7
|
+
|
|
8
|
+
const DDL = `
|
|
9
|
+
CREATE TABLE IF NOT EXISTS lattice_platform_members (
|
|
10
|
+
user_id TEXT NOT NULL PRIMARY KEY,
|
|
11
|
+
role TEXT NOT NULL,
|
|
12
|
+
granted_by TEXT,
|
|
13
|
+
granted_at TEXT NOT NULL,
|
|
14
|
+
updated_at TEXT NOT NULL
|
|
15
|
+
);
|
|
16
|
+
`;
|
|
17
|
+
|
|
18
|
+
interface MemberRow {
|
|
19
|
+
user_id: string;
|
|
20
|
+
role: PlatformRole;
|
|
21
|
+
granted_by: string | null;
|
|
22
|
+
granted_at: string;
|
|
23
|
+
updated_at: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function mapRow(row: MemberRow): PlatformMember {
|
|
27
|
+
return {
|
|
28
|
+
userId: row.user_id,
|
|
29
|
+
role: row.role,
|
|
30
|
+
...(row.granted_by !== null ? { grantedBy: row.granted_by } : {}),
|
|
31
|
+
grantedAt: parseISO(row.granted_at),
|
|
32
|
+
updatedAt: parseISO(row.updated_at),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* SQLite implementation of {@link PlatformMemberStore}.
|
|
38
|
+
*
|
|
39
|
+
* Persists platform grants in the local `lattice_platform_members` table.
|
|
40
|
+
* Intended for single-process, file-backed deployments.
|
|
41
|
+
*/
|
|
42
|
+
export class LocalPlatformMemberStore implements PlatformMemberStore {
|
|
43
|
+
private readonly db: DatabaseWrapper;
|
|
44
|
+
|
|
45
|
+
constructor(db: DatabaseWrapper) {
|
|
46
|
+
this.db = db;
|
|
47
|
+
ensureTable(db, DDL);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Lists every platform member ordered by user id. */
|
|
51
|
+
async list(): Promise<PlatformMember[]> {
|
|
52
|
+
const rows = this.db
|
|
53
|
+
.prepare(
|
|
54
|
+
`SELECT * FROM lattice_platform_members ORDER BY user_id ASC`,
|
|
55
|
+
)
|
|
56
|
+
.all() as unknown as MemberRow[];
|
|
57
|
+
return rows.map(mapRow);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Returns the platform member for a user, or null. */
|
|
61
|
+
async get(userId: string): Promise<PlatformMember | null> {
|
|
62
|
+
const row = this.db
|
|
63
|
+
.prepare(`SELECT * FROM lattice_platform_members WHERE user_id = ?`)
|
|
64
|
+
.get(userId) as unknown as MemberRow | undefined;
|
|
65
|
+
return row ? mapRow(row) : null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Inserts or replaces a member keyed by user id.
|
|
70
|
+
*
|
|
71
|
+
* The supplied grant replaces any existing row in full (last write wins),
|
|
72
|
+
* including `grantedAt` and `updatedAt`.
|
|
73
|
+
*/
|
|
74
|
+
async upsert(member: PlatformMember): Promise<PlatformMember> {
|
|
75
|
+
this.db
|
|
76
|
+
.prepare(
|
|
77
|
+
`INSERT INTO lattice_platform_members
|
|
78
|
+
(user_id, role, granted_by, granted_at, updated_at)
|
|
79
|
+
VALUES (?, ?, ?, ?, ?)
|
|
80
|
+
ON CONFLICT(user_id) DO UPDATE SET
|
|
81
|
+
role = excluded.role,
|
|
82
|
+
granted_by = excluded.granted_by,
|
|
83
|
+
granted_at = excluded.granted_at,
|
|
84
|
+
updated_at = excluded.updated_at`,
|
|
85
|
+
)
|
|
86
|
+
.run(
|
|
87
|
+
member.userId,
|
|
88
|
+
member.role,
|
|
89
|
+
member.grantedBy ?? null,
|
|
90
|
+
member.grantedAt.toISOString(),
|
|
91
|
+
member.updatedAt.toISOString(),
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const stored = await this.get(member.userId);
|
|
95
|
+
if (!stored) {
|
|
96
|
+
throw new Error(`Platform member "${member.userId}" was not persisted`);
|
|
97
|
+
}
|
|
98
|
+
return stored;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Deletes a member. Returns whether a row was removed. */
|
|
102
|
+
async delete(userId: string): Promise<boolean> {
|
|
103
|
+
const result = this.db
|
|
104
|
+
.prepare(`DELETE FROM lattice_platform_members WHERE user_id = ?`)
|
|
105
|
+
.run(userId);
|
|
106
|
+
return result.changes > 0;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PlatformSettingsVersionConflictError,
|
|
3
|
+
type PlatformSettingsDocument,
|
|
4
|
+
type PlatformSettingsRecord,
|
|
5
|
+
type PlatformSettingsStore,
|
|
6
|
+
} from "@axiom-lattice/protocols";
|
|
7
|
+
import { DatabaseWrapper, ensureTable, nowISO, parseISO } from "../database";
|
|
8
|
+
|
|
9
|
+
const SETTINGS_ID = "default";
|
|
10
|
+
const COLUMNS = "id, document, version, updated_by, updated_at";
|
|
11
|
+
|
|
12
|
+
function normalizeUpdatedBy(updatedBy?: string): string | null {
|
|
13
|
+
const trimmed = updatedBy?.trim();
|
|
14
|
+
return trimmed ? trimmed : null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const DDL = `
|
|
18
|
+
CREATE TABLE IF NOT EXISTS lattice_platform_settings (
|
|
19
|
+
id TEXT NOT NULL PRIMARY KEY,
|
|
20
|
+
document TEXT NOT NULL,
|
|
21
|
+
version INTEGER NOT NULL,
|
|
22
|
+
updated_by TEXT,
|
|
23
|
+
updated_at TEXT NOT NULL
|
|
24
|
+
);
|
|
25
|
+
`;
|
|
26
|
+
|
|
27
|
+
interface SettingsRow {
|
|
28
|
+
id: string;
|
|
29
|
+
document: string;
|
|
30
|
+
version: number;
|
|
31
|
+
updated_by: string | null;
|
|
32
|
+
updated_at: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function mapRow(row: SettingsRow): PlatformSettingsRecord {
|
|
36
|
+
const document = JSON.parse(row.document) as PlatformSettingsDocument;
|
|
37
|
+
return {
|
|
38
|
+
...document,
|
|
39
|
+
version: Number(row.version),
|
|
40
|
+
...(row.updated_by !== null ? { updatedBy: row.updated_by } : {}),
|
|
41
|
+
updatedAt: parseISO(row.updated_at),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** SQLite implementation of {@link PlatformSettingsStore}. */
|
|
46
|
+
export class LocalPlatformSettingsStore implements PlatformSettingsStore {
|
|
47
|
+
private readonly db: DatabaseWrapper;
|
|
48
|
+
|
|
49
|
+
constructor(db: DatabaseWrapper) {
|
|
50
|
+
this.db = db;
|
|
51
|
+
ensureTable(db, DDL);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async get(): Promise<PlatformSettingsRecord | null> {
|
|
55
|
+
const row = this.db
|
|
56
|
+
.prepare(
|
|
57
|
+
`SELECT ${COLUMNS} FROM lattice_platform_settings WHERE id = ?`,
|
|
58
|
+
)
|
|
59
|
+
.get(SETTINGS_ID) as unknown as SettingsRow | undefined;
|
|
60
|
+
return row ? mapRow(row) : null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async save(
|
|
64
|
+
document: PlatformSettingsDocument,
|
|
65
|
+
expectedVersion: number,
|
|
66
|
+
updatedBy?: string,
|
|
67
|
+
): Promise<PlatformSettingsRecord> {
|
|
68
|
+
const version = expectedVersion + 1;
|
|
69
|
+
const updatedAt = nowISO();
|
|
70
|
+
const result =
|
|
71
|
+
expectedVersion === 0
|
|
72
|
+
? this.db
|
|
73
|
+
.prepare(
|
|
74
|
+
`INSERT INTO lattice_platform_settings
|
|
75
|
+
(id, document, version, updated_by, updated_at)
|
|
76
|
+
VALUES (?, ?, ?, ?, ?)
|
|
77
|
+
ON CONFLICT (id) DO NOTHING`,
|
|
78
|
+
)
|
|
79
|
+
.run(
|
|
80
|
+
SETTINGS_ID,
|
|
81
|
+
JSON.stringify(document),
|
|
82
|
+
version,
|
|
83
|
+
normalizeUpdatedBy(updatedBy),
|
|
84
|
+
updatedAt,
|
|
85
|
+
)
|
|
86
|
+
: this.db
|
|
87
|
+
.prepare(
|
|
88
|
+
`UPDATE lattice_platform_settings
|
|
89
|
+
SET document = ?, version = ?, updated_by = ?, updated_at = ?
|
|
90
|
+
WHERE id = ? AND version = ?`,
|
|
91
|
+
)
|
|
92
|
+
.run(
|
|
93
|
+
JSON.stringify(document),
|
|
94
|
+
version,
|
|
95
|
+
normalizeUpdatedBy(updatedBy),
|
|
96
|
+
updatedAt,
|
|
97
|
+
SETTINGS_ID,
|
|
98
|
+
expectedVersion,
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
if (result.changes === 0) {
|
|
102
|
+
const actual = this.db
|
|
103
|
+
.prepare(`SELECT version FROM lattice_platform_settings WHERE id = ?`)
|
|
104
|
+
.get(SETTINGS_ID) as unknown as { version: number } | undefined;
|
|
105
|
+
throw new PlatformSettingsVersionConflictError(
|
|
106
|
+
expectedVersion,
|
|
107
|
+
actual ? Number(actual.version) : 0,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const stored = await this.get();
|
|
112
|
+
if (!stored) throw new Error("Platform settings were not persisted");
|
|
113
|
+
return stored;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ExternalIdentityConflictError,
|
|
3
|
+
ExternalIdentityPersistError,
|
|
4
|
+
type UserExternalIdentity,
|
|
5
|
+
type UserExternalIdentityStore,
|
|
6
|
+
} from "@axiom-lattice/protocols";
|
|
7
|
+
import { DatabaseWrapper } from "../database";
|
|
8
|
+
import { ensureTable, parseISO } from "../database";
|
|
9
|
+
|
|
10
|
+
const DDL = `
|
|
11
|
+
CREATE TABLE IF NOT EXISTS lt_user_identities (
|
|
12
|
+
user_id TEXT NOT NULL,
|
|
13
|
+
provider_id TEXT NOT NULL,
|
|
14
|
+
issuer TEXT NOT NULL,
|
|
15
|
+
subject TEXT NOT NULL,
|
|
16
|
+
email TEXT,
|
|
17
|
+
linked_at TEXT NOT NULL,
|
|
18
|
+
PRIMARY KEY (provider_id, issuer, subject)
|
|
19
|
+
);
|
|
20
|
+
CREATE INDEX IF NOT EXISTS idx_lt_user_identities_user
|
|
21
|
+
ON lt_user_identities (user_id);
|
|
22
|
+
`;
|
|
23
|
+
|
|
24
|
+
interface IdentityRow {
|
|
25
|
+
user_id: string;
|
|
26
|
+
provider_id: string;
|
|
27
|
+
issuer: string;
|
|
28
|
+
subject: string;
|
|
29
|
+
email: string | null;
|
|
30
|
+
linked_at: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function mapRow(row: IdentityRow): UserExternalIdentity {
|
|
34
|
+
return {
|
|
35
|
+
userId: row.user_id,
|
|
36
|
+
providerId: row.provider_id,
|
|
37
|
+
issuer: row.issuer,
|
|
38
|
+
subject: row.subject,
|
|
39
|
+
...(row.email === null ? {} : { email: row.email }),
|
|
40
|
+
linkedAt: parseISO(row.linked_at),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class LocalUserExternalIdentityStore implements UserExternalIdentityStore {
|
|
45
|
+
private readonly db: DatabaseWrapper;
|
|
46
|
+
|
|
47
|
+
constructor(db: DatabaseWrapper) {
|
|
48
|
+
this.db = db;
|
|
49
|
+
ensureTable(db, DDL);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async getByProviderSubject(
|
|
53
|
+
providerId: string,
|
|
54
|
+
issuer: string,
|
|
55
|
+
subject: string,
|
|
56
|
+
): Promise<UserExternalIdentity | null> {
|
|
57
|
+
const row = this.db
|
|
58
|
+
.prepare(
|
|
59
|
+
`SELECT * FROM lt_user_identities
|
|
60
|
+
WHERE provider_id = ? AND issuer = ? AND subject = ?`,
|
|
61
|
+
)
|
|
62
|
+
.get(providerId, issuer, subject) as unknown as IdentityRow | undefined;
|
|
63
|
+
return row ? mapRow(row) : null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async listByUser(userId: string): Promise<UserExternalIdentity[]> {
|
|
67
|
+
const rows = this.db
|
|
68
|
+
.prepare(`SELECT * FROM lt_user_identities WHERE user_id = ? ORDER BY linked_at ASC`)
|
|
69
|
+
.all(userId) as unknown as IdentityRow[];
|
|
70
|
+
return rows.map(mapRow);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async link(identity: UserExternalIdentity): Promise<void> {
|
|
74
|
+
this.db.prepare(
|
|
75
|
+
`INSERT INTO lt_user_identities (user_id, provider_id, issuer, subject, email, linked_at)
|
|
76
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
77
|
+
ON CONFLICT(provider_id, issuer, subject) DO NOTHING`,
|
|
78
|
+
).run(
|
|
79
|
+
identity.userId,
|
|
80
|
+
identity.providerId,
|
|
81
|
+
identity.issuer,
|
|
82
|
+
identity.subject,
|
|
83
|
+
identity.email ?? null,
|
|
84
|
+
identity.linkedAt.toISOString(),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const existing = await this.getByProviderSubject(
|
|
88
|
+
identity.providerId,
|
|
89
|
+
identity.issuer,
|
|
90
|
+
identity.subject,
|
|
91
|
+
);
|
|
92
|
+
if (!existing) {
|
|
93
|
+
throw new ExternalIdentityPersistError(
|
|
94
|
+
identity.providerId,
|
|
95
|
+
identity.issuer,
|
|
96
|
+
identity.subject,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
if (existing.userId !== identity.userId) {
|
|
100
|
+
throw new ExternalIdentityConflictError(
|
|
101
|
+
identity.providerId,
|
|
102
|
+
identity.issuer,
|
|
103
|
+
identity.subject,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
if (identity.email !== undefined && identity.email !== existing.email) {
|
|
107
|
+
this.db.prepare(
|
|
108
|
+
`UPDATE lt_user_identities SET email = ?
|
|
109
|
+
WHERE provider_id = ? AND issuer = ? AND subject = ?`,
|
|
110
|
+
).run(identity.email, identity.providerId, identity.issuer, identity.subject);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async unlink(providerId: string, issuer: string, subject: string): Promise<void> {
|
|
115
|
+
this.db.prepare(
|
|
116
|
+
`DELETE FROM lt_user_identities
|
|
117
|
+
WHERE provider_id = ? AND issuer = ? AND subject = ?`,
|
|
118
|
+
).run(providerId, issuer, subject);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -23,6 +23,7 @@ CREATE TABLE IF NOT EXISTS lt_users (
|
|
|
23
23
|
updated_at TEXT NOT NULL
|
|
24
24
|
);
|
|
25
25
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_lt_users_email ON lt_users(email);
|
|
26
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_lt_users_email_lower ON lt_users(lower(email));
|
|
26
27
|
`;
|
|
27
28
|
|
|
28
29
|
interface UserRow {
|
|
@@ -59,8 +60,8 @@ export class LocalUserStore implements UserStore {
|
|
|
59
60
|
|
|
60
61
|
async getUserByEmail(email: string): Promise<User | null> {
|
|
61
62
|
const row = this.db.prepare(
|
|
62
|
-
`SELECT * FROM lt_users WHERE email =
|
|
63
|
-
).get(email) as unknown as UserRow | undefined;
|
|
63
|
+
`SELECT * FROM lt_users WHERE lower(email) = lower(?)`,
|
|
64
|
+
).get(email.trim()) as unknown as UserRow | undefined;
|
|
64
65
|
return row ? mapRowToUser(row) : null;
|
|
65
66
|
}
|
|
66
67
|
|