local-llm-gateway 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/CHANGELOG.md +33 -0
- package/README.md +16 -8
- package/cli/dist/api/client.js +4 -0
- package/cli/dist/commands/open.js +40 -0
- package/cli/dist/index.js +6 -0
- package/cli/dist/key-diagnosis.js +7 -7
- package/cli/dist/repl.js +11 -1
- package/dist/src/api/endpoints.js +39 -1
- package/dist/src/api/openapi.js +9 -2
- package/dist/src/api/schemas.js +2 -0
- package/dist/src/app.js +27 -2
- package/dist/src/config.js +3 -0
- package/dist/src/lib/web-root.js +16 -0
- package/dist/src/plugins/auth.js +31 -8
- package/dist/src/plugins/metrics.js +1 -1
- package/dist/src/plugins/web-guard.js +37 -0
- package/dist/src/routes/web-page.js +42 -0
- package/dist/src/routes/web-session.js +52 -0
- package/dist/src/server.js +0 -0
- package/dist/src/services/web-session.service.js +50 -0
- package/dist/src/storage/postgres/index.js +2 -0
- package/dist/src/storage/postgres/schema.js +19 -0
- package/dist/src/storage/postgres/web-sessions.js +50 -0
- package/dist/src/storage/sqlite/index.js +2 -0
- package/dist/src/storage/sqlite/schema.js +18 -0
- package/dist/src/storage/sqlite/web-sessions.js +50 -0
- package/dist/web/assets/index-C03RJbF2.js +17 -0
- package/dist/web/assets/index-CDL8Tl5X.css +1 -0
- package/dist/web/index.html +14 -0
- package/drizzle/0003_pale_spectrum.sql +24 -0
- package/drizzle/meta/0003_snapshot.json +735 -0
- package/drizzle/meta/_journal.json +7 -0
- package/drizzle/sqlite/0001_blushing_jack_murdock.sql +24 -0
- package/drizzle/sqlite/meta/0001_snapshot.json +698 -0
- package/drizzle/sqlite/meta/_journal.json +7 -0
- package/package.json +22 -9
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Browser sign-in for the web chat. The rules, in one place:
|
|
2
|
+
// - `llm open` (holding an API key) asks for a one-time code: random, 60 seconds, works once.
|
|
3
|
+
// - The page exchanges the code for a session token, sent back only as an HttpOnly cookie.
|
|
4
|
+
// - A session ends after 30 days without use; "last seen" is written at most once a minute.
|
|
5
|
+
// Codes and tokens are 256-bit random values; only their SHA-256 hashes are stored.
|
|
6
|
+
import { randomBytes } from 'node:crypto';
|
|
7
|
+
import { sha256 } from '../lib/hash.js';
|
|
8
|
+
export const CODE_TTL_MS = 60_000;
|
|
9
|
+
export const SESSION_IDLE_MS = 30 * 24 * 3_600_000;
|
|
10
|
+
const TOUCH_EVERY_MS = 60_000;
|
|
11
|
+
const token = () => randomBytes(32).toString('base64url');
|
|
12
|
+
export class WebSessionService {
|
|
13
|
+
store;
|
|
14
|
+
now;
|
|
15
|
+
constructor(store, now = () => new Date()) {
|
|
16
|
+
this.store = store;
|
|
17
|
+
this.now = now;
|
|
18
|
+
}
|
|
19
|
+
async createCode(userId) {
|
|
20
|
+
const now = this.now();
|
|
21
|
+
const code = token();
|
|
22
|
+
const expiresAt = new Date(now.getTime() + CODE_TTL_MS);
|
|
23
|
+
await this.store.deleteExpired(new Date(now.getTime() - 24 * 3_600_000)); // cheap housekeeping
|
|
24
|
+
await this.store.createCode(userId, sha256(code), expiresAt);
|
|
25
|
+
return { code, expiresAt };
|
|
26
|
+
}
|
|
27
|
+
/** The raw session token to put in the cookie, or null if the code is unknown, used or expired. */
|
|
28
|
+
async redeem(code) {
|
|
29
|
+
const now = this.now();
|
|
30
|
+
const hit = await this.store.redeemCode(sha256(code), now);
|
|
31
|
+
if (!hit)
|
|
32
|
+
return null;
|
|
33
|
+
const raw = token();
|
|
34
|
+
await this.store.createSession(hit.userId, sha256(raw), now, new Date(now.getTime() + SESSION_IDLE_MS));
|
|
35
|
+
return { token: raw, maxAgeSeconds: SESSION_IDLE_MS / 1000 };
|
|
36
|
+
}
|
|
37
|
+
async authenticate(rawToken) {
|
|
38
|
+
const now = this.now();
|
|
39
|
+
const s = await this.store.findActiveSession(sha256(rawToken), now);
|
|
40
|
+
if (!s)
|
|
41
|
+
return null;
|
|
42
|
+
if (now.getTime() - s.lastSeenAt.getTime() >= TOUCH_EVERY_MS) {
|
|
43
|
+
await this.store.touchSession(s.id, now, new Date(now.getTime() + SESSION_IDLE_MS));
|
|
44
|
+
}
|
|
45
|
+
return { userId: s.userId, sessionId: s.id };
|
|
46
|
+
}
|
|
47
|
+
signOut(rawToken) {
|
|
48
|
+
return this.store.revokeSession(sha256(rawToken), this.now());
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -6,11 +6,13 @@ import { PgConversationsStore } from './conversations.js';
|
|
|
6
6
|
import { PgGenerationsStore } from './generations.js';
|
|
7
7
|
import { PgApiKeysStore, PgUsersStore } from './identity.js';
|
|
8
8
|
import { PgMessagesStore } from './messages.js';
|
|
9
|
+
import { PgWebSessionsStore } from './web-sessions.js';
|
|
9
10
|
export function createPostgresStorage(url) {
|
|
10
11
|
const { db, close } = createDb(url);
|
|
11
12
|
return {
|
|
12
13
|
db,
|
|
13
14
|
users: new PgUsersStore(db),
|
|
15
|
+
webSessions: new PgWebSessionsStore(db),
|
|
14
16
|
apiKeys: new PgApiKeysStore(db),
|
|
15
17
|
conversations: new PgConversationsStore(db),
|
|
16
18
|
messages: new PgMessagesStore(db),
|
|
@@ -66,3 +66,22 @@ export const generations = pgTable('generations', {
|
|
|
66
66
|
index('generations_user_created_idx').on(t.userId, t.createdAt),
|
|
67
67
|
index('generations_request_idx').on(t.requestId),
|
|
68
68
|
]);
|
|
69
|
+
// Browser sign-in (web chat). Only hashes are stored, like API keys.
|
|
70
|
+
// A one-time code (from `llm open`) is exchanged once for a session cookie.
|
|
71
|
+
export const webCodes = pgTable('web_codes', {
|
|
72
|
+
id: uuid('id').primaryKey().defaultRandom(),
|
|
73
|
+
userId: uuid('user_id').notNull().references(() => users.id),
|
|
74
|
+
codeHash: text('code_hash').notNull().unique(),
|
|
75
|
+
expiresAt: ts('expires_at').notNull(),
|
|
76
|
+
usedAt: ts('used_at'),
|
|
77
|
+
createdAt: ts('created_at').notNull().defaultNow(),
|
|
78
|
+
});
|
|
79
|
+
export const webSessions = pgTable('web_sessions', {
|
|
80
|
+
id: uuid('id').primaryKey().defaultRandom(),
|
|
81
|
+
userId: uuid('user_id').notNull().references(() => users.id),
|
|
82
|
+
tokenHash: text('token_hash').notNull().unique(),
|
|
83
|
+
createdAt: ts('created_at').notNull().defaultNow(),
|
|
84
|
+
lastSeenAt: ts('last_seen_at').notNull(),
|
|
85
|
+
expiresAt: ts('expires_at').notNull(), // slides forward with use (idle timeout)
|
|
86
|
+
revokedAt: ts('revoked_at'),
|
|
87
|
+
}, (t) => [index('web_sessions_user_idx').on(t.userId)]);
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { and, eq, gt, isNotNull, isNull, lt, or } from 'drizzle-orm';
|
|
2
|
+
import { webCodes, webSessions } from './schema.js';
|
|
3
|
+
export class PgWebSessionsStore {
|
|
4
|
+
db;
|
|
5
|
+
constructor(db) {
|
|
6
|
+
this.db = db;
|
|
7
|
+
}
|
|
8
|
+
async createCode(userId, codeHash, expiresAt) {
|
|
9
|
+
await this.db.insert(webCodes).values({ userId, codeHash, expiresAt });
|
|
10
|
+
}
|
|
11
|
+
async redeemCode(codeHash, now) {
|
|
12
|
+
// One UPDATE: two concurrent redeems can't both see "unused" (row lock on update).
|
|
13
|
+
const [row] = await this.db
|
|
14
|
+
.update(webCodes)
|
|
15
|
+
.set({ usedAt: now })
|
|
16
|
+
.where(and(eq(webCodes.codeHash, codeHash), isNull(webCodes.usedAt), gt(webCodes.expiresAt, now)))
|
|
17
|
+
.returning({ userId: webCodes.userId });
|
|
18
|
+
return row ?? null;
|
|
19
|
+
}
|
|
20
|
+
async createSession(userId, tokenHash, now, expiresAt) {
|
|
21
|
+
const [row] = await this.db
|
|
22
|
+
.insert(webSessions)
|
|
23
|
+
.values({ userId, tokenHash, lastSeenAt: now, expiresAt })
|
|
24
|
+
.returning({ id: webSessions.id });
|
|
25
|
+
return row;
|
|
26
|
+
}
|
|
27
|
+
async findActiveSession(tokenHash, now) {
|
|
28
|
+
const [row] = await this.db
|
|
29
|
+
.select({ id: webSessions.id, userId: webSessions.userId, lastSeenAt: webSessions.lastSeenAt })
|
|
30
|
+
.from(webSessions)
|
|
31
|
+
.where(and(eq(webSessions.tokenHash, tokenHash), isNull(webSessions.revokedAt), gt(webSessions.expiresAt, now)))
|
|
32
|
+
.limit(1);
|
|
33
|
+
return row ?? null;
|
|
34
|
+
}
|
|
35
|
+
async touchSession(id, now, expiresAt) {
|
|
36
|
+
await this.db.update(webSessions).set({ lastSeenAt: now, expiresAt }).where(eq(webSessions.id, id));
|
|
37
|
+
}
|
|
38
|
+
async revokeSession(tokenHash, now) {
|
|
39
|
+
const rows = await this.db
|
|
40
|
+
.update(webSessions)
|
|
41
|
+
.set({ revokedAt: now })
|
|
42
|
+
.where(and(eq(webSessions.tokenHash, tokenHash), isNull(webSessions.revokedAt)))
|
|
43
|
+
.returning({ id: webSessions.id });
|
|
44
|
+
return rows.length > 0;
|
|
45
|
+
}
|
|
46
|
+
async deleteExpired(before) {
|
|
47
|
+
await this.db.delete(webCodes).where(or(lt(webCodes.expiresAt, before), isNotNull(webCodes.usedAt)));
|
|
48
|
+
await this.db.delete(webSessions).where(or(lt(webSessions.expiresAt, before), lt(webSessions.revokedAt, before)));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -5,12 +5,14 @@ import { SqliteConversationsStore } from './conversations.js';
|
|
|
5
5
|
import { SqliteGenerationsStore } from './generations.js';
|
|
6
6
|
import { SqliteApiKeysStore, SqliteUsersStore } from './identity.js';
|
|
7
7
|
import { SqliteMessagesStore } from './messages.js';
|
|
8
|
+
import { SqliteWebSessionsStore } from './web-sessions.js';
|
|
8
9
|
/** `path` is a database file, or `:memory:` for a private in-memory database (tests). */
|
|
9
10
|
export function createSqliteStorage(path) {
|
|
10
11
|
const { sqlite, db } = openSqlite(path);
|
|
11
12
|
return {
|
|
12
13
|
db,
|
|
13
14
|
users: new SqliteUsersStore(db),
|
|
15
|
+
webSessions: new SqliteWebSessionsStore(db),
|
|
14
16
|
apiKeys: new SqliteApiKeysStore(db),
|
|
15
17
|
conversations: new SqliteConversationsStore(db),
|
|
16
18
|
messages: new SqliteMessagesStore(db),
|
|
@@ -72,3 +72,21 @@ export const generations = sqliteTable('generations', {
|
|
|
72
72
|
index('generations_user_created_idx').on(t.userId, t.createdAt),
|
|
73
73
|
index('generations_request_idx').on(t.requestId),
|
|
74
74
|
]);
|
|
75
|
+
// Browser sign-in (web chat). Mirrors the Postgres tables; only hashes are stored.
|
|
76
|
+
export const webCodes = sqliteTable('web_codes', {
|
|
77
|
+
id: id(),
|
|
78
|
+
userId: text('user_id').notNull().references(() => users.id),
|
|
79
|
+
codeHash: text('code_hash').notNull().unique(),
|
|
80
|
+
expiresAt: ms('expires_at').notNull(),
|
|
81
|
+
usedAt: ms('used_at'),
|
|
82
|
+
createdAt: createdAt(),
|
|
83
|
+
});
|
|
84
|
+
export const webSessions = sqliteTable('web_sessions', {
|
|
85
|
+
id: id(),
|
|
86
|
+
userId: text('user_id').notNull().references(() => users.id),
|
|
87
|
+
tokenHash: text('token_hash').notNull().unique(),
|
|
88
|
+
createdAt: createdAt(),
|
|
89
|
+
lastSeenAt: ms('last_seen_at').notNull(),
|
|
90
|
+
expiresAt: ms('expires_at').notNull(), // slides forward with use (idle timeout)
|
|
91
|
+
revokedAt: ms('revoked_at'),
|
|
92
|
+
}, (t) => [index('web_sessions_user_idx').on(t.userId)]);
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { and, eq, gt, isNotNull, isNull, lt, or } from 'drizzle-orm';
|
|
2
|
+
import { webCodes, webSessions } from './schema.js';
|
|
3
|
+
export class SqliteWebSessionsStore {
|
|
4
|
+
db;
|
|
5
|
+
constructor(db) {
|
|
6
|
+
this.db = db;
|
|
7
|
+
}
|
|
8
|
+
async createCode(userId, codeHash, expiresAt) {
|
|
9
|
+
this.db.insert(webCodes).values({ userId, codeHash, expiresAt }).run();
|
|
10
|
+
}
|
|
11
|
+
async redeemCode(codeHash, now) {
|
|
12
|
+
// One UPDATE ... RETURNING: SQLite runs it as a single write, so a code works once.
|
|
13
|
+
return (this.db
|
|
14
|
+
.update(webCodes)
|
|
15
|
+
.set({ usedAt: now })
|
|
16
|
+
.where(and(eq(webCodes.codeHash, codeHash), isNull(webCodes.usedAt), gt(webCodes.expiresAt, now)))
|
|
17
|
+
.returning({ userId: webCodes.userId })
|
|
18
|
+
.get() ?? null);
|
|
19
|
+
}
|
|
20
|
+
async createSession(userId, tokenHash, now, expiresAt) {
|
|
21
|
+
return this.db
|
|
22
|
+
.insert(webSessions)
|
|
23
|
+
.values({ userId, tokenHash, lastSeenAt: now, expiresAt })
|
|
24
|
+
.returning({ id: webSessions.id })
|
|
25
|
+
.get();
|
|
26
|
+
}
|
|
27
|
+
async findActiveSession(tokenHash, now) {
|
|
28
|
+
return (this.db
|
|
29
|
+
.select({ id: webSessions.id, userId: webSessions.userId, lastSeenAt: webSessions.lastSeenAt })
|
|
30
|
+
.from(webSessions)
|
|
31
|
+
.where(and(eq(webSessions.tokenHash, tokenHash), isNull(webSessions.revokedAt), gt(webSessions.expiresAt, now)))
|
|
32
|
+
.get() ?? null);
|
|
33
|
+
}
|
|
34
|
+
async touchSession(id, now, expiresAt) {
|
|
35
|
+
this.db.update(webSessions).set({ lastSeenAt: now, expiresAt }).where(eq(webSessions.id, id)).run();
|
|
36
|
+
}
|
|
37
|
+
async revokeSession(tokenHash, now) {
|
|
38
|
+
const rows = this.db
|
|
39
|
+
.update(webSessions)
|
|
40
|
+
.set({ revokedAt: now })
|
|
41
|
+
.where(and(eq(webSessions.tokenHash, tokenHash), isNull(webSessions.revokedAt)))
|
|
42
|
+
.returning({ id: webSessions.id })
|
|
43
|
+
.all();
|
|
44
|
+
return rows.length > 0;
|
|
45
|
+
}
|
|
46
|
+
async deleteExpired(before) {
|
|
47
|
+
this.db.delete(webCodes).where(or(lt(webCodes.expiresAt, before), isNotNull(webCodes.usedAt))).run();
|
|
48
|
+
this.db.delete(webSessions).where(or(lt(webSessions.expiresAt, before), lt(webSessions.revokedAt, before))).run();
|
|
49
|
+
}
|
|
50
|
+
}
|