@pexis/auth 0.0.0-stage → 0.1.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.
@@ -0,0 +1,208 @@
1
+ // OAuth clients identified by a Client ID Metadata Document: the client_id is
2
+ // an https URL, and the document there names the client and where codes may
3
+ // be sent. Fetching a URL someone else chose is a server-side request forgery
4
+ // risk, so every rule here is about what may be fetched and what is believed.
5
+ /** A client that cannot be used, with a reason safe to show the person. */
6
+ export class ClientError extends Error {
7
+ }
8
+ const MAX_BYTES = 10 * 1024;
9
+ const TIME_LIMIT_MS = 5000;
10
+ const MIN_CACHE_S = 5 * 60;
11
+ const MAX_CACHE_S = 24 * 60 * 60;
12
+ const FAILURE_CACHE_S = 60;
13
+ const BLOCKED_SUFFIXES = ['localhost', 'local', 'internal', 'test', 'lan', 'corp', 'home.arpa', 'svc', 'example', 'invalid', 'onion'];
14
+ const isIpHost = (host) => host.startsWith('[') || /^\d{1,3}(\.\d{1,3}){3}$/.test(host);
15
+ /** The client_id as a URL, or a ClientError saying why it cannot be one. */
16
+ export function clientIdUrl(raw) {
17
+ let url;
18
+ try {
19
+ url = new URL(raw);
20
+ }
21
+ catch {
22
+ throw new ClientError('The client_id is not a URL.');
23
+ }
24
+ // Checked on the parsed URL, so other spellings of an address (2130706433,
25
+ // 0x7f.1) are already normalized to the dotted form.
26
+ const host = url.hostname;
27
+ if (url.protocol !== 'https:')
28
+ throw new ClientError('The client_id must be an https URL.');
29
+ if (url.port !== '' || url.username || url.password || url.hash)
30
+ throw new ClientError('The client_id must have no port, user name or fragment.');
31
+ if (url.pathname === '/' || url.pathname === '')
32
+ throw new ClientError('The client_id must have a path.');
33
+ if (isIpHost(host))
34
+ throw new ClientError('The client_id must use a name, not an IP address.');
35
+ if (host.endsWith('.') || !host.includes('.'))
36
+ throw new ClientError('The client_id must use a full domain name.');
37
+ if (BLOCKED_SUFFIXES.some((suffix) => host === suffix || host.endsWith(`.${suffix}`)))
38
+ throw new ClientError('The client_id must be on the public internet.');
39
+ return url;
40
+ }
41
+ // Addresses on the person's own computer. RFC 8252 prefers the IP literals,
42
+ // since a name can be remapped, but the MCP authorization spec allows
43
+ // localhost and MCP clients such as Claude Code use it. Browsers and operating
44
+ // systems resolve localhost to the computer itself (RFC 6761), and whoever
45
+ // could change that already controls the computer. Exactly "localhost": no
46
+ // subdomains, no trailing dot. One risk the IP literals do not have:
47
+ // localhost may resolve to 127.0.0.1 or ::1, so another program listening on
48
+ // the other address at the same port could receive the code (RFC 8252 §8.3);
49
+ // PKCE makes a code useless without the client's verifier.
50
+ const LOOPBACK_HOSTS = new Set(['127.0.0.1', '[::1]', 'localhost']);
51
+ /** A redirect URI a client may list: https, or http on a loopback address for apps on the person's computer (RFC 8252). */
52
+ export function checkRedirectUri(raw) {
53
+ let url;
54
+ try {
55
+ url = new URL(raw);
56
+ }
57
+ catch {
58
+ throw new ClientError('A redirect URI is not a URL.');
59
+ }
60
+ if (url.hash || url.username || url.password)
61
+ throw new ClientError('A redirect URI must have no fragment, user name or password.');
62
+ if (url.protocol === 'https:')
63
+ return url;
64
+ if (isLoopback(url))
65
+ return url;
66
+ throw new ClientError('A redirect URI must be https, or http on localhost, 127.0.0.1 or [::1].');
67
+ }
68
+ export const isLoopback = (url) => url.protocol === 'http:' && LOOPBACK_HOSTS.has(url.hostname);
69
+ /** Whether a requested redirect URI is one the client listed: exact, except the port of a loopback URI (RFC 8252 §7.3). */
70
+ export function redirectAllowed(client, requested) {
71
+ let want;
72
+ try {
73
+ want = checkRedirectUri(requested);
74
+ }
75
+ catch {
76
+ return false;
77
+ }
78
+ return client.redirectUris.some((listed) => {
79
+ if (listed === requested)
80
+ return true;
81
+ const have = new URL(listed);
82
+ if (!isLoopback(have) || !isLoopback(want))
83
+ return false;
84
+ return have.hostname === want.hostname && have.pathname === want.pathname && have.search === want.search;
85
+ });
86
+ }
87
+ /** Web fetch, for clients the app lists by name. No redirects, a byte limit counted after decompression. */
88
+ export const webMetadataFetch = async (url, { signal, maxBytes }) => {
89
+ const res = await fetch(url, { redirect: 'error', signal, credentials: 'omit', headers: { accept: 'application/json' } });
90
+ const reader = res.body?.getReader();
91
+ const chunks = [];
92
+ let size = 0;
93
+ while (reader) {
94
+ const { done, value } = await reader.read();
95
+ if (done)
96
+ break;
97
+ size += value.byteLength;
98
+ if (size > maxBytes) {
99
+ await reader.cancel();
100
+ throw new ClientError(`The client metadata is larger than ${maxBytes} bytes.`);
101
+ }
102
+ chunks.push(value);
103
+ }
104
+ const body = new Uint8Array(size);
105
+ let at = 0;
106
+ for (const chunk of chunks) {
107
+ body.set(chunk, at);
108
+ at += chunk.byteLength;
109
+ }
110
+ return { status: res.status, headers: res.headers, body };
111
+ };
112
+ function cacheSeconds(headers) {
113
+ const control = headers.get('cache-control') ?? '';
114
+ if (/\bno-store\b|\bno-cache\b/i.test(control))
115
+ return MIN_CACHE_S;
116
+ const maxAge = /\bmax-age=(\d+)/i.exec(control);
117
+ const seconds = maxAge ? Number(maxAge[1]) : MIN_CACHE_S;
118
+ return Math.min(MAX_CACHE_S, Math.max(MIN_CACHE_S, seconds));
119
+ }
120
+ function parse(clientId, response) {
121
+ if (response.status !== 200)
122
+ throw new ClientError(`The client metadata could not be read (HTTP ${response.status}).`);
123
+ const type = (response.headers.get('content-type') ?? '').split(';')[0].trim().toLowerCase();
124
+ if (type !== 'application/json')
125
+ throw new ClientError('The client metadata is not application/json.');
126
+ let doc;
127
+ try {
128
+ doc = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(response.body));
129
+ }
130
+ catch {
131
+ throw new ClientError('The client metadata is not valid JSON.');
132
+ }
133
+ if (typeof doc !== 'object' || doc === null || Array.isArray(doc))
134
+ throw new ClientError('The client metadata is not a JSON object.');
135
+ const d = doc;
136
+ // Exactly the URL it was fetched from: a document cannot speak for another client.
137
+ if (d.client_id !== clientId)
138
+ throw new ClientError('The client metadata names a different client_id.');
139
+ if (typeof d.client_name !== 'string' || d.client_name.trim() === '' || d.client_name.length > 100)
140
+ throw new ClientError('The client metadata needs a client_name of at most 100 characters.');
141
+ if (!Array.isArray(d.redirect_uris) || d.redirect_uris.length === 0 || d.redirect_uris.length > 10 || !d.redirect_uris.every((u) => typeof u === 'string')) {
142
+ throw new ClientError('The client metadata needs redirect_uris: one to ten URLs.');
143
+ }
144
+ for (const uri of d.redirect_uris)
145
+ checkRedirectUri(uri);
146
+ return { clientId, name: d.client_name.trim(), redirectUris: [...d.redirect_uris] };
147
+ }
148
+ export function clientLoader(options) {
149
+ const maxEntries = options.maxEntries ?? 1000;
150
+ const concurrency = options.concurrency ?? 8;
151
+ const perHost = options.perHost ?? 2;
152
+ const cache = new Map();
153
+ const inFlight = new Map();
154
+ const hosts = new Map();
155
+ let running = 0;
156
+ const remember = (clientId, entry) => {
157
+ cache.delete(clientId);
158
+ cache.set(clientId, entry);
159
+ while (cache.size > maxEntries)
160
+ cache.delete(cache.keys().next().value);
161
+ };
162
+ async function fetchClient(clientId, url) {
163
+ const host = url.hostname;
164
+ // Refused rather than queued, so a flood of client_ids cannot pile up requests.
165
+ if (running >= concurrency || (hosts.get(host) ?? 0) >= perHost)
166
+ throw new ClientError('Too many clients are being checked right now. Try again in a moment.');
167
+ running++;
168
+ hosts.set(host, (hosts.get(host) ?? 0) + 1);
169
+ try {
170
+ const response = await options.fetch(url, { signal: AbortSignal.timeout(TIME_LIMIT_MS), maxBytes: MAX_BYTES });
171
+ const client = parse(clientId, response);
172
+ remember(clientId, { until: Date.now() + cacheSeconds(response.headers) * 1000, client });
173
+ return client;
174
+ }
175
+ catch (error) {
176
+ const message = error instanceof ClientError ? error.message : 'The client metadata could not be fetched.';
177
+ // A failure is remembered briefly, so a bad client_id cannot make the server fetch in a loop.
178
+ remember(clientId, { until: Date.now() + FAILURE_CACHE_S * 1000, error: message });
179
+ throw new ClientError(message);
180
+ }
181
+ finally {
182
+ running--;
183
+ const left = (hosts.get(host) ?? 1) - 1;
184
+ if (left === 0)
185
+ hosts.delete(host);
186
+ else
187
+ hosts.set(host, left);
188
+ }
189
+ }
190
+ return {
191
+ async load(clientId) {
192
+ const url = clientIdUrl(clientId);
193
+ const cached = cache.get(clientId);
194
+ if (cached && cached.until > Date.now()) {
195
+ if (cached.client)
196
+ return cached.client;
197
+ throw new ClientError(cached.error ?? 'The client could not be used.');
198
+ }
199
+ const pending = inFlight.get(clientId);
200
+ if (pending)
201
+ return pending;
202
+ const started = fetchClient(clientId, url).finally(() => inFlight.delete(clientId));
203
+ inFlight.set(clientId, started);
204
+ return started;
205
+ },
206
+ };
207
+ }
208
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/server/client.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,4EAA4E;AAC5E,8EAA8E;AAC9E,8EAA8E;AAwB9E,2EAA2E;AAC3E,MAAM,OAAO,WAAY,SAAQ,KAAK;CAAG;AAEzC,MAAM,SAAS,GAAG,EAAE,GAAG,IAAI,CAAC;AAC5B,MAAM,aAAa,GAAG,IAAI,CAAC;AAC3B,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE,CAAC;AAC3B,MAAM,WAAW,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;AACjC,MAAM,eAAe,GAAG,EAAE,CAAC;AAC3B,MAAM,gBAAgB,GAAG,CAAC,WAAW,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAEtI,MAAM,QAAQ,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,yBAAyB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAEhG,4EAA4E;AAC5E,MAAM,UAAU,WAAW,CAAC,GAAW;IACrC,IAAI,GAAQ,CAAC;IACb,IAAI,CAAC;QACH,GAAG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AQ;YACjB,MAAM,GAAG,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC;YAClC,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;YACnC,IAAI,MAAM,IAAI,MAAM,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;gBACxC,IAAI,MAAM,CAAC,MAAM;oBAAE,OAAO,MAAM,CAAC,MAAM,CAAC;gBACxC,MAAM,IAAI,WAAW,CAAC,MAAM,CAAC,KAAK,IAAI,+BAA+B,CAAC,CAAC;YACzE,CAAC;YACD,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;YACvC,IAAI,OAAO;gBAAE,OAAO,OAAO,CAAC;YAC5B,MAAM,OAAO,GAAG,WAAW,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;YACpF,QAAQ,CAAC,GAAG,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;YAChC,OAAO,OAAO,CAAC;QACjB,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { hashSecret, memoryOAuthStore, newSecret, type CodeDetails, type OAuthGrant, type OAuthStore, type SecretKind, type StoredSecret, type Use } from './store.js';
2
+ export { checkRedirectUri, ClientError, clientIdUrl, clientLoader, isLoopback, redirectAllowed, webMetadataFetch, type ClientLoader, type ClientLoaderOptions, type ClientMetadata, type MetadataFetch, type MetadataResponse, } from './client.js';
3
+ export { bearerToken, oauthServer, SCOPES, type OAuthServerOptions } from './plugin.js';
@@ -0,0 +1,7 @@
1
+ // @pexis/auth/server: the site as an OAuth authorization server, so MCP
2
+ // clients can sign in as a person. Experimental; built in steps (see
3
+ // ARCHITECTURE.md, MCP sign-in).
4
+ export { hashSecret, memoryOAuthStore, newSecret } from './store.js';
5
+ export { checkRedirectUri, ClientError, clientIdUrl, clientLoader, isLoopback, redirectAllowed, webMetadataFetch, } from './client.js';
6
+ export { bearerToken, oauthServer, SCOPES } from './plugin.js';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,qEAAqE;AACrE,iCAAiC;AAEjC,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,SAAS,EAAoG,MAAM,YAAY,CAAC;AACvK,OAAO,EACL,gBAAgB,EAChB,WAAW,EACX,WAAW,EACX,YAAY,EACZ,UAAU,EACV,eAAe,EACf,gBAAgB,GAMjB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,EAA2B,MAAM,aAAa,CAAC"}
@@ -0,0 +1,37 @@
1
+ import { type Plugin } from '@pexis/core';
2
+ import { type User } from '@pexis/sessions';
3
+ import { type MetadataFetch } from './client.js';
4
+ import { type OAuthStore } from './store.js';
5
+ export declare const SCOPES: readonly ['actions:read', 'actions:write'];
6
+ export interface OAuthServerOptions {
7
+ /**
8
+ * The person a grant acts for, looked up again on every request (cached for
9
+ * a minute), so a banned, deleted or demoted account loses its agents at
10
+ * once. Return null to revoke the grant.
11
+ */
12
+ loadUser: (id: string) => User | null | Promise<User | null>;
13
+ /**
14
+ * Which clients may sign in: exact client_id URLs, or host names. 'any'
15
+ * allows every public https client_id, and needs fetchMetadata from
16
+ * @pexis/node/public-fetch, which refuses private addresses.
17
+ */
18
+ clients: readonly string[] | 'any';
19
+ /** How client metadata is fetched. Default: web fetch, allowed only with a list of clients. */
20
+ fetchMetadata?: MetadataFetch;
21
+ store?: OAuthStore;
22
+ /** Days a grant lasts at most. Default 30; loopback clients (apps on the person's computer) get at most 7. */
23
+ grantDays?: number;
24
+ /** The connected-apps page. Default "/account/connected-apps". */
25
+ connectedAppsPath?: string;
26
+ /** The MCP endpoint the tokens are for. Default "/pexis/mcp". */
27
+ resourcePath?: string;
28
+ }
29
+ /** The Bearer token in an Authorization header, or null. Other schemes (Basic) are not ours. */
30
+ export declare function bearerToken(request: Request): string | null;
31
+ /**
32
+ * Lets MCP clients sign in as a person with OAuth 2.1, so they can call
33
+ * actions that need a permission. Needs sessions() and auth().
34
+ *
35
+ * @experimental Still being designed: may change, or go, without a deprecation period.
36
+ */
37
+ export declare function oauthServer(options: OAuthServerOptions): Plugin;