Skip to content

Commit 6f9bd66

Browse files
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 <radoslav@stacklok.com>
1 parent dfaa8f3 commit 6f9bd66

4 files changed

Lines changed: 9 additions & 9 deletions

File tree

internal/api/handlers/v0/auth/common.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/hex"
1111
"errors"
1212
"fmt"
13+
"io"
1314
"math/big"
1415
"net"
1516
"regexp"
@@ -20,6 +21,11 @@ import (
2021
"github.com/modelcontextprotocol/registry/internal/config"
2122
)
2223

24+
func readBody(r io.Reader) string {
25+
b, _ := io.ReadAll(r)
26+
return string(b)
27+
}
28+
2329
// ErrSignatureMismatch is returned by VerifySignature when the signature is structurally
2430
// valid but does not verify against the public key. Distinguishing this from structural
2531
// failures (wrong size, bad key format) lets the caller add fingerprint hints only when

internal/api/handlers/v0/auth/github_at.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"io"
87
"net/http"
98
"regexp"
109
"strings"
@@ -123,8 +122,7 @@ func (h *GitHubHandler) getGitHubUser(ctx context.Context, token string) (*GitHu
123122
defer resp.Body.Close()
124123

125124
if resp.StatusCode != http.StatusOK {
126-
body, _ := io.ReadAll(resp.Body)
127-
return nil, fmt.Errorf("GitHub API error (status %d): %s", resp.StatusCode, body)
125+
return nil, fmt.Errorf("GitHub API error (status %d): %s", resp.StatusCode, readBody(resp.Body))
128126
}
129127

130128
var user GitHubUserOrOrg
@@ -152,8 +150,7 @@ func (h *GitHubHandler) getGitHubUserOrgs(ctx context.Context, username string,
152150
defer resp.Body.Close()
153151

154152
if resp.StatusCode != http.StatusOK {
155-
body, _ := io.ReadAll(resp.Body)
156-
return nil, fmt.Errorf("GitHub API error (status %d): %s", resp.StatusCode, body)
153+
return nil, fmt.Errorf("GitHub API error (status %d): %s", resp.StatusCode, readBody(resp.Body))
157154
}
158155

159156
var orgs []GitHubUserOrOrg

internal/api/handlers/v0/auth/github_oidc.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/base64"
77
"encoding/json"
88
"fmt"
9-
"io"
109
"math/big"
1110
"net/http"
1211
"strings"
@@ -149,8 +148,7 @@ func (v *GitHubOIDCValidator) fetchJWKS(ctx context.Context) (*JWKS, error) {
149148
defer resp.Body.Close()
150149

151150
if resp.StatusCode != http.StatusOK {
152-
body, _ := io.ReadAll(resp.Body)
153-
return nil, fmt.Errorf("JWKS endpoint returned status %d: %s", resp.StatusCode, body)
151+
return nil, fmt.Errorf("JWKS endpoint returned status %d: %s", resp.StatusCode, readBody(resp.Body))
154152
}
155153

156154
var jwks JWKS

internal/database/postgres.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ func (db *PostgreSQL) ListServers(
198198
whereConditions = append(whereConditions, cursorCondition)
199199
args = append(args, cursorArgs...)
200200
}
201-
_ = argIndex // Silence unused variable warning
202201

203202
// Build the WHERE clause
204203
whereClause := ""

0 commit comments

Comments
 (0)