-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathtoken_test.go
More file actions
339 lines (311 loc) · 11.3 KB
/
token_test.go
File metadata and controls
339 lines (311 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package middleware
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
ghcontext "github.com/github/github-mcp-server/pkg/context"
"github.com/github/github-mcp-server/pkg/http/headers"
"github.com/github/github-mcp-server/pkg/http/oauth"
"github.com/github/github-mcp-server/pkg/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestExtractUserToken(t *testing.T) {
oauthCfg := &oauth.Config{
BaseURL: "https://example.com",
AuthorizationServer: "https://github.com/login/oauth",
}
tests := []struct {
name string
authHeader string
expectedStatusCode int
expectedCode string
expectedTokenType utils.TokenType
expectedToken string
expectTokenInfo bool
expectWWWAuth bool
}{
// Missing authorization header
{
name: "missing Authorization header returns 401 with WWW-Authenticate",
authHeader: "",
expectedStatusCode: http.StatusUnauthorized,
expectedCode: "missing_token",
expectTokenInfo: false,
expectWWWAuth: true,
},
// Personal Access Token (classic) - ghp_ prefix
{
name: "personal access token (classic) with Bearer prefix",
authHeader: "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectedStatusCode: http.StatusOK,
expectedTokenType: utils.TokenTypePersonalAccessToken,
expectedToken: "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectTokenInfo: true,
},
{
name: "personal access token (classic) with bearer lowercase",
authHeader: "bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectedStatusCode: http.StatusOK,
expectedTokenType: utils.TokenTypePersonalAccessToken,
expectedToken: "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectTokenInfo: true,
},
{
name: "personal access token (classic) without Bearer prefix",
authHeader: "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectedStatusCode: http.StatusOK,
expectedTokenType: utils.TokenTypePersonalAccessToken,
expectedToken: "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectTokenInfo: true,
},
// Fine-grained Personal Access Token - github_pat_ prefix
{
name: "fine-grained personal access token with Bearer prefix",
authHeader: "Bearer github_pat_xxxxxxxxxxxxxxxxxxxxxxx",
expectedStatusCode: http.StatusOK,
expectedTokenType: utils.TokenTypeFineGrainedPersonalAccessToken,
expectedToken: "github_pat_xxxxxxxxxxxxxxxxxxxxxxx",
expectTokenInfo: true,
},
{
name: "fine-grained personal access token without Bearer prefix",
authHeader: "github_pat_xxxxxxxxxxxxxxxxxxxxxxx",
expectedStatusCode: http.StatusOK,
expectedTokenType: utils.TokenTypeFineGrainedPersonalAccessToken,
expectedToken: "github_pat_xxxxxxxxxxxxxxxxxxxxxxx",
expectTokenInfo: true,
},
// OAuth Access Token - gho_ prefix
{
name: "OAuth access token with Bearer prefix",
authHeader: "Bearer gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectedStatusCode: http.StatusOK,
expectedTokenType: utils.TokenTypeOAuthAccessToken,
expectedToken: "gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectTokenInfo: true,
},
{
name: "OAuth access token without Bearer prefix",
authHeader: "gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectedStatusCode: http.StatusOK,
expectedTokenType: utils.TokenTypeOAuthAccessToken,
expectedToken: "gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectTokenInfo: true,
},
// User-to-Server GitHub App Token - ghu_ prefix
{
name: "user-to-server GitHub App token with Bearer prefix",
authHeader: "Bearer ghu_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectedStatusCode: http.StatusOK,
expectedTokenType: utils.TokenTypeUserToServerGitHubAppToken,
expectedToken: "ghu_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectTokenInfo: true,
},
{
name: "user-to-server GitHub App token without Bearer prefix",
authHeader: "ghu_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectedStatusCode: http.StatusOK,
expectedTokenType: utils.TokenTypeUserToServerGitHubAppToken,
expectedToken: "ghu_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectTokenInfo: true,
},
// Server-to-Server GitHub App Token (installation token) - ghs_ prefix
{
name: "server-to-server GitHub App token with Bearer prefix",
authHeader: "Bearer ghs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectedStatusCode: http.StatusOK,
expectedTokenType: utils.TokenTypeServerToServerGitHubAppToken,
expectedToken: "ghs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectTokenInfo: true,
},
{
name: "server-to-server GitHub App token without Bearer prefix",
authHeader: "ghs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectedStatusCode: http.StatusOK,
expectedTokenType: utils.TokenTypeServerToServerGitHubAppToken,
expectedToken: "ghs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
expectTokenInfo: true,
},
// Old-style Personal Access Token (40 hex characters, pre-2021)
{
name: "old-style personal access token (40 hex chars) with Bearer prefix",
authHeader: "Bearer 0123456789abcdef0123456789abcdef01234567",
expectedStatusCode: http.StatusOK,
expectedTokenType: utils.TokenTypePersonalAccessToken,
expectedToken: "0123456789abcdef0123456789abcdef01234567",
expectTokenInfo: true,
},
{
name: "old-style personal access token (40 hex chars) without Bearer prefix",
authHeader: "0123456789abcdef0123456789abcdef01234567",
expectedStatusCode: http.StatusOK,
expectedTokenType: utils.TokenTypePersonalAccessToken,
expectedToken: "0123456789abcdef0123456789abcdef01234567",
expectTokenInfo: true,
},
// Error cases
{
name: "unsupported GitHub-Bearer header returns 400",
authHeader: "GitHub-Bearer some_encrypted_token",
expectedStatusCode: http.StatusBadRequest,
expectedCode: "invalid_token",
expectTokenInfo: false,
},
{
name: "invalid token format returns 400",
authHeader: "Bearer invalid_token_format",
expectedStatusCode: http.StatusBadRequest,
expectedCode: "invalid_token",
expectTokenInfo: false,
},
{
name: "unrecognized prefix returns 400",
authHeader: "Bearer xyz_notavalidprefix",
expectedStatusCode: http.StatusBadRequest,
expectedCode: "invalid_token",
expectTokenInfo: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var capturedTokenInfo *ghcontext.TokenInfo
var tokenInfoCaptured bool
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
capturedTokenInfo, tokenInfoCaptured = ghcontext.GetTokenInfo(r.Context())
w.WriteHeader(http.StatusOK)
})
middleware := ExtractUserToken(oauthCfg)
handler := middleware(nextHandler)
req := httptest.NewRequest(http.MethodGet, "/test", nil)
if tt.authHeader != "" {
req.Header.Set(headers.AuthorizationHeader, tt.authHeader)
}
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, tt.expectedStatusCode, rr.Code)
if tt.expectedCode != "" {
var body struct {
Code string `json:"code"`
}
require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &body))
assert.Equal(t, tt.expectedCode, body.Code)
}
if tt.expectWWWAuth {
wwwAuth := rr.Header().Get("WWW-Authenticate")
assert.NotEmpty(t, wwwAuth, "expected WWW-Authenticate header")
assert.Contains(t, wwwAuth, "Bearer resource_metadata=")
}
if tt.expectTokenInfo {
require.True(t, tokenInfoCaptured, "expected TokenInfo to be present in context")
require.NotNil(t, capturedTokenInfo)
assert.Equal(t, tt.expectedTokenType, capturedTokenInfo.TokenType)
assert.Equal(t, tt.expectedToken, capturedTokenInfo.Token)
} else {
assert.False(t, tokenInfoCaptured, "expected no TokenInfo in context")
}
})
}
}
func TestExtractUserToken_NilOAuthConfig(t *testing.T) {
var capturedTokenInfo *ghcontext.TokenInfo
var tokenInfoCaptured bool
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
capturedTokenInfo, tokenInfoCaptured = ghcontext.GetTokenInfo(r.Context())
w.WriteHeader(http.StatusOK)
})
middleware := ExtractUserToken(nil)
handler := middleware(nextHandler)
req := httptest.NewRequest(http.MethodGet, "/test", nil)
req.Header.Set(headers.AuthorizationHeader, "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
require.True(t, tokenInfoCaptured)
require.NotNil(t, capturedTokenInfo)
assert.Equal(t, utils.TokenTypePersonalAccessToken, capturedTokenInfo.TokenType)
}
func TestExtractUserToken_MissingAuthHeader_WWWAuthenticateFormat(t *testing.T) {
oauthCfg := &oauth.Config{
BaseURL: "https://api.example.com",
AuthorizationServer: "https://github.com/login/oauth",
ResourcePath: "/mcp",
}
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
middleware := ExtractUserToken(oauthCfg)
handler := middleware(nextHandler)
req := httptest.NewRequest(http.MethodGet, "/test", nil)
// No Authorization header
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusUnauthorized, rr.Code)
var body struct {
Code string `json:"code"`
}
require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &body))
assert.Equal(t, "missing_token", body.Code)
wwwAuth := rr.Header().Get("WWW-Authenticate")
assert.NotEmpty(t, wwwAuth)
assert.Contains(t, wwwAuth, "Bearer")
assert.Contains(t, wwwAuth, "resource_metadata=")
assert.Contains(t, wwwAuth, "/.well-known/oauth-protected-resource")
}
func TestSendAuthChallenge(t *testing.T) {
tests := []struct {
name string
oauthCfg *oauth.Config
requestPath string
expectedContains []string
}{
{
name: "with base URL configured",
oauthCfg: &oauth.Config{
BaseURL: "https://mcp.example.com",
},
requestPath: "/api/test",
expectedContains: []string{
"Bearer",
"resource_metadata=",
"https://mcp.example.com/.well-known/oauth-protected-resource",
},
},
{
name: "with nil config uses request host",
oauthCfg: nil,
requestPath: "/api/test",
expectedContains: []string{
"Bearer",
"resource_metadata=",
"/.well-known/oauth-protected-resource",
},
},
{
name: "with resource path configured",
oauthCfg: &oauth.Config{
BaseURL: "https://mcp.example.com",
ResourcePath: "/mcp",
},
requestPath: "/api/test",
expectedContains: []string{
"Bearer",
"resource_metadata=",
"/mcp",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rr := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, tt.requestPath, nil)
sendAuthChallenge(rr, req, tt.oauthCfg)
assert.Equal(t, http.StatusUnauthorized, rr.Code)
wwwAuth := rr.Header().Get("WWW-Authenticate")
for _, expected := range tt.expectedContains {
assert.Contains(t, wwwAuth, expected)
}
})
}
}