| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package extauth |
| 6 |
|
| 7 |
import ( |
| 8 |
"net/http" |
| 9 |
"net/http/httptest" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/modelcontextprotocol/go-sdk/auth" |
| 14 |
"github.com/modelcontextprotocol/go-sdk/internal/oauthtest" |
| 15 |
"github.com/modelcontextprotocol/go-sdk/oauthex" |
| 16 |
"golang.org/x/oauth2" |
| 17 |
) |
| 18 |
|
| 19 |
func validClientCredentialsConfig() *ClientCredentialsHandlerConfig { |
| 20 |
return &ClientCredentialsHandlerConfig{ |
| 21 |
Credentials: &oauthex.ClientCredentials{ |
| 22 |
ClientID: "test-client", |
| 23 |
ClientSecretAuth: &oauthex.ClientSecretAuth{ClientSecret: "test-secret"}, |
| 24 |
}, |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
func TestNewClientCredentialsHandler_Validation(t *testing.T) { |
| 29 |
tests := []struct { |
| 30 |
name string |
| 31 |
config *ClientCredentialsHandlerConfig |
| 32 |
wantError string |
| 33 |
}{ |
| 34 |
{ |
| 35 |
name: "nil config", |
| 36 |
config: nil, |
| 37 |
wantError: "config must be provided", |
| 38 |
}, |
| 39 |
{ |
| 40 |
name: "nil credentials", |
| 41 |
config: func() *ClientCredentialsHandlerConfig { |
| 42 |
c := validClientCredentialsConfig() |
| 43 |
c.Credentials = nil |
| 44 |
return c |
| 45 |
}(), |
| 46 |
wantError: "credentials are required", |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "missing client ID", |
| 50 |
config: func() *ClientCredentialsHandlerConfig { |
| 51 |
c := validClientCredentialsConfig() |
| 52 |
c.Credentials.ClientID = "" |
| 53 |
return c |
| 54 |
}(), |
| 55 |
wantError: "ClientID is required", |
| 56 |
}, |
| 57 |
{ |
| 58 |
name: "missing client secret auth", |
| 59 |
config: func() *ClientCredentialsHandlerConfig { |
| 60 |
c := validClientCredentialsConfig() |
| 61 |
c.Credentials.ClientSecretAuth = nil |
| 62 |
return c |
| 63 |
}(), |
| 64 |
wantError: "clientSecretAuth is required", |
| 65 |
}, |
| 66 |
{ |
| 67 |
name: "empty client secret", |
| 68 |
config: func() *ClientCredentialsHandlerConfig { |
| 69 |
c := validClientCredentialsConfig() |
| 70 |
c.Credentials.ClientSecretAuth.ClientSecret = "" |
| 71 |
return c |
| 72 |
}(), |
| 73 |
wantError: "ClientSecret is required", |
| 74 |
}, |
| 75 |
{ |
| 76 |
name: "valid config", |
| 77 |
config: validClientCredentialsConfig(), |
| 78 |
}, |
| 79 |
} |
| 80 |
|
| 81 |
for _, tc := range tests { |
| 82 |
t.Run(tc.name, func(t *testing.T) { |
| 83 |
_, err := NewClientCredentialsHandler(tc.config) |
| 84 |
if tc.wantError != "" { |
| 85 |
if err == nil { |
| 86 |
t.Errorf("expected error containing %q, got nil", tc.wantError) |
| 87 |
} else if !strings.Contains(err.Error(), tc.wantError) { |
| 88 |
t.Errorf("error %q does not contain %q", err.Error(), tc.wantError) |
| 89 |
} |
| 90 |
} else if err != nil { |
| 91 |
t.Errorf("unexpected error: %v", err) |
| 92 |
} |
| 93 |
}) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func TestClientCredentialsHandler_Authorize(t *testing.T) { |
| 98 |
authServer := oauthtest.NewFakeAuthorizationServer(oauthtest.Config{ |
| 99 |
MetadataEndpointConfig: &oauthtest.MetadataEndpointConfig{ |
| 100 |
ServeOAuthInsertedEndpoint: true, |
| 101 |
}, |
| 102 |
RegistrationConfig: &oauthtest.RegistrationConfig{ |
| 103 |
PreregisteredClients: map[string]oauthtest.ClientInfo{ |
| 104 |
"test-client": {Secret: "test-secret"}, |
| 105 |
}, |
| 106 |
}, |
| 107 |
ClientCredentialsConfig: &oauthtest.ClientCredentialsConfig{ |
| 108 |
Enabled: true, |
| 109 |
}, |
| 110 |
}) |
| 111 |
authServer.Start(t) |
| 112 |
|
| 113 |
resourceMux := http.NewServeMux() |
| 114 |
resourceServer := httptest.NewServer(resourceMux) |
| 115 |
t.Cleanup(resourceServer.Close) |
| 116 |
resourceURL := resourceServer.URL + "/resource" |
| 117 |
|
| 118 |
resourceMux.Handle("/.well-known/oauth-protected-resource/resource", auth.ProtectedResourceMetadataHandler(&oauthex.ProtectedResourceMetadata{ |
| 119 |
Resource: resourceURL, |
| 120 |
AuthorizationServers: []string{authServer.URL()}, |
| 121 |
ScopesSupported: []string{"mcp:read", "mcp:write"}, |
| 122 |
})) |
| 123 |
|
| 124 |
t.Run("successful authorization", func(t *testing.T) { |
| 125 |
handler, err := NewClientCredentialsHandler(validClientCredentialsConfig()) |
| 126 |
if err != nil { |
| 127 |
t.Fatal(err) |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
ts, err := handler.TokenSource(t.Context()) |
| 132 |
if err != nil { |
| 133 |
t.Fatal(err) |
| 134 |
} |
| 135 |
if ts != nil { |
| 136 |
t.Fatal("expected nil TokenSource before Authorize") |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
resp := &http.Response{ |
| 141 |
StatusCode: http.StatusUnauthorized, |
| 142 |
Header: http.Header{}, |
| 143 |
Body: http.NoBody, |
| 144 |
} |
| 145 |
req := httptest.NewRequest("GET", resourceURL, nil) |
| 146 |
if err := handler.Authorize(t.Context(), req, resp); err != nil { |
| 147 |
t.Fatalf("Authorize failed: %v", err) |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
ts, err = handler.TokenSource(t.Context()) |
| 152 |
if err != nil { |
| 153 |
t.Fatal(err) |
| 154 |
} |
| 155 |
if ts == nil { |
| 156 |
t.Fatal("expected non-nil TokenSource after Authorize") |
| 157 |
} |
| 158 |
tok, err := ts.Token() |
| 159 |
if err != nil { |
| 160 |
t.Fatalf("Token() failed: %v", err) |
| 161 |
} |
| 162 |
if tok.AccessToken != "test_access_token" { |
| 163 |
t.Errorf("got access token %q, want %q", tok.AccessToken, "test_access_token") |
| 164 |
} |
| 165 |
}) |
| 166 |
|
| 167 |
t.Run("bad credentials", func(t *testing.T) { |
| 168 |
config := validClientCredentialsConfig() |
| 169 |
config.Credentials.ClientSecretAuth.ClientSecret = "wrong-secret" |
| 170 |
handler, err := NewClientCredentialsHandler(config) |
| 171 |
if err != nil { |
| 172 |
t.Fatal(err) |
| 173 |
} |
| 174 |
|
| 175 |
resp := &http.Response{ |
| 176 |
StatusCode: http.StatusUnauthorized, |
| 177 |
Header: http.Header{}, |
| 178 |
Body: http.NoBody, |
| 179 |
} |
| 180 |
req := httptest.NewRequest("GET", resourceURL, nil) |
| 181 |
if err := handler.Authorize(t.Context(), req, resp); err == nil { |
| 182 |
t.Error("expected Authorize to fail with bad credentials") |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
ts, err := handler.TokenSource(t.Context()) |
| 187 |
if err != nil { |
| 188 |
t.Fatal(err) |
| 189 |
} |
| 190 |
if ts != nil { |
| 191 |
t.Error("expected nil TokenSource after failed Authorize") |
| 192 |
} |
| 193 |
}) |
| 194 |
|
| 195 |
t.Run("scopes from WWW-Authenticate challenge", func(t *testing.T) { |
| 196 |
handler, err := NewClientCredentialsHandler(validClientCredentialsConfig()) |
| 197 |
if err != nil { |
| 198 |
t.Fatal(err) |
| 199 |
} |
| 200 |
|
| 201 |
resp := &http.Response{ |
| 202 |
StatusCode: http.StatusUnauthorized, |
| 203 |
Header: http.Header{ |
| 204 |
"WWW-Authenticate": []string{`Bearer scope="read write"`}, |
| 205 |
}, |
| 206 |
Body: http.NoBody, |
| 207 |
} |
| 208 |
req := httptest.NewRequest("GET", resourceURL, nil) |
| 209 |
if err := handler.Authorize(t.Context(), req, resp); err != nil { |
| 210 |
t.Fatalf("Authorize failed: %v", err) |
| 211 |
} |
| 212 |
|
| 213 |
ts, err := handler.TokenSource(t.Context()) |
| 214 |
if err != nil { |
| 215 |
t.Fatal(err) |
| 216 |
} |
| 217 |
if ts == nil { |
| 218 |
t.Fatal("expected non-nil TokenSource") |
| 219 |
} |
| 220 |
}) |
| 221 |
|
| 222 |
t.Run("PRM via resource_metadata in challenge", func(t *testing.T) { |
| 223 |
prmMux := http.NewServeMux() |
| 224 |
prmMux.Handle("/custom-prm", auth.ProtectedResourceMetadataHandler(&oauthex.ProtectedResourceMetadata{ |
| 225 |
Resource: resourceURL, |
| 226 |
AuthorizationServers: []string{authServer.URL()}, |
| 227 |
})) |
| 228 |
prmServer := httptest.NewServer(prmMux) |
| 229 |
t.Cleanup(prmServer.Close) |
| 230 |
|
| 231 |
handler, err := NewClientCredentialsHandler(validClientCredentialsConfig()) |
| 232 |
if err != nil { |
| 233 |
t.Fatal(err) |
| 234 |
} |
| 235 |
|
| 236 |
resp := &http.Response{ |
| 237 |
StatusCode: http.StatusUnauthorized, |
| 238 |
Header: http.Header{ |
| 239 |
"WWW-Authenticate": []string{`Bearer resource_metadata="` + prmServer.URL + `/custom-prm"`}, |
| 240 |
}, |
| 241 |
Body: http.NoBody, |
| 242 |
} |
| 243 |
req := httptest.NewRequest("GET", resourceURL, nil) |
| 244 |
if err := handler.Authorize(t.Context(), req, resp); err != nil { |
| 245 |
t.Fatalf("Authorize with resource_metadata challenge failed: %v", err) |
| 246 |
} |
| 247 |
|
| 248 |
ts, err := handler.TokenSource(t.Context()) |
| 249 |
if err != nil { |
| 250 |
t.Fatal(err) |
| 251 |
} |
| 252 |
if ts == nil { |
| 253 |
t.Fatal("expected non-nil TokenSource") |
| 254 |
} |
| 255 |
}) |
| 256 |
} |
| 257 |
|
| 258 |
func TestSelectTokenAuthMethod(t *testing.T) { |
| 259 |
tests := []struct { |
| 260 |
name string |
| 261 |
supported []string |
| 262 |
want oauth2.AuthStyle |
| 263 |
}{ |
| 264 |
{ |
| 265 |
name: "prefers client_secret_post", |
| 266 |
supported: []string{"client_secret_basic", "client_secret_post"}, |
| 267 |
want: oauth2.AuthStyleInParams, |
| 268 |
}, |
| 269 |
{ |
| 270 |
name: "falls back to client_secret_basic", |
| 271 |
supported: []string{"client_secret_basic"}, |
| 272 |
want: oauth2.AuthStyleInHeader, |
| 273 |
}, |
| 274 |
{ |
| 275 |
name: "auto detect when none supported", |
| 276 |
supported: []string{"private_key_jwt"}, |
| 277 |
want: oauth2.AuthStyleAutoDetect, |
| 278 |
}, |
| 279 |
{ |
| 280 |
name: "auto detect when empty", |
| 281 |
supported: nil, |
| 282 |
want: oauth2.AuthStyleAutoDetect, |
| 283 |
}, |
| 284 |
} |
| 285 |
for _, tc := range tests { |
| 286 |
t.Run(tc.name, func(t *testing.T) { |
| 287 |
got := selectTokenAuthMethod(tc.supported) |
| 288 |
if got != tc.want { |
| 289 |
t.Errorf("selectTokenAuthMethod(%v) = %v, want %v", tc.supported, got, tc.want) |
| 290 |
} |
| 291 |
}) |
| 292 |
} |
| 293 |
} |