| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package extauth |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"net/http" |
| 12 |
"net/url" |
| 13 |
"slices" |
| 14 |
"strings" |
| 15 |
|
| 16 |
"github.com/modelcontextprotocol/go-sdk/auth" |
| 17 |
"github.com/modelcontextprotocol/go-sdk/oauthex" |
| 18 |
"golang.org/x/oauth2" |
| 19 |
"golang.org/x/oauth2/clientcredentials" |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
type ClientCredentialsHandlerConfig struct { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
Credentials *oauthex.ClientCredentials |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
HTTPClient *http.Client |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
type ClientCredentialsHandler struct { |
| 48 |
config *ClientCredentialsHandlerConfig |
| 49 |
tokenSource oauth2.TokenSource |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
var _ auth.OAuthHandler = (*ClientCredentialsHandler)(nil) |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
func NewClientCredentialsHandler(config *ClientCredentialsHandlerConfig) (*ClientCredentialsHandler, error) { |
| 58 |
if config == nil { |
| 59 |
return nil, fmt.Errorf("config must be provided") |
| 60 |
} |
| 61 |
if config.Credentials == nil { |
| 62 |
return nil, fmt.Errorf("credentials are required") |
| 63 |
} |
| 64 |
if err := config.Credentials.Validate(); err != nil { |
| 65 |
return nil, fmt.Errorf("invalid credentials: %w", err) |
| 66 |
} |
| 67 |
if config.Credentials.ClientSecretAuth == nil { |
| 68 |
return nil, fmt.Errorf("clientSecretAuth is required for client credentials grant") |
| 69 |
} |
| 70 |
return &ClientCredentialsHandler{config: config}, nil |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
func (h *ClientCredentialsHandler) TokenSource(ctx context.Context) (oauth2.TokenSource, error) { |
| 76 |
return h.tokenSource, nil |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
func (h *ClientCredentialsHandler) Authorize(ctx context.Context, req *http.Request, resp *http.Response) error { |
| 87 |
defer resp.Body.Close() |
| 88 |
defer io.Copy(io.Discard, resp.Body) |
| 89 |
|
| 90 |
httpClient := h.config.HTTPClient |
| 91 |
if httpClient == nil { |
| 92 |
httpClient = http.DefaultClient |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
wwwChallenges, err := oauthex.ParseWWWAuthenticate(resp.Header[http.CanonicalHeaderKey("WWW-Authenticate")]) |
| 97 |
if err != nil { |
| 98 |
return fmt.Errorf("failed to parse WWW-Authenticate header: %v", err) |
| 99 |
} |
| 100 |
|
| 101 |
prm, err := getProtectedResourceMetadata(ctx, wwwChallenges, req.URL.String(), httpClient) |
| 102 |
if err != nil { |
| 103 |
return err |
| 104 |
} |
| 105 |
|
| 106 |
if len(prm.AuthorizationServers) == 0 { |
| 107 |
return fmt.Errorf("protected resource metadata has no authorization servers specified") |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
asm, err := auth.GetAuthServerMetadata(ctx, prm.AuthorizationServers[0], httpClient) |
| 112 |
if err != nil { |
| 113 |
return fmt.Errorf("failed to get authorization server metadata: %w", err) |
| 114 |
} |
| 115 |
if asm == nil { |
| 116 |
|
| 117 |
authServerURL := prm.AuthorizationServers[0] |
| 118 |
asm = &oauthex.AuthServerMeta{ |
| 119 |
Issuer: authServerURL, |
| 120 |
TokenEndpoint: authServerURL + "/token", |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
scopes := scopesFromChallenges(wwwChallenges) |
| 126 |
if len(scopes) == 0 && len(prm.ScopesSupported) > 0 { |
| 127 |
scopes = prm.ScopesSupported |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
creds := h.config.Credentials |
| 132 |
cfg := &clientcredentials.Config{ |
| 133 |
ClientID: creds.ClientID, |
| 134 |
ClientSecret: creds.ClientSecretAuth.ClientSecret, |
| 135 |
TokenURL: asm.TokenEndpoint, |
| 136 |
Scopes: scopes, |
| 137 |
AuthStyle: selectTokenAuthMethod(asm.TokenEndpointAuthMethodsSupported), |
| 138 |
} |
| 139 |
|
| 140 |
ctxWithClient := context.WithValue(ctx, oauth2.HTTPClient, httpClient) |
| 141 |
h.tokenSource = cfg.TokenSource(ctxWithClient) |
| 142 |
|
| 143 |
|
| 144 |
if _, err := h.tokenSource.Token(); err != nil { |
| 145 |
h.tokenSource = nil |
| 146 |
return fmt.Errorf("client credentials token request failed: %w", err) |
| 147 |
} |
| 148 |
return nil |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
func getProtectedResourceMetadata(ctx context.Context, wwwChallenges []oauthex.Challenge, mcpServerURL string, httpClient *http.Client) (*oauthex.ProtectedResourceMetadata, error) { |
| 154 |
|
| 155 |
|
| 156 |
for _, u := range protectedResourceMetadataURLs(resourceMetadataURLFromChallenges(wwwChallenges), mcpServerURL) { |
| 157 |
prm, err := oauthex.GetProtectedResourceMetadata(ctx, u.url, u.resource, httpClient) |
| 158 |
if err != nil { |
| 159 |
continue |
| 160 |
} |
| 161 |
if prm == nil { |
| 162 |
continue |
| 163 |
} |
| 164 |
if len(prm.AuthorizationServers) == 0 { |
| 165 |
|
| 166 |
return nil, fmt.Errorf("protected resource metadata has no authorization servers specified") |
| 167 |
} |
| 168 |
return prm, nil |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
u, err := url.Parse(mcpServerURL) |
| 173 |
if err != nil { |
| 174 |
return nil, fmt.Errorf("failed to parse MCP server URL: %v", err) |
| 175 |
} |
| 176 |
u.Path = "" |
| 177 |
return &oauthex.ProtectedResourceMetadata{ |
| 178 |
AuthorizationServers: []string{u.String()}, |
| 179 |
Resource: mcpServerURL, |
| 180 |
}, nil |
| 181 |
} |
| 182 |
|
| 183 |
type prmURL struct { |
| 184 |
url string |
| 185 |
resource string |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
func protectedResourceMetadataURLs(metadataURL, resourceURL string) []prmURL { |
| 191 |
var urls []prmURL |
| 192 |
if metadataURL != "" { |
| 193 |
urls = append(urls, prmURL{url: metadataURL, resource: resourceURL}) |
| 194 |
} |
| 195 |
ru, err := url.Parse(resourceURL) |
| 196 |
if err != nil { |
| 197 |
return urls |
| 198 |
} |
| 199 |
mu := *ru |
| 200 |
|
| 201 |
mu.Path = "/.well-known/oauth-protected-resource/" + strings.TrimLeft(ru.Path, "/") |
| 202 |
urls = append(urls, prmURL{url: mu.String(), resource: resourceURL}) |
| 203 |
|
| 204 |
mu.Path = "/.well-known/oauth-protected-resource" |
| 205 |
ru.Path = "" |
| 206 |
urls = append(urls, prmURL{url: mu.String(), resource: ru.String()}) |
| 207 |
return urls |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
func resourceMetadataURLFromChallenges(cs []oauthex.Challenge) string { |
| 213 |
for _, c := range cs { |
| 214 |
if u := c.Params["resource_metadata"]; u != "" { |
| 215 |
return u |
| 216 |
} |
| 217 |
} |
| 218 |
return "" |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
func scopesFromChallenges(cs []oauthex.Challenge) []string { |
| 224 |
for _, c := range cs { |
| 225 |
if c.Scheme == "bearer" && c.Params["scope"] != "" { |
| 226 |
return strings.Fields(c.Params["scope"]) |
| 227 |
} |
| 228 |
} |
| 229 |
return nil |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
func selectTokenAuthMethod(supported []string) oauth2.AuthStyle { |
| 236 |
prefOrder := []string{ |
| 237 |
"client_secret_post", |
| 238 |
"client_secret_basic", |
| 239 |
} |
| 240 |
for _, method := range prefOrder { |
| 241 |
if slices.Contains(supported, method) { |
| 242 |
switch method { |
| 243 |
case "client_secret_post": |
| 244 |
return oauth2.AuthStyleInParams |
| 245 |
case "client_secret_basic": |
| 246 |
return oauth2.AuthStyleInHeader |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
return oauth2.AuthStyleAutoDetect |
| 251 |
} |