From 6f9bd6661c45ee477dee15b21b57c171352473ca Mon Sep 17 00:00:00 2001 From: Mesut Oezdil <114185853+mesutoezdil@users.noreply.github.com> Date: Mon, 4 May 2026 12:57:02 +0200 Subject: [PATCH] refactor: extract readBody helper to remove duplicate io.ReadAll calls (#1241) The same pattern for reading an error response body appeared in three places across the auth handlers: - `internal/api/handlers/v0/auth/github_at.go` (twice) - `internal/api/handlers/v0/auth/github_oidc.go` (once) A shared `readBody` helper function added to `common.go` now handles this in one place. The `io` import is removed from both files since it is no longer used directly there. --------- Co-authored-by: Radoslav Dimitrov --- internal/api/handlers/v0/auth/common.go | 6 ++++++ internal/api/handlers/v0/auth/github_at.go | 7 ++----- internal/api/handlers/v0/auth/github_oidc.go | 4 +--- internal/database/postgres.go | 1 - 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/api/handlers/v0/auth/common.go b/internal/api/handlers/v0/auth/common.go index c283655..2c81303 100644 --- a/internal/api/handlers/v0/auth/common.go +++ b/internal/api/handlers/v0/auth/common.go @@ -10,6 +10,7 @@ import ( "encoding/hex" "errors" "fmt" + "io" "math/big" "net" "regexp" @@ -20,6 +21,11 @@ import ( "github.com/modelcontextprotocol/registry/internal/config" ) +func readBody(r io.Reader) string { + b, _ := io.ReadAll(r) + return string(b) +} + // ErrSignatureMismatch is returned by VerifySignature when the signature is structurally // valid but does not verify against the public key. Distinguishing this from structural // failures (wrong size, bad key format) lets the caller add fingerprint hints only when diff --git a/internal/api/handlers/v0/auth/github_at.go b/internal/api/handlers/v0/auth/github_at.go index fbb950c..b495f4d 100644 --- a/internal/api/handlers/v0/auth/github_at.go +++ b/internal/api/handlers/v0/auth/github_at.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "io" "net/http" "regexp" "strings" @@ -123,8 +122,7 @@ func (h *GitHubHandler) getGitHubUser(ctx context.Context, token string) (*GitHu defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - body, _ := io.ReadAll(resp.Body) - return nil, fmt.Errorf("GitHub API error (status %d): %s", resp.StatusCode, body) + return nil, fmt.Errorf("GitHub API error (status %d): %s", resp.StatusCode, readBody(resp.Body)) } var user GitHubUserOrOrg @@ -152,8 +150,7 @@ func (h *GitHubHandler) getGitHubUserOrgs(ctx context.Context, username string, defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - body, _ := io.ReadAll(resp.Body) - return nil, fmt.Errorf("GitHub API error (status %d): %s", resp.StatusCode, body) + return nil, fmt.Errorf("GitHub API error (status %d): %s", resp.StatusCode, readBody(resp.Body)) } var orgs []GitHubUserOrOrg diff --git a/internal/api/handlers/v0/auth/github_oidc.go b/internal/api/handlers/v0/auth/github_oidc.go index 4170640..6a6761b 100644 --- a/internal/api/handlers/v0/auth/github_oidc.go +++ b/internal/api/handlers/v0/auth/github_oidc.go @@ -6,7 +6,6 @@ import ( "encoding/base64" "encoding/json" "fmt" - "io" "math/big" "net/http" "strings" @@ -149,8 +148,7 @@ func (v *GitHubOIDCValidator) fetchJWKS(ctx context.Context) (*JWKS, error) { defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - body, _ := io.ReadAll(resp.Body) - return nil, fmt.Errorf("JWKS endpoint returned status %d: %s", resp.StatusCode, body) + return nil, fmt.Errorf("JWKS endpoint returned status %d: %s", resp.StatusCode, readBody(resp.Body)) } var jwks JWKS diff --git a/internal/database/postgres.go b/internal/database/postgres.go index 2749ad8..b0b2d98 100644 --- a/internal/database/postgres.go +++ b/internal/database/postgres.go @@ -198,7 +198,6 @@ func (db *PostgreSQL) ListServers( whereConditions = append(whereConditions, cursorCondition) args = append(args, cursorArgs...) } - _ = argIndex // Silence unused variable warning // Build the WHERE clause whereClause := ""