auth: change OAuthHandler to take http Request and Response (#603)
Change OAuthHandler signature from func(context.Context, OAuthHandlerArgs) to func(req *http.Request, res *http.Response). - Remove OAuthHandlerArgs struct - Update HTTPTransport to pass req and resp to handler - Update tests to use new signature - Handler can now call oauthex.GetProtectedResourceMetadataFromHeader with proper validation against request URL This change fixes an impedance mismatch between OAuthHandler and the protected resource metadata functions of the oauthex package. The new signature allows handlers to properly validate resource metadata against the request URL, as required by RFC 9728. Fixes #600
This commit is contained in:
parent
d256a9c109
commit
72afbc91bd
1 changed files with 13 additions and 30 deletions
|
|
@ -8,26 +8,19 @@ package auth
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/oauthex"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// An OAuthHandler conducts an OAuth flow and returns a [oauth2.TokenSource] if the authorization
|
||||
// is approved, or an error if not.
|
||||
type OAuthHandler func(context.Context, OAuthHandlerArgs) (oauth2.TokenSource, error)
|
||||
|
||||
// OAuthHandlerArgs are arguments to an [OAuthHandler].
|
||||
type OAuthHandlerArgs struct {
|
||||
// The URL to fetch protected resource metadata, extracted from the WWW-Authenticate header.
|
||||
// Empty if not present or there was an error obtaining it.
|
||||
ResourceMetadataURL string
|
||||
}
|
||||
// The handler receives the HTTP request and response that triggered the authentication flow.
|
||||
// To obtain the protected resource metadata, call [oauthex.GetProtectedResourceMetadataFromHeader].
|
||||
type OAuthHandler func(req *http.Request, res *http.Response) (oauth2.TokenSource, error)
|
||||
|
||||
// HTTPTransport is an [http.RoundTripper] that follows the MCP
|
||||
// OAuth protocol when it encounters a 401 Unauthorized response.
|
||||
|
|
@ -112,10 +105,7 @@ func (t *HTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|||
// TODO: We hold the lock for the entire OAuth flow. This could be a long
|
||||
// time. Is there a better way?
|
||||
if _, ok := t.opts.Base.(*oauth2.Transport); !ok {
|
||||
authHeaders := resp.Header[http.CanonicalHeaderKey("WWW-Authenticate")]
|
||||
ts, err := t.handler(req.Context(), OAuthHandlerArgs{
|
||||
ResourceMetadataURL: extractResourceMetadataURL(authHeaders),
|
||||
})
|
||||
ts, err := t.handler(req, resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -131,11 +121,3 @@ func (t *HTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|||
|
||||
return t.opts.Base.RoundTrip(req)
|
||||
}
|
||||
|
||||
func extractResourceMetadataURL(authHeaders []string) string {
|
||||
cs, err := oauthex.ParseWWWAuthenticate(authHeaders)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return oauthex.ResourceMetadataURL(cs)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue