<!-- Provide a brief summary of your changes --> ## Motivation and Context Currently the `dns` and `http` based auth flows share code well in the publisher tool. However there is a lot of duplication in the auth endpoint. This on it's own is not a problem but it does make enhancements to the proof record (the `v=MCPv1; ...` string) harder to keep in sync. This PR adds a `common.go` file to share logic between the two. This supports work I am prototyping for https://github.com/modelcontextprotocol/registry/issues/482. ## How Has This Been Tested? Added new test cases for the permissions. Ran them before and after the refactoring to ensure no behavior change. Ran existing UTs. Ran the integration tests. Manually exercised the flows locally. ## Breaking Changes None. ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [x] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [x] My code follows the repository's style guidelines - [x] New and existing tests pass locally - [x] I have added appropriate error handling - [ ] I have added or updated documentation as needed ## Additional context <!-- Add any other context, implementation notes, or design decisions -->
146 lines
4.8 KiB
Go
146 lines
4.8 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0"
|
|
"github.com/modelcontextprotocol/registry/internal/auth"
|
|
"github.com/modelcontextprotocol/registry/internal/config"
|
|
)
|
|
|
|
// MaxKeyResponseSize is the maximum size of the response body from the HTTP endpoint.
|
|
const MaxKeyResponseSize = 4096
|
|
|
|
// HTTPTokenExchangeInput represents the input for HTTP-based authentication
|
|
type HTTPTokenExchangeInput struct {
|
|
Body SignatureTokenExchangeInput
|
|
}
|
|
|
|
// HTTPKeyFetcher defines the interface for fetching HTTP keys
|
|
type HTTPKeyFetcher interface {
|
|
FetchKey(ctx context.Context, domain string) (string, error)
|
|
}
|
|
|
|
// DefaultHTTPKeyFetcher uses Go's standard HTTP client
|
|
type DefaultHTTPKeyFetcher struct {
|
|
client *http.Client
|
|
}
|
|
|
|
// NewDefaultHTTPKeyFetcher creates a new HTTP key fetcher with timeout
|
|
func NewDefaultHTTPKeyFetcher() *DefaultHTTPKeyFetcher {
|
|
return &DefaultHTTPKeyFetcher{
|
|
client: &http.Client{
|
|
Timeout: 10 * time.Second,
|
|
// Disable redirects for security purposes:
|
|
// Prevents people doing weird things like sending us to internal endpoints at different paths
|
|
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
|
|
return http.ErrUseLastResponse
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// NewDefaultHTTPKeyFetcherWithClient creates a new HTTP key fetcher with a custom HTTP client.
|
|
// This is primarily useful in tests to inject transports or TLS settings.
|
|
func NewDefaultHTTPKeyFetcherWithClient(client *http.Client) *DefaultHTTPKeyFetcher {
|
|
return &DefaultHTTPKeyFetcher{client: client}
|
|
}
|
|
|
|
// FetchKey fetches the public key from the well-known HTTP endpoint
|
|
func (f *DefaultHTTPKeyFetcher) FetchKey(ctx context.Context, domain string) (string, error) {
|
|
url := fmt.Sprintf("https://%s/.well-known/mcp-registry-auth", domain)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to create request: %w", err)
|
|
}
|
|
|
|
req.Header.Set("Accept", "text/plain")
|
|
req.Header.Set("User-Agent", "mcp-registry/1.0")
|
|
|
|
resp, err := f.client.Do(req)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to fetch key: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return "", fmt.Errorf("HTTP %d: failed to fetch key from %s", resp.StatusCode, url)
|
|
}
|
|
|
|
// Limit response size to prevent DoS attacks.
|
|
// Read up to MaxKeyResponseSize+1 and error if exceeded.
|
|
limited := io.LimitReader(resp.Body, MaxKeyResponseSize+1)
|
|
body, err := io.ReadAll(limited)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to read response body: %w", err)
|
|
}
|
|
if len(body) > MaxKeyResponseSize {
|
|
return "", fmt.Errorf("HTTP auth key response too large")
|
|
}
|
|
|
|
return strings.TrimSpace(string(body)), nil
|
|
}
|
|
|
|
// HTTPAuthHandler handles HTTP-based authentication
|
|
type HTTPAuthHandler struct {
|
|
CoreAuthHandler
|
|
fetcher HTTPKeyFetcher
|
|
}
|
|
|
|
// NewHTTPAuthHandler creates a new HTTP authentication handler
|
|
func NewHTTPAuthHandler(cfg *config.Config) *HTTPAuthHandler {
|
|
return &HTTPAuthHandler{
|
|
CoreAuthHandler: *NewCoreAuthHandler(cfg),
|
|
fetcher: NewDefaultHTTPKeyFetcher(),
|
|
}
|
|
}
|
|
|
|
// SetFetcher sets a custom HTTP key fetcher (used for testing)
|
|
func (h *HTTPAuthHandler) SetFetcher(fetcher HTTPKeyFetcher) {
|
|
h.fetcher = fetcher
|
|
}
|
|
|
|
// RegisterHTTPEndpoint registers the HTTP authentication endpoint
|
|
func RegisterHTTPEndpoint(api huma.API, cfg *config.Config) {
|
|
handler := NewHTTPAuthHandler(cfg)
|
|
|
|
// HTTP authentication endpoint
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "exchange-http-token",
|
|
Method: http.MethodPost,
|
|
Path: "/v0/auth/http",
|
|
Summary: "Exchange HTTP signature for Registry JWT",
|
|
Description: "Authenticate using HTTP-hosted public key and signed timestamp",
|
|
Tags: []string{"auth"},
|
|
}, func(ctx context.Context, input *HTTPTokenExchangeInput) (*v0.Response[auth.TokenResponse], error) {
|
|
response, err := handler.ExchangeToken(ctx, input.Body.Domain, input.Body.Timestamp, input.Body.SignedTimestamp)
|
|
if err != nil {
|
|
return nil, huma.Error401Unauthorized("HTTP authentication failed", err)
|
|
}
|
|
|
|
return &v0.Response[auth.TokenResponse]{
|
|
Body: *response,
|
|
}, nil
|
|
})
|
|
}
|
|
|
|
// ExchangeToken exchanges HTTP signature for a Registry JWT token
|
|
func (h *HTTPAuthHandler) ExchangeToken(ctx context.Context, domain, timestamp, signedTimestamp string) (*auth.TokenResponse, error) {
|
|
keyFetcher := func(ctx context.Context, domain string) ([]string, error) {
|
|
keyResponse, err := h.fetcher.FetchKey(ctx, domain)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to fetch public key: %w", err)
|
|
}
|
|
return []string{keyResponse}, nil
|
|
}
|
|
|
|
allowSubdomains := false
|
|
return h.CoreAuthHandler.ExchangeToken(ctx, domain, timestamp, signedTimestamp, keyFetcher, allowSubdomains, auth.MethodHTTP)
|
|
}
|