fix(pier): MCP 401 challenge names only resource_metadata
- drop the response-writer wrapper that appended scope="read" to every 401 - the SDK handler now gets the original writer; RequireBearerToken still sets no Scopes - TestPierTracerSmoke pins the exact challenge for a bad and a missing bearer - README and docs/services/mcp.md describe the header without a scope parameter
This commit is contained in:
@@ -247,8 +247,12 @@ func (p *Pier) bodyLimit(next http.Handler) http.Handler {
|
||||
|
||||
// bearer verifies the bearer of every request afresh and stores the
|
||||
// resolved Identity on the request for the session router and the SDK
|
||||
// handlers; a failure is a 401 carrying WWW-Authenticate with the
|
||||
// protected resource metadata URL and the read scope.
|
||||
// handlers. A failure is a 401 whose WWW-Authenticate names only the
|
||||
// protected resource metadata URL. It carries no scope parameter, so a
|
||||
// client requests the scopes the metadata document advertises and the
|
||||
// authorization server's consent decides the grant; tools are then filtered
|
||||
// per granted scope. No Scopes option is set: the SDK would answer 403 to
|
||||
// every token lacking them.
|
||||
func (p *Pier) bearer(next http.Handler) http.Handler {
|
||||
verify := func(ctx context.Context, token string, r *http.Request) (*auth.TokenInfo, error) {
|
||||
ctx = towel.WithClientIP(ctx, p.clientIP(r))
|
||||
@@ -263,13 +267,10 @@ func (p *Pier) bearer(next http.Handler) http.Handler {
|
||||
Extra: map[string]any{extraIdentity: id, extraCall: callInfoFrom(r)},
|
||||
}, nil
|
||||
}
|
||||
h := auth.RequireBearerToken(verify, &auth.RequireBearerTokenOptions{
|
||||
return auth.RequireBearerToken(verify, &auth.RequireBearerTokenOptions{
|
||||
ResourceMetadataURL: p.prmURL,
|
||||
AllowMissingExpiration: true,
|
||||
})(next)
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
h.ServeHTTP(&challengeWriter{ResponseWriter: w}, r)
|
||||
})
|
||||
}
|
||||
|
||||
// clientIP is the request's client address: the host's answer when it
|
||||
@@ -285,30 +286,6 @@ func (p *Pier) clientIP(r *http.Request) string {
|
||||
return r.RemoteAddr
|
||||
}
|
||||
|
||||
// challengeWriter completes the 401 challenge with scope="read", which the
|
||||
// SDK middleware would otherwise enforce on every request.
|
||||
type challengeWriter struct {
|
||||
http.ResponseWriter
|
||||
}
|
||||
|
||||
func (c *challengeWriter) WriteHeader(status int) {
|
||||
if status == http.StatusUnauthorized {
|
||||
h := c.Header()
|
||||
if v := h.Get("WWW-Authenticate"); v != "" && !strings.Contains(v, "scope=") {
|
||||
h.Set("WWW-Authenticate", v+`, scope="read"`)
|
||||
}
|
||||
}
|
||||
c.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
func (c *challengeWriter) Flush() {
|
||||
if f, ok := c.ResponseWriter.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *challengeWriter) Unwrap() http.ResponseWriter { return c.ResponseWriter }
|
||||
|
||||
// identityFromRequest reads the Identity the bearer middleware stored.
|
||||
func identityFromRequest(r *http.Request) (Identity, bool) {
|
||||
ti := auth.TokenInfoFromContext(r.Context())
|
||||
|
||||
Reference in New Issue
Block a user