diff --git a/internal/runtime/dcr_oauth_token_health_test.go b/internal/runtime/dcr_oauth_token_health_test.go new file mode 100644 index 000000000..ee7baff16 --- /dev/null +++ b/internal/runtime/dcr_oauth_token_health_test.go @@ -0,0 +1,95 @@ +package runtime + +import ( + "context" + "testing" + "time" + + "github.com/mark3labs/mcp-go/client/transport" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" + "github.com/smart-mcp-proxy/mcpproxy-go/internal/contracts" + "github.com/smart-mcp-proxy/mcpproxy-go/internal/health" + "github.com/smart-mcp-proxy/mcpproxy-go/internal/oauth" + "github.com/smart-mcp-proxy/mcpproxy-go/internal/runtime/stateview" +) + +// DCR persists a client registration before a login obtains an access token. +// A zero expiry makes a real token long-lived, but cannot make a registration +// authenticated. The server projection must agree with PersistentTokenStore. +func TestGetAllServers_DCRRequiresAccessToken(t *testing.T) { + for _, explicit := range []bool{false, true} { + mode := "autodiscovery" + if explicit { + mode = "explicit" + } + t.Run(mode, func(t *testing.T) { + for _, tc := range []struct { + name string + accessToken string + refreshToken string + expiresAt time.Time + authenticated bool + status string + valid bool + }{ + {name: "DCR only", status: "none"}, + {name: "no access token with expiry and refresh", refreshToken: "synthetic-refresh", expiresAt: time.Now().Add(time.Hour), status: "none"}, + {name: "non-expiring token", accessToken: "synthetic-access", authenticated: true, status: "authenticated", valid: true}, + {name: "unexpired token", accessToken: "synthetic-access", expiresAt: time.Now().Add(time.Hour), authenticated: true, status: "authenticated", valid: true}, + {name: "expired token", accessToken: "synthetic-access", expiresAt: time.Now().Add(-time.Hour), authenticated: true, status: "expired"}, + } { + t.Run(tc.name, func(t *testing.T) { + rt := newStaleTokenRuntime(t) + sc := &config.ServerConfig{ + Name: "dcr-fixture", URL: "https://mcp.example.invalid/mcp", + Protocol: "http", Enabled: true, + } + if explicit { + sc.OAuth = &config.OAuthConfig{PKCEEnabled: true} + } + db := rt.storageManager.GetBoltDB() + key := oauth.GenerateServerKey(sc.Name, sc.URL) + require.NoError(t, db.UpdateOAuthClientCredentials(key, "synthetic-client", "", 0, "")) + record, err := db.GetOAuthToken(key) + require.NoError(t, err) + if tc.accessToken != "" || tc.refreshToken != "" || !tc.expiresAt.IsZero() { + record.AccessToken = tc.accessToken + record.RefreshToken = tc.refreshToken + record.ExpiresAt = tc.expiresAt + require.NoError(t, db.SaveOAuthToken(record)) + } + if tc.accessToken == "" { + store := oauth.NewPersistentTokenStore(sc.Name, sc.URL, db) + token, err := store.GetToken(context.Background()) + require.ErrorIs(t, err, transport.ErrNoToken) + require.Nil(t, token) + } + rt.Supervisor().StateView().UpdateServer(sc.Name, func(s *stateview.ServerStatus) { + s.Name, s.Config, s.Enabled, s.State = sc.Name, sc, true, "pending_auth" + }) + + server := findServer(t, rt, sc.Name) + assert.Equal(t, tc.authenticated, server["authenticated"]) + assert.Equal(t, tc.status, server["oauth_status"]) + oauthConfig, ok := server["oauth"].(map[string]interface{}) + require.True(t, ok, "a client registration still identifies an OAuth server") + assert.Equal(t, tc.valid, oauthConfig["token_valid"] == true) + if tc.accessToken == "" { + assert.NotContains(t, server, "token_expires_at") + assert.NotContains(t, oauthConfig, "token_expires_at") + hs, ok := server["health"].(*contracts.HealthStatus) + require.True(t, ok) + assert.Equal(t, health.ActionLogin, hs.Action) + } + stored, err := db.GetOAuthToken(key) + require.NoError(t, err) + assert.Equal(t, "synthetic-client", stored.ClientID, "status reads preserve the registration") + assert.Equal(t, tc.accessToken, stored.AccessToken) + }) + } + }) + } +} diff --git a/internal/runtime/runtime.go b/internal/runtime/runtime.go index 79adf83a5..0ff81be33 100644 --- a/internal/runtime/runtime.go +++ b/internal/runtime/runtime.go @@ -2773,7 +2773,9 @@ func (r *Runtime) GetAllServers() ([]map[string]interface{}, error) { zap.Bool("token_nil", token == nil), zap.Error(err)) - if err == nil && token != nil { + // DCR persists client credentials before the code exchange. Like + // PersistentTokenStore.GetToken, require an actual access token. + if err == nil && token != nil && token.AccessToken != "" { authenticated = true tokenExpiresAt = token.ExpiresAt hasRefreshToken = token.RefreshToken != "" @@ -2807,7 +2809,12 @@ func (r *Runtime) GetAllServers() ([]map[string]interface{}, error) { oauthStatus = string(oauth.OAuthStatusAuthenticated) } } else { - // No token found - check if OAuth config exists to determine status + // A DCR-only record still identifies an autodiscovered OAuth + // server, but is not evidence of a completed login. + if err == nil && token != nil && oauthConfig == nil { + oauthConfig = map[string]interface{}{"autodiscovery": true} + } + // No access token - check if OAuth config exists to determine status if oauthConfig != nil { oauthStatus = string(oauth.OAuthStatusNone) }