|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "crypto/rsa" |
| 6 | + "crypto/x509" |
| 7 | + "encoding/json" |
| 8 | + "encoding/pem" |
| 9 | + "io" |
| 10 | + "log/slog" |
| 11 | + "net/http" |
| 12 | + "net/http/httptest" |
| 13 | + "sync/atomic" |
| 14 | + "testing" |
| 15 | + "time" |
| 16 | + |
| 17 | + ghcontext "github.com/github/github-mcp-server/pkg/context" |
| 18 | + "github.com/github/github-mcp-server/pkg/github/appauth" |
| 19 | + "github.com/github/github-mcp-server/pkg/utils" |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +func generateAppKey(t *testing.T) []byte { |
| 25 | + t.Helper() |
| 26 | + key, err := rsa.GenerateKey(rand.Reader, 2048) |
| 27 | + require.NoError(t, err) |
| 28 | + return pem.EncodeToMemory(&pem.Block{ |
| 29 | + Type: "RSA PRIVATE KEY", |
| 30 | + Bytes: x509.MarshalPKCS1PrivateKey(key), |
| 31 | + }) |
| 32 | +} |
| 33 | + |
| 34 | +func newAppTransport(t *testing.T, baseURL string) *appauth.Transport { |
| 35 | + t.Helper() |
| 36 | + tr, err := appauth.NewTransport(http.DefaultTransport, appauth.Config{ |
| 37 | + AppID: 12345, |
| 38 | + PrivateKey: generateAppKey(t), |
| 39 | + InstallationID: 67890, |
| 40 | + BaseURL: baseURL, |
| 41 | + }) |
| 42 | + require.NoError(t, err) |
| 43 | + return tr |
| 44 | +} |
| 45 | + |
| 46 | +func TestWithGitHubAppToken_InjectsToken(t *testing.T) { |
| 47 | + var hits atomic.Int32 |
| 48 | + ghAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 49 | + hits.Add(1) |
| 50 | + w.WriteHeader(http.StatusCreated) |
| 51 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 52 | + "token": "ghs_injected_token", |
| 53 | + "expires_at": time.Now().Add(1 * time.Hour), |
| 54 | + }) |
| 55 | + })) |
| 56 | + defer ghAPI.Close() |
| 57 | + |
| 58 | + tr := newAppTransport(t, ghAPI.URL) |
| 59 | + |
| 60 | + var captured *ghcontext.TokenInfo |
| 61 | + next := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 62 | + info, _ := ghcontext.GetTokenInfo(r.Context()) |
| 63 | + captured = info |
| 64 | + }) |
| 65 | + |
| 66 | + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 67 | + handler := WithGitHubAppToken(tr, logger)(next) |
| 68 | + |
| 69 | + req := httptest.NewRequest(http.MethodPost, "/mcp", nil) |
| 70 | + rr := httptest.NewRecorder() |
| 71 | + handler.ServeHTTP(rr, req) |
| 72 | + |
| 73 | + assert.Equal(t, http.StatusOK, rr.Code) |
| 74 | + require.NotNil(t, captured) |
| 75 | + assert.Equal(t, "ghs_injected_token", captured.Token) |
| 76 | + assert.Equal(t, utils.TokenTypeServerToServerGitHubAppToken, captured.TokenType) |
| 77 | + assert.Equal(t, int32(1), hits.Load()) |
| 78 | +} |
| 79 | + |
| 80 | +func TestWithGitHubAppToken_PreservesExistingTokenInfo(t *testing.T) { |
| 81 | + var hits atomic.Int32 |
| 82 | + ghAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 83 | + hits.Add(1) |
| 84 | + w.WriteHeader(http.StatusCreated) |
| 85 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 86 | + "token": "ghs_should_not_be_used", |
| 87 | + "expires_at": time.Now().Add(1 * time.Hour), |
| 88 | + }) |
| 89 | + })) |
| 90 | + defer ghAPI.Close() |
| 91 | + |
| 92 | + tr := newAppTransport(t, ghAPI.URL) |
| 93 | + |
| 94 | + pre := &ghcontext.TokenInfo{ |
| 95 | + Token: "ghp_explicit", |
| 96 | + TokenType: utils.TokenTypePersonalAccessToken, |
| 97 | + } |
| 98 | + |
| 99 | + var captured *ghcontext.TokenInfo |
| 100 | + next := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 101 | + info, _ := ghcontext.GetTokenInfo(r.Context()) |
| 102 | + captured = info |
| 103 | + }) |
| 104 | + |
| 105 | + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 106 | + handler := WithGitHubAppToken(tr, logger)(next) |
| 107 | + |
| 108 | + req := httptest.NewRequest(http.MethodPost, "/mcp", nil) |
| 109 | + req = req.WithContext(ghcontext.WithTokenInfo(req.Context(), pre)) |
| 110 | + rr := httptest.NewRecorder() |
| 111 | + handler.ServeHTTP(rr, req) |
| 112 | + |
| 113 | + assert.Equal(t, http.StatusOK, rr.Code) |
| 114 | + require.NotNil(t, captured) |
| 115 | + assert.Equal(t, "ghp_explicit", captured.Token) |
| 116 | + assert.Equal(t, int32(0), hits.Load(), "installation token should not have been fetched") |
| 117 | +} |
| 118 | + |
| 119 | +func TestWithGitHubAppToken_PropagatesFetchError(t *testing.T) { |
| 120 | + ghAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 121 | + http.Error(w, "boom", http.StatusInternalServerError) |
| 122 | + })) |
| 123 | + defer ghAPI.Close() |
| 124 | + |
| 125 | + tr := newAppTransport(t, ghAPI.URL) |
| 126 | + |
| 127 | + nextCalled := false |
| 128 | + next := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { |
| 129 | + nextCalled = true |
| 130 | + }) |
| 131 | + |
| 132 | + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 133 | + handler := WithGitHubAppToken(tr, logger)(next) |
| 134 | + |
| 135 | + req := httptest.NewRequest(http.MethodPost, "/mcp", nil) |
| 136 | + rr := httptest.NewRecorder() |
| 137 | + handler.ServeHTTP(rr, req) |
| 138 | + |
| 139 | + assert.Equal(t, http.StatusInternalServerError, rr.Code) |
| 140 | + assert.False(t, nextCalled, "next handler must not run when token fetch fails") |
| 141 | +} |
0 commit comments