@mcpfn/auth 0.0.6 → 0.0.7

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 CHANGED
@@ -91,3 +91,39 @@ containing only that exact Bearer token. An AuthProvider's `authorize()` hook
91
91
  receives the normalized MCP resource URL before trusted context is created. `auth-diagnose` and
92
92
  `diagnoseMcpAuthorization()` perform read-only discovery without browser or
93
93
  credential actions.
94
+
95
+ ### Authorization endpoint errors
96
+
97
+ Unknown clients and unregistered `redirect_uri` values always get a JSON error
98
+ from the authorization server, never a redirect. Once the `redirect_uri`
99
+ matches, later validation errors (for example a missing `code_challenge` or
100
+ `state`, an unsupported `response_type`, or a disallowed `resource`) are
101
+ redirected to that URI with `error`, `error_description`, and `state` (echoed
102
+ only when the request supplied a single valid `state`), only for clients whose
103
+ `source` is listed in
104
+ `authorizationErrors.redirectSources`. The default is
105
+ `["pre-registered", "enterprise"]`, the clients whose redirect URIs the operator
106
+ provisioned.
107
+
108
+ Dynamically registered and Client ID Metadata Document clients choose their
109
+ own redirect URIs without authentication, so redirecting their errors would
110
+ make the issuer an open redirector (RFC 9700 §4.11.2). For them the endpoint
111
+ returns an HTTP 400 `text/html` page with `cache-control: no-store`, showing
112
+ the HTML-escaped OAuth error code and description, with no link to the
113
+ redirect URI and no reflected `state`. `renderMcpAuthorizationErrorPage(error)`
114
+ builds that default page.
115
+
116
+ ```ts
117
+ createMcpAuthorizationCompatibilityHandler({
118
+ // ...
119
+ authorizationErrors: {
120
+ // Optional: replace the default page for non-redirected errors.
121
+ render: ({ error, client, request }) => renderHostErrorPage(error, client, request),
122
+ },
123
+ });
124
+ ```
125
+
126
+ Since 0.0.7 this is the default; earlier versions redirected errors for every
127
+ client source. To restore that behaviour, pass all four sources:
128
+ `authorizationErrors: { redirectSources: ["pre-registered", "enterprise",
129
+ "dynamic", "client-metadata-document"] }`.
package/dist/index.cjs CHANGED
@@ -6201,8 +6201,19 @@ function normalizeAuthorizationResource(value, allowedResources) {
6201
6201
  }
6202
6202
  return resource;
6203
6203
  }
6204
+ var DEFAULT_AUTHORIZATION_ERROR_REDIRECT_SOURCES = [
6205
+ "pre-registered",
6206
+ "enterprise"
6207
+ ];
6208
+ var CLIENT_REGISTRATION_SOURCES = /* @__PURE__ */ new Set([
6209
+ "pre-registered",
6210
+ "dynamic",
6211
+ "client-metadata-document",
6212
+ "enterprise"
6213
+ ]);
6204
6214
  function createMcpAuthorizationCompatibilityHandler(options) {
6205
6215
  validateClientMetadataDocumentOptions(options.clientMetadataDocuments);
6216
+ validateAuthorizationErrorOptions(options.authorizationErrors);
6206
6217
  const issuer = normalizeIssuer(options.issuer, options.allowInsecureLoopbackIssuer ?? false);
6207
6218
  const rfc8414IssuerPath = issuer.pathname === "/" ? "" : issuer.pathname;
6208
6219
  const issuerPath = issuer.pathname === "/" ? "" : trimTrailingSlashes(issuer.pathname);
@@ -6444,7 +6455,7 @@ async function handleAuthorizationRequest(options, url2, request) {
6444
6455
  clientId,
6445
6456
  registrationSource: client.source
6446
6457
  });
6447
- return trustedAuthorizationErrorRedirect(trustedRedirect, url2, error2);
6458
+ return authorizationErrorResponse(options, client, trustedRedirect, url2, request, error2);
6448
6459
  }
6449
6460
  await emit(options, "authorization-request", "succeeded", void 0, {
6450
6461
  clientId,
@@ -6453,6 +6464,55 @@ async function handleAuthorizationRequest(options, url2, request) {
6453
6464
  });
6454
6465
  return options.authorize(validated, request);
6455
6466
  }
6467
+ function validateAuthorizationErrorOptions(options) {
6468
+ if (options === void 0) return;
6469
+ const sources = options.redirectSources;
6470
+ if (sources !== void 0 && (!Array.isArray(sources) || sources.some((source) => !CLIENT_REGISTRATION_SOURCES.has(source)))) {
6471
+ throw new TypeError(
6472
+ "authorizationErrors.redirectSources must contain only client registration sources"
6473
+ );
6474
+ }
6475
+ if (options.render !== void 0 && typeof options.render !== "function") {
6476
+ throw new TypeError("authorizationErrors.render must be a function");
6477
+ }
6478
+ }
6479
+ async function authorizationErrorResponse(options, client, redirectUri, authorizationUrl, request, error2) {
6480
+ const errors = options.authorizationErrors;
6481
+ const redirectSources = errors?.redirectSources ?? DEFAULT_AUTHORIZATION_ERROR_REDIRECT_SOURCES;
6482
+ if (redirectSources.includes(client.source)) {
6483
+ return trustedAuthorizationErrorRedirect(redirectUri, authorizationUrl, error2);
6484
+ }
6485
+ if (errors?.render) return errors.render({ error: error2, client, request });
6486
+ return renderMcpAuthorizationErrorPage(error2);
6487
+ }
6488
+ function renderMcpAuthorizationErrorPage(error2) {
6489
+ const body = [
6490
+ "<!doctype html>",
6491
+ '<html lang="en">',
6492
+ '<head><meta charset="utf-8"><meta name="robots" content="noindex">',
6493
+ "<title>Authorization request failed</title></head>",
6494
+ "<body>",
6495
+ "<h1>Authorization request failed</h1>",
6496
+ `<p>Error: <code>${escapeHtml(error2.code)}</code></p>`,
6497
+ `<p>${escapeHtml(error2.message)}</p>`,
6498
+ "</body>",
6499
+ "</html>"
6500
+ ].join("\n");
6501
+ return new Response(body, {
6502
+ status: 400,
6503
+ headers: {
6504
+ "cache-control": "no-store",
6505
+ pragma: "no-cache",
6506
+ "content-type": "text/html; charset=utf-8",
6507
+ "content-security-policy": "default-src 'none'; frame-ancestors 'none'",
6508
+ "referrer-policy": "no-referrer",
6509
+ "x-content-type-options": "nosniff"
6510
+ }
6511
+ });
6512
+ }
6513
+ function escapeHtml(value) {
6514
+ return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;").replaceAll("'", "&#39;");
6515
+ }
6456
6516
  function trustedAuthorizationErrorRedirect(redirectUri, authorizationUrl, error2) {
6457
6517
  const redirect = new URL(redirectUri);
6458
6518
  redirect.searchParams.set("error", error2.code);
@@ -7728,6 +7788,7 @@ exports.normalizeMcpClientRegistration = normalizeMcpClientRegistration;
7728
7788
  exports.normalizeMcpRedirectUri = normalizeMcpRedirectUri;
7729
7789
  exports.protectedResourceMetadataUrl = protectedResourceMetadataUrl;
7730
7790
  exports.readBearerToken = readBearerToken;
7791
+ exports.renderMcpAuthorizationErrorPage = renderMcpAuthorizationErrorPage;
7731
7792
  exports.validateMcpAuthorizationRequest = validateMcpAuthorizationRequest;
7732
7793
  exports.withEnterpriseManagedAuthorization = withEnterpriseManagedAuthorization;
7733
7794
  //# sourceMappingURL=index.cjs.map