registry/cmd/publisher/commands/login_test.go
Radoslav Dimitrov 90baa0528f
fix: store tokens in ~/.config/mcp-publisher/ instead of cwd (#1166)
## Summary

Fixes #663

- **Security fix**: `mcp-publisher login` no longer writes
`.mcpregistry_github_token` and `.mcpregistry_registry_token` to the
current working directory. GitHub tokens are kept in memory during the
login→exchange flow and cleared immediately after.
- **Token location**: Unified token moved from `~/.mcp_publisher_token`
to `~/.config/mcp-publisher/token.json` (dir 0700, file 0600), following
the convention used by `gh`, `docker`, and `kubectl`.
- **Dead code removed**: `NeedsLogin()` was on the `Provider` interface
but never called — removed from interface and all 4 implementations.
`forceLogin` field also removed.
- **Auth tests added**: 11 new tests for `GitHubATProvider` (package
previously had 0 tests).
- **Test isolation**: `SetupTestToken` now uses `t.TempDir()` +
`t.Setenv("HOME", ...)` instead of writing to real `$HOME`.

### Breaking change

Users must re-login after upgrading (`mcp-publisher login <method>`).
Existing tokens at `~/.mcp_publisher_token` are not auto-migrated.
`logout` cleans up both old and new locations.

## Test plan

- [x] `make check` passes (lint, unit tests, integration tests)
- [x] `mcp-publisher login none` stores token at
`~/.config/mcp-publisher/token.json`
- [x] No `.mcpregistry_*` files created in cwd after login (with
`--token`, `MCP_GITHUB_TOKEN`, or device flow)
- [x] `mcp-publisher publish` reads token from new location
- [x] `mcp-publisher validate` reads registry URL from new location
- [x] `mcp-publisher logout` cleans up new location, legacy
`~/.mcp_publisher_token`, and cwd `.mcpregistry_*` files
- [x] `mcp-publisher logout` when not logged in shows "Not logged in"
- [x] File permissions: dir 0700, file 0600

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 03:23:07 -04:00

97 lines
2.8 KiB
Go

package commands_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/modelcontextprotocol/registry/cmd/publisher/commands"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoginCommand_WritesTokenToConfigDir(t *testing.T) {
tempHome := t.TempDir()
t.Setenv("HOME", tempHome)
// Mock registry that returns a token for "none" auth
server := setupNoneAuthServer(t)
err := commands.LoginCommand([]string{"none", "--registry", server.URL})
require.NoError(t, err)
// Verify token written to new location
tokenPath := filepath.Join(tempHome, ".config", "mcp-publisher", "token.json")
data, err := os.ReadFile(tokenPath)
require.NoError(t, err)
var tokenInfo map[string]string
require.NoError(t, json.Unmarshal(data, &tokenInfo))
assert.Equal(t, "none", tokenInfo["method"])
assert.Equal(t, server.URL, tokenInfo["registry"])
assert.NotEmpty(t, tokenInfo["token"])
}
func TestLoginCommand_CreatesDirectoryWithCorrectPerms(t *testing.T) {
tempHome := t.TempDir()
t.Setenv("HOME", tempHome)
server := setupNoneAuthServer(t)
err := commands.LoginCommand([]string{"none", "--registry", server.URL})
require.NoError(t, err)
// Verify directory permissions
dir := filepath.Join(tempHome, ".config", "mcp-publisher")
info, err := os.Stat(dir)
require.NoError(t, err)
assert.Equal(t, os.FileMode(0700), info.Mode().Perm())
// Verify file permissions
tokenPath := filepath.Join(dir, "token.json")
info, err = os.Stat(tokenPath)
require.NoError(t, err)
assert.Equal(t, os.FileMode(0600), info.Mode().Perm())
}
func TestLoginCommand_DoesNotWriteLegacyFiles(t *testing.T) {
tempHome := t.TempDir()
t.Setenv("HOME", tempHome)
server := setupNoneAuthServer(t)
err := commands.LoginCommand([]string{"none", "--registry", server.URL})
require.NoError(t, err)
// Verify no legacy file at $HOME
_, err = os.Stat(filepath.Join(tempHome, ".mcp_publisher_token"))
assert.True(t, os.IsNotExist(err), "should not create legacy ~/.mcp_publisher_token")
// Verify no .mcpregistry_* files in cwd
cwd, err := os.Getwd()
require.NoError(t, err)
for _, name := range []string{".mcpregistry_github_token", ".mcpregistry_registry_token"} {
_, err := os.Stat(filepath.Join(cwd, name))
assert.True(t, os.IsNotExist(err), "should not create %s in cwd", name)
}
}
// setupNoneAuthServer creates a mock registry that handles the "none" auth flow.
func setupNoneAuthServer(t *testing.T) *httptest.Server {
t.Helper()
mux := http.NewServeMux()
mux.HandleFunc("/v0/auth/none", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]interface{}{
"registry_token": "test-registry-jwt",
"expires_at": 9999999999,
})
})
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
return server
}