@casys/mcp-server 0.26.1 → 0.27.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 +1779 -0
- package/LICENSE +21 -0
- package/README.md +15 -4
- package/esm/_dnt.polyfills.d.ts +12 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -0
- package/esm/_dnt.polyfills.js +15 -0
- package/esm/_dnt.shims.d.ts +2 -0
- package/esm/_dnt.shims.d.ts.map +1 -0
- package/esm/_dnt.shims.js +57 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_chars.d.ts +33 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_chars.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_chars.js +48 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_dumper_state.d.ts +106 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_dumper_state.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_dumper_state.js +636 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_loader_state.d.ts +67 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_loader_state.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_loader_state.js +1393 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_schema.d.ts +44 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_schema.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_schema.js +117 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/binary.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/binary.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/binary.js +103 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/bool.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/bool.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/bool.js +32 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/float.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/float.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/float.js +96 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/int.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/int.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/int.js +159 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/map.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/map.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/map.js +14 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/merge.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/merge.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/merge.js +10 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/nil.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/nil.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/nil.js +22 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/omap.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/omap.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/omap.js +29 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/pairs.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/pairs.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/pairs.js +19 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/regexp.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/regexp.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/regexp.js +30 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/seq.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/seq.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/seq.js +10 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/set.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/set.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/set.js +14 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/str.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/str.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/str.js +9 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/timestamp.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/timestamp.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/timestamp.js +81 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/undefined.d.ts +3 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/undefined.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/undefined.js +20 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type.d.ts +32 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_type.js +6 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_utils.d.ts +4 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_utils.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/_utils.js +13 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/mod.d.ts +51 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/mod.js +53 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/parse.d.ts +79 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/parse.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/parse.js +91 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/stringify.d.ts +100 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/stringify.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/stringify.js +33 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/types.d.ts +111 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/types.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/yaml/1.2.0/types.js +104 -0
- package/esm/mod.d.ts +102 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +103 -0
- package/esm/package.json +3 -0
- package/esm/src/auth/as-metadata-proxy.d.ts +112 -0
- package/esm/src/auth/as-metadata-proxy.d.ts.map +1 -0
- package/esm/src/auth/as-metadata-proxy.js +171 -0
- package/esm/src/auth/config.d.ts +117 -0
- package/esm/src/auth/config.d.ts.map +1 -0
- package/esm/src/auth/config.js +219 -0
- package/esm/src/auth/jwt-provider.d.ts +157 -0
- package/esm/src/auth/jwt-provider.d.ts.map +1 -0
- package/esm/src/auth/jwt-provider.js +187 -0
- package/esm/src/auth/middleware.d.ts +61 -0
- package/esm/src/auth/middleware.d.ts.map +1 -0
- package/esm/src/auth/middleware.js +209 -0
- package/esm/src/auth/mod.d.ts +23 -0
- package/esm/src/auth/mod.d.ts.map +1 -0
- package/esm/src/auth/mod.js +27 -0
- package/esm/src/auth/multi-tenant-middleware.d.ts +131 -0
- package/esm/src/auth/multi-tenant-middleware.d.ts.map +1 -0
- package/esm/src/auth/multi-tenant-middleware.js +128 -0
- package/esm/src/auth/presets.d.ts +231 -0
- package/esm/src/auth/presets.d.ts.map +1 -0
- package/esm/src/auth/presets.js +223 -0
- package/{src/auth/provider.ts → esm/src/auth/provider.d.ts} +13 -15
- package/esm/src/auth/provider.d.ts.map +1 -0
- package/esm/src/auth/provider.js +34 -0
- package/esm/src/auth/scope-middleware.d.ts +30 -0
- package/esm/src/auth/scope-middleware.d.ts.map +1 -0
- package/esm/src/auth/scope-middleware.js +48 -0
- package/esm/src/auth/static-token-provider.d.ts +155 -0
- package/esm/src/auth/static-token-provider.d.ts.map +1 -0
- package/esm/src/auth/static-token-provider.js +256 -0
- package/esm/src/auth/types.d.ts +151 -0
- package/esm/src/auth/types.d.ts.map +1 -0
- package/esm/src/auth/types.js +64 -0
- package/esm/src/client-auth/callback-server.d.ts +34 -0
- package/esm/src/client-auth/callback-server.d.ts.map +1 -0
- package/esm/src/client-auth/callback-server.js +141 -0
- package/esm/src/client-auth/client-id-metadata.d.ts +29 -0
- package/esm/src/client-auth/client-id-metadata.d.ts.map +1 -0
- package/esm/src/client-auth/client-id-metadata.js +149 -0
- package/esm/src/client-auth/connect.d.ts +32 -0
- package/esm/src/client-auth/connect.d.ts.map +1 -0
- package/esm/src/client-auth/connect.js +47 -0
- package/esm/src/client-auth/mod.d.ts +19 -0
- package/esm/src/client-auth/mod.d.ts.map +1 -0
- package/{src/client-auth/mod.ts → esm/src/client-auth/mod.js} +1 -26
- package/esm/src/client-auth/provider.d.ts +78 -0
- package/esm/src/client-auth/provider.d.ts.map +1 -0
- package/esm/src/client-auth/provider.js +246 -0
- package/esm/src/client-auth/token-store/file-store.d.ts +20 -0
- package/esm/src/client-auth/token-store/file-store.d.ts.map +1 -0
- package/esm/src/client-auth/token-store/file-store.js +62 -0
- package/esm/src/client-auth/token-store/memory-store.d.ts +14 -0
- package/esm/src/client-auth/token-store/memory-store.d.ts.map +1 -0
- package/esm/src/client-auth/token-store/memory-store.js +25 -0
- package/esm/src/client-auth/types.d.ts +89 -0
- package/esm/src/client-auth/types.d.ts.map +1 -0
- package/esm/src/client-auth/types.js +10 -0
- package/esm/src/concurrency/rate-limiter.d.ts +87 -0
- package/esm/src/concurrency/rate-limiter.d.ts.map +1 -0
- package/esm/src/concurrency/rate-limiter.js +165 -0
- package/esm/src/concurrency/request-queue.d.ts +71 -0
- package/esm/src/concurrency/request-queue.d.ts.map +1 -0
- package/esm/src/concurrency/request-queue.js +128 -0
- package/esm/src/http/body.d.ts +20 -0
- package/esm/src/http/body.d.ts.map +1 -0
- package/esm/src/http/body.js +63 -0
- package/esm/src/http/request-guards.d.ts +54 -0
- package/esm/src/http/request-guards.d.ts.map +1 -0
- package/esm/src/http/request-guards.js +114 -0
- package/esm/src/http/wire.d.ts +26 -0
- package/esm/src/http/wire.d.ts.map +1 -0
- package/{src/http/wire.ts → esm/src/http/wire.js} +13 -19
- package/esm/src/inspector/launcher.d.ts +48 -0
- package/esm/src/inspector/launcher.d.ts.map +1 -0
- package/esm/src/inspector/launcher.js +100 -0
- package/esm/src/mcp-app.d.ts +728 -0
- package/esm/src/mcp-app.d.ts.map +1 -0
- package/esm/src/mcp-app.js +3530 -0
- package/{src/middleware/backpressure.ts → esm/src/middleware/backpressure.d.ts} +2 -14
- package/esm/src/middleware/backpressure.d.ts.map +1 -0
- package/esm/src/middleware/backpressure.js +31 -0
- package/esm/src/middleware/mod.d.ts +11 -0
- package/esm/src/middleware/mod.d.ts.map +1 -0
- package/{src/middleware/mod.ts → esm/src/middleware/mod.js} +0 -10
- package/{src/middleware/rate-limit.ts → esm/src/middleware/rate-limit.d.ts} +2 -25
- package/esm/src/middleware/rate-limit.d.ts.map +1 -0
- package/esm/src/middleware/rate-limit.js +33 -0
- package/esm/src/middleware/runner.d.ts +32 -0
- package/esm/src/middleware/runner.d.ts.map +1 -0
- package/esm/src/middleware/runner.js +49 -0
- package/esm/src/middleware/types.d.ts +76 -0
- package/esm/src/middleware/types.d.ts.map +1 -0
- package/esm/src/middleware/types.js +9 -0
- package/esm/src/middleware/validation.d.ts +24 -0
- package/esm/src/middleware/validation.d.ts.map +1 -0
- package/esm/src/middleware/validation.js +30 -0
- package/esm/src/mrtr/capability-check.d.ts +75 -0
- package/esm/src/mrtr/capability-check.d.ts.map +1 -0
- package/esm/src/mrtr/capability-check.js +233 -0
- package/esm/src/mrtr/mod.d.ts +18 -0
- package/esm/src/mrtr/mod.d.ts.map +1 -0
- package/esm/src/mrtr/mod.js +12 -0
- package/esm/src/mrtr/replay-store.d.ts +50 -0
- package/esm/src/mrtr/replay-store.d.ts.map +1 -0
- package/esm/src/mrtr/replay-store.js +61 -0
- package/esm/src/mrtr/request-state.d.ts +181 -0
- package/esm/src/mrtr/request-state.d.ts.map +1 -0
- package/esm/src/mrtr/request-state.js +313 -0
- package/esm/src/mrtr/retry-guard.d.ts +61 -0
- package/esm/src/mrtr/retry-guard.d.ts.map +1 -0
- package/esm/src/mrtr/retry-guard.js +53 -0
- package/esm/src/mrtr/types.d.ts +99 -0
- package/esm/src/mrtr/types.d.ts.map +1 -0
- package/esm/src/mrtr/types.js +14 -0
- package/esm/src/observability/metrics.d.ts +104 -0
- package/esm/src/observability/metrics.d.ts.map +1 -0
- package/esm/src/observability/metrics.js +207 -0
- package/{src/observability/mod.ts → esm/src/observability/mod.d.ts} +2 -11
- package/esm/src/observability/mod.d.ts.map +1 -0
- package/esm/src/observability/mod.js +11 -0
- package/esm/src/observability/otel.d.ts +73 -0
- package/esm/src/observability/otel.d.ts.map +1 -0
- package/esm/src/observability/otel.js +260 -0
- package/esm/src/runtime/runtime.d.ts +10 -0
- package/esm/src/runtime/runtime.d.ts.map +1 -0
- package/esm/src/runtime/runtime.deno.d.ts +50 -0
- package/esm/src/runtime/runtime.deno.d.ts.map +1 -0
- package/esm/src/runtime/runtime.deno.js +120 -0
- package/{src/runtime/runtime.ts → esm/src/runtime/runtime.js} +5 -14
- package/esm/src/runtime/runtime.node.d.ts +51 -0
- package/esm/src/runtime/runtime.node.d.ts.map +1 -0
- package/esm/src/runtime/runtime.node.js +268 -0
- package/{src/runtime/types.ts → esm/src/runtime/types.d.ts} +28 -51
- package/esm/src/runtime/types.d.ts.map +1 -0
- package/esm/src/runtime/types.js +12 -0
- package/esm/src/security/channel-hmac.d.ts +43 -0
- package/esm/src/security/channel-hmac.d.ts.map +1 -0
- package/{src/security/channel-hmac.ts → esm/src/security/channel-hmac.js} +12 -17
- package/esm/src/security/csp.d.ts +50 -0
- package/esm/src/security/csp.d.ts.map +1 -0
- package/esm/src/security/csp.js +63 -0
- package/esm/src/security/message-signer.d.ts +88 -0
- package/esm/src/security/message-signer.d.ts.map +1 -0
- package/esm/src/security/message-signer.js +166 -0
- package/esm/src/subscriptions/subscription-registry.d.ts +256 -0
- package/esm/src/subscriptions/subscription-registry.d.ts.map +1 -0
- package/esm/src/subscriptions/subscription-registry.js +406 -0
- package/{src/tasks/mod.ts → esm/src/tasks/mod.d.ts} +3 -23
- package/esm/src/tasks/mod.d.ts.map +1 -0
- package/esm/src/tasks/mod.js +11 -0
- package/esm/src/tasks/task-store.d.ts +342 -0
- package/esm/src/tasks/task-store.d.ts.map +1 -0
- package/esm/src/tasks/task-store.js +649 -0
- package/esm/src/transport/request-headers.d.ts +145 -0
- package/esm/src/transport/request-headers.d.ts.map +1 -0
- package/esm/src/transport/request-headers.js +525 -0
- package/esm/src/types.d.ts +776 -0
- package/esm/src/types.d.ts.map +1 -0
- package/esm/src/types.js +92 -0
- package/esm/src/ui/viewer-utils.d.ts +45 -0
- package/esm/src/ui/viewer-utils.d.ts.map +1 -0
- package/esm/src/ui/viewer-utils.js +97 -0
- package/esm/src/validation/schema-validator.d.ts +115 -0
- package/esm/src/validation/schema-validator.d.ts.map +1 -0
- package/esm/src/validation/schema-validator.js +249 -0
- package/package.json +35 -24
- package/mod.ts +0 -302
- package/src/auth/as-metadata-proxy.ts +0 -288
- package/src/auth/config.ts +0 -368
- package/src/auth/jwt-provider.ts +0 -327
- package/src/auth/middleware.ts +0 -257
- package/src/auth/mod.ts +0 -89
- package/src/auth/multi-tenant-middleware.ts +0 -236
- package/src/auth/presets.ts +0 -351
- package/src/auth/scope-middleware.ts +0 -59
- package/src/auth/static-token-provider.ts +0 -197
- package/src/auth/types.ts +0 -207
- package/src/client-auth/callback-server.ts +0 -166
- package/src/client-auth/client-id-metadata.ts +0 -260
- package/src/client-auth/connect.ts +0 -72
- package/src/client-auth/provider.ts +0 -300
- package/src/client-auth/token-store/file-store.ts +0 -73
- package/src/client-auth/token-store/memory-store.ts +0 -32
- package/src/client-auth/types.ts +0 -95
- package/src/concurrency/rate-limiter.ts +0 -190
- package/src/concurrency/request-queue.ts +0 -140
- package/src/http/body.ts +0 -72
- package/src/http/request-guards.ts +0 -137
- package/src/inspector/launcher.ts +0 -131
- package/src/mcp-app.ts +0 -4595
- package/src/middleware/runner.ts +0 -63
- package/src/middleware/types.ts +0 -104
- package/src/middleware/validation.ts +0 -28
- package/src/mrtr/capability-check.ts +0 -303
- package/src/mrtr/mod.ts +0 -54
- package/src/mrtr/replay-store.ts +0 -103
- package/src/mrtr/request-state.ts +0 -435
- package/src/mrtr/retry-guard.ts +0 -110
- package/src/mrtr/types.ts +0 -107
- package/src/observability/metrics.ts +0 -336
- package/src/observability/otel.ts +0 -330
- package/src/runtime/runtime.deno.ts +0 -149
- package/src/runtime/runtime.node.ts +0 -324
- package/src/security/csp.ts +0 -89
- package/src/security/message-signer.ts +0 -223
- package/src/subscriptions/subscription-registry.ts +0 -558
- package/src/tasks/task-store.ts +0 -1014
- package/src/transport/request-headers.ts +0 -661
- package/src/types.ts +0 -908
- package/src/ui/viewer-utils.ts +0 -117
- package/src/validation/schema-validator.ts +0 -307
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JWT Auth Provider using JWKS for token validation.
|
|
3
|
+
*
|
|
4
|
+
* Validates JWT tokens against a remote JWKS endpoint,
|
|
5
|
+
* checking issuer, audience, and expiration.
|
|
6
|
+
*
|
|
7
|
+
* @module lib/server/auth/jwt-provider
|
|
8
|
+
*/
|
|
9
|
+
import { AuthProvider } from "./provider.js";
|
|
10
|
+
import { type AuthInfo, type HttpsUrl, type ProtectedResourceMetadata } from "./types.js";
|
|
11
|
+
/**
|
|
12
|
+
* Fields shared by both branches of {@link JwtAuthProviderOptions}.
|
|
13
|
+
*/
|
|
14
|
+
interface JwtAuthProviderOptionsBase {
|
|
15
|
+
/** JWT issuer (iss claim) */
|
|
16
|
+
issuer: string;
|
|
17
|
+
/** JWT audience (aud claim) */
|
|
18
|
+
audience: string;
|
|
19
|
+
/**
|
|
20
|
+
* JWKS URI for signature validation. Defaults to
|
|
21
|
+
* `{issuer}/.well-known/jwks.json` when omitted.
|
|
22
|
+
*/
|
|
23
|
+
jwksUri?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Authorization servers that issue valid tokens. Each entry is a branded
|
|
26
|
+
* {@link HttpsUrl} — raw strings are rejected at compile time. Construct
|
|
27
|
+
* via {@link httpsUrl}.
|
|
28
|
+
*/
|
|
29
|
+
authorizationServers: HttpsUrl[];
|
|
30
|
+
/** Scopes supported by this server */
|
|
31
|
+
scopesSupported?: string[];
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Options where `resource` is a verified HTTP(S) URL. The metadata URL is
|
|
35
|
+
* auto-derived when `resourceMetadataUrl` is omitted, by applying RFC 9728
|
|
36
|
+
* § 3.1 insertion to `resource`.
|
|
37
|
+
*
|
|
38
|
+
* Tagged with `kind: "url"` so TypeScript narrowing is sound at the function
|
|
39
|
+
* body level (see 0.17.0 CHANGELOG — this fixes finding C1 from the 0.16.0
|
|
40
|
+
* review where narrowing on `resourceMetadataUrl === undefined` failed to
|
|
41
|
+
* propagate because `HttpsUrl extends string` collapsed the union).
|
|
42
|
+
*/
|
|
43
|
+
export interface JwtAuthProviderOptionsUrlResource extends JwtAuthProviderOptionsBase {
|
|
44
|
+
/** Discriminant tag — MUST be `"url"` for this branch. */
|
|
45
|
+
kind: "url";
|
|
46
|
+
/** RFC 9728 § 2 resource identifier, pre-validated as HTTP(S) URL. */
|
|
47
|
+
resource: HttpsUrl;
|
|
48
|
+
/**
|
|
49
|
+
* Metadata URL, optional — auto-derived from `resource` when omitted.
|
|
50
|
+
*/
|
|
51
|
+
resourceMetadataUrl?: HttpsUrl;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Options where `resource` is an opaque URI (per RFC 9728 § 2 — e.g., an
|
|
55
|
+
* OIDC project ID used as JWT audience). The metadata URL is MANDATORY
|
|
56
|
+
* because it cannot be derived from an opaque identifier.
|
|
57
|
+
*
|
|
58
|
+
* Tagged with `kind: "opaque"` to make the DU structurally disjoint (see
|
|
59
|
+
* finding C2 from the 0.16.0 review — without the tag, `UrlResource` was
|
|
60
|
+
* a structural subtype of `OpaqueResource` because `HttpsUrl extends
|
|
61
|
+
* string`).
|
|
62
|
+
*/
|
|
63
|
+
export interface JwtAuthProviderOptionsOpaqueResource extends JwtAuthProviderOptionsBase {
|
|
64
|
+
/** Discriminant tag — MUST be `"opaque"` for this branch. */
|
|
65
|
+
kind: "opaque";
|
|
66
|
+
/** RFC 9728 § 2 opaque resource identifier (NOT an URL). */
|
|
67
|
+
resource: string;
|
|
68
|
+
/**
|
|
69
|
+
* Metadata URL, REQUIRED — cannot be derived from opaque `resource`.
|
|
70
|
+
* Construct via {@link httpsUrl}.
|
|
71
|
+
*/
|
|
72
|
+
resourceMetadataUrl: HttpsUrl;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Configuration for JwtAuthProvider.
|
|
76
|
+
*
|
|
77
|
+
* 0.17.0: tagged discriminated union on `kind: "url" | "opaque"`. TypeScript
|
|
78
|
+
* narrowing is now structurally sound — `if (options.kind === "url")` truly
|
|
79
|
+
* narrows `options.resource` to `HttpsUrl` at the constructor body level.
|
|
80
|
+
* The tag also makes the branches strictly disjoint: a caller cannot
|
|
81
|
+
* accidentally satisfy `OpaqueResource` with an `HttpsUrl`-branded `resource`
|
|
82
|
+
* (as was possible in 0.16.x — finding C2 from the type-design review).
|
|
83
|
+
*
|
|
84
|
+
* - `kind: "url"` → {@link JwtAuthProviderOptionsUrlResource},
|
|
85
|
+
* `resource: HttpsUrl` (wrap with {@link httpsUrl}),
|
|
86
|
+
* `resourceMetadataUrl` optional (auto-derived).
|
|
87
|
+
* - `kind: "opaque"` → {@link JwtAuthProviderOptionsOpaqueResource},
|
|
88
|
+
* `resource: string` (raw opaque identifier),
|
|
89
|
+
* `resourceMetadataUrl: HttpsUrl` REQUIRED.
|
|
90
|
+
*
|
|
91
|
+
* The preset factories and the public `buildJwtAuthProvider` bridge accept
|
|
92
|
+
* raw string `resource` and set the `kind` tag automatically via detection
|
|
93
|
+
* — only callers that construct `JwtAuthProvider` directly (not through the
|
|
94
|
+
* bridge) need to supply `kind` themselves. Custom OIDC providers written
|
|
95
|
+
* on top of `buildJwtAuthProvider` are unaffected.
|
|
96
|
+
*
|
|
97
|
+
* BREAKING from 0.16.x: every direct `new JwtAuthProvider(...)` site must
|
|
98
|
+
* add `kind: "url"` or `kind: "opaque"`. Preset users are unaffected.
|
|
99
|
+
* See `postmortems/phase8-deployment.md` § 7 and `CHANGELOG.md` 0.15.0 /
|
|
100
|
+
* 0.15.1 / 0.16.0 for the bug-class history this refactor closes.
|
|
101
|
+
*/
|
|
102
|
+
export type JwtAuthProviderOptions = JwtAuthProviderOptionsUrlResource | JwtAuthProviderOptionsOpaqueResource;
|
|
103
|
+
/**
|
|
104
|
+
* JWT Auth Provider with JWKS validation.
|
|
105
|
+
*
|
|
106
|
+
* @example URL resource (auto-derives metadata URL)
|
|
107
|
+
* ```typescript
|
|
108
|
+
* import { httpsUrl, JwtAuthProvider } from "@casys/mcp-server";
|
|
109
|
+
*
|
|
110
|
+
* const provider = new JwtAuthProvider({
|
|
111
|
+
* kind: "url",
|
|
112
|
+
* issuer: "https://accounts.google.com",
|
|
113
|
+
* audience: "https://my-mcp.example.com",
|
|
114
|
+
* resource: httpsUrl("https://my-mcp.example.com"),
|
|
115
|
+
* authorizationServers: [httpsUrl("https://accounts.google.com")],
|
|
116
|
+
* });
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* @example Opaque resource (explicit metadata URL required)
|
|
120
|
+
* ```typescript
|
|
121
|
+
* import { httpsUrl, JwtAuthProvider } from "@casys/mcp-server";
|
|
122
|
+
*
|
|
123
|
+
* // RFC 9728 § 2 Option B: OIDC project ID as JWT audience
|
|
124
|
+
* const provider = new JwtAuthProvider({
|
|
125
|
+
* kind: "opaque",
|
|
126
|
+
* issuer: "https://my-tenant.zitadel.cloud",
|
|
127
|
+
* audience: "367545125829670172",
|
|
128
|
+
* resource: "367545125829670172",
|
|
129
|
+
* resourceMetadataUrl: httpsUrl(
|
|
130
|
+
* "https://my-mcp.example.com/.well-known/oauth-protected-resource",
|
|
131
|
+
* ),
|
|
132
|
+
* authorizationServers: [httpsUrl("https://my-tenant.zitadel.cloud")],
|
|
133
|
+
* });
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
export declare class JwtAuthProvider extends AuthProvider {
|
|
137
|
+
private jwks;
|
|
138
|
+
private options;
|
|
139
|
+
private readonly resourceMetadataUrl;
|
|
140
|
+
private tokenCache;
|
|
141
|
+
private static readonly MAX_CACHE_SIZE;
|
|
142
|
+
private static readonly DEFAULT_CACHE_TTL_MS;
|
|
143
|
+
constructor(options: JwtAuthProviderOptions);
|
|
144
|
+
verifyToken(token: string): Promise<AuthInfo | null>;
|
|
145
|
+
/**
|
|
146
|
+
* Hash a token for cache key (avoids storing raw tokens in memory)
|
|
147
|
+
*/
|
|
148
|
+
private hashToken;
|
|
149
|
+
getResourceMetadata(): ProtectedResourceMetadata;
|
|
150
|
+
/**
|
|
151
|
+
* Extract scopes from JWT payload.
|
|
152
|
+
* Supports: "scope" claim (space-separated string) and "scp" claim (array).
|
|
153
|
+
*/
|
|
154
|
+
private extractScopes;
|
|
155
|
+
}
|
|
156
|
+
export {};
|
|
157
|
+
//# sourceMappingURL=jwt-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwt-provider.d.ts","sourceRoot":"","sources":["../../../src/src/auth/jwt-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,QAAQ,EAEb,KAAK,yBAAyB,EAC/B,MAAM,YAAY,CAAC;AAOpB;;GAEG;AACH,UAAU,0BAA0B;IAClC,6BAA6B;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,+BAA+B;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;;OAIG;IACH,oBAAoB,EAAE,QAAQ,EAAE,CAAC;IACjC,sCAAsC;IACtC,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;CAC5B;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,iCACf,SAAQ,0BAA0B;IAClC,0DAA0D;IAC1D,IAAI,EAAE,KAAK,CAAC;IACZ,sEAAsE;IACtE,QAAQ,EAAE,QAAQ,CAAC;IACnB;;OAEG;IACH,mBAAmB,CAAC,EAAE,QAAQ,CAAC;CAChC;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,oCACf,SAAQ,0BAA0B;IAClC,6DAA6D;IAC7D,IAAI,EAAE,QAAQ,CAAC;IACf,4DAA4D;IAC5D,QAAQ,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,mBAAmB,EAAE,QAAQ,CAAC;CAC/B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,MAAM,sBAAsB,GAC9B,iCAAiC,GACjC,oCAAoC,CAAC;AAczC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,qBAAa,eAAgB,SAAQ,YAAY;IAC/C,OAAO,CAAC,IAAI,CAAwC;IACpD,OAAO,CAAC,OAAO,CAAyB;IACxC,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAW;IAI/C,OAAO,CAAC,UAAU,CAAiC;IACnD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAQ;IAC9C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,oBAAoB,CAAiB;gBAEjD,OAAO,EAAE,sBAAsB;IAuDrC,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;IA0D1D;;OAEG;YACW,SAAS;IAQvB,mBAAmB,IAAI,yBAAyB;IAUhD;;;OAGG;IACH,OAAO,CAAC,aAAa;CAStB"}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JWT Auth Provider using JWKS for token validation.
|
|
3
|
+
*
|
|
4
|
+
* Validates JWT tokens against a remote JWKS endpoint,
|
|
5
|
+
* checking issuer, audience, and expiration.
|
|
6
|
+
*
|
|
7
|
+
* @module lib/server/auth/jwt-provider
|
|
8
|
+
*/
|
|
9
|
+
import { createRemoteJWKSet, jwtVerify } from "jose";
|
|
10
|
+
import { AuthProvider } from "./provider.js";
|
|
11
|
+
import { httpsUrl, } from "./types.js";
|
|
12
|
+
import { isOtelEnabled, recordAuthEvent } from "../observability/otel.js";
|
|
13
|
+
/**
|
|
14
|
+
* JWT Auth Provider with JWKS validation.
|
|
15
|
+
*
|
|
16
|
+
* @example URL resource (auto-derives metadata URL)
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { httpsUrl, JwtAuthProvider } from "@casys/mcp-server";
|
|
19
|
+
*
|
|
20
|
+
* const provider = new JwtAuthProvider({
|
|
21
|
+
* kind: "url",
|
|
22
|
+
* issuer: "https://accounts.google.com",
|
|
23
|
+
* audience: "https://my-mcp.example.com",
|
|
24
|
+
* resource: httpsUrl("https://my-mcp.example.com"),
|
|
25
|
+
* authorizationServers: [httpsUrl("https://accounts.google.com")],
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example Opaque resource (explicit metadata URL required)
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { httpsUrl, JwtAuthProvider } from "@casys/mcp-server";
|
|
32
|
+
*
|
|
33
|
+
* // RFC 9728 § 2 Option B: OIDC project ID as JWT audience
|
|
34
|
+
* const provider = new JwtAuthProvider({
|
|
35
|
+
* kind: "opaque",
|
|
36
|
+
* issuer: "https://my-tenant.zitadel.cloud",
|
|
37
|
+
* audience: "367545125829670172",
|
|
38
|
+
* resource: "367545125829670172",
|
|
39
|
+
* resourceMetadataUrl: httpsUrl(
|
|
40
|
+
* "https://my-mcp.example.com/.well-known/oauth-protected-resource",
|
|
41
|
+
* ),
|
|
42
|
+
* authorizationServers: [httpsUrl("https://my-tenant.zitadel.cloud")],
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export class JwtAuthProvider extends AuthProvider {
|
|
47
|
+
jwks;
|
|
48
|
+
options;
|
|
49
|
+
resourceMetadataUrl;
|
|
50
|
+
// Token verification cache: hash(token) → AuthInfo with TTL
|
|
51
|
+
// Prevents redundant JWKS fetches (network round-trip per tool call)
|
|
52
|
+
tokenCache = new Map();
|
|
53
|
+
static MAX_CACHE_SIZE = 1000;
|
|
54
|
+
static DEFAULT_CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
|
|
55
|
+
constructor(options) {
|
|
56
|
+
super();
|
|
57
|
+
if (!options.issuer) {
|
|
58
|
+
throw new Error("[JwtAuthProvider] issuer is required");
|
|
59
|
+
}
|
|
60
|
+
if (!options.audience) {
|
|
61
|
+
throw new Error("[JwtAuthProvider] audience is required");
|
|
62
|
+
}
|
|
63
|
+
if (!options.resource) {
|
|
64
|
+
throw new Error("[JwtAuthProvider] resource is required");
|
|
65
|
+
}
|
|
66
|
+
if (!options.authorizationServers?.length) {
|
|
67
|
+
throw new Error("[JwtAuthProvider] at least one authorizationServer is required");
|
|
68
|
+
}
|
|
69
|
+
// 0.17.0: narrow on the `kind` discriminant tag. TypeScript structurally
|
|
70
|
+
// narrows the DU here — after `if (options.kind === "opaque")`, `options`
|
|
71
|
+
// is `JwtAuthProviderOptionsOpaqueResource` and `resourceMetadataUrl` is
|
|
72
|
+
// guaranteed non-undefined; after `else`, `options` is
|
|
73
|
+
// `JwtAuthProviderOptionsUrlResource` and `options.resource` is
|
|
74
|
+
// `HttpsUrl` (no widening, no runtime guess). This fixes finding C1 from
|
|
75
|
+
// the 0.16.0 review — the previous narrowing on `resourceMetadataUrl !==
|
|
76
|
+
// undefined` was unsound because `HttpsUrl extends string` collapsed the
|
|
77
|
+
// union back to `string`. Tagging makes the branches strictly disjoint.
|
|
78
|
+
//
|
|
79
|
+
// URL branch derivation follows RFC 9728 § 3.1: when `resource` has a
|
|
80
|
+
// path or query component, the well-known suffix is INSERTED between
|
|
81
|
+
// the host and the path, not appended after it:
|
|
82
|
+
//
|
|
83
|
+
// resource = https://api.example.com/v1/mcp
|
|
84
|
+
// metadata = https://api.example.com/.well-known/oauth-protected-resource/v1/mcp
|
|
85
|
+
//
|
|
86
|
+
// Fragments (`#...`) are intentionally dropped — they're client-side-only
|
|
87
|
+
// per RFC 3986 § 3.5 and never part of a server-side metadata endpoint.
|
|
88
|
+
if (options.kind === "opaque") {
|
|
89
|
+
this.resourceMetadataUrl = options.resourceMetadataUrl;
|
|
90
|
+
}
|
|
91
|
+
else if (options.resourceMetadataUrl !== undefined) {
|
|
92
|
+
this.resourceMetadataUrl = options.resourceMetadataUrl;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
const parsed = new URL(options.resource);
|
|
96
|
+
const pathPart = parsed.pathname === "/" ? "" : parsed.pathname;
|
|
97
|
+
this.resourceMetadataUrl = httpsUrl(`${parsed.origin}/.well-known/oauth-protected-resource${pathPart}${parsed.search}`);
|
|
98
|
+
}
|
|
99
|
+
this.options = options;
|
|
100
|
+
const jwksUri = options.jwksUri ??
|
|
101
|
+
`${options.issuer}/.well-known/jwks.json`;
|
|
102
|
+
this.jwks = createRemoteJWKSet(new URL(jwksUri));
|
|
103
|
+
}
|
|
104
|
+
async verifyToken(token) {
|
|
105
|
+
// Check cache first (avoids JWKS network round-trip)
|
|
106
|
+
const cacheKey = await this.hashToken(token);
|
|
107
|
+
const cached = this.tokenCache.get(cacheKey);
|
|
108
|
+
if (cached && Date.now() < cached.expiresAt) {
|
|
109
|
+
if (isOtelEnabled()) {
|
|
110
|
+
recordAuthEvent("cache_hit", {
|
|
111
|
+
subject: cached.authInfo.subject ?? "",
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return cached.authInfo;
|
|
115
|
+
}
|
|
116
|
+
// Evict expired entry if present
|
|
117
|
+
if (cached)
|
|
118
|
+
this.tokenCache.delete(cacheKey);
|
|
119
|
+
try {
|
|
120
|
+
const { payload } = await jwtVerify(token, this.jwks, {
|
|
121
|
+
issuer: this.options.issuer,
|
|
122
|
+
audience: this.options.audience,
|
|
123
|
+
});
|
|
124
|
+
const authInfo = {
|
|
125
|
+
subject: payload.sub ?? "unknown",
|
|
126
|
+
clientId: payload.azp ??
|
|
127
|
+
payload.client_id,
|
|
128
|
+
scopes: this.extractScopes(payload),
|
|
129
|
+
claims: payload,
|
|
130
|
+
expiresAt: payload.exp,
|
|
131
|
+
};
|
|
132
|
+
// Cache with TTL = min(token remaining lifetime, 5 minutes)
|
|
133
|
+
const tokenExpiresMs = payload.exp ? payload.exp * 1000 : Infinity;
|
|
134
|
+
const cacheTtl = Math.min(tokenExpiresMs - Date.now(), JwtAuthProvider.DEFAULT_CACHE_TTL_MS);
|
|
135
|
+
if (cacheTtl > 0) {
|
|
136
|
+
// Evict oldest entries if cache is full
|
|
137
|
+
if (this.tokenCache.size >= JwtAuthProvider.MAX_CACHE_SIZE) {
|
|
138
|
+
const oldestKey = this.tokenCache.keys().next().value;
|
|
139
|
+
if (oldestKey)
|
|
140
|
+
this.tokenCache.delete(oldestKey);
|
|
141
|
+
}
|
|
142
|
+
this.tokenCache.set(cacheKey, {
|
|
143
|
+
authInfo: Object.freeze(authInfo),
|
|
144
|
+
expiresAt: Date.now() + cacheTtl,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
return authInfo;
|
|
148
|
+
}
|
|
149
|
+
catch (err) {
|
|
150
|
+
// Log the actual rejection reason so JWKS fetch failures, audience
|
|
151
|
+
// mismatches, and expiration errors are distinguishable in logs.
|
|
152
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
153
|
+
console.error(`[JwtAuthProvider] Token rejected: ${msg}`);
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Hash a token for cache key (avoids storing raw tokens in memory)
|
|
159
|
+
*/
|
|
160
|
+
async hashToken(token) {
|
|
161
|
+
const data = new TextEncoder().encode(token);
|
|
162
|
+
const hash = await crypto.subtle.digest("SHA-256", data);
|
|
163
|
+
return Array.from(new Uint8Array(hash)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
164
|
+
}
|
|
165
|
+
getResourceMetadata() {
|
|
166
|
+
return {
|
|
167
|
+
resource: this.options.resource,
|
|
168
|
+
resource_metadata_url: this.resourceMetadataUrl,
|
|
169
|
+
authorization_servers: this.options.authorizationServers,
|
|
170
|
+
scopes_supported: this.options.scopesSupported,
|
|
171
|
+
bearer_methods_supported: ["header"],
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Extract scopes from JWT payload.
|
|
176
|
+
* Supports: "scope" claim (space-separated string) and "scp" claim (array).
|
|
177
|
+
*/
|
|
178
|
+
extractScopes(payload) {
|
|
179
|
+
if (typeof payload.scope === "string") {
|
|
180
|
+
return payload.scope.split(" ").filter(Boolean);
|
|
181
|
+
}
|
|
182
|
+
if (Array.isArray(payload.scp)) {
|
|
183
|
+
return payload.scp.filter((s) => typeof s === "string");
|
|
184
|
+
}
|
|
185
|
+
return [];
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authentication middleware and utilities.
|
|
3
|
+
*
|
|
4
|
+
* Provides Bearer token extraction, 401/403 response factories,
|
|
5
|
+
* and the auth middleware for the pipeline.
|
|
6
|
+
*
|
|
7
|
+
* @module lib/server/auth/middleware
|
|
8
|
+
*/
|
|
9
|
+
import type { AuthProvider } from "./provider.js";
|
|
10
|
+
import type { AuthInfo } from "./types.js";
|
|
11
|
+
import type { Middleware } from "../middleware/types.js";
|
|
12
|
+
/** 401 Unauthorized: no token, or an invalid one. */
|
|
13
|
+
export declare const JSONRPC_UNAUTHORIZED = -31401;
|
|
14
|
+
/** 403 Forbidden: authenticated, but missing a required scope. */
|
|
15
|
+
export declare const JSONRPC_FORBIDDEN = -31403;
|
|
16
|
+
/**
|
|
17
|
+
* Authentication error with structured information
|
|
18
|
+
* for generating proper HTTP error responses.
|
|
19
|
+
*/
|
|
20
|
+
export declare class AuthError extends Error {
|
|
21
|
+
readonly code: "missing_token" | "invalid_token" | "insufficient_scope";
|
|
22
|
+
readonly resourceMetadataUrl: string;
|
|
23
|
+
readonly requiredScopes?: string[] | undefined;
|
|
24
|
+
constructor(code: "missing_token" | "invalid_token" | "insufficient_scope", resourceMetadataUrl: string, requiredScopes?: string[] | undefined);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Extract Bearer token from Authorization header.
|
|
28
|
+
*
|
|
29
|
+
* @param request - HTTP Request
|
|
30
|
+
* @returns Token string or null if not present/invalid format
|
|
31
|
+
*/
|
|
32
|
+
export declare function extractBearerToken(request: Request): string | null;
|
|
33
|
+
/**
|
|
34
|
+
* Create a 401 Unauthorized response with WWW-Authenticate header.
|
|
35
|
+
*
|
|
36
|
+
* @param resourceMetadataUrl - URL to the Protected Resource Metadata endpoint
|
|
37
|
+
* @param error - OAuth error code
|
|
38
|
+
* @param errorDescription - Human-readable error description
|
|
39
|
+
*/
|
|
40
|
+
export declare function createUnauthorizedResponse(resourceMetadataUrl: string, error?: string, errorDescription?: string): Response;
|
|
41
|
+
/**
|
|
42
|
+
* Create a 403 Forbidden response for insufficient scopes.
|
|
43
|
+
*
|
|
44
|
+
* @param requiredScopes - Scopes that were required but missing
|
|
45
|
+
* @param resourceMetadataUrl - URL to the Protected Resource Metadata endpoint
|
|
46
|
+
*/
|
|
47
|
+
export declare function createForbiddenResponse(requiredScopes: string[], resourceMetadataUrl: string): Response;
|
|
48
|
+
export declare function freezeAuthInfo(authInfo: AuthInfo): AuthInfo;
|
|
49
|
+
/**
|
|
50
|
+
* Create an authentication middleware for the MCP pipeline.
|
|
51
|
+
*
|
|
52
|
+
* Extracts the Bearer token from the HTTP request, validates it
|
|
53
|
+
* via the AuthProvider, and injects `authInfo` into the context.
|
|
54
|
+
*
|
|
55
|
+
* For STDIO transport (no `ctx.request`), the middleware is skipped
|
|
56
|
+
* since STDIO is a local transport with no auth needed.
|
|
57
|
+
*
|
|
58
|
+
* @param provider - AuthProvider implementation
|
|
59
|
+
*/
|
|
60
|
+
export declare function createAuthMiddleware(provider: AuthProvider): Middleware;
|
|
61
|
+
//# sourceMappingURL=middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../../src/src/auth/middleware.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAuBzD,qDAAqD;AACrD,eAAO,MAAM,oBAAoB,SAAS,CAAC;AAE3C,kEAAkE;AAClE,eAAO,MAAM,iBAAiB,SAAS,CAAC;AAMxC;;;GAGG;AACH,qBAAa,SAAU,SAAQ,KAAK;aAEhB,IAAI,EAChB,eAAe,GACf,eAAe,GACf,oBAAoB;aACR,mBAAmB,EAAE,MAAM;aAC3B,cAAc,CAAC,EAAE,MAAM,EAAE;gBALzB,IAAI,EAChB,eAAe,GACf,eAAe,GACf,oBAAoB,EACR,mBAAmB,EAAE,MAAM,EAC3B,cAAc,CAAC,EAAE,MAAM,EAAE,YAAA;CAW5C;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAKlE;AAED;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CACxC,mBAAmB,EAAE,MAAM,EAC3B,KAAK,CAAC,EAAE,MAAM,EACd,gBAAgB,CAAC,EAAE,MAAM,GACxB,QAAQ,CA8BV;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,cAAc,EAAE,MAAM,EAAE,EACxB,mBAAmB,EAAE,MAAM,GAC1B,QAAQ,CA2BV;AAED,wBAAgB,cAAc,CAAC,QAAQ,EAAE,QAAQ,GAAG,QAAQ,CAgB3D;AAUD;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,YAAY,GAAG,UAAU,CAyDvE"}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authentication middleware and utilities.
|
|
3
|
+
*
|
|
4
|
+
* Provides Bearer token extraction, 401/403 response factories,
|
|
5
|
+
* and the auth middleware for the pipeline.
|
|
6
|
+
*
|
|
7
|
+
* @module lib/server/auth/middleware
|
|
8
|
+
*/
|
|
9
|
+
import { isOtelEnabled, recordAuthEvent } from "../observability/otel.js";
|
|
10
|
+
// ── Auth error codes ────────────────────────────────────────────────────────
|
|
11
|
+
//
|
|
12
|
+
// Both codes moved OUT of the JSON-RPC reserved range (`-32768`..`-32000`) for
|
|
13
|
+
// spec 2026-07-28, which is stricter than earlier readings of it suggested:
|
|
14
|
+
//
|
|
15
|
+
// * `-32002` — "Implementations of this protocol version **MUST NOT** emit
|
|
16
|
+
// these codes", because 2025-11-25 and earlier used it for resource-not-found
|
|
17
|
+
// (now `-32602`). Keeping it for 403 was a conformance violation, not a
|
|
18
|
+
// grandfathered liberty. Two prior reviews, including one of this file, read
|
|
19
|
+
// the allocation policy as permitting it. It does not.
|
|
20
|
+
// * `-32001` — inside `-32000`..`-32019`, where "new implementations
|
|
21
|
+
// **SHOULD NOT** use codes from this sub-range at all" and receivers "**MUST
|
|
22
|
+
// NOT** assume any specific meaning".
|
|
23
|
+
//
|
|
24
|
+
// The spec's own guidance for codes it does not define: "**SHOULD** be allocated
|
|
25
|
+
// outside the JSON-RPC reserved range". Hence `-31401` / `-31403`, mnemonic for
|
|
26
|
+
// the HTTP statuses they accompany.
|
|
27
|
+
//
|
|
28
|
+
// BREAKING for anything matching on the old numbers — see the changelog.
|
|
29
|
+
/** 401 Unauthorized: no token, or an invalid one. */
|
|
30
|
+
export const JSONRPC_UNAUTHORIZED = -31401;
|
|
31
|
+
/** 403 Forbidden: authenticated, but missing a required scope. */
|
|
32
|
+
export const JSONRPC_FORBIDDEN = -31403;
|
|
33
|
+
function escapeAuthenticateParam(s) {
|
|
34
|
+
return s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Authentication error with structured information
|
|
38
|
+
* for generating proper HTTP error responses.
|
|
39
|
+
*/
|
|
40
|
+
export class AuthError extends Error {
|
|
41
|
+
code;
|
|
42
|
+
resourceMetadataUrl;
|
|
43
|
+
requiredScopes;
|
|
44
|
+
constructor(code, resourceMetadataUrl, requiredScopes) {
|
|
45
|
+
super(code === "missing_token"
|
|
46
|
+
? "Authorization header with Bearer token required"
|
|
47
|
+
: code === "invalid_token"
|
|
48
|
+
? "Invalid or expired token"
|
|
49
|
+
: `Insufficient scope: requires ${requiredScopes?.join(", ")}`);
|
|
50
|
+
this.code = code;
|
|
51
|
+
this.resourceMetadataUrl = resourceMetadataUrl;
|
|
52
|
+
this.requiredScopes = requiredScopes;
|
|
53
|
+
this.name = "AuthError";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Extract Bearer token from Authorization header.
|
|
58
|
+
*
|
|
59
|
+
* @param request - HTTP Request
|
|
60
|
+
* @returns Token string or null if not present/invalid format
|
|
61
|
+
*/
|
|
62
|
+
export function extractBearerToken(request) {
|
|
63
|
+
const auth = request.headers.get("Authorization");
|
|
64
|
+
if (!auth?.startsWith("Bearer "))
|
|
65
|
+
return null;
|
|
66
|
+
const token = auth.slice(7).trim();
|
|
67
|
+
return token.length > 0 ? token : null;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Create a 401 Unauthorized response with WWW-Authenticate header.
|
|
71
|
+
*
|
|
72
|
+
* @param resourceMetadataUrl - URL to the Protected Resource Metadata endpoint
|
|
73
|
+
* @param error - OAuth error code
|
|
74
|
+
* @param errorDescription - Human-readable error description
|
|
75
|
+
*/
|
|
76
|
+
export function createUnauthorizedResponse(resourceMetadataUrl, error, errorDescription) {
|
|
77
|
+
const parts = [
|
|
78
|
+
`Bearer resource_metadata="${escapeAuthenticateParam(resourceMetadataUrl)}"`,
|
|
79
|
+
];
|
|
80
|
+
if (error)
|
|
81
|
+
parts.push(`error="${escapeAuthenticateParam(error)}"`);
|
|
82
|
+
if (errorDescription) {
|
|
83
|
+
parts.push(`error_description="${escapeAuthenticateParam(errorDescription)}"`);
|
|
84
|
+
}
|
|
85
|
+
return new Response(JSON.stringify({
|
|
86
|
+
jsonrpc: "2.0",
|
|
87
|
+
id: null,
|
|
88
|
+
error: {
|
|
89
|
+
code: JSONRPC_UNAUTHORIZED,
|
|
90
|
+
message: errorDescription ?? "Unauthorized",
|
|
91
|
+
},
|
|
92
|
+
}), {
|
|
93
|
+
status: 401,
|
|
94
|
+
headers: {
|
|
95
|
+
"Content-Type": "application/json",
|
|
96
|
+
"WWW-Authenticate": parts.join(", "),
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Create a 403 Forbidden response for insufficient scopes.
|
|
102
|
+
*
|
|
103
|
+
* @param requiredScopes - Scopes that were required but missing
|
|
104
|
+
* @param resourceMetadataUrl - URL to the Protected Resource Metadata endpoint
|
|
105
|
+
*/
|
|
106
|
+
export function createForbiddenResponse(requiredScopes, resourceMetadataUrl) {
|
|
107
|
+
const requiredScopesDescription = requiredScopes.join(", ");
|
|
108
|
+
const parts = [
|
|
109
|
+
`Bearer resource_metadata="${escapeAuthenticateParam(resourceMetadataUrl)}"`,
|
|
110
|
+
`error="insufficient_scope"`,
|
|
111
|
+
`error_description="${escapeAuthenticateParam(requiredScopesDescription)}"`,
|
|
112
|
+
];
|
|
113
|
+
return new Response(JSON.stringify({
|
|
114
|
+
jsonrpc: "2.0",
|
|
115
|
+
id: null,
|
|
116
|
+
error: {
|
|
117
|
+
code: JSONRPC_FORBIDDEN,
|
|
118
|
+
message: `Forbidden: requires scopes ${requiredScopes.join(", ")}`,
|
|
119
|
+
},
|
|
120
|
+
}), {
|
|
121
|
+
status: 403,
|
|
122
|
+
headers: {
|
|
123
|
+
"Content-Type": "application/json",
|
|
124
|
+
"WWW-Authenticate": parts.join(", "),
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
export function freezeAuthInfo(authInfo) {
|
|
129
|
+
const scopes = Object.freeze([...authInfo.scopes]);
|
|
130
|
+
const frozen = {
|
|
131
|
+
...authInfo,
|
|
132
|
+
scopes,
|
|
133
|
+
...(authInfo.claims
|
|
134
|
+
? {
|
|
135
|
+
claims: deepFreezeJson(structuredClone(authInfo.claims)),
|
|
136
|
+
}
|
|
137
|
+
: {}),
|
|
138
|
+
};
|
|
139
|
+
return Object.freeze(frozen);
|
|
140
|
+
}
|
|
141
|
+
function deepFreezeJson(value) {
|
|
142
|
+
if (typeof value !== "object" || value === null)
|
|
143
|
+
return value;
|
|
144
|
+
for (const nested of Object.values(value)) {
|
|
145
|
+
deepFreezeJson(nested);
|
|
146
|
+
}
|
|
147
|
+
return Object.freeze(value);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Create an authentication middleware for the MCP pipeline.
|
|
151
|
+
*
|
|
152
|
+
* Extracts the Bearer token from the HTTP request, validates it
|
|
153
|
+
* via the AuthProvider, and injects `authInfo` into the context.
|
|
154
|
+
*
|
|
155
|
+
* For STDIO transport (no `ctx.request`), the middleware is skipped
|
|
156
|
+
* since STDIO is a local transport with no auth needed.
|
|
157
|
+
*
|
|
158
|
+
* @param provider - AuthProvider implementation
|
|
159
|
+
*/
|
|
160
|
+
export function createAuthMiddleware(provider) {
|
|
161
|
+
return async (ctx, next) => {
|
|
162
|
+
// STDIO transport: no request, skip auth
|
|
163
|
+
if (!ctx.request) {
|
|
164
|
+
return next();
|
|
165
|
+
}
|
|
166
|
+
// 0.15.0+: provider's getResourceMetadata() always returns a valid
|
|
167
|
+
// absolute URL in resource_metadata_url (type system guarantees it).
|
|
168
|
+
// No derivation needed at the middleware level anymore.
|
|
169
|
+
const metadataUrl = provider.getResourceMetadata().resource_metadata_url;
|
|
170
|
+
// Already authenticated by the HTTP-level gate (verifyHttpAuth).
|
|
171
|
+
// Skip the redundant verifyToken call to avoid JWKS race conditions
|
|
172
|
+
// when concurrent requests arrive on a cold cache.
|
|
173
|
+
if (ctx.authInfo) {
|
|
174
|
+
ctx.resourceMetadataUrl = metadataUrl;
|
|
175
|
+
return next();
|
|
176
|
+
}
|
|
177
|
+
const token = extractBearerToken(ctx.request);
|
|
178
|
+
if (!token) {
|
|
179
|
+
if (isOtelEnabled()) {
|
|
180
|
+
recordAuthEvent("reject", {
|
|
181
|
+
reason: "missing_token",
|
|
182
|
+
tool: ctx.toolName ?? "",
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
throw new AuthError("missing_token", metadataUrl);
|
|
186
|
+
}
|
|
187
|
+
const authInfo = await provider.verifyToken(token);
|
|
188
|
+
if (!authInfo) {
|
|
189
|
+
if (isOtelEnabled()) {
|
|
190
|
+
recordAuthEvent("reject", {
|
|
191
|
+
reason: "invalid_token",
|
|
192
|
+
tool: ctx.toolName ?? "",
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
throw new AuthError("invalid_token", metadataUrl);
|
|
196
|
+
}
|
|
197
|
+
if (isOtelEnabled()) {
|
|
198
|
+
recordAuthEvent("verify", {
|
|
199
|
+
subject: authInfo.subject ?? "",
|
|
200
|
+
tool: ctx.toolName ?? "",
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
// Deep-freeze authInfo to prevent mutation by downstream middlewares
|
|
204
|
+
ctx.authInfo = freezeAuthInfo(authInfo);
|
|
205
|
+
// Propagate resourceMetadataUrl for downstream middlewares (scope-middleware)
|
|
206
|
+
ctx.resourceMetadataUrl = metadataUrl;
|
|
207
|
+
return next();
|
|
208
|
+
};
|
|
209
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auth module for McpApp.
|
|
3
|
+
*
|
|
4
|
+
* @module lib/server/auth
|
|
5
|
+
*/
|
|
6
|
+
export type { AuthInfo, AuthOptions, HttpsUrl, ProtectedResourceMetadata, } from "./types.js";
|
|
7
|
+
export { httpsUrl, tryHttpsUrl } from "./types.js";
|
|
8
|
+
export { AuthProvider } from "./provider.js";
|
|
9
|
+
export { AuthError, createAuthMiddleware, createForbiddenResponse, createUnauthorizedResponse, extractBearerToken, } from "./middleware.js";
|
|
10
|
+
export { createScopeMiddleware } from "./scope-middleware.js";
|
|
11
|
+
export { createMultiTenantMiddleware } from "./multi-tenant-middleware.js";
|
|
12
|
+
export type { MultiTenantMiddlewareOptions, TenantResolution, TenantResolver, } from "./multi-tenant-middleware.js";
|
|
13
|
+
export { JwtAuthProvider } from "./jwt-provider.js";
|
|
14
|
+
export type { JwtAuthProviderOptions, JwtAuthProviderOptionsOpaqueResource, JwtAuthProviderOptionsUrlResource, } from "./jwt-provider.js";
|
|
15
|
+
export { buildJwtAuthProvider, createAuth0AuthProvider, createGitHubAuthProvider, createGoogleAuthProvider, createOIDCAuthProvider, } from "./presets.js";
|
|
16
|
+
export type { BuildJwtAuthProviderOptions, OIDCPresetOptions, PresetOptions, } from "./presets.js";
|
|
17
|
+
export { createAsMetadataHandler } from "./as-metadata-proxy.js";
|
|
18
|
+
export type { AsMetadataProxyOptions } from "./as-metadata-proxy.js";
|
|
19
|
+
export { createAuthProviderFromConfig, loadAuthConfig } from "./config.js";
|
|
20
|
+
export type { Auth0AuthConfig, AuthConfig, AuthProviderName, GitHubAuthConfig, GoogleAuthConfig, OIDCAuthConfig, } from "./config.js";
|
|
21
|
+
export { createStaticTokenAuthProvider, StaticTokenAuthProvider, } from "./static-token-provider.js";
|
|
22
|
+
export type { StaticTokenAuthProviderOptions, StaticTokenCredential, } from "./static-token-provider.js";
|
|
23
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../../src/src/auth/mod.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,YAAY,EACV,QAAQ,EACR,WAAW,EACX,QAAQ,EACR,yBAAyB,GAC1B,MAAM,YAAY,CAAC;AAIpB,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGnD,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAG7C,OAAO,EACL,SAAS,EACT,oBAAoB,EACpB,uBAAuB,EACvB,0BAA0B,EAC1B,kBAAkB,GACnB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAG9D,OAAO,EAAE,2BAA2B,EAAE,MAAM,8BAA8B,CAAC;AAC3E,YAAY,EACV,4BAA4B,EAC5B,gBAAgB,EAChB,cAAc,GACf,MAAM,8BAA8B,CAAC;AAGtC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAIpD,YAAY,EACV,sBAAsB,EACtB,oCAAoC,EACpC,iCAAiC,GAClC,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EACL,oBAAoB,EACpB,uBAAuB,EACvB,wBAAwB,EACxB,wBAAwB,EACxB,sBAAsB,GACvB,MAAM,cAAc,CAAC;AACtB,YAAY,EACV,2BAA2B,EAC3B,iBAAiB,EACjB,aAAa,GACd,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACjE,YAAY,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAGrE,OAAO,EAAE,4BAA4B,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAG3E,YAAY,EACV,eAAe,EACf,UAAU,EACV,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,GACf,MAAM,aAAa,CAAC;AAGrB,OAAO,EACL,6BAA6B,EAC7B,uBAAuB,GACxB,MAAM,4BAA4B,CAAC;AACpC,YAAY,EACV,8BAA8B,EAC9B,qBAAqB,GACtB,MAAM,4BAA4B,CAAC"}
|