@zgeoff/mcp-auth 0.3.0 → 0.5.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.
@@ -1,3 +1,8 @@
1
+ import type { Kysely } from 'kysely';
1
2
  import type { GrantID } from '../types.ts';
2
3
  import type { Store } from './open-store.ts';
4
+ import type { StoreSchema } from './store-schema.ts';
3
5
  export declare function removeGrantRows(store: Store, grantIDs: readonly GrantID[]): Promise<GrantID[]>;
6
+ export declare function buildGrantRemoval(db: Kysely<StoreSchema>, grantIDs: readonly GrantID[]): import("kysely").DeleteQueryBuilder<StoreSchema, "user", {
7
+ id: GrantID;
8
+ }>;
@@ -1,13 +1,18 @@
1
- import type { MCPAuth } from '../types.ts';
1
+ import type { Database } from 'bun:sqlite';
2
+ import type { GrantID, MCPAuth } from '../types.ts';
2
3
  import { authorizeTestClient } from './authorize-test-client.ts';
3
4
  import { seedClient } from './seed-client.ts';
4
5
  import { seedPasskey } from './seed-passkey.ts';
5
6
  export interface TestDeps {
6
7
  readonly auth: MCPAuth<string, unknown>;
8
+ readonly database: Database;
9
+ readonly otherDatabase?: Database;
7
10
  }
8
11
  export declare function buildSeeder(deps: TestDeps): {
9
12
  client: (input?: Parameters<typeof seedClient>[1]) => Promise<import("../types.ts").ClientView>;
10
13
  enrollmentToken: () => Promise<string>;
11
14
  passkey: (input: Parameters<typeof seedPasskey>[1]) => Promise<void>;
12
15
  grant: (input: Parameters<typeof authorizeTestClient>[1]) => Promise<import("./authorize-test-client.ts").TestClientAuthorization>;
16
+ keyBoundAccessTokens: () => void;
17
+ removedGrant: (grantID: GrantID) => void;
13
18
  };
@@ -9,4 +9,6 @@ export { hookSQLiteStatements } from './hook-sqlite-statements.ts';
9
9
  export { openTestAuth } from './open-test-auth.ts';
10
10
  export { seedClient } from './seed-client.ts';
11
11
  export { seedEnrollmentToken } from './seed-enrollment-token.ts';
12
+ export { seedKeyBoundAccessTokens } from './seed-key-bound-access-tokens.ts';
12
13
  export { seedPasskey } from './seed-passkey.ts';
14
+ export { seedRemovedGrant } from './seed-removed-grant.ts';
@@ -0,0 +1,2 @@
1
+ import type { Database } from 'bun:sqlite';
2
+ export declare function seedKeyBoundAccessTokens(database: Database): void;
@@ -0,0 +1,3 @@
1
+ import type { Database } from 'bun:sqlite';
2
+ import type { GrantID } from '../types.ts';
3
+ export declare function seedRemovedGrant(database: Database, grantID: GrantID): void;
package/dist/types.d.ts CHANGED
@@ -150,7 +150,7 @@ export interface MCPAuth<Scope extends string, Limits = null> extends MCPAuthAdm
150
150
  readonly paths: MCPAuthPaths;
151
151
  readonly routes: MCPAuthRoutes;
152
152
  readonly verify: (bearer: string) => Promise<Access<Scope, Limits> | null>;
153
- readonly findClientScopes: (clientID: string) => Promise<readonly Scope[] | null>;
153
+ readonly readClientScopes: (clientID: string) => Promise<readonly Scope[] | null>;
154
154
  replaceServiceTokens(file: ServiceTokenFile<Scope>): void;
155
155
  }
156
156
  export interface ConsentRequest {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zgeoff/mcp-auth",
3
- "version": "0.3.0",
3
+ "version": "0.5.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",
@@ -45,8 +45,8 @@
45
45
  "@better-auth/mcp": "1.7.15",
46
46
  "@better-auth/oauth-provider": "1.7.15",
47
47
  "@simplewebauthn/server": "14.1.0",
48
- "@zgeoff/kysely-bun-sqlite": "0.3.3",
49
- "@zgeoff/std": "0.4.5",
48
+ "@zgeoff/kysely-bun-sqlite": "0.5.0",
49
+ "@zgeoff/std": "0.6.0",
50
50
  "better-auth": "1.7.15",
51
51
  "reflect-metadata": "0.2.2",
52
52
  "ts-pattern": "5.9.2",
@@ -94,8 +94,7 @@ export async function openMCPAuth<Scope extends string, Limits = null>(
94
94
  );
95
95
  },
96
96
 
97
- // oxlint-disable-next-line zgeoff/verb-contract -- the contract names this call findClientScopes
98
- findClientScopes: (clientID) => {
97
+ readClientScopes: (clientID) => {
99
98
  const service = services.state.findSnapshot()?.verifier.findScopes(clientID) ?? null;
100
99
 
101
100
  return service === null
@@ -3,7 +3,6 @@ import { mcp } from '@better-auth/mcp';
3
3
  import { oauthProvider } from '@better-auth/oauth-provider';
4
4
  import type { OAuthOptions, Scope as ProviderScope } from '@better-auth/oauth-provider';
5
5
  import { betterAuth } from 'better-auth';
6
- import type { BetterAuthPlugin } from 'better-auth';
7
6
  import type { Kysely } from 'kysely';
8
7
  import type { MCPAuthPaths } from '../types.ts';
9
8
  import { deriveTokenHash } from '../utils/derive-token-hash.ts';
@@ -86,11 +85,7 @@ export function createAuthServer(options: AuthServerOptions) {
86
85
  lastUsedAt: { type: 'date', required: false, input: false },
87
86
  },
88
87
  },
89
-
90
- // The provider's endpoint types do not satisfy the plugin type under
91
- // exactOptionalPropertyTypes.
92
- // oxlint-disable-next-line typescript/consistent-type-assertions -- better-auth plugin type under exactOptionalPropertyTypes
93
- plugins: [provider as BetterAuthPlugin, createGrantPlugin()],
88
+ plugins: [provider, createGrantPlugin()],
94
89
  });
95
90
  }
96
91
 
@@ -1,5 +1,7 @@
1
+ import type { Kysely } from 'kysely';
1
2
  import type { GrantID } from '../types.ts';
2
3
  import type { Store } from './open-store.ts';
4
+ import type { StoreSchema } from './store-schema.ts';
3
5
 
4
6
  // One delete ends a grant: every token and session of it references the row
5
7
  // with on delete cascade. The hook runs after the delete commits.
@@ -11,11 +13,7 @@ export async function removeGrantRows(
11
13
  return [];
12
14
  }
13
15
 
14
- const removed = await store.db
15
- .deleteFrom('user')
16
- .where('id', 'in', grantIDs)
17
- .returning('id')
18
- .execute();
16
+ const removed = await buildGrantRemoval(store.db, grantIDs).execute();
19
17
 
20
18
  const removedIDs = removed.map((row) => row.id);
21
19
 
@@ -25,3 +23,8 @@ export async function removeGrantRows(
25
23
 
26
24
  return removedIDs;
27
25
  }
26
+
27
+ // The delete itself, so a test can compile it and run it on another connection.
28
+ export function buildGrantRemoval(db: Kysely<StoreSchema>, grantIDs: readonly GrantID[]) {
29
+ return db.deleteFrom('user').where('id', 'in', grantIDs).returning('id');
30
+ }
@@ -1,11 +1,20 @@
1
- import type { MCPAuth } from '../types.ts';
1
+ import type { Database } from 'bun:sqlite';
2
+ import type { GrantID, MCPAuth } from '../types.ts';
2
3
  import { authorizeTestClient } from './authorize-test-client.ts';
3
4
  import { seedClient } from './seed-client.ts';
4
5
  import { seedEnrollmentToken } from './seed-enrollment-token.ts';
6
+ import { seedKeyBoundAccessTokens } from './seed-key-bound-access-tokens.ts';
5
7
  import { seedPasskey } from './seed-passkey.ts';
8
+ import { seedRemovedGrant } from './seed-removed-grant.ts';
6
9
 
7
10
  export interface TestDeps {
8
11
  readonly auth: MCPAuth<string, unknown>;
12
+
13
+ // The seeds that write rows directly write to it.
14
+ readonly database: Database;
15
+
16
+ // A second connection on the same file, for the seeds that stand in for another process.
17
+ readonly otherDatabase?: Database;
9
18
  }
10
19
 
11
20
  export function buildSeeder(deps: TestDeps) {
@@ -15,5 +24,11 @@ export function buildSeeder(deps: TestDeps) {
15
24
  passkey: (input: Parameters<typeof seedPasskey>[1]) => seedPasskey(deps.auth, input),
16
25
  grant: (input: Parameters<typeof authorizeTestClient>[1]) =>
17
26
  authorizeTestClient(deps.auth, input),
27
+ keyBoundAccessTokens: () => {
28
+ seedKeyBoundAccessTokens(deps.database);
29
+ },
30
+ removedGrant: (grantID: GrantID) => {
31
+ seedRemovedGrant(deps.otherDatabase ?? deps.database, grantID);
32
+ },
18
33
  };
19
34
  }
@@ -9,4 +9,6 @@ export { hookSQLiteStatements } from './hook-sqlite-statements.ts';
9
9
  export { openTestAuth } from './open-test-auth.ts';
10
10
  export { seedClient } from './seed-client.ts';
11
11
  export { seedEnrollmentToken } from './seed-enrollment-token.ts';
12
+ export { seedKeyBoundAccessTokens } from './seed-key-bound-access-tokens.ts';
12
13
  export { seedPasskey } from './seed-passkey.ts';
14
+ export { seedRemovedGrant } from './seed-removed-grant.ts';
@@ -0,0 +1,7 @@
1
+ import type { Database } from 'bun:sqlite';
2
+
3
+ // The token route drops DPoP, so no code of the core makes a key-bound token. This marks every
4
+ // stored access token as bound to a key.
5
+ export function seedKeyBoundAccessTokens(database: Database): void {
6
+ database.run('update oauthAccessToken set confirmation = ?', ['{"jkt":"thumbprint"}']);
7
+ }
@@ -0,0 +1,20 @@
1
+ import type { Database } from 'bun:sqlite';
2
+ import { buildSQLiteDialect } from '@zgeoff/kysely-bun-sqlite';
3
+ import { Kysely } from 'kysely';
4
+ import invariant from 'tiny-invariant';
5
+ import { buildGrantRemoval } from '../store/remove-grant-rows.ts';
6
+ import type { StoreSchema } from '../store/store-schema.ts';
7
+ import type { GrantID } from '../types.ts';
8
+
9
+ // Removes the grant row with the core's own delete, compiled and run synchronously, so a race test
10
+ // can call it from a statement hook on a second connection.
11
+ export function seedRemovedGrant(database: Database, grantID: GrantID): void {
12
+ const db = new Kysely<StoreSchema>({ dialect: buildSQLiteDialect(database) });
13
+
14
+ const removal = buildGrantRemoval(db, [grantID]).compile();
15
+ const bindings = removal.parameters.filter((parameter) => typeof parameter === 'string');
16
+
17
+ invariant(bindings.length === removal.parameters.length, 'the grant delete binds only grant IDs');
18
+
19
+ database.query(removal.sql).all(...bindings);
20
+ }
package/src/types.ts CHANGED
@@ -227,7 +227,7 @@ export interface MCPAuth<Scope extends string, Limits = null> extends MCPAuthAdm
227
227
  readonly verify: (bearer: string) => Promise<Access<Scope, Limits> | null>;
228
228
 
229
229
  // The scopes a client holds now, or null when it holds none.
230
- readonly findClientScopes: (clientID: string) => Promise<readonly Scope[] | null>;
230
+ readonly readClientScopes: (clientID: string) => Promise<readonly Scope[] | null>;
231
231
 
232
232
  // Swaps the service clients that the token route and `verify` read. Throws,
233
233
  // and keeps the old ones, when `serviceTokens` was not passed or the file