Add WWW-Authenticate challenge to Orbit MCP 401 responses

What does this MR do and why?

Applies the same RFC 9728 §5.1 WWW-Authenticate fix to the Orbit MCP endpoint (POST /api/v4/orbit/mcp), which has the identical missing-header gap as /api/v4/mcp and already has its own protected-resource metadata route.

Unauthenticated requests now advertise:

WWW-Authenticate: Bearer realm="GitLab", resource_metadata="<gitlab_url>/.well-known/oauth-protected-resource/api/v4/orbit/mcp"

How

Wires the shared API::Helpers::Mcp::AuthChallenge helper (introduced in the companion MR) into API::Orbit::Mcp. The metadata URL is derived from the request path, so no per-endpoint configuration is needed.

Dependency

Depends on !243210 (merged), which introduces the shared helper. This branch is based on that MR's branch; once it merges to master, this MR's diff will show only the Orbit changes. Please merge !243210 (merged) first.

References

Manual verification (GDK)

The 401 is raised before any feature-flag/licence check (authenticate! runs first in the before block), so an unauthenticated request is enough — no MCP feature setup is required. $GDK is your GDK base URL (e.g. http://gdk.test:3000).

Reproduce the bug (on master, before this MR):

curl -si -X POST "$GDK/api/v4/orbit/mcp" \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json, text/event-stream' \
  -d '{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"probe","version":"1"}},"id":1}' \
  | grep -iE '^HTTP/|^www-authenticate'

Output on master — 401 with no WWW-Authenticate header:

HTTP/1.1 401 Unauthorized

With this MR the same request returns the RFC 9728 challenge:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="GitLab", resource_metadata="$GDK/.well-known/oauth-protected-resource/api/v4/orbit/mcp"

Confirm the advertised metadata URL resolves (already served on master):

curl -s "$GDK/.well-known/oauth-protected-resource/api/v4/orbit/mcp" | jq

Returns the resource metadata JSON (resource, authorization_servers, scopes_supported).

Automated coverage: the request specs assert the exact header value on both the POST and GET unauthenticated paths.

RFC 9728 §5.1 compliance

Per RFC 9728 §5.1, a 401 from a protected resource advertises the metadata location via the resource_metadata parameter in WWW-Authenticate. This MR emits:

WWW-Authenticate: Bearer realm="GitLab", resource_metadata="<gitlab_url>/.well-known/oauth-protected-resource/api/v4/orbit/mcp"
  • resource_metadata — quoted URL of the protected-resource metadata ✅ (the parameter introduced by §5.1)
  • realm — optional per §5.1 (governed by RFC 6750); included for clarity ✅
  • Metadata URL uses the §3 / §3.1 path-insertion form — the well-known segment inserted between host and resource path ✅ — matching the route already registered in config/routes.rb.

Subpath (relative URL root) install

For an install on a relative URL root (e.g. external_url 'https://git.example.org/gitlab'), request.path includes the root (/gitlab/api/v4/orbit/mcp) and Gitlab.config.gitlab.url already contains it, so the helper strips the root to avoid duplicating it. Expected header:

WWW-Authenticate: Bearer realm="GitLab", resource_metadata="https://git.example.org/gitlab/.well-known/oauth-protected-resource/api/v4/orbit/mcp"

The /gitlab prefix must appear exactly once. Covered by the unit spec spec/lib/api/helpers/mcp/auth_challenge_spec.rb.

Edited by Niklas Janz

Merge request reports

Loading
Loading