authChallenge injects the RFC 6750 §3 WWW-Authenticate header on any 401 response. Every 401 on this Bearer-accepting surface must advertise the scheme so clients know how to retry (and OAuth-bearer failures get the §3.1 error params so MCP clients can trigger the re-flow). The legacy mux set this a
(build func(r *http.Request) string)
| 71 | // The challenge value comes from the injected builder (agent.API. |
| 72 | // WWWAuthenticateChallenge), so both surfaces emit identical challenges. |
| 73 | func authChallenge(build func(r *http.Request) string) func(http.Handler) http.Handler { |
| 74 | return func(next http.Handler) http.Handler { |
| 75 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 76 | if build == nil { |
| 77 | next.ServeHTTP(w, r) |
| 78 | return |
| 79 | } |
| 80 | next.ServeHTTP(&challengeWriter{ResponseWriter: w, r: r, build: build}, r) |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // challengeWriter sets WWW-Authenticate just before the status line is written, |
| 86 | // but only when that status is 401. Setting it in WriteHeader (and lazily on a |