@zgeoff/mcp-auth 0.2.4 → 0.4.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 +3 -1
- package/dist/store/remove-grant-rows.d.ts +5 -0
- package/dist/test-utils/build-seeder.d.ts +6 -1
- package/dist/test-utils/index.d.ts +2 -0
- package/dist/test-utils/seed-key-bound-access-tokens.d.ts +2 -0
- package/dist/test-utils/seed-removed-grant.d.ts +3 -0
- package/dist/types.d.ts +3 -4
- package/package.json +3 -3
- package/src/open-mcp-auth.ts +1 -2
- package/src/store/create-auth-server.ts +9 -2
- package/src/store/remove-grant-rows.ts +8 -5
- package/src/test-utils/build-seeder.ts +16 -1
- package/src/test-utils/index.ts +2 -0
- package/src/test-utils/seed-key-bound-access-tokens.ts +7 -0
- package/src/test-utils/seed-removed-grant.ts +20 -0
- package/src/types.ts +3 -4
package/README.md
CHANGED
|
@@ -91,4 +91,6 @@ parse leaves the old one in place.
|
|
|
91
91
|
|
|
92
92
|
## Documentation
|
|
93
93
|
|
|
94
|
-
The [
|
|
94
|
+
The [architecture doc](../../docs/architecture/mcp-auth.md) explains how grants, tokens, service
|
|
95
|
+
clients and sign-ins work, and the decision records 0001 to 0003 in `docs/decisions/` give the
|
|
96
|
+
reasons for its main choices.
|
|
@@ -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 {
|
|
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';
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Database } from 'bun:sqlite';
|
|
2
|
+
import type { Clock as StdClock } from '@zgeoff/std';
|
|
2
3
|
import type { Kysely } from 'kysely';
|
|
3
4
|
import type { Tagged } from 'type-fest';
|
|
4
5
|
export type GrantID = Tagged<string, 'GrantID'>;
|
|
@@ -28,9 +29,7 @@ export interface MCPAuthTables {
|
|
|
28
29
|
readonly passkey: string;
|
|
29
30
|
readonly enrollmentToken: string;
|
|
30
31
|
}
|
|
31
|
-
export
|
|
32
|
-
readonly now: () => number;
|
|
33
|
-
}
|
|
32
|
+
export type Clock = Pick<StdClock, 'now'>;
|
|
34
33
|
export interface Requester {
|
|
35
34
|
readonly socketAddress: string | null;
|
|
36
35
|
readonly connectingIP: {
|
|
@@ -151,7 +150,7 @@ export interface MCPAuth<Scope extends string, Limits = null> extends MCPAuthAdm
|
|
|
151
150
|
readonly paths: MCPAuthPaths;
|
|
152
151
|
readonly routes: MCPAuthRoutes;
|
|
153
152
|
readonly verify: (bearer: string) => Promise<Access<Scope, Limits> | null>;
|
|
154
|
-
readonly
|
|
153
|
+
readonly readClientScopes: (clientID: string) => Promise<readonly Scope[] | null>;
|
|
155
154
|
replaceServiceTokens(file: ServiceTokenFile<Scope>): void;
|
|
156
155
|
}
|
|
157
156
|
export interface ConsentRequest {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zgeoff/mcp-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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.
|
|
49
|
-
"@zgeoff/std": "0.
|
|
48
|
+
"@zgeoff/kysely-bun-sqlite": "0.4.0",
|
|
49
|
+
"@zgeoff/std": "0.5.0",
|
|
50
50
|
"better-auth": "1.7.15",
|
|
51
51
|
"reflect-metadata": "0.2.2",
|
|
52
52
|
"ts-pattern": "5.9.2",
|
package/src/open-mcp-auth.ts
CHANGED
|
@@ -94,8 +94,7 @@ export async function openMCPAuth<Scope extends string, Limits = null>(
|
|
|
94
94
|
);
|
|
95
95
|
},
|
|
96
96
|
|
|
97
|
-
|
|
98
|
-
findClientScopes: (clientID) => {
|
|
97
|
+
readClientScopes: (clientID) => {
|
|
99
98
|
const service = services.state.findSnapshot()?.verifier.findScopes(clientID) ?? null;
|
|
100
99
|
|
|
101
100
|
return service === null
|
|
@@ -89,9 +89,16 @@ export function createAuthServer(options: AuthServerOptions) {
|
|
|
89
89
|
|
|
90
90
|
// The provider's endpoint types do not satisfy the plugin type under
|
|
91
91
|
// exactOptionalPropertyTypes.
|
|
92
|
-
|
|
93
|
-
plugins: [provider as BetterAuthPlugin, createGrantPlugin()],
|
|
92
|
+
plugins: [toBetterAuthPlugin(provider), createGrantPlugin()],
|
|
94
93
|
});
|
|
95
94
|
}
|
|
96
95
|
|
|
96
|
+
// The exported signature states the type that better-auth takes; the implementation passes the
|
|
97
|
+
// provider on as it is.
|
|
98
|
+
function toBetterAuthPlugin(plugin: ReturnType<typeof oauthProvider>): BetterAuthPlugin;
|
|
99
|
+
|
|
100
|
+
function toBetterAuthPlugin(plugin: unknown): unknown {
|
|
101
|
+
return plugin;
|
|
102
|
+
}
|
|
103
|
+
|
|
97
104
|
export type AuthServer = ReturnType<typeof createAuthServer>;
|
|
@@ -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 {
|
|
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
|
}
|
package/src/test-utils/index.ts
CHANGED
|
@@ -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
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Database } from 'bun:sqlite';
|
|
2
|
+
import type { Clock as StdClock } from '@zgeoff/std';
|
|
2
3
|
import type { Kysely } from 'kysely';
|
|
3
4
|
import type { Tagged } from 'type-fest';
|
|
4
5
|
|
|
@@ -44,9 +45,7 @@ export interface MCPAuthTables {
|
|
|
44
45
|
// The clock for the times the core stamps itself: sign-ins in progress,
|
|
45
46
|
// enrollments, lastUsedAt and removeExpired. better-auth stamps token, code
|
|
46
47
|
// and session expiries from Date.now().
|
|
47
|
-
export
|
|
48
|
-
readonly now: () => number;
|
|
49
|
-
}
|
|
48
|
+
export type Clock = Pick<StdClock, 'now'>;
|
|
50
49
|
|
|
51
50
|
export interface Requester {
|
|
52
51
|
readonly socketAddress: string | null;
|
|
@@ -228,7 +227,7 @@ export interface MCPAuth<Scope extends string, Limits = null> extends MCPAuthAdm
|
|
|
228
227
|
readonly verify: (bearer: string) => Promise<Access<Scope, Limits> | null>;
|
|
229
228
|
|
|
230
229
|
// The scopes a client holds now, or null when it holds none.
|
|
231
|
-
readonly
|
|
230
|
+
readonly readClientScopes: (clientID: string) => Promise<readonly Scope[] | null>;
|
|
232
231
|
|
|
233
232
|
// Swaps the service clients that the token route and `verify` read. Throws,
|
|
234
233
|
// and keeps the old ones, when `serviceTokens` was not passed or the file
|