registry/internal/api/handlers/v0/auth/none.go
adam jones 12ab632d32
auth: Add GitHub OIDC auth (#278)
Adds support for auth'ing to the registry via [GitHub Actions
OIDC](https://docs.github.com/en/actions/concepts/security/openid-connect).

Fixes #271

## Motivation and Context

People will probably want to publish to the registry from CI
environments. DNS auth can work here fine
(https://github.com/modelcontextprotocol/registry/issues/270). But for
people who haven't set up DNS, the standard GitHub device flow won't
really work here. We could make this nice by supporting GitHub Actions
OIDC.

## How Has This Been Tested?

Tested locally with a real GitHub OIDC token from a personal repo.

## 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
- [x] I have added or updated documentation as needed
2025-08-18 22:05:06 +01:00

81 lines
2.3 KiB
Go

package auth
import (
"context"
"fmt"
"net/http"
"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"
"github.com/modelcontextprotocol/registry/internal/model"
)
// NoneHandler handles anonymous authentication
type NoneHandler struct {
config *config.Config
jwtManager *auth.JWTManager
}
// NewNoneHandler creates a new anonymous authentication handler
func NewNoneHandler(cfg *config.Config) *NoneHandler {
return &NoneHandler{
config: cfg,
jwtManager: auth.NewJWTManager(cfg),
}
}
// RegisterNoneEndpoint registers the anonymous authentication endpoint
func RegisterNoneEndpoint(api huma.API, cfg *config.Config) {
if !cfg.EnableAnonymousAuth {
return
}
handler := NewNoneHandler(cfg)
// Anonymous token endpoint
huma.Register(api, huma.Operation{
OperationID: "get-anonymous-token",
Method: http.MethodPost,
Path: "/v0/auth/none",
Summary: "Get anonymous Registry JWT",
Description: "Get a short-lived Registry JWT token for publishing to io.modelcontextprotocol.anonymous/* namespace",
Tags: []string{"auth"},
}, func(ctx context.Context, _ *struct{}) (*v0.Response[auth.TokenResponse], error) {
response, err := handler.GetAnonymousToken(ctx)
if err != nil {
return nil, huma.Error500InternalServerError("Failed to generate token", err)
}
return &v0.Response[auth.TokenResponse]{
Body: *response,
}, nil
})
}
// GetAnonymousToken generates an anonymous Registry JWT token
func (h *NoneHandler) GetAnonymousToken(ctx context.Context) (*auth.TokenResponse, error) {
// Build permissions for anonymous namespace only
permissions := []auth.Permission{
{
Action: auth.PermissionActionPublish,
ResourcePattern: "io.modelcontextprotocol.anonymous/*",
},
}
// Create JWT claims for anonymous user
claims := auth.JWTClaims{
AuthMethod: model.AuthMethodNone,
AuthMethodSubject: "anonymous",
Permissions: permissions,
}
// Generate Registry JWT token
tokenResponse, err := h.jwtManager.GenerateTokenResponse(ctx, claims)
if err != nil {
return nil, fmt.Errorf("failed to generate JWT token: %w", err)
}
return tokenResponse, nil
}