@tenonai/yew 0.0.0-stage → 1.4.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 +296 -0
- package/LICENSE +21 -0
- package/README.md +55 -2
- package/THIRD-PARTY-NOTICES.md +122 -0
- package/bin/yew.js +9 -0
- package/dist/cli/app.js +198 -0
- package/dist/cli/export.js +57 -0
- package/dist/cli/io.js +65 -0
- package/dist/cli/key.js +113 -0
- package/dist/cli/main.js +44 -0
- package/dist/cli/mcp.js +76 -0
- package/dist/cli/remote.js +116 -0
- package/dist/cli/run.js +72 -0
- package/dist/cli/schedule.js +123 -0
- package/dist/cli/send.js +126 -0
- package/dist/cli/serve.js +301 -0
- package/dist/cli/service.js +241 -0
- package/dist/cli/system.js +44 -0
- package/dist/remote/access.js +107 -0
- package/dist/remote/connector.js +278 -0
- package/dist/remote/identity.js +71 -0
- package/dist/remote/noise.js +287 -0
- package/dist/remote/protocol.js +51 -0
- package/dist/remote/push.js +37 -0
- package/dist/server/agent/compaction.js +242 -0
- package/dist/server/agent/context.js +119 -0
- package/dist/server/agent/held.js +48 -0
- package/dist/server/agent/helper.js +122 -0
- package/dist/server/agent/mailroom.js +510 -0
- package/dist/server/agent/prompt.js +268 -0
- package/dist/server/agent/runner.js +1159 -0
- package/dist/server/artifacts.js +216 -0
- package/dist/server/budget.js +88 -0
- package/dist/server/data-export.js +105 -0
- package/dist/server/data-usage.js +143 -0
- package/dist/server/db/database.js +49 -0
- package/dist/server/db/migrations.js +347 -0
- package/dist/server/errors.js +16 -0
- package/dist/server/events.js +17 -0
- package/dist/server/export.js +378 -0
- package/dist/server/folder-picker.js +32 -0
- package/dist/server/from-jig.js +98 -0
- package/dist/server/http/agents.js +78 -0
- package/dist/server/http/api.js +468 -0
- package/dist/server/http/app.js +111 -0
- package/dist/server/http/artifacts.js +53 -0
- package/dist/server/http/data.js +54 -0
- package/dist/server/http/devices.js +47 -0
- package/dist/server/http/events.js +134 -0
- package/dist/server/http/idempotency.js +53 -0
- package/dist/server/http/input.js +106 -0
- package/dist/server/http/mail.js +52 -0
- package/dist/server/http/mcp.js +134 -0
- package/dist/server/http/notes.js +91 -0
- package/dist/server/http/problem.js +55 -0
- package/dist/server/http/providers.js +131 -0
- package/dist/server/http/schedules.js +58 -0
- package/dist/server/http/search.js +42 -0
- package/dist/server/http/security.js +168 -0
- package/dist/server/http/skills.js +53 -0
- package/dist/server/http/system.js +26 -0
- package/dist/server/ids.js +30 -0
- package/dist/server/keys.js +51 -0
- package/dist/server/mail/envelope.js +152 -0
- package/dist/server/mail/post.js +603 -0
- package/dist/server/mail/store.js +279 -0
- package/dist/server/manage.js +601 -0
- package/dist/server/mcp-builtin.js +179 -0
- package/dist/server/mcp-config.js +305 -0
- package/dist/server/mcp-oauth.js +448 -0
- package/dist/server/mcp.js +668 -0
- package/dist/server/model/gateway.js +410 -0
- package/dist/server/paths.js +34 -0
- package/dist/server/permissions/engine.js +249 -0
- package/dist/server/permissions/paths.js +93 -0
- package/dist/server/permissions/shell.js +635 -0
- package/dist/server/push.js +345 -0
- package/dist/server/reveal.js +36 -0
- package/dist/server/runtime.js +17 -0
- package/dist/server/schedules.js +356 -0
- package/dist/server/settings.js +80 -0
- package/dist/server/skills.js +262 -0
- package/dist/server/store.js +1077 -0
- package/dist/server/tools/files.js +318 -0
- package/dist/server/tools/html.js +221 -0
- package/dist/server/tools/jobs.js +38 -0
- package/dist/server/tools/mail.js +680 -0
- package/dist/server/tools/match.js +81 -0
- package/dist/server/tools/misc.js +253 -0
- package/dist/server/tools/process.js +128 -0
- package/dist/server/tools/read-state.js +40 -0
- package/dist/server/tools/registry.js +43 -0
- package/dist/server/tools/search.js +175 -0
- package/dist/server/tools/shell.js +124 -0
- package/dist/server/tools/types.js +43 -0
- package/dist/server/tools/web-search.js +198 -0
- package/dist/server/tools/web.js +240 -0
- package/dist/shared/cron.js +105 -0
- package/dist/shared/mcp-presets.js +170 -0
- package/dist/shared/messages.js +45 -0
- package/dist/shared/policy.js +42 -0
- package/dist/shared/types.js +34 -0
- package/dist/shared/version.js +8 -0
- package/mcp-builtin/package-lock.json +3398 -0
- package/mcp-builtin/package.json +14 -0
- package/mcp-builtin/presets/gitee/package-lock.json +105 -0
- package/mcp-builtin/presets/gitee/package.json +10 -0
- package/mcp-builtin/presets/notion/package-lock.json +2243 -0
- package/mcp-builtin/presets/notion/package.json +10 -0
- package/mcp-builtin/presets/sentry/package-lock.json +28 -0
- package/mcp-builtin/presets/sentry/package.json +10 -0
- package/package.json +75 -5
- package/web/dist/THIRD-PARTY-NOTICES.txt +1944 -0
- package/web/dist/assets/index-B9oWPSod.css +1 -0
- package/web/dist/assets/index-C5qBMzFX.js +68 -0
- package/web/dist/icons/app-icon.png +0 -0
- package/web/dist/icons/apple-touch-icon.png +0 -0
- package/web/dist/icons/favicon-16.png +0 -0
- package/web/dist/icons/favicon-32.png +0 -0
- package/web/dist/icons/favicon-64.png +0 -0
- package/web/dist/icons/tenon-mark.svg +1 -0
- package/web/dist/icons/yew-mark.svg +1 -0
- package/web/dist/index.html +18 -0
- package/web/dist/mascot/gift.webp +0 -0
- package/web/dist/mascot/greeting.webp +0 -0
- package/web/dist/mascot/happy.webp +0 -0
- package/web/dist/mascot/lost.webp +0 -0
- package/web/dist/mascot/shush.webp +0 -0
- package/web/dist/mascot/sleepy.webp +0 -0
- package/web/dist/mascot/sorry.webp +0 -0
- package/web/dist/mascot/thinking.webp +0 -0
- package/web/dist/mascot/waking.webp +0 -0
- package/web/dist/mascot/writing.webp +0 -0
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signing in to MCP servers in the browser (blueprint 6.11, item 40), as the MCP authorization spec
|
|
3
|
+
* describes it with OAuth 2.1. Yew finds the server's authorization server (RFC 9728 and RFC 8414),
|
|
4
|
+
* registers itself as a client where the server allows that (RFC 7591), or uses the client the person
|
|
5
|
+
* made for it (Google's servers), and sends the person's browser to sign in with PKCE and a state.
|
|
6
|
+
* The browser comes back to http://127.0.0.1:<port>/oauth/callback with a code, which Yew trades for
|
|
7
|
+
* tokens. Tokens and registered clients are secrets: they are kept in the system keychain, never in
|
|
8
|
+
* mcp.json, the database or logs.
|
|
9
|
+
*
|
|
10
|
+
* What a server says about where to sign in is checked before it is used (security review,
|
|
11
|
+
* 2026-11-18): every address is https; the details are for this server and from the sign-in server
|
|
12
|
+
* Yew asked; PKCE with S256 is supported; a sign-in server that names itself in the browser's return
|
|
13
|
+
* is the one the browser went to (RFC 9207). Requests that carry a code, a secret or a token do not
|
|
14
|
+
* follow redirects. Signing out asks the server to void the tokens where it says how (RFC 7009).
|
|
15
|
+
*/
|
|
16
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
17
|
+
/** The server answered that it wants a signed-in request. */
|
|
18
|
+
export class SignInRequired extends Error {
|
|
19
|
+
challenge;
|
|
20
|
+
constructor(challenge) {
|
|
21
|
+
super("the server asks you to sign in");
|
|
22
|
+
this.challenge = challenge;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** How long a started sign-in waits for the browser to come back. */
|
|
26
|
+
const PENDING_MS = 10 * 60 * 1000;
|
|
27
|
+
/** How long what Yew found out about where to sign in is used again. */
|
|
28
|
+
const FOUND_MS = 10 * 60 * 1000;
|
|
29
|
+
const REQUEST_MS = 20_000;
|
|
30
|
+
/** Voiding tokens on sign-out is a courtesy to the server: it does not hold up signing out for long. */
|
|
31
|
+
const REVOKE_MS = 5_000;
|
|
32
|
+
const LOOPBACK = new Set(["127.0.0.1", "localhost", "[::1]"]);
|
|
33
|
+
/**
|
|
34
|
+
* Whether Yew uses an address it was told about while signing in: https only, so a sign-in page, a
|
|
35
|
+
* code or a token never goes in the clear and a server cannot point Yew at other machines on the
|
|
36
|
+
* network. A server on this computer may use http on this computer.
|
|
37
|
+
*/
|
|
38
|
+
function usable(url, local) {
|
|
39
|
+
if (typeof url !== "string")
|
|
40
|
+
return false;
|
|
41
|
+
try {
|
|
42
|
+
const at = new URL(url);
|
|
43
|
+
return (at.protocol === "https:" || (local && at.protocol === "http:" && LOOPBACK.has(at.hostname)));
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** The same address, a trailing slash aside. */
|
|
50
|
+
function same(a, b) {
|
|
51
|
+
try {
|
|
52
|
+
return new URL(a).href.replace(/\/+$/, "") === new URL(b).href.replace(/\/+$/, "");
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const b64url = (bytes) => bytes.toString("base64url");
|
|
59
|
+
/** A parameter of a WWW-Authenticate challenge, such as resource_metadata or scope. */
|
|
60
|
+
function param(challenge, name) {
|
|
61
|
+
if (!challenge)
|
|
62
|
+
return null;
|
|
63
|
+
return new RegExp(`${name}="([^"]*)"`).exec(challenge)?.[1] ?? null;
|
|
64
|
+
}
|
|
65
|
+
function tokensFrom(answer, now, previousRefresh) {
|
|
66
|
+
const expires = Number(answer.expires_in);
|
|
67
|
+
const refresh = typeof answer.refresh_token === "string" ? answer.refresh_token : previousRefresh;
|
|
68
|
+
return {
|
|
69
|
+
access: String(answer.access_token),
|
|
70
|
+
...(refresh ? { refresh } : {}),
|
|
71
|
+
...(Number.isFinite(expires) && expires > 0 ? { expiresAt: now + expires * 1000 } : {}),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export class McpAuth {
|
|
75
|
+
#keys;
|
|
76
|
+
#redirect;
|
|
77
|
+
#version;
|
|
78
|
+
#fetch;
|
|
79
|
+
#now;
|
|
80
|
+
#pending = new Map();
|
|
81
|
+
#refreshing = new Map();
|
|
82
|
+
#found = new Map();
|
|
83
|
+
constructor(options) {
|
|
84
|
+
this.#keys = options.keys;
|
|
85
|
+
this.#redirect = options.redirectUri;
|
|
86
|
+
this.#version = options.version;
|
|
87
|
+
this.#fetch = options.fetch ?? fetch;
|
|
88
|
+
this.#now = options.now ?? Date.now;
|
|
89
|
+
}
|
|
90
|
+
async #load(server) {
|
|
91
|
+
const raw = await this.#keys.get(server);
|
|
92
|
+
if (!raw)
|
|
93
|
+
return null;
|
|
94
|
+
try {
|
|
95
|
+
return JSON.parse(raw);
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async #save(server, saved) {
|
|
102
|
+
await this.#keys.set(server, JSON.stringify(saved));
|
|
103
|
+
}
|
|
104
|
+
/** Whether Yew holds a sign-in for this server at this address. */
|
|
105
|
+
async signedIn(server, resource) {
|
|
106
|
+
const saved = await this.#load(server);
|
|
107
|
+
return Boolean(saved?.tokens && saved.resource === resource);
|
|
108
|
+
}
|
|
109
|
+
/** A current access token for the server, refreshed shortly before it runs out; null without one. */
|
|
110
|
+
async bearer(server, resource) {
|
|
111
|
+
const saved = await this.#load(server);
|
|
112
|
+
if (!saved?.tokens || saved.resource !== resource)
|
|
113
|
+
return null;
|
|
114
|
+
const { tokens } = saved;
|
|
115
|
+
if (!tokens.expiresAt || tokens.expiresAt - 60_000 > this.#now())
|
|
116
|
+
return tokens.access;
|
|
117
|
+
if (!tokens.refresh)
|
|
118
|
+
return null;
|
|
119
|
+
let running = this.#refreshing.get(server);
|
|
120
|
+
if (!running) {
|
|
121
|
+
running = this.#refresh(server, saved, tokens.refresh).finally(() => this.#refreshing.delete(server));
|
|
122
|
+
this.#refreshing.set(server, running);
|
|
123
|
+
}
|
|
124
|
+
return running;
|
|
125
|
+
}
|
|
126
|
+
async #refresh(server, saved, refresh) {
|
|
127
|
+
const body = new URLSearchParams({
|
|
128
|
+
grant_type: "refresh_token",
|
|
129
|
+
refresh_token: refresh,
|
|
130
|
+
client_id: saved.client.id,
|
|
131
|
+
resource: saved.resource,
|
|
132
|
+
});
|
|
133
|
+
if (saved.client.secret)
|
|
134
|
+
body.set("client_secret", saved.client.secret);
|
|
135
|
+
const answer = await this.#post(saved.endpoints.token, body);
|
|
136
|
+
if (!answer || typeof answer.access_token !== "string") {
|
|
137
|
+
// The sign-in no longer works: the person signs in again.
|
|
138
|
+
const { tokens: _gone, ...rest } = saved;
|
|
139
|
+
await this.#save(server, rest);
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
const tokens = tokensFrom(answer, this.#now(), refresh);
|
|
143
|
+
await this.#save(server, { ...saved, tokens });
|
|
144
|
+
return tokens.access;
|
|
145
|
+
}
|
|
146
|
+
/** The site the person's browser goes to to sign in to a server, for the settings to name. */
|
|
147
|
+
async where(resource, challenge = null) {
|
|
148
|
+
const found = await this.#discovered(resource, challenge);
|
|
149
|
+
return new URL(found.endpoints.authorization).host;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Starts signing in to a server and returns the address the person's browser opens. `challenge` is
|
|
153
|
+
* the server's WWW-Authenticate header from its refusal, when there was one.
|
|
154
|
+
*/
|
|
155
|
+
async begin(server, resource, settings = {}, challenge = null) {
|
|
156
|
+
const found = await this.#discovered(resource, challenge);
|
|
157
|
+
const saved = await this.#load(server);
|
|
158
|
+
let client;
|
|
159
|
+
if (settings.clientId) {
|
|
160
|
+
client = {
|
|
161
|
+
id: settings.clientId,
|
|
162
|
+
...(settings.clientSecret ? { secret: settings.clientSecret } : {}),
|
|
163
|
+
redirectUri: this.#redirect,
|
|
164
|
+
registered: false,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
else if (saved?.client.registered &&
|
|
168
|
+
saved.resource === resource &&
|
|
169
|
+
saved.client.redirectUri === this.#redirect &&
|
|
170
|
+
same(saved.endpoints.issuer, found.endpoints.issuer)) {
|
|
171
|
+
client = saved.client;
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
client = await this.#register(found.endpoints);
|
|
175
|
+
}
|
|
176
|
+
const verifier = b64url(randomBytes(32));
|
|
177
|
+
const state = b64url(randomBytes(24));
|
|
178
|
+
const url = new URL(found.endpoints.authorization);
|
|
179
|
+
const scopes = settings.scopes?.length ? settings.scopes.join(" ") : found.scope;
|
|
180
|
+
const params = {
|
|
181
|
+
response_type: "code",
|
|
182
|
+
client_id: client.id,
|
|
183
|
+
redirect_uri: client.redirectUri,
|
|
184
|
+
code_challenge: b64url(createHash("sha256").update(verifier).digest()),
|
|
185
|
+
code_challenge_method: "S256",
|
|
186
|
+
state,
|
|
187
|
+
resource,
|
|
188
|
+
...(scopes ? { scope: scopes } : {}),
|
|
189
|
+
};
|
|
190
|
+
// Google gives a refresh token only when asked for offline access with consent.
|
|
191
|
+
if (new URL(found.endpoints.issuer).host === "accounts.google.com") {
|
|
192
|
+
params.access_type = "offline";
|
|
193
|
+
params.prompt = "consent";
|
|
194
|
+
}
|
|
195
|
+
for (const [key, value] of Object.entries(params))
|
|
196
|
+
url.searchParams.set(key, value);
|
|
197
|
+
const now = this.#now();
|
|
198
|
+
for (const [key, pending] of this.#pending) {
|
|
199
|
+
if (now - pending.createdAt > PENDING_MS)
|
|
200
|
+
this.#pending.delete(key);
|
|
201
|
+
}
|
|
202
|
+
this.#pending.set(state, {
|
|
203
|
+
server,
|
|
204
|
+
verifier,
|
|
205
|
+
saved: {
|
|
206
|
+
resource,
|
|
207
|
+
endpoints: found.endpoints,
|
|
208
|
+
client,
|
|
209
|
+
...(saved?.tokens && saved.resource === resource ? { tokens: saved.tokens } : {}),
|
|
210
|
+
},
|
|
211
|
+
createdAt: now,
|
|
212
|
+
});
|
|
213
|
+
return url.toString();
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* The browser came back with a code: trade it for tokens. `iss` is the sign-in server's name in the
|
|
217
|
+
* return, when it gave one. Resolves with the server's name.
|
|
218
|
+
*/
|
|
219
|
+
async complete(state, code, error, iss = null) {
|
|
220
|
+
const pending = this.#pending.get(state);
|
|
221
|
+
this.#pending.delete(state);
|
|
222
|
+
if (!pending || this.#now() - pending.createdAt > PENDING_MS) {
|
|
223
|
+
throw new Error("This sign-in is not valid any more. Start it again from Yew.");
|
|
224
|
+
}
|
|
225
|
+
const { saved } = pending;
|
|
226
|
+
// RFC 9207: a return that names a sign-in server, or that should and does not, comes from the
|
|
227
|
+
// one the browser was sent to, or it is not used.
|
|
228
|
+
if (iss !== null ? !same(iss, saved.endpoints.issuer) : saved.endpoints.namesItself) {
|
|
229
|
+
throw new Error("The sign-in came back from another server than the one Yew sent you to.");
|
|
230
|
+
}
|
|
231
|
+
if (error || !code)
|
|
232
|
+
throw new Error(`The sign-in was not completed (${error ?? "no code"}).`);
|
|
233
|
+
const body = new URLSearchParams({
|
|
234
|
+
grant_type: "authorization_code",
|
|
235
|
+
code,
|
|
236
|
+
redirect_uri: saved.client.redirectUri,
|
|
237
|
+
client_id: saved.client.id,
|
|
238
|
+
code_verifier: pending.verifier,
|
|
239
|
+
resource: saved.resource,
|
|
240
|
+
});
|
|
241
|
+
if (saved.client.secret)
|
|
242
|
+
body.set("client_secret", saved.client.secret);
|
|
243
|
+
const answer = await this.#post(saved.endpoints.token, body);
|
|
244
|
+
if (!answer || typeof answer.access_token !== "string") {
|
|
245
|
+
throw new Error("The server did not give Yew a token.");
|
|
246
|
+
}
|
|
247
|
+
await this.#save(pending.server, { ...saved, tokens: tokensFrom(answer, this.#now()) });
|
|
248
|
+
return pending.server;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Signs out: asks the server to void the tokens where it says how (RFC 7009), then forgets them and
|
|
252
|
+
* the registered client. Whether the server voids them or not, Yew keeps nothing.
|
|
253
|
+
*/
|
|
254
|
+
async forget(server) {
|
|
255
|
+
const saved = await this.#load(server);
|
|
256
|
+
const revocation = saved?.endpoints.revocation;
|
|
257
|
+
if (saved?.tokens && revocation) {
|
|
258
|
+
const tokens = [
|
|
259
|
+
[saved.tokens.refresh, "refresh_token"],
|
|
260
|
+
[saved.tokens.access, "access_token"],
|
|
261
|
+
];
|
|
262
|
+
for (const [token, hint] of tokens) {
|
|
263
|
+
if (!token)
|
|
264
|
+
continue;
|
|
265
|
+
const body = new URLSearchParams({
|
|
266
|
+
token,
|
|
267
|
+
token_type_hint: hint,
|
|
268
|
+
client_id: saved.client.id,
|
|
269
|
+
});
|
|
270
|
+
if (saved.client.secret)
|
|
271
|
+
body.set("client_secret", saved.client.secret);
|
|
272
|
+
await this.#post(revocation, body, REVOKE_MS);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
await this.#keys.delete(server);
|
|
276
|
+
}
|
|
277
|
+
/** A GET for details; it may follow a redirect, but only to an address Yew would use. */
|
|
278
|
+
async #json(url, local) {
|
|
279
|
+
try {
|
|
280
|
+
const response = await this.#fetch(url, {
|
|
281
|
+
headers: { Accept: "application/json", "User-Agent": `yew/${this.#version}` },
|
|
282
|
+
signal: AbortSignal.timeout(REQUEST_MS),
|
|
283
|
+
});
|
|
284
|
+
if (!response.ok || !usable(response.url || url, local))
|
|
285
|
+
return null;
|
|
286
|
+
return (await response.json());
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* A POST that carries a code, a verifier, a secret or a token. A redirect is not followed: fetch
|
|
294
|
+
* would send the whole body on to the new address, whichever site that is.
|
|
295
|
+
*/
|
|
296
|
+
async #post(url, body, ms = REQUEST_MS) {
|
|
297
|
+
try {
|
|
298
|
+
const response = await this.#fetch(url, {
|
|
299
|
+
method: "POST",
|
|
300
|
+
redirect: "error",
|
|
301
|
+
headers: {
|
|
302
|
+
"Content-Type": typeof body === "string" ? "application/json" : "application/x-www-form-urlencoded",
|
|
303
|
+
Accept: "application/json",
|
|
304
|
+
"User-Agent": `yew/${this.#version}`,
|
|
305
|
+
},
|
|
306
|
+
body,
|
|
307
|
+
signal: AbortSignal.timeout(ms),
|
|
308
|
+
});
|
|
309
|
+
if (!response.ok)
|
|
310
|
+
return null;
|
|
311
|
+
return (await response.json());
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Where to sign in, found out once and used again for a while: the site the settings name is the
|
|
319
|
+
* one the browser then goes to.
|
|
320
|
+
*/
|
|
321
|
+
#discovered(resource, challenge) {
|
|
322
|
+
const key = `${resource}\n${challenge ?? ""}`;
|
|
323
|
+
const now = this.#now();
|
|
324
|
+
const kept = this.#found.get(key);
|
|
325
|
+
if (kept && now - kept.at < FOUND_MS)
|
|
326
|
+
return kept.value;
|
|
327
|
+
const value = this.#discover(resource, challenge);
|
|
328
|
+
this.#found.set(key, { at: now, value });
|
|
329
|
+
value.catch(() => {
|
|
330
|
+
if (this.#found.get(key)?.value === value)
|
|
331
|
+
this.#found.delete(key);
|
|
332
|
+
});
|
|
333
|
+
return value;
|
|
334
|
+
}
|
|
335
|
+
/** Where to sign in for a server, from its protected-resource metadata and its issuer's metadata. */
|
|
336
|
+
async #discover(resource, challenge) {
|
|
337
|
+
const at = new URL(resource);
|
|
338
|
+
const local = at.protocol === "http:" && LOOPBACK.has(at.hostname);
|
|
339
|
+
if (!usable(resource, local)) {
|
|
340
|
+
throw new Error("Yew signs in only to servers at https addresses.");
|
|
341
|
+
}
|
|
342
|
+
const named = param(challenge, "resource_metadata");
|
|
343
|
+
if (named !== null && !usable(named, local)) {
|
|
344
|
+
throw new Error(`The server pointed to an address that is not https (${named}).`);
|
|
345
|
+
}
|
|
346
|
+
const path = at.pathname === "/" ? "" : at.pathname;
|
|
347
|
+
const metadataUrls = [
|
|
348
|
+
named,
|
|
349
|
+
`${at.origin}/.well-known/oauth-protected-resource${path}`,
|
|
350
|
+
`${at.origin}/.well-known/oauth-protected-resource`,
|
|
351
|
+
].filter((url) => Boolean(url));
|
|
352
|
+
let metadata = null;
|
|
353
|
+
for (const url of metadataUrls) {
|
|
354
|
+
metadata = await this.#json(url, local);
|
|
355
|
+
if (metadata)
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
// RFC 9728, 3.3: details about another address are not used.
|
|
359
|
+
if (metadata && !(typeof metadata.resource === "string" && same(metadata.resource, resource))) {
|
|
360
|
+
throw new Error(`The server's sign-in details are for another address (${String(metadata.resource)}).`);
|
|
361
|
+
}
|
|
362
|
+
const servers = metadata?.authorization_servers;
|
|
363
|
+
const issuer = Array.isArray(servers) && typeof servers[0] === "string" ? servers[0] : at.origin;
|
|
364
|
+
if (!usable(issuer, local)) {
|
|
365
|
+
throw new Error(`The server named a sign-in server that is not at an https address (${issuer}).`);
|
|
366
|
+
}
|
|
367
|
+
const base = new URL(issuer);
|
|
368
|
+
const issuerPath = base.pathname === "/" ? "" : base.pathname.replace(/\/$/, "");
|
|
369
|
+
const candidates = [
|
|
370
|
+
`${base.origin}/.well-known/oauth-authorization-server${issuerPath}`,
|
|
371
|
+
`${base.origin}/.well-known/openid-configuration${issuerPath}`,
|
|
372
|
+
`${base.origin}${issuerPath}/.well-known/openid-configuration`,
|
|
373
|
+
];
|
|
374
|
+
let problem = "The server did not say where to sign in.";
|
|
375
|
+
for (const url of candidates) {
|
|
376
|
+
const found = await this.#json(url, local);
|
|
377
|
+
if (!found ||
|
|
378
|
+
typeof found.authorization_endpoint !== "string" ||
|
|
379
|
+
typeof found.token_endpoint !== "string") {
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
// RFC 8414, 3.3: the details are those of the sign-in server Yew asked about.
|
|
383
|
+
if (typeof found.issuer !== "string" || !same(found.issuer, issuer)) {
|
|
384
|
+
problem = `The sign-in server's details are for another server (${String(found.issuer)}).`;
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
// The MCP authorization spec: no sign-in without PKCE with S256.
|
|
388
|
+
const methods = found.code_challenge_methods_supported;
|
|
389
|
+
if (!Array.isArray(methods) || !methods.includes("S256")) {
|
|
390
|
+
problem =
|
|
391
|
+
"The sign-in server does not say it supports PKCE with S256, which signing in needs.";
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
const addresses = [
|
|
395
|
+
found.authorization_endpoint,
|
|
396
|
+
found.token_endpoint,
|
|
397
|
+
found.registration_endpoint,
|
|
398
|
+
found.revocation_endpoint,
|
|
399
|
+
].filter((value) => typeof value === "string");
|
|
400
|
+
const unusable = addresses.find((address) => !usable(address, local));
|
|
401
|
+
if (unusable !== undefined) {
|
|
402
|
+
problem = `The sign-in server named an address that is not https (${unusable}).`;
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
return {
|
|
406
|
+
endpoints: {
|
|
407
|
+
issuer: found.issuer,
|
|
408
|
+
authorization: found.authorization_endpoint,
|
|
409
|
+
token: found.token_endpoint,
|
|
410
|
+
...(typeof found.registration_endpoint === "string"
|
|
411
|
+
? { registration: found.registration_endpoint }
|
|
412
|
+
: {}),
|
|
413
|
+
...(typeof found.revocation_endpoint === "string"
|
|
414
|
+
? { revocation: found.revocation_endpoint }
|
|
415
|
+
: {}),
|
|
416
|
+
...(found.authorization_response_iss_parameter_supported === true
|
|
417
|
+
? { namesItself: true }
|
|
418
|
+
: {}),
|
|
419
|
+
},
|
|
420
|
+
scope: param(challenge, "scope"),
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
throw new Error(problem);
|
|
424
|
+
}
|
|
425
|
+
/** Registers Yew as a public client of the authorization server (RFC 7591). */
|
|
426
|
+
async #register(endpoints) {
|
|
427
|
+
if (!endpoints.registration) {
|
|
428
|
+
throw new Error("This server only accepts clients registered in advance. Add its client ID and secret to its entry in mcp.json.");
|
|
429
|
+
}
|
|
430
|
+
const answer = await this.#post(endpoints.registration, JSON.stringify({
|
|
431
|
+
client_name: "Yew",
|
|
432
|
+
redirect_uris: [this.#redirect],
|
|
433
|
+
grant_types: ["authorization_code", "refresh_token"],
|
|
434
|
+
response_types: ["code"],
|
|
435
|
+
token_endpoint_auth_method: "none",
|
|
436
|
+
software_version: this.#version,
|
|
437
|
+
}));
|
|
438
|
+
if (!answer || typeof answer.client_id !== "string") {
|
|
439
|
+
throw new Error("The server did not let Yew register to sign in.");
|
|
440
|
+
}
|
|
441
|
+
return {
|
|
442
|
+
id: answer.client_id,
|
|
443
|
+
...(typeof answer.client_secret === "string" ? { secret: answer.client_secret } : {}),
|
|
444
|
+
redirectUri: this.#redirect,
|
|
445
|
+
registered: true,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
}
|