## Motivation and Context See https://github.com/modelcontextprotocol/registry/issues/482#issuecomment-3436993157 for full details. This adds ECDSA P-384 (a popular NIST approved curve) in addition to Ed25519. ## How Has This Been Tested? I have tested with a local deployment against `mcp.joelverhagen.com`. Also, I have added significant tests. ## Breaking Changes None. (hopefully) ## 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) - [x] 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 ## Additional context <!-- Add any other context, implementation notes, or design decisions --> @domdomegg and I spoke a bit about this on Discord: https://discord.com/channels/1358869848138059966/1369487942862504016/1423710918180606052 --------- Co-authored-by: adam jones <domdomegg+git@gmail.com>
122 lines
3.7 KiB
Go
122 lines
3.7 KiB
Go
package commands
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/modelcontextprotocol/registry/cmd/publisher/auth"
|
|
)
|
|
|
|
const (
|
|
DefaultRegistryURL = "https://registry.modelcontextprotocol.io"
|
|
TokenFileName = ".mcp_publisher_token" //nolint:gosec // Not a credential, just a filename
|
|
)
|
|
|
|
type CryptoAlgorithm auth.CryptoAlgorithm
|
|
|
|
func (c *CryptoAlgorithm) String() string {
|
|
return string(*c)
|
|
}
|
|
|
|
func (c *CryptoAlgorithm) Set(v string) error {
|
|
switch v {
|
|
case string(auth.AlgorithmEd25519), string(auth.AlgorithmECDSAP384):
|
|
*c = CryptoAlgorithm(v)
|
|
return nil
|
|
}
|
|
return fmt.Errorf("invalid algorithm: %q (allowed: ed25519, ecdsap384)", v)
|
|
}
|
|
|
|
func LoginCommand(args []string) error {
|
|
if len(args) < 1 {
|
|
return errors.New("authentication method required\n\nUsage: mcp-publisher login <method>\n\nMethods:\n github Interactive GitHub authentication\n github-oidc GitHub Actions OIDC authentication\n dns DNS-based authentication (requires --domain and --private-key)\n http HTTP-based authentication (requires --domain and --private-key)\n none Anonymous authentication (for testing)")
|
|
}
|
|
|
|
method := args[0]
|
|
|
|
// Parse remaining flags based on method
|
|
loginFlags := flag.NewFlagSet("login", flag.ExitOnError)
|
|
var domain string
|
|
var privateKey string
|
|
var cryptoAlgorithm = CryptoAlgorithm(auth.AlgorithmEd25519)
|
|
var registryURL string
|
|
|
|
loginFlags.StringVar(®istryURL, "registry", DefaultRegistryURL, "Registry URL")
|
|
|
|
if method == "dns" || method == "http" {
|
|
loginFlags.StringVar(&domain, "domain", "", "Domain name")
|
|
loginFlags.StringVar(&privateKey, "private-key", "", "Private key (hex)")
|
|
loginFlags.Var(&cryptoAlgorithm, "algorithm", "Cryptographic algorithm (ed25519, ecdsap384)")
|
|
}
|
|
|
|
if err := loginFlags.Parse(args[1:]); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create auth provider based on method
|
|
var authProvider auth.Provider
|
|
switch method {
|
|
case "github":
|
|
authProvider = auth.NewGitHubATProvider(true, registryURL)
|
|
case "github-oidc":
|
|
authProvider = auth.NewGitHubOIDCProvider(registryURL)
|
|
case "dns":
|
|
if domain == "" || privateKey == "" {
|
|
return errors.New("dns authentication requires --domain and --private-key")
|
|
}
|
|
authProvider = auth.NewDNSProvider(registryURL, domain, privateKey, auth.CryptoAlgorithm(cryptoAlgorithm))
|
|
case "http":
|
|
if domain == "" || privateKey == "" {
|
|
return errors.New("http authentication requires --domain and --private-key")
|
|
}
|
|
authProvider = auth.NewHTTPProvider(registryURL, domain, privateKey, auth.CryptoAlgorithm(cryptoAlgorithm))
|
|
case "none":
|
|
authProvider = auth.NewNoneProvider(registryURL)
|
|
default:
|
|
return fmt.Errorf("unknown authentication method: %s\nFor a list of available methods, run: mcp-publisher login", method)
|
|
}
|
|
|
|
// Perform login
|
|
ctx := context.Background()
|
|
_, _ = fmt.Fprintf(os.Stdout, "Logging in with %s...\n", method)
|
|
|
|
if err := authProvider.Login(ctx); err != nil {
|
|
return fmt.Errorf("login failed: %w", err)
|
|
}
|
|
|
|
// Get and save token
|
|
token, err := authProvider.GetToken(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get token: %w", err)
|
|
}
|
|
|
|
// Save token to file
|
|
homeDir, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get home directory: %w", err)
|
|
}
|
|
|
|
tokenPath := filepath.Join(homeDir, TokenFileName)
|
|
tokenData := map[string]string{
|
|
"token": token,
|
|
"method": method,
|
|
"registry": registryURL,
|
|
}
|
|
|
|
jsonData, err := json.Marshal(tokenData)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal token data: %w", err)
|
|
}
|
|
|
|
if err := os.WriteFile(tokenPath, jsonData, 0600); err != nil {
|
|
return fmt.Errorf("failed to save token: %w", err)
|
|
}
|
|
|
|
_, _ = fmt.Fprintln(os.Stdout, "✓ Successfully logged in")
|
|
return nil
|
|
}
|