Skip to content

Commit d3ca549

Browse files
committed
feat(server): make WWW-Authenticate header value configurable
NewHTTPUnauthorizedHandler now accepts the WWW-Authenticate header value as a parameter rather than hardcoding 'Bearer realm="buildkite"', allowing callers to set any valid challenge scheme.
1 parent 81fd5e3 commit d3ca549

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

internal/commands/http.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func (c *HTTPCmd) Run(ctx context.Context, globals *Globals) error {
5959
mcp.NewStreamableHTTPHandler(factory, &mcp.StreamableHTTPOptions{
6060
Stateless: true,
6161
}),
62+
`Bearer realm="buildkite"`,
6263
)
6364
mux.Handle("/mcp", handler)
6465

pkg/server/unauthorized.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ func signalUnauthorized(ctx context.Context) {
1919
// NewHTTPUnauthorizedHandler wraps an HTTP handler to return HTTP 401 when the
2020
// Buildkite API returns a 401, instead of a 200 with a JSON-RPC error body.
2121
// Works for both JSON and SSE transport modes in stateless operation.
22-
func NewHTTPUnauthorizedHandler(handler http.Handler) http.Handler {
22+
//
23+
// wwwAuthenticate is the value of the WWW-Authenticate header sent on 401
24+
// responses (e.g. `Bearer realm="buildkite"`).
25+
func NewHTTPUnauthorizedHandler(handler http.Handler, wwwAuthenticate string) http.Handler {
2326
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2427
flag := &atomic.Bool{}
2528
ctx := context.WithValue(r.Context(), unauthorizedContextKey{}, flag)
@@ -34,7 +37,7 @@ func NewHTTPUnauthorizedHandler(handler http.Handler) http.Handler {
3437
for k := range h {
3538
delete(h, k)
3639
}
37-
h.Set("WWW-Authenticate", `Bearer realm="buildkite"`)
40+
h.Set("WWW-Authenticate", wwwAuthenticate)
3841
w.WriteHeader(http.StatusUnauthorized)
3942
}
4043
})

pkg/server/unauthorized_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestNewHTTPUnauthorizedHandler_NormalRequest(t *testing.T) {
1616
_, _ = w.Write([]byte(`{"result":"ok"}`))
1717
})
1818

19-
handler := NewHTTPUnauthorizedHandler(inner)
19+
handler := NewHTTPUnauthorizedHandler(inner, `Bearer realm="buildkite"`)
2020
rec := httptest.NewRecorder()
2121
req := httptest.NewRequest(http.MethodPost, "/mcp", nil)
2222

@@ -37,7 +37,7 @@ func TestNewHTTPUnauthorizedHandler_UnauthorizedSignal(t *testing.T) {
3737
_, _ = w.Write([]byte(`{"error":"unauthorized"}`))
3838
})
3939

40-
handler := NewHTTPUnauthorizedHandler(inner)
40+
handler := NewHTTPUnauthorizedHandler(inner, `Bearer realm="buildkite"`)
4141
rec := httptest.NewRecorder()
4242
req := httptest.NewRequest(http.MethodPost, "/mcp", nil)
4343

0 commit comments

Comments
 (0)