Skip to content

Commit f87313f

Browse files
Address code review feedback
- Add DefaultAuthTimeout constant for configurability - Fix string index check to handle idx >= 0 correctly - Use cmd.Context() for proper cancellation support - Add named constant for PKCE verifier length in test - Remove unused context import Co-authored-by: SamMorrowDrums <4811358+SamMorrowDrums@users.noreply.github.com>
1 parent 7813b50 commit f87313f

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

cmd/github-mcp-server/main.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"errors"
65
"fmt"
76
"os"
@@ -33,23 +32,22 @@ var (
3332
Use: "stdio",
3433
Short: "Start stdio server",
3534
Long: `Start a server that communicates via standard input/output streams using JSON-RPC messages.`,
36-
RunE: func(_ *cobra.Command, _ []string) error {
35+
RunE: func(cmd *cobra.Command, _ []string) error {
3736
token := viper.GetString("personal_access_token")
3837

3938
// If no token provided, check if OAuth is configured
4039
if token == "" {
4140
oauthClientID := viper.GetString("oauth_client_id")
4241
if oauthClientID != "" {
43-
// Perform interactive OAuth flow
44-
ctx := context.Background()
42+
// Perform interactive OAuth flow with the command's context
4543
oauthCfg := oauth.GetGitHubOAuthConfig(
4644
oauthClientID,
4745
viper.GetString("oauth_client_secret"),
4846
getOAuthScopes(),
4947
viper.GetString("host"), // Pass the gh-host configuration
5048
)
5149

52-
result, err := oauth.StartInteractiveFlow(ctx, oauthCfg)
50+
result, err := oauth.StartInteractiveFlow(cmd.Context(), oauthCfg)
5351
if err != nil {
5452
return fmt.Errorf("OAuth flow failed: %w", err)
5553
}

internal/oauth/oauth.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import (
1818
"golang.org/x/oauth2"
1919
)
2020

21+
const (
22+
// DefaultAuthTimeout is the default timeout for the OAuth authorization flow
23+
DefaultAuthTimeout = 5 * time.Minute
24+
)
25+
2126
// Config holds the OAuth configuration
2227
type Config struct {
2328
ClientID string
@@ -133,8 +138,8 @@ func StartInteractiveFlow(ctx context.Context, cfg Config) (*Result, error) {
133138
return nil, fmt.Errorf("callback error: %w", err)
134139
case <-ctx.Done():
135140
return nil, fmt.Errorf("context cancelled: %w", ctx.Err())
136-
case <-time.After(5 * time.Minute):
137-
return nil, fmt.Errorf("authorization timeout after 5 minutes")
141+
case <-time.After(DefaultAuthTimeout):
142+
return nil, fmt.Errorf("authorization timeout after %v", DefaultAuthTimeout)
138143
}
139144

140145
// Shutdown server gracefully
@@ -297,7 +302,7 @@ func getOAuthEndpoints(host string) (authURL, tokenURL string) {
297302
}
298303

299304
// Remove any trailing slashes or paths
300-
if idx := strings.Index(hostname, "/"); idx > 0 {
305+
if idx := strings.Index(hostname, "/"); idx >= 0 {
301306
hostname = hostname[:idx]
302307
}
303308

internal/oauth/oauth_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ import (
77
"github.com/stretchr/testify/require"
88
)
99

10+
const (
11+
// expectedPKCEVerifierMinLength is the expected minimum length of a PKCE verifier
12+
// Base64URL encoding of 32 bytes = 43 characters (32 * 8 / 6, rounded up)
13+
expectedPKCEVerifierMinLength = 43
14+
)
15+
1016
func TestGeneratePKCEVerifier(t *testing.T) {
1117
verifier, err := generatePKCEVerifier()
1218
require.NoError(t, err)
1319
require.NotEmpty(t, verifier)
1420

1521
// Verifier should be at least 43 characters (base64url of 32 bytes)
16-
assert.GreaterOrEqual(t, len(verifier), 43)
22+
assert.GreaterOrEqual(t, len(verifier), expectedPKCEVerifierMinLength)
1723

1824
// Generate another one to ensure they're different
1925
verifier2, err := generatePKCEVerifier()

0 commit comments

Comments
 (0)