@zurchin/sdk 0.0.0 → 2.16.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 +212 -0
- package/LICENSE +55 -0
- package/README.md +189 -1
- package/dist/auth.d.ts +152 -0
- package/dist/auth.js +189 -0
- package/dist/credential.d.ts +96 -0
- package/dist/credential.js +297 -0
- package/dist/declared-agents.d.ts +9 -0
- package/dist/declared-agents.js +46 -0
- package/dist/index.d.ts +1187 -0
- package/dist/index.js +714 -0
- package/dist/next.d.ts +104 -0
- package/dist/next.js +300 -0
- package/dist/node.d.ts +139 -0
- package/dist/node.js +402 -0
- package/dist/verdict.d.ts +57 -0
- package/dist/verdict.js +116 -0
- package/package.json +66 -4
package/dist/next.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { type Place, type VerifyAnswer } from './index.ts';
|
|
2
|
+
export { VERDICT_HEADER } from './index.ts';
|
|
3
|
+
export { DECLARED_AGENT_TOKENS, declaredAgentToken } from './declared-agents.ts';
|
|
4
|
+
/**
|
|
5
|
+
* The work a request may leave running after its response: `NextFetchEvent`,
|
|
6
|
+
* a Worker's `ExecutionContext`, Netlify's `Context` (whose `ip` is read).
|
|
7
|
+
*/
|
|
8
|
+
export type WaitUntil = {
|
|
9
|
+
waitUntil(promise: Promise<unknown>): void;
|
|
10
|
+
ip?: string;
|
|
11
|
+
};
|
|
12
|
+
type CommonOptions = {
|
|
13
|
+
/** An API key with `read` access. Without one the helper does nothing, so local development and previews are untouched. */
|
|
14
|
+
apiKey?: string | null;
|
|
15
|
+
baseUrl?: string;
|
|
16
|
+
/** The URL the agent called, when your proxy sees another (a different host behind a load balancer). Defaults to `request.url`. */
|
|
17
|
+
url?: (request: Request) => string;
|
|
18
|
+
/** Called with Pidgeon's answer to each check of a signed request, after the response. */
|
|
19
|
+
onAnswer?: (answer: VerifyAnswer, request: Request) => void | Promise<void>;
|
|
20
|
+
/** Called when the check failed (Pidgeon unreachable, slow, or your key refused). Defaults to `console.warn`. */
|
|
21
|
+
onError?: (error: unknown, request: Request) => void;
|
|
22
|
+
/** How long a check may take before it is given up, in milliseconds. 5,000 by default. */
|
|
23
|
+
timeoutMs?: number;
|
|
24
|
+
/**
|
|
25
|
+
* The visitor's address, for checking a declared agent against its
|
|
26
|
+
* vendor's published ranges. By default Netlify's `context.ip`, or on
|
|
27
|
+
* Vercel `x-real-ip` (which Vercel sets, and a visitor can't). Elsewhere,
|
|
28
|
+
* pass it, or the address goes unchecked.
|
|
29
|
+
*/
|
|
30
|
+
ip?: (request: Request) => string | null | undefined;
|
|
31
|
+
fetch?: typeof fetch;
|
|
32
|
+
};
|
|
33
|
+
/** `observe`: record every agent's request with Pidgeon, never act on it. */
|
|
34
|
+
export type BouncerOptions = CommonOptions & {
|
|
35
|
+
mode?: 'observe';
|
|
36
|
+
/**
|
|
37
|
+
* Where on your site a request is (`signup`, `checkout`, `api`), or null
|
|
38
|
+
* for anywhere else: recorded with each visit, so the dashboard shows which
|
|
39
|
+
* agents reached sign-up or checkout. Observe mode never acts on it.
|
|
40
|
+
*/
|
|
41
|
+
place?: (request: Request) => Place | null;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* `enforce`: act on your site rules where `place` names a place. A `block`
|
|
45
|
+
* is answered with a 403; `onAnswer` gets every answer, before the response,
|
|
46
|
+
* to act on `allow` and `limit` yourself. Pidgeon unreachable or slow lets
|
|
47
|
+
* the request through and calls `onError`.
|
|
48
|
+
*/
|
|
49
|
+
export type EnforceOptions = CommonOptions & {
|
|
50
|
+
mode: 'enforce';
|
|
51
|
+
/** Where on your site a request is, or null for a page no rule acts on (it is observed). */
|
|
52
|
+
place: (request: Request) => Place | null;
|
|
53
|
+
/** The answer to a blocked request. A plain 403 by default. */
|
|
54
|
+
blocked?: (answer: VerifyAnswer, request: Request) => Response;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Whether a request is one Bouncer has something to say about: it carries a
|
|
58
|
+
* signature, or claims to be an agent with `Signature-Agent` alone (which
|
|
59
|
+
* Pidgeon records as `unsigned`). A CORS preflight never is.
|
|
60
|
+
*/
|
|
61
|
+
export declare function carriesAgentHeaders(request: Request): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Whether a request is one Bouncer reports: it carries an agent's headers
|
|
64
|
+
* (above), or its User-Agent names an agent that doesn't sign
|
|
65
|
+
* (`DECLARED_AGENT_TOKENS`), which Pidgeon records as `declared`. A person's
|
|
66
|
+
* visit never is, nor is a CORS preflight.
|
|
67
|
+
*/
|
|
68
|
+
export declare function isAgentRequest(request: Request): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* The headers Pidgeon needs, one string each (`Headers` already joins a
|
|
71
|
+
* repeated header with `, `). The visitor's cookies and credentials are left
|
|
72
|
+
* out unless the signature covers them.
|
|
73
|
+
*/
|
|
74
|
+
export declare function headersForVerify(request: Request): Record<string, string>;
|
|
75
|
+
/**
|
|
76
|
+
* Make the proxy step: call it with each request and the proxy's event. In
|
|
77
|
+
* `observe` mode it returns at once and the check runs after the response.
|
|
78
|
+
* In `enforce` mode it answers a Promise: a 403 Response for a blocked agent,
|
|
79
|
+
* or null to go on.
|
|
80
|
+
*/
|
|
81
|
+
export declare function bouncer(options: BouncerOptions): (request: Request, event?: WaitUntil) => void;
|
|
82
|
+
export declare function bouncer(options: EnforceOptions): (request: Request, event?: WaitUntil) => Promise<Response | null>;
|
|
83
|
+
/**
|
|
84
|
+
* For a sign-up behind a hook that cannot see the agent's request (Supabase,
|
|
85
|
+
* Auth0, WorkOS): verify the agent now, before the response, and answer the
|
|
86
|
+
* verdict to hand on, or null when the request is not an agent's, the
|
|
87
|
+
* signature did not hold, or Pidgeon could not be asked. Awaited, so call it
|
|
88
|
+
* only on the sign-up's own request, not on every page.
|
|
89
|
+
*
|
|
90
|
+
* ```ts
|
|
91
|
+
* // proxy.ts, on POST /signup
|
|
92
|
+
* const headers = new Headers(request.headers);
|
|
93
|
+
* headers.delete(VERDICT_HEADER); // never pass on one the visitor sent
|
|
94
|
+
* const verdict = await verdictFor(request, { apiKey: process.env.PIDGEON_API_KEY });
|
|
95
|
+
* if (verdict) headers.set(VERDICT_HEADER, verdict);
|
|
96
|
+
* return NextResponse.next({ request: { headers } });
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export declare function verdictFor(request: Request, options: Omit<CommonOptions, 'onAnswer'> & {
|
|
100
|
+
/** The agent's address as your platform reports it (on Vercel, `ipAddress(request)` from `@vercel/functions`). */
|
|
101
|
+
ip?: string | null;
|
|
102
|
+
/** Where the site rule is read for the verdict's `decision`. `signup` by default. */
|
|
103
|
+
place?: Place;
|
|
104
|
+
}): Promise<string | null>;
|
package/dist/next.js
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pidgeon Bouncer for a Next.js proxy (`proxy.ts`, `middleware.ts` before
|
|
3
|
+
* Next 16), or anything else that hands you a Fetch API `Request` and a
|
|
4
|
+
* `waitUntil`: Vercel Functions, Cloudflare Workers, Hono.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* // proxy.ts
|
|
8
|
+
* import { NextResponse, type NextFetchEvent, type NextRequest } from 'next/server';
|
|
9
|
+
* import { bouncer } from '@zurchin/sdk/next';
|
|
10
|
+
*
|
|
11
|
+
* const observe = bouncer({ apiKey: process.env.PIDGEON_API_KEY });
|
|
12
|
+
*
|
|
13
|
+
* export function proxy(request: NextRequest, event: NextFetchEvent) {
|
|
14
|
+
* observe(request, event);
|
|
15
|
+
* return NextResponse.next();
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* `observe` mode (the default) asks Pidgeon about every request that
|
|
20
|
+
* carries an agent's signature, or whose User-Agent names an agent that
|
|
21
|
+
* doesn't sign (ChatGPT-User, Claude-User, Perplexity-User: `declared`),
|
|
22
|
+
* after the response is on its way
|
|
23
|
+
* (`waitUntil`): the visit is never slowed or blocked, and the check shows up
|
|
24
|
+
* in your dashboard. It never throws into your request. A signed request is
|
|
25
|
+
* checked (`POST /v1/verify`); an agent that only names itself is recorded
|
|
26
|
+
* with others in a batch a second later (`POST /v1/visits`), without its
|
|
27
|
+
* query string, and isn't counted as a verification.
|
|
28
|
+
*
|
|
29
|
+
* `enforce` mode acts on your site rules (Bouncer → Rules in the dashboard).
|
|
30
|
+
* `place` names where a request is; for those, the check is awaited and a
|
|
31
|
+
* `block` is answered with a 403. Everything else is observed as above.
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* const guard = bouncer({
|
|
35
|
+
* apiKey: process.env.PIDGEON_API_KEY,
|
|
36
|
+
* mode: 'enforce',
|
|
37
|
+
* place: (request) => (new URL(request.url).pathname.startsWith('/signup') ? 'signup' : null),
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* export async function proxy(request: NextRequest, event: NextFetchEvent) {
|
|
41
|
+
* return (await guard(request, event)) ?? NextResponse.next();
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
import { declaredAgentToken } from "./declared-agents.js";
|
|
46
|
+
import { Pidgeon, DEFAULT_BASE_URL } from "./index.js";
|
|
47
|
+
export { VERDICT_HEADER } from "./index.js";
|
|
48
|
+
export { DECLARED_AGENT_TOKENS, declaredAgentToken } from "./declared-agents.js";
|
|
49
|
+
/** The most visits sent at once, and how long the first waits for others. */
|
|
50
|
+
const BATCH = 100;
|
|
51
|
+
const BATCH_WAIT_MS = 1_000;
|
|
52
|
+
/** The 403 a blocked agent gets, saying why in the rule's words. */
|
|
53
|
+
function blockedResponse(answer) {
|
|
54
|
+
return new Response(`Pidgeon Bouncer: ${answer.rule ?? 'this agent is not let in here.'}\n`, {
|
|
55
|
+
status: 403,
|
|
56
|
+
headers: { 'content-type': 'text/plain; charset=utf-8' },
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/** Headers only an agent sends: a Web Bot Auth signature, or the claim to be one. */
|
|
60
|
+
const AGENT_HEADERS = ['signature-agent', 'signature', 'signature-input'];
|
|
61
|
+
/** A visitor's credentials, never sent to Pidgeon unless the agent signed them. */
|
|
62
|
+
const PRIVATE_HEADERS = ['cookie', 'authorization', 'proxy-authorization'];
|
|
63
|
+
/**
|
|
64
|
+
* Whether a request is one Bouncer has something to say about: it carries a
|
|
65
|
+
* signature, or claims to be an agent with `Signature-Agent` alone (which
|
|
66
|
+
* Pidgeon records as `unsigned`). A CORS preflight never is.
|
|
67
|
+
*/
|
|
68
|
+
export function carriesAgentHeaders(request) {
|
|
69
|
+
if (request.method === 'OPTIONS')
|
|
70
|
+
return false;
|
|
71
|
+
return AGENT_HEADERS.some((name) => request.headers.has(name));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Whether a request is one Bouncer reports: it carries an agent's headers
|
|
75
|
+
* (above), or its User-Agent names an agent that doesn't sign
|
|
76
|
+
* (`DECLARED_AGENT_TOKENS`), which Pidgeon records as `declared`. A person's
|
|
77
|
+
* visit never is, nor is a CORS preflight.
|
|
78
|
+
*/
|
|
79
|
+
export function isAgentRequest(request) {
|
|
80
|
+
if (request.method === 'OPTIONS')
|
|
81
|
+
return false;
|
|
82
|
+
return (carriesAgentHeaders(request) || declaredAgentToken(request.headers.get('user-agent')) !== null);
|
|
83
|
+
}
|
|
84
|
+
/** The visitor's address, as the platform reports it: see `ip` in the options. */
|
|
85
|
+
function addressOf(request, event, ip) {
|
|
86
|
+
try {
|
|
87
|
+
if (ip)
|
|
88
|
+
return ip(request) ?? null;
|
|
89
|
+
if (typeof event?.ip === 'string' && event.ip)
|
|
90
|
+
return event.ip;
|
|
91
|
+
const env = globalThis.process
|
|
92
|
+
?.env;
|
|
93
|
+
if (env?.VERCEL)
|
|
94
|
+
return request.headers.get('x-real-ip');
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
// An address is a nicety: without one the check goes on, unchecked.
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The headers Pidgeon needs, one string each (`Headers` already joins a
|
|
103
|
+
* repeated header with `, `). The visitor's cookies and credentials are left
|
|
104
|
+
* out unless the signature covers them.
|
|
105
|
+
*/
|
|
106
|
+
export function headersForVerify(request) {
|
|
107
|
+
const covered = request.headers.get('signature-input')?.toLowerCase() ?? '';
|
|
108
|
+
const out = {};
|
|
109
|
+
request.headers.forEach((value, name) => {
|
|
110
|
+
if (PRIVATE_HEADERS.includes(name) && !covered.includes(`"${name}"`))
|
|
111
|
+
return;
|
|
112
|
+
out[name] = value;
|
|
113
|
+
});
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
export function bouncer(options) {
|
|
117
|
+
const apiKey = options.apiKey?.trim();
|
|
118
|
+
if (!apiKey)
|
|
119
|
+
return options.mode === 'enforce' ? async () => null : () => undefined;
|
|
120
|
+
const timeoutMs = options.timeoutMs ?? 5_000;
|
|
121
|
+
const baseFetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
122
|
+
const pidgeon = new Pidgeon({
|
|
123
|
+
apiKey,
|
|
124
|
+
baseUrl: options.baseUrl,
|
|
125
|
+
fetch: (input, init) => baseFetch(input, { ...init, signal: AbortSignal.timeout(timeoutMs) }),
|
|
126
|
+
});
|
|
127
|
+
const verifyEndpoint = `${pidgeon.baseUrl || DEFAULT_BASE_URL}/v1/verify`;
|
|
128
|
+
const onError = options.onError ??
|
|
129
|
+
((error) => console.warn('Pidgeon Bouncer: the check failed', error instanceof Error ? error.message : error));
|
|
130
|
+
const fail = (error, request) => {
|
|
131
|
+
try {
|
|
132
|
+
onError(error, request);
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
// A failing handler must not fail the request either.
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
// Declared agents' visits, sent together: the first waits a second for others, with the
|
|
139
|
+
// request's `waitUntil` holding the instance open; a full batch goes at once.
|
|
140
|
+
let queue = [];
|
|
141
|
+
let waiting = false;
|
|
142
|
+
const send = async () => {
|
|
143
|
+
while (queue.length) {
|
|
144
|
+
const batch = queue.slice(0, BATCH);
|
|
145
|
+
queue = queue.slice(BATCH);
|
|
146
|
+
try {
|
|
147
|
+
await pidgeon.visits.record(batch);
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
fail(error, null);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
const record = (request, url, event, place) => {
|
|
155
|
+
const ip = addressOf(request, event, options.ip);
|
|
156
|
+
queue.push({
|
|
157
|
+
method: request.method,
|
|
158
|
+
// The query string never leaves your site: Pidgeon stores the path only.
|
|
159
|
+
url: `${url.origin}${url.pathname}`,
|
|
160
|
+
user_agent: request.headers.get('user-agent') ?? '',
|
|
161
|
+
...(ip ? { ip } : {}),
|
|
162
|
+
...(place ? { place } : {}),
|
|
163
|
+
at: new Date().toISOString(),
|
|
164
|
+
});
|
|
165
|
+
let work = null;
|
|
166
|
+
if (queue.length >= BATCH)
|
|
167
|
+
work = send();
|
|
168
|
+
else if (!waiting) {
|
|
169
|
+
waiting = true;
|
|
170
|
+
work = new Promise((resolve) => setTimeout(resolve, BATCH_WAIT_MS)).then(() => {
|
|
171
|
+
waiting = false;
|
|
172
|
+
return send();
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
if (work && event)
|
|
176
|
+
event.waitUntil(work);
|
|
177
|
+
};
|
|
178
|
+
const check = async (request, url, place) => {
|
|
179
|
+
try {
|
|
180
|
+
// Only signed requests are checked here, with no address: their verdict then names none.
|
|
181
|
+
const answer = await pidgeon.verify({ method: request.method, url, headers: headersForVerify(request) }, place ? { place } : {});
|
|
182
|
+
await options.onAnswer?.(answer, request);
|
|
183
|
+
return answer;
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
try {
|
|
187
|
+
onError(error, request);
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
// A failing handler must not fail the request either.
|
|
191
|
+
}
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
if (options.mode === 'enforce') {
|
|
196
|
+
const enforce = options;
|
|
197
|
+
return async (request, event) => {
|
|
198
|
+
try {
|
|
199
|
+
if (!isAgentRequest(request))
|
|
200
|
+
return null;
|
|
201
|
+
const url = new URL(enforce.url ? enforce.url(request) : request.url);
|
|
202
|
+
if (`${url.origin}${url.pathname}` === verifyEndpoint)
|
|
203
|
+
return null;
|
|
204
|
+
const place = enforce.place(request);
|
|
205
|
+
// An agent that only names itself is never blocked (your rules decide `limit`): recorded, not awaited.
|
|
206
|
+
if (!carriesAgentHeaders(request)) {
|
|
207
|
+
record(request, url, event, place);
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
if (!place) {
|
|
211
|
+
const work = check(request, url.toString()).then(() => undefined);
|
|
212
|
+
if (event)
|
|
213
|
+
event.waitUntil(work);
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
const answer = await check(request, url.toString(), place);
|
|
217
|
+
if (answer?.decision !== 'block')
|
|
218
|
+
return null;
|
|
219
|
+
return enforce.blocked ? enforce.blocked(answer, request) : blockedResponse(answer);
|
|
220
|
+
}
|
|
221
|
+
catch (error) {
|
|
222
|
+
try {
|
|
223
|
+
onError(error, request);
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
// As above.
|
|
227
|
+
}
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
return (request, event) => {
|
|
233
|
+
try {
|
|
234
|
+
if (!isAgentRequest(request))
|
|
235
|
+
return;
|
|
236
|
+
const url = new URL(options.url ? options.url(request) : request.url);
|
|
237
|
+
// Pidgeon's own endpoint, should a proxy in front of it carry this step: never check the check.
|
|
238
|
+
if (`${url.origin}${url.pathname}` === verifyEndpoint)
|
|
239
|
+
return;
|
|
240
|
+
const place = options.place?.(request) ?? null;
|
|
241
|
+
if (!carriesAgentHeaders(request)) {
|
|
242
|
+
record(request, url, event, place);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
const work = check(request, url.toString(), place ?? undefined).then(() => undefined);
|
|
246
|
+
if (event)
|
|
247
|
+
event.waitUntil(work);
|
|
248
|
+
}
|
|
249
|
+
catch (error) {
|
|
250
|
+
try {
|
|
251
|
+
onError(error, request);
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
// As above.
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* For a sign-up behind a hook that cannot see the agent's request (Supabase,
|
|
261
|
+
* Auth0, WorkOS): verify the agent now, before the response, and answer the
|
|
262
|
+
* verdict to hand on, or null when the request is not an agent's, the
|
|
263
|
+
* signature did not hold, or Pidgeon could not be asked. Awaited, so call it
|
|
264
|
+
* only on the sign-up's own request, not on every page.
|
|
265
|
+
*
|
|
266
|
+
* ```ts
|
|
267
|
+
* // proxy.ts, on POST /signup
|
|
268
|
+
* const headers = new Headers(request.headers);
|
|
269
|
+
* headers.delete(VERDICT_HEADER); // never pass on one the visitor sent
|
|
270
|
+
* const verdict = await verdictFor(request, { apiKey: process.env.PIDGEON_API_KEY });
|
|
271
|
+
* if (verdict) headers.set(VERDICT_HEADER, verdict);
|
|
272
|
+
* return NextResponse.next({ request: { headers } });
|
|
273
|
+
* ```
|
|
274
|
+
*/
|
|
275
|
+
export async function verdictFor(request, options) {
|
|
276
|
+
const apiKey = options.apiKey?.trim();
|
|
277
|
+
if (!apiKey || !carriesAgentHeaders(request))
|
|
278
|
+
return null;
|
|
279
|
+
const timeoutMs = options.timeoutMs ?? 5_000;
|
|
280
|
+
const baseFetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
281
|
+
const pidgeon = new Pidgeon({
|
|
282
|
+
apiKey,
|
|
283
|
+
baseUrl: options.baseUrl,
|
|
284
|
+
fetch: (input, init) => baseFetch(input, { ...init, signal: AbortSignal.timeout(timeoutMs) }),
|
|
285
|
+
});
|
|
286
|
+
try {
|
|
287
|
+
const url = options.url ? options.url(request) : request.url;
|
|
288
|
+
const answer = await pidgeon.verify({ method: request.method, url, headers: headersForVerify(request) }, { ...(options.ip ? { ip: options.ip } : {}), place: options.place ?? 'signup' });
|
|
289
|
+
return 'verdict' in answer && answer.verdict ? answer.verdict : null;
|
|
290
|
+
}
|
|
291
|
+
catch (error) {
|
|
292
|
+
try {
|
|
293
|
+
(options.onError ?? ((e) => console.warn('Pidgeon Bouncer: no verdict', e)))(error, request);
|
|
294
|
+
}
|
|
295
|
+
catch {
|
|
296
|
+
// A failing handler must not fail the sign-up.
|
|
297
|
+
}
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
}
|
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Pidgeon, type Agent } from './index.ts';
|
|
2
|
+
type Env = Record<string, string | undefined>;
|
|
3
|
+
/** Which variable holds the API key, if one does: `ZURCHIN_API_KEY` wins over `PIDGEON_API_KEY`. */
|
|
4
|
+
export declare function apiKeyVariable(env?: Env): 'ZURCHIN_API_KEY' | 'PIDGEON_API_KEY' | undefined;
|
|
5
|
+
export declare function configDir(env?: Env): string;
|
|
6
|
+
export declare function baseUrlOf(env?: Env): string;
|
|
7
|
+
export type Session = {
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
issuer: string;
|
|
10
|
+
tokenEndpoint: string;
|
|
11
|
+
revocationEndpoint: string | null;
|
|
12
|
+
resource: string;
|
|
13
|
+
clientId: string;
|
|
14
|
+
accessToken: string;
|
|
15
|
+
refreshToken: string | null;
|
|
16
|
+
/** Milliseconds since the epoch. */
|
|
17
|
+
expiresAt: number;
|
|
18
|
+
scope: string;
|
|
19
|
+
};
|
|
20
|
+
export declare function readSession(baseUrl: string, env?: Env): Promise<Session>;
|
|
21
|
+
type Metadata = {
|
|
22
|
+
issuer: string;
|
|
23
|
+
authorization_endpoint: string;
|
|
24
|
+
token_endpoint: string;
|
|
25
|
+
registration_endpoint?: string;
|
|
26
|
+
revocation_endpoint?: string;
|
|
27
|
+
};
|
|
28
|
+
/** RFC 9728 then RFC 8414: who guards `<base>/mcp`, and where its endpoints are. */
|
|
29
|
+
export declare function discover(baseUrl: string, fetchImpl?: typeof fetch): Promise<{
|
|
30
|
+
resource: string;
|
|
31
|
+
metadata: Metadata;
|
|
32
|
+
}>;
|
|
33
|
+
/** Open a URL in the person's browser; where none can be opened, they are shown the URL instead. */
|
|
34
|
+
export declare function openInBrowser(url: string): void;
|
|
35
|
+
/**
|
|
36
|
+
* Sign in with a browser: the person signs in to Pidgeon there and chooses
|
|
37
|
+
* what this machine may do (`scope` is the most it asks for). Resolves once
|
|
38
|
+
* they answer, with the session saved.
|
|
39
|
+
*
|
|
40
|
+
* `onUrl` is told the address to open, which is also opened in the browser
|
|
41
|
+
* unless `openBrowser` is false (a headless machine shows it instead).
|
|
42
|
+
*/
|
|
43
|
+
export declare function signIn(options?: {
|
|
44
|
+
baseUrl?: string;
|
|
45
|
+
scope?: 'read' | 'draft' | 'send' | 'admin';
|
|
46
|
+
clientName?: string;
|
|
47
|
+
onUrl?: (url: string) => void;
|
|
48
|
+
openBrowser?: boolean;
|
|
49
|
+
timeoutMs?: number;
|
|
50
|
+
fetch?: typeof fetch;
|
|
51
|
+
env?: Env;
|
|
52
|
+
}): Promise<Session>;
|
|
53
|
+
/** A live access token for the saved session, refreshed (and saved) when it is about to run out. */
|
|
54
|
+
export declare function sessionToken(baseUrl: string, options?: {
|
|
55
|
+
fetch?: typeof fetch;
|
|
56
|
+
env?: Env;
|
|
57
|
+
}): Promise<string>;
|
|
58
|
+
/** End the session: Pidgeon is told (which ends the connection there), and the tokens are forgotten. */
|
|
59
|
+
export declare function signOut(baseUrl: string, options?: {
|
|
60
|
+
fetch?: typeof fetch;
|
|
61
|
+
env?: Env;
|
|
62
|
+
}): Promise<boolean>;
|
|
63
|
+
/**
|
|
64
|
+
* The API client for this machine: `ZURCHIN_API_KEY` or `PIDGEON_API_KEY` if set, else the
|
|
65
|
+
* signed-in session (refreshed as needed). Nothing is checked until the first call.
|
|
66
|
+
*/
|
|
67
|
+
export declare function clientFromEnvironment(options?: {
|
|
68
|
+
env?: Env;
|
|
69
|
+
baseUrl?: string;
|
|
70
|
+
fetch?: typeof fetch;
|
|
71
|
+
}): Pidgeon;
|
|
72
|
+
export type StoredAgentKey = {
|
|
73
|
+
agent_id: string;
|
|
74
|
+
name: string;
|
|
75
|
+
kid: string;
|
|
76
|
+
directory: string;
|
|
77
|
+
public_jwk: JsonWebKey;
|
|
78
|
+
private_jwk: JsonWebKey;
|
|
79
|
+
created_at: string;
|
|
80
|
+
};
|
|
81
|
+
/** Where an agent's key is kept on this machine. */
|
|
82
|
+
export declare const agentKeyPath: (agentId: string, env?: Env) => string;
|
|
83
|
+
export declare function saveAgentKey(entry: StoredAgentKey, env?: Env): Promise<string>;
|
|
84
|
+
export declare function readAgentKey(agentId: string, env?: Env): Promise<StoredAgentKey | null>;
|
|
85
|
+
/** Every agent key kept here, without their private halves. */
|
|
86
|
+
export declare function listAgentKeys(env?: Env): Promise<(Omit<StoredAgentKey, 'private_jwk'> & {
|
|
87
|
+
path: string;
|
|
88
|
+
})[]>;
|
|
89
|
+
export declare function forgetAgentKey(agentId: string, env?: Env): Promise<void>;
|
|
90
|
+
/** Your agent, from a key file this machine keeps: `fetch` signs every request as it. */
|
|
91
|
+
export declare function agentFromKeyFile(path: string): Promise<{
|
|
92
|
+
fetch: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
93
|
+
}>;
|
|
94
|
+
/**
|
|
95
|
+
* Register an agent, make its key on this machine, publish the public half
|
|
96
|
+
* signed with it (the possession proof the dashboard sends too), and keep
|
|
97
|
+
* the key here. Answers where the key is, never the key.
|
|
98
|
+
*/
|
|
99
|
+
export declare function setUpAgent(pidgeon: Pidgeon, input: {
|
|
100
|
+
name: string;
|
|
101
|
+
purpose?: string;
|
|
102
|
+
podId?: string;
|
|
103
|
+
}, env?: Env): Promise<{
|
|
104
|
+
agent: Agent & {
|
|
105
|
+
directory: string;
|
|
106
|
+
};
|
|
107
|
+
kid: string;
|
|
108
|
+
keyFile: string;
|
|
109
|
+
}>;
|
|
110
|
+
/**
|
|
111
|
+
* A new key for an agent already registered, made and kept here. It replaces
|
|
112
|
+
* the key this machine kept for that agent; the old one stays published
|
|
113
|
+
* until it is revoked in the dashboard.
|
|
114
|
+
*/
|
|
115
|
+
export declare function addAgentKey(pidgeon: Pidgeon, agentId: string, env?: Env): Promise<{
|
|
116
|
+
kid: string;
|
|
117
|
+
keyFile: string;
|
|
118
|
+
}>;
|
|
119
|
+
/**
|
|
120
|
+
* For a server that cannot make keys (the hosted MCP server): make the
|
|
121
|
+
* agent's key here, keep it, and answer what publishing it needs, never the
|
|
122
|
+
* key: the public half, and the signature headers of a `POST` to
|
|
123
|
+
* `publishUrl` made with the key, which is the possession proof. The proof
|
|
124
|
+
* holds for `lifetimeSeconds` (ten minutes), long enough to hand it over.
|
|
125
|
+
*/
|
|
126
|
+
export declare function makeKeyProof(input: {
|
|
127
|
+
agentId: string;
|
|
128
|
+
directory: string;
|
|
129
|
+
publishUrl: string;
|
|
130
|
+
name?: string;
|
|
131
|
+
lifetimeSeconds?: number;
|
|
132
|
+
}, env?: Env): Promise<{
|
|
133
|
+
agent_id: string;
|
|
134
|
+
kid: string;
|
|
135
|
+
key_file: string;
|
|
136
|
+
public_jwk: JsonWebKey;
|
|
137
|
+
signature: Record<string, string>;
|
|
138
|
+
}>;
|
|
139
|
+
export {};
|