@@ -11,30 +11,60 @@ import (
1111)
1212
1313// detectOAuthRequirement checks if the server requires OAuth authentication
14- // by making a test request and checking for WWW-Authenticate header.
14+ // by making test requests and checking for WWW-Authenticate header.
15+ // It tries GET first, then POST if GET returns 405 Method Not Allowed.
1516// See https://modelcontextprotocol.io/specification/draft/basic/authorization#authorization-server-location.
1617func detectOAuthRequirement (url string ) bool {
18+ httpClient := & http.Client {}
19+
20+ // Try GET request first
1721 req , err := http .NewRequest (http .MethodGet , url , http .NoBody )
1822 if err != nil {
19- slog .Debug ("Failed to create test request for OAuth detection" , "error" , err )
23+ slog .Debug ("Failed to create GET test request for OAuth detection" , "error" , err )
2024 return false
2125 }
2226
23- httpClient := & http.Client {}
2427 resp , err := httpClient .Do (req )
2528 if err != nil {
26- slog .Debug ("Failed to make test request for OAuth detection" , "error" , err )
29+ slog .Debug ("Failed to make GET test request for OAuth detection" , "error" , err )
2730 return false
2831 }
2932 defer resp .Body .Close ()
3033
34+ // Check for WWW-Authenticate header in GET response
3135 wwwAuth := resp .Header .Get ("WWW-Authenticate" )
3236 if wwwAuth != "" {
33- slog .Debug ("Detected OAuth requirement" , "www-authenticate" , wwwAuth )
37+ slog .Debug ("Detected OAuth requirement via GET " , "www-authenticate" , wwwAuth )
3438 return strings .Contains (strings .ToLower (wwwAuth ), "bearer" ) ||
3539 strings .Contains (strings .ToLower (wwwAuth ), "oauth" )
3640 }
3741
42+ // If GET returned 405 Method Not Allowed, try POST
43+ if resp .StatusCode == http .StatusMethodNotAllowed {
44+ slog .Debug ("GET returned 405, trying POST for OAuth detection" )
45+
46+ postReq , err := http .NewRequest (http .MethodPost , url , http .NoBody )
47+ if err != nil {
48+ slog .Debug ("Failed to create POST test request for OAuth detection" , "error" , err )
49+ return false
50+ }
51+
52+ postResp , err := httpClient .Do (postReq )
53+ if err != nil {
54+ slog .Debug ("Failed to make POST test request for OAuth detection" , "error" , err )
55+ return false
56+ }
57+ defer postResp .Body .Close ()
58+
59+ // Check for WWW-Authenticate header in POST response
60+ postWwwAuth := postResp .Header .Get ("WWW-Authenticate" )
61+ if postWwwAuth != "" {
62+ slog .Debug ("Detected OAuth requirement via POST" , "www-authenticate" , postWwwAuth )
63+ return strings .Contains (strings .ToLower (postWwwAuth ), "bearer" ) ||
64+ strings .Contains (strings .ToLower (postWwwAuth ), "oauth" )
65+ }
66+ }
67+
3868 return false
3969}
4070
0 commit comments