@zgeoff/mcp-auth 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/README.md +4 -2
- package/dist/index.d.ts +1 -0
- package/dist/passkeys/verify-passkey-assertion.d.ts +1 -0
- package/dist/passkeys/verify-passkey-registration.d.ts +1 -0
- package/dist/store/check-cascade-keys.d.ts +2 -2
- package/dist/store/check-foreign-keys.d.ts +2 -2
- package/dist/store/check-user-columns.d.ts +2 -2
- package/dist/store/create-owned-tables.d.ts +2 -2
- package/dist/store/open-store.d.ts +1 -0
- package/dist/store/read-table-columns.d.ts +2 -2
- package/dist/test-utils/open-test-auth.d.ts +2 -0
- package/dist/types.d.ts +5 -3
- package/package.json +2 -1
- package/src/admin/build-admin.ts +1 -1
- package/src/admin/build-client-admin.ts +24 -7
- package/src/index.ts +1 -0
- package/src/passkeys/verify-passkey-assertion.ts +3 -0
- package/src/passkeys/verify-passkey-registration.ts +3 -0
- package/src/store/check-cascade-keys.ts +6 -6
- package/src/store/check-foreign-keys.ts +7 -4
- package/src/store/check-user-columns.ts +3 -3
- package/src/store/create-owned-tables.ts +22 -11
- package/src/store/open-store.ts +27 -7
- package/src/store/read-table-columns.ts +8 -4
- package/src/test-utils/open-test-auth.ts +6 -1
- package/src/types.ts +15 -4
package/README.md
CHANGED
|
@@ -19,8 +19,10 @@ Kysely is a peer dependency. Elysia 2 is an optional peer dependency, needed onl
|
|
|
19
19
|
|
|
20
20
|
## Usage
|
|
21
21
|
|
|
22
|
-
The app opens the server once at start, on a `bun:sqlite` connection that it owns.
|
|
23
|
-
|
|
22
|
+
The app opens the server once at start, on a `bun:sqlite` connection that it owns. An app whose own
|
|
23
|
+
Kysely runs on that connection passes the Kysely as `database` instead, so both share one driver
|
|
24
|
+
lock. Each route is a function from `Request` to `Response`, and `verify` turns a bearer token into
|
|
25
|
+
an `Access` or `null`.
|
|
24
26
|
|
|
25
27
|
```ts
|
|
26
28
|
import { DEFAULT_PATHS, openMCPAuth } from '@zgeoff/mcp-auth';
|
package/dist/index.d.ts
CHANGED
|
@@ -3,5 +3,6 @@ export { MCPAuthError } from './mcp-auth-error.ts';
|
|
|
3
3
|
export type { MCPAuthErrorCode } from './mcp-auth-error.ts';
|
|
4
4
|
export { openMCPAuth } from './open-mcp-auth.ts';
|
|
5
5
|
export { openMCPAuthAdmin } from './open-mcp-auth-admin.ts';
|
|
6
|
+
export { normalizeClientName } from './utils/normalize-client-name.ts';
|
|
6
7
|
export { parseServiceTokenFile } from './service-tokens/parse-service-token-file.ts';
|
|
7
8
|
export type * from './types.ts';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare function checkCascadeKeys(
|
|
1
|
+
import type { QueryExecutorProvider } from 'kysely';
|
|
2
|
+
export declare function checkCascadeKeys(db: QueryExecutorProvider): Promise<void>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare function checkForeignKeys(
|
|
1
|
+
import type { QueryExecutorProvider } from 'kysely';
|
|
2
|
+
export declare function checkForeignKeys(db: QueryExecutorProvider): Promise<void>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare function checkUserColumns(
|
|
1
|
+
import type { QueryExecutorProvider } from 'kysely';
|
|
2
|
+
export declare function checkUserColumns(db: QueryExecutorProvider): Promise<void>;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { QueryExecutorProvider } from 'kysely';
|
|
2
2
|
import type { MCPAuthTables } from '../types.ts';
|
|
3
|
-
export declare function createOwnedTables(
|
|
3
|
+
export declare function createOwnedTables(db: QueryExecutorProvider, tables: MCPAuthTables): Promise<void>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare function readTableColumns(
|
|
1
|
+
import type { QueryExecutorProvider } from 'kysely';
|
|
2
|
+
export declare function readTableColumns(db: QueryExecutorProvider, table: string): Promise<string[]>;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { Database } from 'bun:sqlite';
|
|
2
|
+
import type { Kysely } from 'kysely';
|
|
2
3
|
import type { GrantID, GrantLimits, MCPAuth, SecurityEvent, ServiceTokenFile } from '../types.ts';
|
|
3
4
|
import { buildStubClock } from './build-stub-clock.ts';
|
|
4
5
|
interface TestAuthInput<Limits> {
|
|
5
6
|
readonly database?: Database;
|
|
7
|
+
readonly appKysely?: Kysely<any> | undefined;
|
|
6
8
|
readonly grantLimits?: GrantLimits<Limits>;
|
|
7
9
|
readonly serviceTokens?: {
|
|
8
10
|
readonly prefix: string;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Database } from 'bun:sqlite';
|
|
2
|
+
import type { Kysely } from 'kysely';
|
|
2
3
|
import type { Tagged } from 'type-fest';
|
|
3
4
|
export type GrantID = Tagged<string, 'GrantID'>;
|
|
4
5
|
export type OAuthClientID = Tagged<string, 'OAuthClientID'>;
|
|
@@ -54,7 +55,7 @@ export interface SecuritySink {
|
|
|
54
55
|
readonly record: (event: SecurityEvent) => void;
|
|
55
56
|
}
|
|
56
57
|
export interface MCPAuthAdminOptions<Scope extends string> {
|
|
57
|
-
readonly database: Database
|
|
58
|
+
readonly database: Database | Kysely<any>;
|
|
58
59
|
readonly scopes: readonly Scope[];
|
|
59
60
|
readonly tables: MCPAuthTables;
|
|
60
61
|
readonly clock: Clock;
|
|
@@ -111,8 +112,9 @@ export interface MCPAuthAdmin {
|
|
|
111
112
|
readonly clients: {
|
|
112
113
|
readonly list: () => Promise<ClientView[]>;
|
|
113
114
|
readonly add: (name: string, redirectURIs: readonly string[]) => Promise<ClientView>;
|
|
114
|
-
readonly
|
|
115
|
-
readonly
|
|
115
|
+
readonly readByName: (name: string) => Promise<ClientView[]>;
|
|
116
|
+
readonly update: (clientID: OAuthClientID, redirectURIs: readonly string[]) => Promise<ClientView>;
|
|
117
|
+
readonly remove: (clientID: OAuthClientID) => Promise<number>;
|
|
116
118
|
};
|
|
117
119
|
readonly grants: {
|
|
118
120
|
readonly list: () => Promise<GrantView[]>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zgeoff/mcp-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A shared OAuth server for the MCP route of an app, built on better-auth, with passkey sign-in for the owner",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"better-auth",
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
"@zgeoff/kysely-bun-sqlite": "0.3.2",
|
|
49
49
|
"@zgeoff/std": "0.4.3",
|
|
50
50
|
"better-auth": "1.7.15",
|
|
51
|
+
"reflect-metadata": "0.2.2",
|
|
51
52
|
"ts-pattern": "5.9.2",
|
|
52
53
|
"type-fest": "5.10.0",
|
|
53
54
|
"zod": "4.6.7"
|
package/src/admin/build-admin.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type { SignInState } from '../sign-in/create-sign-in-state.ts';
|
|
|
5
5
|
import type { Store } from '../store/open-store.ts';
|
|
6
6
|
import { removeGrantRows } from '../store/remove-grant-rows.ts';
|
|
7
7
|
import type { GrantID, MCPAuthAdmin, OAuthClientID } from '../types.ts';
|
|
8
|
+
import { normalizeClientName } from '../utils/normalize-client-name.ts';
|
|
8
9
|
import { checkRedirectURIs } from './check-redirect-uris.ts';
|
|
9
10
|
import { readClientName } from './read-client-name.ts';
|
|
10
11
|
import { toClientView } from './to-client-view.ts';
|
|
@@ -16,11 +17,15 @@ export function buildClientAdmin(
|
|
|
16
17
|
const readClient = (name: string) =>
|
|
17
18
|
store.db.selectFrom('oauthClient').selectAll().where('name', '=', name).executeTakeFirst();
|
|
18
19
|
|
|
19
|
-
const requireClient = async (
|
|
20
|
-
const client = await
|
|
20
|
+
const requireClient = async (clientID: OAuthClientID) => {
|
|
21
|
+
const client = await store.db
|
|
22
|
+
.selectFrom('oauthClient')
|
|
23
|
+
.selectAll()
|
|
24
|
+
.where('clientId', '=', clientID)
|
|
25
|
+
.executeTakeFirst();
|
|
21
26
|
|
|
22
27
|
if (client === undefined) {
|
|
23
|
-
throw new MCPAuthError('not-found', 'no client has that
|
|
28
|
+
throw new MCPAuthError('not-found', 'no client has that ID');
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
return client;
|
|
@@ -33,6 +38,18 @@ export function buildClientAdmin(
|
|
|
33
38
|
return rows.map((row) => toClientView(row));
|
|
34
39
|
},
|
|
35
40
|
|
|
41
|
+
// A database from the old apps can hold names that clean alike, so the
|
|
42
|
+
// match is on the cleaned stored name.
|
|
43
|
+
readByName: async (name) => {
|
|
44
|
+
const cleaned = readClientName(name);
|
|
45
|
+
|
|
46
|
+
const rows = await store.db.selectFrom('oauthClient').selectAll().orderBy('name').execute();
|
|
47
|
+
|
|
48
|
+
return rows
|
|
49
|
+
.filter((row) => normalizeClientName(row.name, '') === cleaned)
|
|
50
|
+
.map((row) => toClientView(row));
|
|
51
|
+
},
|
|
52
|
+
|
|
36
53
|
add: async (name, redirectURIs) => {
|
|
37
54
|
const cleaned = readClientName(name);
|
|
38
55
|
|
|
@@ -70,8 +87,8 @@ export function buildClientAdmin(
|
|
|
70
87
|
return toClientView(row);
|
|
71
88
|
},
|
|
72
89
|
|
|
73
|
-
update: async (
|
|
74
|
-
const client = await requireClient(
|
|
90
|
+
update: async (clientID, redirectURIs) => {
|
|
91
|
+
const client = await requireClient(clientID);
|
|
75
92
|
|
|
76
93
|
checkRedirectURIs(redirectURIs);
|
|
77
94
|
|
|
@@ -88,8 +105,8 @@ export function buildClientAdmin(
|
|
|
88
105
|
return toClientView(row);
|
|
89
106
|
},
|
|
90
107
|
|
|
91
|
-
remove: async (
|
|
92
|
-
const client = await requireClient(
|
|
108
|
+
remove: async (clientID) => {
|
|
109
|
+
const client = await requireClient(clientID);
|
|
93
110
|
|
|
94
111
|
const grants = await store.db
|
|
95
112
|
.selectFrom('user')
|
package/src/index.ts
CHANGED
|
@@ -3,5 +3,6 @@ export { MCPAuthError } from './mcp-auth-error.ts';
|
|
|
3
3
|
export type { MCPAuthErrorCode } from './mcp-auth-error.ts';
|
|
4
4
|
export { openMCPAuth } from './open-mcp-auth.ts';
|
|
5
5
|
export { openMCPAuthAdmin } from './open-mcp-auth-admin.ts';
|
|
6
|
+
export { normalizeClientName } from './utils/normalize-client-name.ts';
|
|
6
7
|
export { parseServiceTokenFile } from './service-tokens/parse-service-token-file.ts';
|
|
7
8
|
export type * from './types.ts';
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// The certificate code of simplewebauthn needs the polyfill loaded first, and a compiled binary does not load it.
|
|
2
|
+
// oxlint-disable-next-line import/no-unassigned-import -- the polyfill is a side effect
|
|
3
|
+
import 'reflect-metadata';
|
|
1
4
|
import { verifyAuthenticationResponse } from '@simplewebauthn/server';
|
|
2
5
|
import type {
|
|
3
6
|
AuthenticationResponseJSON,
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// The certificate code of simplewebauthn needs the polyfill loaded first, and a compiled binary does not load it.
|
|
2
|
+
// oxlint-disable-next-line import/no-unassigned-import -- the polyfill is a side effect
|
|
3
|
+
import 'reflect-metadata';
|
|
1
4
|
import type { RegistrationResponseJSON } from '@simplewebauthn/server';
|
|
2
5
|
import { verifyRegistrationResponse } from '@simplewebauthn/server';
|
|
3
6
|
import { toTagged } from '@zgeoff/std';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
import type { QueryExecutorProvider } from 'kysely';
|
|
2
3
|
import { z } from 'zod';
|
|
3
4
|
import { MCPAuthError } from '../mcp-auth-error.ts';
|
|
4
5
|
|
|
@@ -11,12 +12,11 @@ const KeyRowSchema = z.object({
|
|
|
11
12
|
on_delete: z.string(),
|
|
12
13
|
});
|
|
13
14
|
|
|
14
|
-
export function checkCascadeKeys(
|
|
15
|
+
export async function checkCascadeKeys(db: QueryExecutorProvider): Promise<void> {
|
|
15
16
|
for (const table of CASCADING_TABLES) {
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
.map((row) => KeyRowSchema.parse(row));
|
|
17
|
+
const result = await sql`PRAGMA foreign_key_list(${sql.raw(`"${table}"`)})`.execute(db);
|
|
18
|
+
|
|
19
|
+
const keys = result.rows.map((row) => KeyRowSchema.parse(row));
|
|
20
20
|
|
|
21
21
|
const isCascading = keys.some(
|
|
22
22
|
(key) =>
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
import type { QueryExecutorProvider } from 'kysely';
|
|
2
3
|
import { z } from 'zod';
|
|
3
4
|
import { MCPAuthError } from '../mcp-auth-error.ts';
|
|
4
5
|
|
|
@@ -6,10 +7,12 @@ const PragmaRowSchema = z.object({ foreign_keys: z.number() });
|
|
|
6
7
|
|
|
7
8
|
// A pragma inside a transaction does nothing, so the core reads the setting
|
|
8
9
|
// back instead of trusting the statement.
|
|
9
|
-
export function checkForeignKeys(
|
|
10
|
-
|
|
10
|
+
export async function checkForeignKeys(db: QueryExecutorProvider): Promise<void> {
|
|
11
|
+
await sql`PRAGMA foreign_keys = ON`.execute(db);
|
|
11
12
|
|
|
12
|
-
const
|
|
13
|
+
const result = await sql`PRAGMA foreign_keys`.execute(db);
|
|
14
|
+
|
|
15
|
+
const row = PragmaRowSchema.parse(result.rows[0]);
|
|
13
16
|
|
|
14
17
|
if (row.foreign_keys === 0) {
|
|
15
18
|
throw new MCPAuthError(
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { QueryExecutorProvider } from 'kysely';
|
|
2
2
|
import { MCPAuthError } from '../mcp-auth-error.ts';
|
|
3
3
|
import { readTableColumns } from './read-table-columns.ts';
|
|
4
4
|
|
|
5
5
|
// A user table from before the grant columns cannot take them as new required
|
|
6
6
|
// columns, so the app's migration comes first.
|
|
7
|
-
export function checkUserColumns(
|
|
8
|
-
const columns = readTableColumns(
|
|
7
|
+
export async function checkUserColumns(db: QueryExecutorProvider): Promise<void> {
|
|
8
|
+
const columns = await readTableColumns(db, 'user');
|
|
9
9
|
|
|
10
10
|
if (columns.length === 0) {
|
|
11
11
|
return;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
import type { QueryExecutorProvider } from 'kysely';
|
|
2
3
|
import { MCPAuthError } from '../mcp-auth-error.ts';
|
|
3
4
|
import type { MCPAuthTables } from '../types.ts';
|
|
4
5
|
import { readTableColumns } from './read-table-columns.ts';
|
|
@@ -16,12 +17,18 @@ const ENROLLMENT_COLUMNS = ['token_hash', 'created_at', 'expires_at'] as const;
|
|
|
16
17
|
|
|
17
18
|
// The core creates its own table and any column of it that is missing. It
|
|
18
19
|
// creates the app's passkey table when absent, and never adds a column to it.
|
|
19
|
-
export function createOwnedTables(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
export async function createOwnedTables(
|
|
21
|
+
db: QueryExecutorProvider,
|
|
22
|
+
tables: MCPAuthTables,
|
|
23
|
+
): Promise<void> {
|
|
24
|
+
await sql
|
|
25
|
+
.raw(
|
|
26
|
+
`create table if not exists "${tables.passkey}" (id text primary key not null, public_key text not null, counter integer not null, transports text not null, created_at text not null, last_used_at text)`,
|
|
27
|
+
)
|
|
28
|
+
.execute(db);
|
|
29
|
+
|
|
30
|
+
const passkeyColumns = await readTableColumns(db, tables.passkey);
|
|
23
31
|
|
|
24
|
-
const passkeyColumns = readTableColumns(sqlite, tables.passkey);
|
|
25
32
|
const missing = PASSKEY_COLUMNS.filter((column) => !passkeyColumns.includes(column));
|
|
26
33
|
|
|
27
34
|
if (missing.length > 0) {
|
|
@@ -31,15 +38,19 @@ export function createOwnedTables(sqlite: Database, tables: MCPAuthTables): void
|
|
|
31
38
|
);
|
|
32
39
|
}
|
|
33
40
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
41
|
+
await sql
|
|
42
|
+
.raw(
|
|
43
|
+
`create table if not exists "${tables.enrollmentToken}" (token_hash text primary key not null, created_at text not null, expires_at text not null)`,
|
|
44
|
+
)
|
|
45
|
+
.execute(db);
|
|
37
46
|
|
|
38
|
-
const enrollmentColumns = readTableColumns(
|
|
47
|
+
const enrollmentColumns = await readTableColumns(db, tables.enrollmentToken);
|
|
39
48
|
|
|
40
49
|
for (const column of ENROLLMENT_COLUMNS) {
|
|
41
50
|
if (!enrollmentColumns.includes(column)) {
|
|
42
|
-
|
|
51
|
+
await sql
|
|
52
|
+
.raw(`alter table "${tables.enrollmentToken}" add column "${column}" text`)
|
|
53
|
+
.execute(db);
|
|
43
54
|
}
|
|
44
55
|
}
|
|
45
56
|
}
|
package/src/store/open-store.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Database } from 'bun:sqlite';
|
|
1
2
|
import { buildSQLiteDialect } from '@zgeoff/kysely-bun-sqlite';
|
|
2
3
|
import { getMigrations } from 'better-auth/db/migration';
|
|
3
4
|
import { Kysely } from 'kysely';
|
|
@@ -15,8 +16,14 @@ export interface Store {
|
|
|
15
16
|
readonly db: Kysely<StoreSchema>;
|
|
16
17
|
readonly auth: AuthServer;
|
|
17
18
|
readonly options: MCPAuthAdminOptions<string>;
|
|
19
|
+
|
|
20
|
+
// Ends the Kysely the store built, and leaves the app's own Kysely running.
|
|
21
|
+
readonly close: () => Promise<void>;
|
|
18
22
|
}
|
|
19
23
|
|
|
24
|
+
// A mapped type has the index signature that withTables asks for.
|
|
25
|
+
type StoreTables = { [Table in keyof StoreSchema]: StoreSchema[Table] };
|
|
26
|
+
|
|
20
27
|
interface StoreOptions {
|
|
21
28
|
readonly origin: string | null;
|
|
22
29
|
readonly paths: MCPAuthPaths;
|
|
@@ -29,12 +36,25 @@ export async function openStore(
|
|
|
29
36
|
options: StoreOptions,
|
|
30
37
|
): Promise<Store> {
|
|
31
38
|
checkTableNames(admin.tables);
|
|
32
|
-
checkForeignKeys(admin.database);
|
|
33
|
-
checkUserColumns(admin.database);
|
|
34
39
|
|
|
35
|
-
|
|
40
|
+
// A Kysely of the app is the app's driver and its one lock: a second Kysely
|
|
41
|
+
// over the same connection has a lock of its own, so a core statement could
|
|
42
|
+
// run inside an app transaction and an app rollback would undo it.
|
|
43
|
+
const db: Kysely<StoreSchema> =
|
|
44
|
+
admin.database instanceof Database
|
|
45
|
+
? new Kysely<StoreSchema>({ dialect: buildSQLiteDialect(admin.database) })
|
|
46
|
+
: admin.database.$extendTables<StoreTables>();
|
|
47
|
+
|
|
48
|
+
const close = async () => {
|
|
49
|
+
if (admin.database instanceof Database) {
|
|
50
|
+
await db.destroy();
|
|
51
|
+
}
|
|
52
|
+
};
|
|
36
53
|
|
|
37
54
|
try {
|
|
55
|
+
await checkForeignKeys(db);
|
|
56
|
+
await checkUserColumns(db);
|
|
57
|
+
|
|
38
58
|
const auth = createAuthServer({
|
|
39
59
|
db,
|
|
40
60
|
origin: options.origin,
|
|
@@ -46,8 +66,8 @@ export async function openStore(
|
|
|
46
66
|
|
|
47
67
|
await migrations.runMigrations();
|
|
48
68
|
|
|
49
|
-
createOwnedTables(
|
|
50
|
-
checkCascadeKeys(
|
|
69
|
+
await createOwnedTables(db, admin.tables);
|
|
70
|
+
await checkCascadeKeys(db);
|
|
51
71
|
|
|
52
72
|
// better-auth starts its context when it is built, and that start queries
|
|
53
73
|
// the database, so a close before it settles destroys the driver under it.
|
|
@@ -63,9 +83,9 @@ export async function openStore(
|
|
|
63
83
|
onGrantRevoked: admin.onGrantRevoked,
|
|
64
84
|
};
|
|
65
85
|
|
|
66
|
-
return { db, auth, options: kept };
|
|
86
|
+
return { db, auth, options: kept, close };
|
|
67
87
|
} catch (error) {
|
|
68
|
-
await
|
|
88
|
+
await close();
|
|
69
89
|
|
|
70
90
|
throw error;
|
|
71
91
|
}
|
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
import type { QueryExecutorProvider } from 'kysely';
|
|
2
3
|
import { z } from 'zod';
|
|
3
4
|
|
|
4
5
|
const ColumnRowSchema = z.object({ name: z.string() });
|
|
5
6
|
|
|
6
7
|
// The columns a table holds, or an empty list when it does not exist. The
|
|
7
8
|
// table name goes into the statement, so the caller has checked it.
|
|
8
|
-
export function readTableColumns(
|
|
9
|
-
|
|
9
|
+
export async function readTableColumns(
|
|
10
|
+
db: QueryExecutorProvider,
|
|
11
|
+
table: string,
|
|
12
|
+
): Promise<string[]> {
|
|
13
|
+
const result = await sql`PRAGMA table_info(${sql.raw(`"${table}"`)})`.execute(db);
|
|
10
14
|
|
|
11
|
-
return rows.map((row) => ColumnRowSchema.parse(row).name);
|
|
15
|
+
return result.rows.map((row) => ColumnRowSchema.parse(row).name);
|
|
12
16
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Database } from 'bun:sqlite';
|
|
2
|
+
import type { Kysely } from 'kysely';
|
|
2
3
|
import { DEFAULT_PATHS } from '../default-paths.ts';
|
|
3
4
|
import { openMCPAuth } from '../open-mcp-auth.ts';
|
|
4
5
|
import type { GrantID, GrantLimits, MCPAuth, SecurityEvent, ServiceTokenFile } from '../types.ts';
|
|
@@ -7,6 +8,10 @@ import { createTestDatabase } from './create-test-database.ts';
|
|
|
7
8
|
|
|
8
9
|
interface TestAuthInput<Limits> {
|
|
9
10
|
readonly database?: Database;
|
|
11
|
+
|
|
12
|
+
// The app's Kysely over the database, which the core then shares.
|
|
13
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- an app's Kysely is typed with its own tables
|
|
14
|
+
readonly appKysely?: Kysely<any> | undefined;
|
|
10
15
|
readonly grantLimits?: GrantLimits<Limits>;
|
|
11
16
|
readonly serviceTokens?: {
|
|
12
17
|
readonly prefix: string;
|
|
@@ -38,7 +43,7 @@ export async function openTestAuth<Limits = null>(
|
|
|
38
43
|
const revoked: GrantID[] = [];
|
|
39
44
|
|
|
40
45
|
const auth = await openMCPAuth({
|
|
41
|
-
database,
|
|
46
|
+
database: input.appKysely ?? database,
|
|
42
47
|
origin: 'https://app.example',
|
|
43
48
|
productName: 'app',
|
|
44
49
|
scopes: ['read', 'write'],
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Database } from 'bun:sqlite';
|
|
2
|
+
import type { Kysely } from 'kysely';
|
|
2
3
|
import type { Tagged } from 'type-fest';
|
|
3
4
|
|
|
4
5
|
export type GrantID = Tagged<string, 'GrantID'>;
|
|
@@ -79,7 +80,10 @@ export interface SecuritySink {
|
|
|
79
80
|
}
|
|
80
81
|
|
|
81
82
|
export interface MCPAuthAdminOptions<Scope extends string> {
|
|
82
|
-
|
|
83
|
+
// The app's own Kysely when one runs on this connection, so the core shares
|
|
84
|
+
// its driver lock. A Database only when no other Kysely uses it.
|
|
85
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- an app's Kysely is typed with its own tables, which only any accepts
|
|
86
|
+
readonly database: Database | Kysely<any>;
|
|
83
87
|
readonly scopes: readonly Scope[];
|
|
84
88
|
readonly tables: MCPAuthTables;
|
|
85
89
|
readonly clock: Clock;
|
|
@@ -140,10 +144,17 @@ export interface MCPAuthAdmin {
|
|
|
140
144
|
readonly clients: {
|
|
141
145
|
readonly list: () => Promise<ClientView[]>;
|
|
142
146
|
readonly add: (name: string, redirectURIs: readonly string[]) => Promise<ClientView>;
|
|
143
|
-
|
|
147
|
+
|
|
148
|
+
// The clients whose stored name cleans to the same text as `name`: more
|
|
149
|
+
// than one when an old database holds two names that clean alike.
|
|
150
|
+
readonly readByName: (name: string) => Promise<ClientView[]>;
|
|
151
|
+
readonly update: (
|
|
152
|
+
clientID: OAuthClientID,
|
|
153
|
+
redirectURIs: readonly string[],
|
|
154
|
+
) => Promise<ClientView>;
|
|
144
155
|
|
|
145
156
|
// The number of grants it revoked.
|
|
146
|
-
readonly remove: (
|
|
157
|
+
readonly remove: (clientID: OAuthClientID) => Promise<number>;
|
|
147
158
|
};
|
|
148
159
|
readonly grants: {
|
|
149
160
|
// Grants with a live refresh token.
|
|
@@ -163,7 +174,7 @@ export interface MCPAuthAdmin {
|
|
|
163
174
|
};
|
|
164
175
|
readonly removeExpired: () => Promise<void>;
|
|
165
176
|
|
|
166
|
-
// Leaves the
|
|
177
|
+
// Leaves the connection open, and the app's Kysely too.
|
|
167
178
|
readonly close: () => Promise<void>;
|
|
168
179
|
}
|
|
169
180
|
|