@zgeoff/mcp-auth 0.0.0 → 0.1.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 +24 -3
- package/dist/elysia/create-mcp-http-app.d.ts +17 -0
- package/dist/elysia/find-bearer-token.d.ts +1 -0
- package/dist/elysia/index.d.ts +2 -0
- package/dist/elysia/is-beside-prefix.d.ts +1 -0
- package/package.json +12 -1
- package/src/elysia/create-mcp-http-app.ts +218 -0
- package/src/elysia/find-bearer-token.ts +6 -0
- package/src/elysia/index.ts +2 -0
- package/src/elysia/is-beside-prefix.ts +7 -0
package/README.md
CHANGED
|
@@ -5,8 +5,9 @@ route. It is built on better-auth and `bun:sqlite`, and holds the OAuth routes a
|
|
|
5
5
|
login for the owner, pre-registered clients, service tokens and grant revoke. Each app passes in its
|
|
6
6
|
own scopes, paths, table names and grant limits.
|
|
7
7
|
|
|
8
|
-
The package exports the core from `@zgeoff/mcp-auth
|
|
9
|
-
`@zgeoff/mcp-auth/testing`. The package needs
|
|
8
|
+
The package exports the core from `@zgeoff/mcp-auth`, an Elysia 2 adapter from
|
|
9
|
+
`@zgeoff/mcp-auth/elysia` and the test helpers from `@zgeoff/mcp-auth/testing`. The package needs
|
|
10
|
+
Bun.
|
|
10
11
|
|
|
11
12
|
## Install
|
|
12
13
|
|
|
@@ -14,7 +15,7 @@ The package exports the core from `@zgeoff/mcp-auth` and the test helpers from
|
|
|
14
15
|
bun add @zgeoff/mcp-auth kysely
|
|
15
16
|
```
|
|
16
17
|
|
|
17
|
-
Kysely is a peer dependency.
|
|
18
|
+
Kysely is a peer dependency. Elysia 2 is an optional peer dependency, needed only for the adapter.
|
|
18
19
|
|
|
19
20
|
## Usage
|
|
20
21
|
|
|
@@ -39,6 +40,25 @@ const auth = await openMCPAuth({
|
|
|
39
40
|
const access = await auth.verify(bearerToken);
|
|
40
41
|
```
|
|
41
42
|
|
|
43
|
+
`createMCPHTTPApp` from `@zgeoff/mcp-auth/elysia` serves the routes in an Elysia app. It refuses a
|
|
44
|
+
wrong Host or Origin header, and checks the bearer token before it hands a request to `mcp.fetch`.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { createMCPHTTPApp } from '@zgeoff/mcp-auth/elysia';
|
|
48
|
+
|
|
49
|
+
app.use(
|
|
50
|
+
createMCPHTTPApp({
|
|
51
|
+
auth,
|
|
52
|
+
hosts: new Set(['app.example.com']),
|
|
53
|
+
origins: new Set(['https://app.example.com']),
|
|
54
|
+
readRequester,
|
|
55
|
+
security: { record: (event) => console.log(event) },
|
|
56
|
+
mcp: { fetch: (request, access) => mcpHandler.fetch(request, access) },
|
|
57
|
+
besidePrefix: '/api',
|
|
58
|
+
}),
|
|
59
|
+
);
|
|
60
|
+
```
|
|
61
|
+
|
|
42
62
|
A command that only manages clients, grants and passkeys calls `openMCPAuthAdmin`, with no origin.
|
|
43
63
|
|
|
44
64
|
## Features
|
|
@@ -49,6 +69,7 @@ A command that only manages clients, grants and passkeys calls `openMCPAuthAdmin
|
|
|
49
69
|
- A revoke that ends every token of a grant with one delete, even in the middle of a refresh.
|
|
50
70
|
- Bearer tokens only: the token route drops DPoP, and `verify` refuses a token bound to a key.
|
|
51
71
|
- Service tokens for clients that cannot run the browser flow.
|
|
72
|
+
- An Elysia adapter that checks Host, Origin and the bearer token of each MCP request.
|
|
52
73
|
- Test helpers: `openTestAuth`, `authorizeTestClient`, and seeds for clients and passkeys.
|
|
53
74
|
|
|
54
75
|
## Documentation
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Elysia } from 'elysia';
|
|
2
|
+
import type { Access, MCPAuth, Requester, SecuritySink } from '../types.ts';
|
|
3
|
+
export interface MCPHTTPAppOptions<Scope extends string, Limits> {
|
|
4
|
+
readonly auth: MCPAuth<Scope, Limits>;
|
|
5
|
+
readonly hosts: ReadonlySet<string>;
|
|
6
|
+
readonly origins: ReadonlySet<string>;
|
|
7
|
+
readonly readRequester: (request: Request) => Requester;
|
|
8
|
+
readonly security: SecuritySink;
|
|
9
|
+
readonly mcp: {
|
|
10
|
+
readonly fetch: (request: Request, access: Access<Scope, Limits>) => Promise<Response>;
|
|
11
|
+
};
|
|
12
|
+
readonly besidePrefix: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function createMCPHTTPApp<Scope extends string, Limits>(options: MCPHTTPAppOptions<Scope, Limits>): Elysia<"", "local", import("elysia").DefaultSingleton, {
|
|
15
|
+
typebox: {};
|
|
16
|
+
error: [];
|
|
17
|
+
}, import("elysia").DefaultMetadata, {}, import("elysia").DefaultEphemeral, import("elysia").DefaultEphemeral>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function findBearerToken(authorization: string | null): string | null;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isBesidePrefix(path: string, prefix: string): boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zgeoff/mcp-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.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",
|
|
@@ -25,6 +25,10 @@
|
|
|
25
25
|
"types": "./dist/index.d.ts",
|
|
26
26
|
"default": "./src/index.ts"
|
|
27
27
|
},
|
|
28
|
+
"./elysia": {
|
|
29
|
+
"types": "./dist/elysia/index.d.ts",
|
|
30
|
+
"default": "./src/elysia/index.ts"
|
|
31
|
+
},
|
|
28
32
|
"./testing": {
|
|
29
33
|
"types": "./dist/test-utils/index.d.ts",
|
|
30
34
|
"default": "./src/test-utils/index.ts"
|
|
@@ -50,14 +54,21 @@
|
|
|
50
54
|
},
|
|
51
55
|
"devDependencies": {
|
|
52
56
|
"@types/bun": "1.4.4",
|
|
57
|
+
"elysia": "2.0.0-rc.3",
|
|
53
58
|
"kysely": "0.29.8",
|
|
54
59
|
"tiny-invariant": "1.3.3",
|
|
55
60
|
"tsrs": "0.18.0-ts7.1.0-dev.20260929",
|
|
56
61
|
"typescript": "7.0.2"
|
|
57
62
|
},
|
|
58
63
|
"peerDependencies": {
|
|
64
|
+
"elysia": ">=2.0.0-rc.3 <3.0.0",
|
|
59
65
|
"kysely": ">=0.29.5 <0.30.0"
|
|
60
66
|
},
|
|
67
|
+
"peerDependenciesMeta": {
|
|
68
|
+
"elysia": {
|
|
69
|
+
"optional": true
|
|
70
|
+
}
|
|
71
|
+
},
|
|
61
72
|
"engines": {
|
|
62
73
|
"bun": ">=1.3"
|
|
63
74
|
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { Elysia } from 'elysia';
|
|
2
|
+
import type { Access, MCPAuth, MCPAuthRouteName, Requester, SecuritySink } from '../types.ts';
|
|
3
|
+
import { findBearerToken } from './find-bearer-token.ts';
|
|
4
|
+
import { isBesidePrefix } from './is-beside-prefix.ts';
|
|
5
|
+
|
|
6
|
+
export interface MCPHTTPAppOptions<Scope extends string, Limits> {
|
|
7
|
+
readonly auth: MCPAuth<Scope, Limits>;
|
|
8
|
+
|
|
9
|
+
// The Host header values the app answers to.
|
|
10
|
+
readonly hosts: ReadonlySet<string>;
|
|
11
|
+
|
|
12
|
+
// The Origin header values a POST may carry.
|
|
13
|
+
readonly origins: ReadonlySet<string>;
|
|
14
|
+
readonly readRequester: (request: Request) => Requester;
|
|
15
|
+
readonly security: SecuritySink;
|
|
16
|
+
readonly mcp: {
|
|
17
|
+
readonly fetch: (request: Request, access: Access<Scope, Limits>) => Promise<Response>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// Paths under this prefix belong to the app. The adapter serves none of
|
|
21
|
+
// them, and checks neither their Host nor their Origin header.
|
|
22
|
+
readonly besidePrefix: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface RouteContext {
|
|
26
|
+
readonly request: Request;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type RouteMethod = 'get' | 'post';
|
|
30
|
+
|
|
31
|
+
interface RouteSpec {
|
|
32
|
+
readonly name: MCPAuthRouteName;
|
|
33
|
+
readonly path: (auth: MCPAuth<string, unknown>) => string;
|
|
34
|
+
readonly methods: readonly RouteMethod[];
|
|
35
|
+
|
|
36
|
+
// A form page refuses a POST that carries no Origin header.
|
|
37
|
+
readonly isPage: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const ROUTES: readonly RouteSpec[] = [
|
|
41
|
+
{
|
|
42
|
+
name: 'authorize',
|
|
43
|
+
path: (auth) => auth.paths.browser.authorize,
|
|
44
|
+
methods: ['get'],
|
|
45
|
+
isPage: false,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: 'login',
|
|
49
|
+
path: (auth) => auth.paths.browser.login,
|
|
50
|
+
methods: ['get', 'post'],
|
|
51
|
+
isPage: true,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: 'enroll',
|
|
55
|
+
path: (auth) => auth.paths.browser.enroll,
|
|
56
|
+
methods: ['get', 'post'],
|
|
57
|
+
isPage: true,
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
name: 'consent',
|
|
61
|
+
path: (auth) => auth.paths.browser.consent,
|
|
62
|
+
methods: ['get', 'post'],
|
|
63
|
+
isPage: true,
|
|
64
|
+
},
|
|
65
|
+
{ name: 'error', path: (auth) => auth.paths.browser.error, methods: ['get'], isPage: false },
|
|
66
|
+
{ name: 'token', path: (auth) => auth.paths.machine.token, methods: ['post'], isPage: false },
|
|
67
|
+
{ name: 'revoke', path: (auth) => auth.paths.machine.revoke, methods: ['post'], isPage: false },
|
|
68
|
+
{
|
|
69
|
+
name: 'authorizationServerMetadata',
|
|
70
|
+
path: (auth) => auth.paths.machine.authorizationServerMetadata,
|
|
71
|
+
methods: ['get'],
|
|
72
|
+
isPage: false,
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: 'resourceMetadata',
|
|
76
|
+
path: (auth) => auth.paths.machine.resourceMetadata,
|
|
77
|
+
methods: ['get'],
|
|
78
|
+
isPage: false,
|
|
79
|
+
},
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
export function createMCPHTTPApp<Scope extends string, Limits>(
|
|
83
|
+
options: MCPHTTPAppOptions<Scope, Limits>,
|
|
84
|
+
) {
|
|
85
|
+
const auth = options.auth;
|
|
86
|
+
|
|
87
|
+
const app = new Elysia();
|
|
88
|
+
|
|
89
|
+
const recordRefusal = (
|
|
90
|
+
request: Request,
|
|
91
|
+
kind: 'bad_host' | 'bad_origin',
|
|
92
|
+
reason: string,
|
|
93
|
+
): void => {
|
|
94
|
+
options.security.record({
|
|
95
|
+
kind,
|
|
96
|
+
outcome: 'rejected',
|
|
97
|
+
reason,
|
|
98
|
+
path: new URL(request.url).pathname,
|
|
99
|
+
requester: options.readRequester(request),
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const isForeignOrigin = (request: Request, isPage: boolean): boolean => {
|
|
104
|
+
if (request.method !== 'POST') {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const origin = request.headers.get('origin');
|
|
109
|
+
|
|
110
|
+
return origin === null ? isPage : !options.origins.has(origin);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const guard = (request: Request, isPage: boolean): Response | null => {
|
|
114
|
+
if (isForeignOrigin(request, isPage)) {
|
|
115
|
+
recordRefusal(request, 'bad_origin', 'origin_not_allowed');
|
|
116
|
+
|
|
117
|
+
return new Response(null, { status: 403 });
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return null;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
app.onRequest((context: RouteContext): Response | undefined => {
|
|
124
|
+
const path = new URL(context.request.url).pathname;
|
|
125
|
+
|
|
126
|
+
const host = context.request.headers.get('host');
|
|
127
|
+
|
|
128
|
+
const isRefused =
|
|
129
|
+
!isBesidePrefix(path, options.besidePrefix) && (host === null || !options.hosts.has(host));
|
|
130
|
+
|
|
131
|
+
if (!isRefused) {
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
recordRefusal(context.request, 'bad_host', 'host_not_allowed');
|
|
136
|
+
|
|
137
|
+
return new Response(null, { status: 403 });
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
for (const spec of ROUTES) {
|
|
141
|
+
const path = spec.path(auth);
|
|
142
|
+
|
|
143
|
+
if (isBesidePrefix(path, options.besidePrefix)) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const answer = (context: RouteContext): Response | Promise<Response> =>
|
|
148
|
+
guard(context.request, spec.isPage) ??
|
|
149
|
+
auth.routes[spec.name](context.request, options.readRequester(context.request));
|
|
150
|
+
|
|
151
|
+
if (spec.methods.includes('get')) {
|
|
152
|
+
app.get(path, answer);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (spec.methods.includes('post')) {
|
|
156
|
+
app.post(path, answer);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const metadataURL = `${auth.issuer}${auth.paths.machine.mcpResourceMetadata}`;
|
|
161
|
+
|
|
162
|
+
const answerMCP = async (request: Request): Promise<Response> => {
|
|
163
|
+
const refused = guard(request, false);
|
|
164
|
+
|
|
165
|
+
if (refused !== null) {
|
|
166
|
+
return refused;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const authorization = request.headers.get('authorization');
|
|
170
|
+
const token = findBearerToken(authorization);
|
|
171
|
+
|
|
172
|
+
if (token === null) {
|
|
173
|
+
const reason = authorization === null ? 'missing' : 'malformed';
|
|
174
|
+
|
|
175
|
+
recordBearerRefusal(request, reason);
|
|
176
|
+
|
|
177
|
+
return buildChallenge(`Bearer resource_metadata="${metadataURL}"`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const access = await auth.verify(token);
|
|
181
|
+
|
|
182
|
+
if (access === null) {
|
|
183
|
+
recordBearerRefusal(request, 'invalid_token');
|
|
184
|
+
|
|
185
|
+
return buildChallenge(`Bearer error="invalid_token", resource_metadata="${metadataURL}"`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return options.mcp.fetch(request, access);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const recordBearerRefusal = (request: Request, reason: string): void => {
|
|
192
|
+
options.security.record({
|
|
193
|
+
kind: 'bearer',
|
|
194
|
+
outcome: 'rejected',
|
|
195
|
+
reason,
|
|
196
|
+
path: new URL(request.url).pathname,
|
|
197
|
+
requester: options.readRequester(request),
|
|
198
|
+
});
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
for (const path of [auth.paths.machine.mcp, auth.paths.machine.root]) {
|
|
202
|
+
if (!isBesidePrefix(path, options.besidePrefix)) {
|
|
203
|
+
app.all(path, (context: RouteContext) => answerMCP(context.request));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (!isBesidePrefix(auth.paths.machine.mcpResourceMetadata, options.besidePrefix)) {
|
|
208
|
+
app.get(auth.paths.machine.mcpResourceMetadata, (context: RouteContext) =>
|
|
209
|
+
auth.routes.resourceMetadata(context.request, options.readRequester(context.request)),
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return app;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function buildChallenge(challenge: string): Response {
|
|
217
|
+
return new Response(null, { status: 401, headers: { 'www-authenticate': challenge } });
|
|
218
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
const BEARER_PATTERN = /^Bearer (?<token>\S+)$/iu;
|
|
2
|
+
|
|
3
|
+
// Null when the header is missing or holds no bearer token.
|
|
4
|
+
export function findBearerToken(authorization: string | null): string | null {
|
|
5
|
+
return BEARER_PATTERN.exec(authorization ?? '')?.groups?.['token'] ?? null;
|
|
6
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// A path is beside the prefix when it is the prefix or lies under it, so
|
|
2
|
+
// `/api` covers `/api` and `/api/x` but not `/apiary`.
|
|
3
|
+
export function isBesidePrefix(path: string, prefix: string): boolean {
|
|
4
|
+
const base = prefix.endsWith('/') ? prefix.slice(0, -1) : prefix;
|
|
5
|
+
|
|
6
|
+
return path === base || path.startsWith(`${base}/`);
|
|
7
|
+
}
|