@pontive/server 0.0.0-stage → 1.23.1
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 +146 -2
- package/dist/auth.d.cts +34 -0
- package/dist/auth.d.ts +34 -0
- package/dist/authorization.cjs +2 -0
- package/dist/authorization.cjs.map +1 -0
- package/dist/authorization.d.cts +222 -0
- package/dist/authorization.d.ts +222 -0
- package/dist/authorization.js +153 -0
- package/dist/authorization.js.map +1 -0
- package/dist/browser-mode.d.cts +23 -0
- package/dist/browser-mode.d.ts +23 -0
- package/dist/config.d.cts +131 -0
- package/dist/config.d.ts +131 -0
- package/dist/cookies.d.cts +50 -0
- package/dist/cookies.d.ts +50 -0
- package/dist/env.d.cts +44 -0
- package/dist/env.d.ts +44 -0
- package/dist/index.cjs +5 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +20 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +700 -0
- package/dist/index.js.map +1 -0
- package/dist/keyless.cjs +16 -0
- package/dist/keyless.cjs.map +1 -0
- package/dist/keyless.d.cts +244 -0
- package/dist/keyless.d.ts +244 -0
- package/dist/keyless.js +355 -0
- package/dist/keyless.js.map +1 -0
- package/dist/management-credential-BNyhHagG.cjs +2 -0
- package/dist/management-credential-BNyhHagG.cjs.map +1 -0
- package/dist/management-credential-VhmMzGDl.js +39 -0
- package/dist/management-credential-VhmMzGDl.js.map +1 -0
- package/dist/management-credential.d.cts +40 -0
- package/dist/management-credential.d.ts +40 -0
- package/dist/mcp-auth-info-BGyWua_9.js +19 -0
- package/dist/mcp-auth-info-BGyWua_9.js.map +1 -0
- package/dist/mcp-auth-info-jblMsSJo.cjs +2 -0
- package/dist/mcp-auth-info-jblMsSJo.cjs.map +1 -0
- package/dist/mcp-auth-info.d.cts +17 -0
- package/dist/mcp-auth-info.d.ts +17 -0
- package/dist/oidc--We55Nrh.cjs +2 -0
- package/dist/oidc--We55Nrh.cjs.map +1 -0
- package/dist/oidc-CB0FqjFz.js +131 -0
- package/dist/oidc-CB0FqjFz.js.map +1 -0
- package/dist/oidc.d.cts +80 -0
- package/dist/oidc.d.ts +80 -0
- package/dist/organization.d.cts +35 -0
- package/dist/organization.d.ts +35 -0
- package/dist/preview-hosts.d.cts +33 -0
- package/dist/preview-hosts.d.ts +33 -0
- package/dist/refresh.d.cts +28 -0
- package/dist/refresh.d.ts +28 -0
- package/dist/resource-server-express.cjs +2 -0
- package/dist/resource-server-express.cjs.map +1 -0
- package/dist/resource-server-express.d.cts +73 -0
- package/dist/resource-server-express.d.ts +73 -0
- package/dist/resource-server-express.js +57 -0
- package/dist/resource-server-express.js.map +1 -0
- package/dist/resource-server-mcp.cjs +2 -0
- package/dist/resource-server-mcp.cjs.map +1 -0
- package/dist/resource-server-mcp.d.cts +70 -0
- package/dist/resource-server-mcp.d.ts +70 -0
- package/dist/resource-server-mcp.js +59 -0
- package/dist/resource-server-mcp.js.map +1 -0
- package/dist/resource-server.cjs +2 -0
- package/dist/resource-server.cjs.map +1 -0
- package/dist/resource-server.d.cts +290 -0
- package/dist/resource-server.d.ts +290 -0
- package/dist/resource-server.js +301 -0
- package/dist/resource-server.js.map +1 -0
- package/dist/seal.d.cts +3 -0
- package/dist/seal.d.ts +3 -0
- package/dist/server-auth.d.cts +100 -0
- package/dist/server-auth.d.ts +100 -0
- package/package.json +96 -4
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { ManagementCredential } from './management-credential.cjs';
|
|
2
|
+
/**
|
|
3
|
+
* Resource-server middleware: protect an API or an MCP server with tokens a
|
|
4
|
+
* Pontive instance issued for it (pontive-spec 44 §11).
|
|
5
|
+
*
|
|
6
|
+
* `createResourceServer` verifies bearer tokens locally against each issuer's
|
|
7
|
+
* keys, answers a missing or failing token with the `WWW-Authenticate`
|
|
8
|
+
* challenge an MCP client follows, and serves the API's protected resource
|
|
9
|
+
* metadata (RFC 9728). Per request it calls nothing but the cached JWKS.
|
|
10
|
+
*
|
|
11
|
+
* This entry speaks Web `Request`/`Response` (Next route handlers, Hono,
|
|
12
|
+
* Workers, Bun, Deno). `@pontive/server/resource-server/express` and
|
|
13
|
+
* `@pontive/server/resource-server/mcp` adapt it to Express and to the
|
|
14
|
+
* MCP TypeScript SDK.
|
|
15
|
+
*/
|
|
16
|
+
/** Who a verified token speaks for, told apart by the subject's id prefix. */
|
|
17
|
+
export type PrincipalKind = "user" | "service_account" | "agent";
|
|
18
|
+
/** What every verified principal carries. */
|
|
19
|
+
interface PrincipalBase {
|
|
20
|
+
/**
|
|
21
|
+
* `usr_…`, `svc_…` or `agt_…`: whom the token speaks for.
|
|
22
|
+
*
|
|
23
|
+
* **For a delegated principal (`isDelegated`) this is the delegator, not the
|
|
24
|
+
* caller.** The caller is the agent in `actor`, and its authority is the
|
|
25
|
+
* intersection of the user's and the agent's, never the user's alone. Do not
|
|
26
|
+
* read or write your own data by `principal.sub` alone for a delegated
|
|
27
|
+
* principal: check by token with the authorization client
|
|
28
|
+
* (`@pontive/server/authorization`), which evaluates the pair, or
|
|
29
|
+
* branch on `principal.isDelegated`.
|
|
30
|
+
*/
|
|
31
|
+
sub: string;
|
|
32
|
+
/** From the sub's id prefix; no claim needed. A delegated token's subject is a user, so its kind is `"user"`. */
|
|
33
|
+
kind: PrincipalKind;
|
|
34
|
+
/** The client the token was issued to. */
|
|
35
|
+
clientId: string;
|
|
36
|
+
/** The organization the token is scoped to, when it is. */
|
|
37
|
+
orgId?: string;
|
|
38
|
+
/** The token's roles. For a delegated token, already intersected by Pontive (the delegator's roles the agent also holds). */
|
|
39
|
+
roles: string[];
|
|
40
|
+
/** The token's permissions. For a delegated token, already intersected by Pontive. */
|
|
41
|
+
permissions: string[];
|
|
42
|
+
/** The token's granted scopes. */
|
|
43
|
+
scopes: string[];
|
|
44
|
+
/** The agent instance, when the subject itself is an agent. */
|
|
45
|
+
agentInstance?: string;
|
|
46
|
+
/** The raw token, e.g. to check permissions by token. */
|
|
47
|
+
token: string;
|
|
48
|
+
/**
|
|
49
|
+
* Every claim of the verified token: the claims template's (say
|
|
50
|
+
* `legacy_user_id`) beside the standard ones the fields above are read
|
|
51
|
+
* from. A claim is whatever the issuer wrote, so check its type before
|
|
52
|
+
* using it.
|
|
53
|
+
*/
|
|
54
|
+
claims: Record<string, unknown>;
|
|
55
|
+
}
|
|
56
|
+
/** Who acts for the subject, from the token's `act` claim (RFC 8693 §4.1). */
|
|
57
|
+
export interface PrincipalActor {
|
|
58
|
+
/** `agt_…` for an agent acting for its delegator; `svc_…` for an API's backend calling downstream for the user. */
|
|
59
|
+
sub: string;
|
|
60
|
+
kind: "agent" | "service_account";
|
|
61
|
+
/** The agent's running copy, when it named one. */
|
|
62
|
+
agentInstance?: string;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* A token an agent holds for its delegator (profile (b), `act.sub` is
|
|
66
|
+
* `agt_…`). `sub` is the user; `actor` is the agent. Its authority is the
|
|
67
|
+
* intersection of both: answer it with a check by token, never by `sub`.
|
|
68
|
+
*/
|
|
69
|
+
export interface DelegatedPrincipal extends PrincipalBase {
|
|
70
|
+
isDelegated: true;
|
|
71
|
+
actor: PrincipalActor & {
|
|
72
|
+
kind: "agent";
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Any other token: the subject acts for itself. An `actor` here is a
|
|
77
|
+
* registered API's backend service account calling downstream for the user
|
|
78
|
+
* (profile (a)), a conduit: only the subject is evaluated.
|
|
79
|
+
*/
|
|
80
|
+
export interface UndelegatedPrincipal extends PrincipalBase {
|
|
81
|
+
isDelegated: false;
|
|
82
|
+
actor?: PrincipalActor & {
|
|
83
|
+
kind: "service_account";
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The verified caller, as the handler sees it. Narrow on `isDelegated`: a
|
|
88
|
+
* delegated principal's `sub` is the user an agent acts for, and its authority
|
|
89
|
+
* is narrower than that user's.
|
|
90
|
+
*/
|
|
91
|
+
export type Principal = DelegatedPrincipal | UndelegatedPrincipal;
|
|
92
|
+
/** The protected resource metadata document (RFC 9728; pontive-spec 44 §9). */
|
|
93
|
+
export interface ProtectedResourceMetadata {
|
|
94
|
+
resource: string;
|
|
95
|
+
authorization_servers: string[];
|
|
96
|
+
scopes_supported?: string[];
|
|
97
|
+
bearer_methods_supported: string[];
|
|
98
|
+
resource_name?: string;
|
|
99
|
+
resource_documentation?: string;
|
|
100
|
+
logo_uri?: string;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* How the resource server gets a management API token to read its own
|
|
104
|
+
* metadata: a token, a function returning one, or the client credentials of
|
|
105
|
+
* the API's backend service account (exchanged at the first issuer for a
|
|
106
|
+
* management-audience token, and reused until it is about to expire).
|
|
107
|
+
*/
|
|
108
|
+
export type MetadataCredential = ManagementCredential;
|
|
109
|
+
export type { ManagementCredential };
|
|
110
|
+
/** An MCP tool a scope covers (pontive-spec 44 §5, §12). Only `name` is read here. */
|
|
111
|
+
export interface ApiScopeTool {
|
|
112
|
+
/** The MCP tool name, as `tools/call` sends it. */
|
|
113
|
+
name: string;
|
|
114
|
+
title?: string;
|
|
115
|
+
read_only?: boolean;
|
|
116
|
+
destructive?: boolean;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* An API scope as the management API returns it (`GET /v1/apis/{api_id}`,
|
|
120
|
+
* pontive-spec 44 §5), so the dashboard's scopes can be pasted as they are.
|
|
121
|
+
* Only `key` and `tools` are read here.
|
|
122
|
+
*/
|
|
123
|
+
export interface ApiScope {
|
|
124
|
+
key: string;
|
|
125
|
+
description?: string;
|
|
126
|
+
display_name?: string;
|
|
127
|
+
origin?: "manual" | "tool_import";
|
|
128
|
+
/** The MCP tools the scope covers; a token holding the scope may call each of them (`requireTool`). */
|
|
129
|
+
tools?: ApiScopeTool[];
|
|
130
|
+
}
|
|
131
|
+
export interface ResourceServerConfig {
|
|
132
|
+
/**
|
|
133
|
+
* The instance's issuer: its auth domain, e.g.
|
|
134
|
+
* `https://acme.auth.us.pontive.app`. A list when the instance has more than
|
|
135
|
+
* one auth domain (a custom domain beside the default): a token from any of
|
|
136
|
+
* them is accepted, and each is listed in `authorization_servers`.
|
|
137
|
+
*/
|
|
138
|
+
issuer: string | string[];
|
|
139
|
+
/**
|
|
140
|
+
* The API's identifier, exactly as registered. Tokens must carry it in
|
|
141
|
+
* `aud`, and it is the metadata's `resource`. When it is an http(s) URL the
|
|
142
|
+
* metadata is served under its origin (RFC 9728 §3.1).
|
|
143
|
+
*/
|
|
144
|
+
resource: string;
|
|
145
|
+
/**
|
|
146
|
+
* The API's scopes, for `scopes_supported`: keys, or the scopes as the
|
|
147
|
+
* management API returns them (`ApiScope`). A scope's `tools` are the MCP
|
|
148
|
+
* tools a token holding it may call (`requireTool`, pontive-spec 44 §12).
|
|
149
|
+
*/
|
|
150
|
+
scopes?: (string | ApiScope)[];
|
|
151
|
+
/**
|
|
152
|
+
* What `requireTool` does with a tool no scope lists. `"deny"` (the
|
|
153
|
+
* default) refuses it with a `403` that names no scope, since no scope
|
|
154
|
+
* could allow it: a tool added to the server after the last tool import is
|
|
155
|
+
* refused, and logged once, until the import lists it. `"allow"` lets any
|
|
156
|
+
* valid token call it.
|
|
157
|
+
*/
|
|
158
|
+
unlistedTools?: "deny" | "allow";
|
|
159
|
+
/** The metadata's `resource_name`. */
|
|
160
|
+
resourceName?: string;
|
|
161
|
+
/** The metadata's `resource_documentation`. */
|
|
162
|
+
resourceDocumentation?: string;
|
|
163
|
+
/** The metadata's `logo_uri`. */
|
|
164
|
+
logoUri?: string;
|
|
165
|
+
/**
|
|
166
|
+
* Read the metadata from Pontive instead of building it from this
|
|
167
|
+
* configuration, for an API whose scopes and display fields are kept in the
|
|
168
|
+
* dashboard. It is fetched on first use and cached for five minutes.
|
|
169
|
+
*
|
|
170
|
+
* When no configured scope lists tools, `requireTool` reads the scopes and
|
|
171
|
+
* their tools from the same API (`GET /v1/apis/{api_id}`, with the
|
|
172
|
+
* same credential and `sys:api:read`), cached the same way.
|
|
173
|
+
*/
|
|
174
|
+
metadata?: {
|
|
175
|
+
fetch: {
|
|
176
|
+
/** The API's id (`api_…`). */
|
|
177
|
+
apiId: string;
|
|
178
|
+
/** A credential the management API accepts for `GET /v1/apis/{api_id}/resource-metadata`. */
|
|
179
|
+
credential: MetadataCredential;
|
|
180
|
+
/**
|
|
181
|
+
* The management API's origin in the instance's region, e.g. `https://api.us.pontive.com`.
|
|
182
|
+
* A `/v1`, `/mgmt` or `/mgmt/v1` suffix is dropped (`managementBase`).
|
|
183
|
+
*/
|
|
184
|
+
managementUrl: string;
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* Also serve `/.well-known/oauth-authorization-server`, proxying the
|
|
189
|
+
* issuer's own document, for older MCP clients that ask the resource server
|
|
190
|
+
* for it. Default false.
|
|
191
|
+
*/
|
|
192
|
+
authorizationServerMetadataProxy?: boolean;
|
|
193
|
+
/** Clock skew allowed on `exp` and `nbf`. Default 60. */
|
|
194
|
+
leewaySeconds?: number;
|
|
195
|
+
/** Defaults to the global `fetch`. Used for discovery, JWKS and metadata. */
|
|
196
|
+
fetch?: typeof fetch;
|
|
197
|
+
/** Where configuration drift is reported. Defaults to `console`. */
|
|
198
|
+
logger?: Pick<Console, "warn">;
|
|
199
|
+
}
|
|
200
|
+
/** What a route needs of the token beyond its being valid. */
|
|
201
|
+
export interface BearerOptions {
|
|
202
|
+
/** Every one of these must be granted. */
|
|
203
|
+
scopes?: string[];
|
|
204
|
+
}
|
|
205
|
+
/** Anything with request headers: a Web `Request`, `Headers`, or a Node/Express request. */
|
|
206
|
+
export type RequestLike = Request | {
|
|
207
|
+
headers: Headers | Record<string, string | string[] | undefined>;
|
|
208
|
+
};
|
|
209
|
+
/** A route handler pair, e.g. to export from a Next `route.ts`. */
|
|
210
|
+
export interface RouteHandlers {
|
|
211
|
+
GET(request: Request): Promise<Response>;
|
|
212
|
+
OPTIONS(request: Request): Promise<Response>;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Why a request was refused: a `401` (no token, or one that failed) or a
|
|
216
|
+
* `403` (a valid token without a scope the route needs). `toResponse()` is the
|
|
217
|
+
* answer to send.
|
|
218
|
+
*/
|
|
219
|
+
export declare class BearerError extends Error {
|
|
220
|
+
readonly status: 401 | 403;
|
|
221
|
+
/** Absent when no token was sent (RFC 6750 §3.1). */
|
|
222
|
+
readonly code: "invalid_token" | "insufficient_scope" | undefined;
|
|
223
|
+
/** The `WWW-Authenticate` header value. */
|
|
224
|
+
readonly wwwAuthenticate: string;
|
|
225
|
+
constructor(status: 401 | 403,
|
|
226
|
+
/** Absent when no token was sent (RFC 6750 §3.1). */
|
|
227
|
+
code: "invalid_token" | "insufficient_scope" | undefined,
|
|
228
|
+
/** The `WWW-Authenticate` header value. */
|
|
229
|
+
wwwAuthenticate: string, description: string);
|
|
230
|
+
toResponse(): Response;
|
|
231
|
+
}
|
|
232
|
+
export interface ResourceServer {
|
|
233
|
+
/** The API identifier tokens must carry in `aud`. */
|
|
234
|
+
readonly resource: string;
|
|
235
|
+
/** The accepted issuers. */
|
|
236
|
+
readonly issuers: readonly string[];
|
|
237
|
+
/** The clock skew allowed on `exp` and `nbf`. */
|
|
238
|
+
readonly leewaySeconds: number;
|
|
239
|
+
/** The metadata's URL, named in every challenge; absent for an identifier that is not an http(s) URL. */
|
|
240
|
+
readonly metadataUrl: string | undefined;
|
|
241
|
+
/** The paths the metadata is served at: the RFC 9728 §3.1 path-suffixed one first. */
|
|
242
|
+
readonly metadataPaths: readonly string[];
|
|
243
|
+
/** Verifies the request's bearer token. Throws a `BearerError` when it is missing, fails, or lacks a scope. */
|
|
244
|
+
verify(request: RequestLike, options?: BearerOptions): Promise<Principal>;
|
|
245
|
+
/** Verifies a raw token, as `verify` does. */
|
|
246
|
+
verifyToken(token: string, options?: BearerOptions): Promise<Principal>;
|
|
247
|
+
/** The protected resource metadata document. */
|
|
248
|
+
metadata(): Promise<ProtectedResourceMetadata>;
|
|
249
|
+
/**
|
|
250
|
+
* A guard for Web-standard handlers: the principal, or the `401`/`403`
|
|
251
|
+
* response to return as it is.
|
|
252
|
+
*/
|
|
253
|
+
requireBearer(options?: BearerOptions): (request: Request) => Promise<Principal | Response>;
|
|
254
|
+
/**
|
|
255
|
+
* The scopes that list an MCP tool (pontive-spec 44 §12), from the
|
|
256
|
+
* configured scopes, else from the API in Pontive when `metadata.fetch` is
|
|
257
|
+
* set. Empty for a tool no scope lists.
|
|
258
|
+
*/
|
|
259
|
+
scopesForTool(name: string): Promise<string[]>;
|
|
260
|
+
/**
|
|
261
|
+
* Refuses a principal that holds none of the scopes listing the tool: a
|
|
262
|
+
* `BearerError` `403` with `error="insufficient_scope"` and
|
|
263
|
+
* `scope="<every scope listing the tool>"`, any one of which would do. A
|
|
264
|
+
* tool no scope lists follows `unlistedTools`.
|
|
265
|
+
*/
|
|
266
|
+
authorizeTool(principal: Principal, name: string): Promise<void>;
|
|
267
|
+
/**
|
|
268
|
+
* `authorizeTool` for every `tools/call` in a JSON-RPC message or batch, as
|
|
269
|
+
* an MCP client posts it; any other message passes. Throws the first refusal.
|
|
270
|
+
* Run it on the parsed body before the MCP transport sees it, so the refusal
|
|
271
|
+
* is an HTTP `403` the client can step up from.
|
|
272
|
+
*/
|
|
273
|
+
authorizeToolCalls(principal: Principal, message: unknown): Promise<void>;
|
|
274
|
+
/**
|
|
275
|
+
* A guard for a Web-standard handler that serves one tool: the principal,
|
|
276
|
+
* or the `401`/`403` response to return as it is.
|
|
277
|
+
*/
|
|
278
|
+
requireTool(name: string, options?: BearerOptions): (request: Request) => Promise<Principal | Response>;
|
|
279
|
+
/** `GET` and `OPTIONS` for the protected resource metadata route. */
|
|
280
|
+
readonly metadataRoute: RouteHandlers;
|
|
281
|
+
/** `GET` and `OPTIONS` for the authorization server metadata proxy (404 unless the proxy is on). */
|
|
282
|
+
readonly authorizationServerMetadataRoute: RouteHandlers;
|
|
283
|
+
/**
|
|
284
|
+
* Answers any of the well-known routes this server serves (both metadata
|
|
285
|
+
* paths, and the AS-metadata proxy when on), or null for any other path. For
|
|
286
|
+
* a catch-all: `return (await rs.handleWellKnown(req)) ?? next()`.
|
|
287
|
+
*/
|
|
288
|
+
handleWellKnown(request: Request): Promise<Response | null>;
|
|
289
|
+
}
|
|
290
|
+
export declare function createResourceServer(config: ResourceServerConfig): ResourceServer;
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { ManagementCredential } from './management-credential.js';
|
|
2
|
+
/**
|
|
3
|
+
* Resource-server middleware: protect an API or an MCP server with tokens a
|
|
4
|
+
* Pontive instance issued for it (pontive-spec 44 §11).
|
|
5
|
+
*
|
|
6
|
+
* `createResourceServer` verifies bearer tokens locally against each issuer's
|
|
7
|
+
* keys, answers a missing or failing token with the `WWW-Authenticate`
|
|
8
|
+
* challenge an MCP client follows, and serves the API's protected resource
|
|
9
|
+
* metadata (RFC 9728). Per request it calls nothing but the cached JWKS.
|
|
10
|
+
*
|
|
11
|
+
* This entry speaks Web `Request`/`Response` (Next route handlers, Hono,
|
|
12
|
+
* Workers, Bun, Deno). `@pontive/server/resource-server/express` and
|
|
13
|
+
* `@pontive/server/resource-server/mcp` adapt it to Express and to the
|
|
14
|
+
* MCP TypeScript SDK.
|
|
15
|
+
*/
|
|
16
|
+
/** Who a verified token speaks for, told apart by the subject's id prefix. */
|
|
17
|
+
export type PrincipalKind = "user" | "service_account" | "agent";
|
|
18
|
+
/** What every verified principal carries. */
|
|
19
|
+
interface PrincipalBase {
|
|
20
|
+
/**
|
|
21
|
+
* `usr_…`, `svc_…` or `agt_…`: whom the token speaks for.
|
|
22
|
+
*
|
|
23
|
+
* **For a delegated principal (`isDelegated`) this is the delegator, not the
|
|
24
|
+
* caller.** The caller is the agent in `actor`, and its authority is the
|
|
25
|
+
* intersection of the user's and the agent's, never the user's alone. Do not
|
|
26
|
+
* read or write your own data by `principal.sub` alone for a delegated
|
|
27
|
+
* principal: check by token with the authorization client
|
|
28
|
+
* (`@pontive/server/authorization`), which evaluates the pair, or
|
|
29
|
+
* branch on `principal.isDelegated`.
|
|
30
|
+
*/
|
|
31
|
+
sub: string;
|
|
32
|
+
/** From the sub's id prefix; no claim needed. A delegated token's subject is a user, so its kind is `"user"`. */
|
|
33
|
+
kind: PrincipalKind;
|
|
34
|
+
/** The client the token was issued to. */
|
|
35
|
+
clientId: string;
|
|
36
|
+
/** The organization the token is scoped to, when it is. */
|
|
37
|
+
orgId?: string;
|
|
38
|
+
/** The token's roles. For a delegated token, already intersected by Pontive (the delegator's roles the agent also holds). */
|
|
39
|
+
roles: string[];
|
|
40
|
+
/** The token's permissions. For a delegated token, already intersected by Pontive. */
|
|
41
|
+
permissions: string[];
|
|
42
|
+
/** The token's granted scopes. */
|
|
43
|
+
scopes: string[];
|
|
44
|
+
/** The agent instance, when the subject itself is an agent. */
|
|
45
|
+
agentInstance?: string;
|
|
46
|
+
/** The raw token, e.g. to check permissions by token. */
|
|
47
|
+
token: string;
|
|
48
|
+
/**
|
|
49
|
+
* Every claim of the verified token: the claims template's (say
|
|
50
|
+
* `legacy_user_id`) beside the standard ones the fields above are read
|
|
51
|
+
* from. A claim is whatever the issuer wrote, so check its type before
|
|
52
|
+
* using it.
|
|
53
|
+
*/
|
|
54
|
+
claims: Record<string, unknown>;
|
|
55
|
+
}
|
|
56
|
+
/** Who acts for the subject, from the token's `act` claim (RFC 8693 §4.1). */
|
|
57
|
+
export interface PrincipalActor {
|
|
58
|
+
/** `agt_…` for an agent acting for its delegator; `svc_…` for an API's backend calling downstream for the user. */
|
|
59
|
+
sub: string;
|
|
60
|
+
kind: "agent" | "service_account";
|
|
61
|
+
/** The agent's running copy, when it named one. */
|
|
62
|
+
agentInstance?: string;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* A token an agent holds for its delegator (profile (b), `act.sub` is
|
|
66
|
+
* `agt_…`). `sub` is the user; `actor` is the agent. Its authority is the
|
|
67
|
+
* intersection of both: answer it with a check by token, never by `sub`.
|
|
68
|
+
*/
|
|
69
|
+
export interface DelegatedPrincipal extends PrincipalBase {
|
|
70
|
+
isDelegated: true;
|
|
71
|
+
actor: PrincipalActor & {
|
|
72
|
+
kind: "agent";
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Any other token: the subject acts for itself. An `actor` here is a
|
|
77
|
+
* registered API's backend service account calling downstream for the user
|
|
78
|
+
* (profile (a)), a conduit: only the subject is evaluated.
|
|
79
|
+
*/
|
|
80
|
+
export interface UndelegatedPrincipal extends PrincipalBase {
|
|
81
|
+
isDelegated: false;
|
|
82
|
+
actor?: PrincipalActor & {
|
|
83
|
+
kind: "service_account";
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The verified caller, as the handler sees it. Narrow on `isDelegated`: a
|
|
88
|
+
* delegated principal's `sub` is the user an agent acts for, and its authority
|
|
89
|
+
* is narrower than that user's.
|
|
90
|
+
*/
|
|
91
|
+
export type Principal = DelegatedPrincipal | UndelegatedPrincipal;
|
|
92
|
+
/** The protected resource metadata document (RFC 9728; pontive-spec 44 §9). */
|
|
93
|
+
export interface ProtectedResourceMetadata {
|
|
94
|
+
resource: string;
|
|
95
|
+
authorization_servers: string[];
|
|
96
|
+
scopes_supported?: string[];
|
|
97
|
+
bearer_methods_supported: string[];
|
|
98
|
+
resource_name?: string;
|
|
99
|
+
resource_documentation?: string;
|
|
100
|
+
logo_uri?: string;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* How the resource server gets a management API token to read its own
|
|
104
|
+
* metadata: a token, a function returning one, or the client credentials of
|
|
105
|
+
* the API's backend service account (exchanged at the first issuer for a
|
|
106
|
+
* management-audience token, and reused until it is about to expire).
|
|
107
|
+
*/
|
|
108
|
+
export type MetadataCredential = ManagementCredential;
|
|
109
|
+
export type { ManagementCredential };
|
|
110
|
+
/** An MCP tool a scope covers (pontive-spec 44 §5, §12). Only `name` is read here. */
|
|
111
|
+
export interface ApiScopeTool {
|
|
112
|
+
/** The MCP tool name, as `tools/call` sends it. */
|
|
113
|
+
name: string;
|
|
114
|
+
title?: string;
|
|
115
|
+
read_only?: boolean;
|
|
116
|
+
destructive?: boolean;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* An API scope as the management API returns it (`GET /v1/apis/{api_id}`,
|
|
120
|
+
* pontive-spec 44 §5), so the dashboard's scopes can be pasted as they are.
|
|
121
|
+
* Only `key` and `tools` are read here.
|
|
122
|
+
*/
|
|
123
|
+
export interface ApiScope {
|
|
124
|
+
key: string;
|
|
125
|
+
description?: string;
|
|
126
|
+
display_name?: string;
|
|
127
|
+
origin?: "manual" | "tool_import";
|
|
128
|
+
/** The MCP tools the scope covers; a token holding the scope may call each of them (`requireTool`). */
|
|
129
|
+
tools?: ApiScopeTool[];
|
|
130
|
+
}
|
|
131
|
+
export interface ResourceServerConfig {
|
|
132
|
+
/**
|
|
133
|
+
* The instance's issuer: its auth domain, e.g.
|
|
134
|
+
* `https://acme.auth.us.pontive.app`. A list when the instance has more than
|
|
135
|
+
* one auth domain (a custom domain beside the default): a token from any of
|
|
136
|
+
* them is accepted, and each is listed in `authorization_servers`.
|
|
137
|
+
*/
|
|
138
|
+
issuer: string | string[];
|
|
139
|
+
/**
|
|
140
|
+
* The API's identifier, exactly as registered. Tokens must carry it in
|
|
141
|
+
* `aud`, and it is the metadata's `resource`. When it is an http(s) URL the
|
|
142
|
+
* metadata is served under its origin (RFC 9728 §3.1).
|
|
143
|
+
*/
|
|
144
|
+
resource: string;
|
|
145
|
+
/**
|
|
146
|
+
* The API's scopes, for `scopes_supported`: keys, or the scopes as the
|
|
147
|
+
* management API returns them (`ApiScope`). A scope's `tools` are the MCP
|
|
148
|
+
* tools a token holding it may call (`requireTool`, pontive-spec 44 §12).
|
|
149
|
+
*/
|
|
150
|
+
scopes?: (string | ApiScope)[];
|
|
151
|
+
/**
|
|
152
|
+
* What `requireTool` does with a tool no scope lists. `"deny"` (the
|
|
153
|
+
* default) refuses it with a `403` that names no scope, since no scope
|
|
154
|
+
* could allow it: a tool added to the server after the last tool import is
|
|
155
|
+
* refused, and logged once, until the import lists it. `"allow"` lets any
|
|
156
|
+
* valid token call it.
|
|
157
|
+
*/
|
|
158
|
+
unlistedTools?: "deny" | "allow";
|
|
159
|
+
/** The metadata's `resource_name`. */
|
|
160
|
+
resourceName?: string;
|
|
161
|
+
/** The metadata's `resource_documentation`. */
|
|
162
|
+
resourceDocumentation?: string;
|
|
163
|
+
/** The metadata's `logo_uri`. */
|
|
164
|
+
logoUri?: string;
|
|
165
|
+
/**
|
|
166
|
+
* Read the metadata from Pontive instead of building it from this
|
|
167
|
+
* configuration, for an API whose scopes and display fields are kept in the
|
|
168
|
+
* dashboard. It is fetched on first use and cached for five minutes.
|
|
169
|
+
*
|
|
170
|
+
* When no configured scope lists tools, `requireTool` reads the scopes and
|
|
171
|
+
* their tools from the same API (`GET /v1/apis/{api_id}`, with the
|
|
172
|
+
* same credential and `sys:api:read`), cached the same way.
|
|
173
|
+
*/
|
|
174
|
+
metadata?: {
|
|
175
|
+
fetch: {
|
|
176
|
+
/** The API's id (`api_…`). */
|
|
177
|
+
apiId: string;
|
|
178
|
+
/** A credential the management API accepts for `GET /v1/apis/{api_id}/resource-metadata`. */
|
|
179
|
+
credential: MetadataCredential;
|
|
180
|
+
/**
|
|
181
|
+
* The management API's origin in the instance's region, e.g. `https://api.us.pontive.com`.
|
|
182
|
+
* A `/v1`, `/mgmt` or `/mgmt/v1` suffix is dropped (`managementBase`).
|
|
183
|
+
*/
|
|
184
|
+
managementUrl: string;
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* Also serve `/.well-known/oauth-authorization-server`, proxying the
|
|
189
|
+
* issuer's own document, for older MCP clients that ask the resource server
|
|
190
|
+
* for it. Default false.
|
|
191
|
+
*/
|
|
192
|
+
authorizationServerMetadataProxy?: boolean;
|
|
193
|
+
/** Clock skew allowed on `exp` and `nbf`. Default 60. */
|
|
194
|
+
leewaySeconds?: number;
|
|
195
|
+
/** Defaults to the global `fetch`. Used for discovery, JWKS and metadata. */
|
|
196
|
+
fetch?: typeof fetch;
|
|
197
|
+
/** Where configuration drift is reported. Defaults to `console`. */
|
|
198
|
+
logger?: Pick<Console, "warn">;
|
|
199
|
+
}
|
|
200
|
+
/** What a route needs of the token beyond its being valid. */
|
|
201
|
+
export interface BearerOptions {
|
|
202
|
+
/** Every one of these must be granted. */
|
|
203
|
+
scopes?: string[];
|
|
204
|
+
}
|
|
205
|
+
/** Anything with request headers: a Web `Request`, `Headers`, or a Node/Express request. */
|
|
206
|
+
export type RequestLike = Request | {
|
|
207
|
+
headers: Headers | Record<string, string | string[] | undefined>;
|
|
208
|
+
};
|
|
209
|
+
/** A route handler pair, e.g. to export from a Next `route.ts`. */
|
|
210
|
+
export interface RouteHandlers {
|
|
211
|
+
GET(request: Request): Promise<Response>;
|
|
212
|
+
OPTIONS(request: Request): Promise<Response>;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Why a request was refused: a `401` (no token, or one that failed) or a
|
|
216
|
+
* `403` (a valid token without a scope the route needs). `toResponse()` is the
|
|
217
|
+
* answer to send.
|
|
218
|
+
*/
|
|
219
|
+
export declare class BearerError extends Error {
|
|
220
|
+
readonly status: 401 | 403;
|
|
221
|
+
/** Absent when no token was sent (RFC 6750 §3.1). */
|
|
222
|
+
readonly code: "invalid_token" | "insufficient_scope" | undefined;
|
|
223
|
+
/** The `WWW-Authenticate` header value. */
|
|
224
|
+
readonly wwwAuthenticate: string;
|
|
225
|
+
constructor(status: 401 | 403,
|
|
226
|
+
/** Absent when no token was sent (RFC 6750 §3.1). */
|
|
227
|
+
code: "invalid_token" | "insufficient_scope" | undefined,
|
|
228
|
+
/** The `WWW-Authenticate` header value. */
|
|
229
|
+
wwwAuthenticate: string, description: string);
|
|
230
|
+
toResponse(): Response;
|
|
231
|
+
}
|
|
232
|
+
export interface ResourceServer {
|
|
233
|
+
/** The API identifier tokens must carry in `aud`. */
|
|
234
|
+
readonly resource: string;
|
|
235
|
+
/** The accepted issuers. */
|
|
236
|
+
readonly issuers: readonly string[];
|
|
237
|
+
/** The clock skew allowed on `exp` and `nbf`. */
|
|
238
|
+
readonly leewaySeconds: number;
|
|
239
|
+
/** The metadata's URL, named in every challenge; absent for an identifier that is not an http(s) URL. */
|
|
240
|
+
readonly metadataUrl: string | undefined;
|
|
241
|
+
/** The paths the metadata is served at: the RFC 9728 §3.1 path-suffixed one first. */
|
|
242
|
+
readonly metadataPaths: readonly string[];
|
|
243
|
+
/** Verifies the request's bearer token. Throws a `BearerError` when it is missing, fails, or lacks a scope. */
|
|
244
|
+
verify(request: RequestLike, options?: BearerOptions): Promise<Principal>;
|
|
245
|
+
/** Verifies a raw token, as `verify` does. */
|
|
246
|
+
verifyToken(token: string, options?: BearerOptions): Promise<Principal>;
|
|
247
|
+
/** The protected resource metadata document. */
|
|
248
|
+
metadata(): Promise<ProtectedResourceMetadata>;
|
|
249
|
+
/**
|
|
250
|
+
* A guard for Web-standard handlers: the principal, or the `401`/`403`
|
|
251
|
+
* response to return as it is.
|
|
252
|
+
*/
|
|
253
|
+
requireBearer(options?: BearerOptions): (request: Request) => Promise<Principal | Response>;
|
|
254
|
+
/**
|
|
255
|
+
* The scopes that list an MCP tool (pontive-spec 44 §12), from the
|
|
256
|
+
* configured scopes, else from the API in Pontive when `metadata.fetch` is
|
|
257
|
+
* set. Empty for a tool no scope lists.
|
|
258
|
+
*/
|
|
259
|
+
scopesForTool(name: string): Promise<string[]>;
|
|
260
|
+
/**
|
|
261
|
+
* Refuses a principal that holds none of the scopes listing the tool: a
|
|
262
|
+
* `BearerError` `403` with `error="insufficient_scope"` and
|
|
263
|
+
* `scope="<every scope listing the tool>"`, any one of which would do. A
|
|
264
|
+
* tool no scope lists follows `unlistedTools`.
|
|
265
|
+
*/
|
|
266
|
+
authorizeTool(principal: Principal, name: string): Promise<void>;
|
|
267
|
+
/**
|
|
268
|
+
* `authorizeTool` for every `tools/call` in a JSON-RPC message or batch, as
|
|
269
|
+
* an MCP client posts it; any other message passes. Throws the first refusal.
|
|
270
|
+
* Run it on the parsed body before the MCP transport sees it, so the refusal
|
|
271
|
+
* is an HTTP `403` the client can step up from.
|
|
272
|
+
*/
|
|
273
|
+
authorizeToolCalls(principal: Principal, message: unknown): Promise<void>;
|
|
274
|
+
/**
|
|
275
|
+
* A guard for a Web-standard handler that serves one tool: the principal,
|
|
276
|
+
* or the `401`/`403` response to return as it is.
|
|
277
|
+
*/
|
|
278
|
+
requireTool(name: string, options?: BearerOptions): (request: Request) => Promise<Principal | Response>;
|
|
279
|
+
/** `GET` and `OPTIONS` for the protected resource metadata route. */
|
|
280
|
+
readonly metadataRoute: RouteHandlers;
|
|
281
|
+
/** `GET` and `OPTIONS` for the authorization server metadata proxy (404 unless the proxy is on). */
|
|
282
|
+
readonly authorizationServerMetadataRoute: RouteHandlers;
|
|
283
|
+
/**
|
|
284
|
+
* Answers any of the well-known routes this server serves (both metadata
|
|
285
|
+
* paths, and the AS-metadata proxy when on), or null for any other path. For
|
|
286
|
+
* a catch-all: `return (await rs.handleWellKnown(req)) ?? next()`.
|
|
287
|
+
*/
|
|
288
|
+
handleWellKnown(request: Request): Promise<Response | null>;
|
|
289
|
+
}
|
|
290
|
+
export declare function createResourceServer(config: ResourceServerConfig): ResourceServer;
|