| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package auth |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"net/http" |
| 11 |
"net/http/httptest" |
| 12 |
"net/http/httputil" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
|
| 16 |
"github.com/google/go-cmp/cmp" |
| 17 |
"github.com/modelcontextprotocol/go-sdk/internal/oauthtest" |
| 18 |
"github.com/modelcontextprotocol/go-sdk/oauthex" |
| 19 |
"golang.org/x/oauth2" |
| 20 |
) |
| 21 |
|
| 22 |
func TestAuthorize(t *testing.T) { |
| 23 |
authServer := oauthtest.NewFakeAuthorizationServer(oauthtest.Config{ |
| 24 |
RegistrationConfig: &oauthtest.RegistrationConfig{ |
| 25 |
PreregisteredClients: map[string]oauthtest.ClientInfo{ |
| 26 |
"test_client_id": { |
| 27 |
Secret: "test_client_secret", |
| 28 |
RedirectURIs: []string{"http://localhost:12345/callback"}, |
| 29 |
}, |
| 30 |
}, |
| 31 |
}, |
| 32 |
}) |
| 33 |
authServer.Start(t) |
| 34 |
|
| 35 |
resourceMux := http.NewServeMux() |
| 36 |
resourceServer := httptest.NewServer(resourceMux) |
| 37 |
t.Cleanup(resourceServer.Close) |
| 38 |
resourceURL := resourceServer.URL + "/resource" |
| 39 |
|
| 40 |
resourceMux.Handle("/.well-known/oauth-protected-resource/resource", ProtectedResourceMetadataHandler(&oauthex.ProtectedResourceMetadata{ |
| 41 |
Resource: resourceURL, |
| 42 |
AuthorizationServers: []string{authServer.URL()}, |
| 43 |
})) |
| 44 |
|
| 45 |
handler, err := NewAuthorizationCodeHandler(&AuthorizationCodeHandlerConfig{ |
| 46 |
RedirectURL: "http://localhost:12345/callback", |
| 47 |
PreregisteredClient: &oauthex.ClientCredentials{ |
| 48 |
ClientID: "test_client_id", |
| 49 |
ClientSecretAuth: &oauthex.ClientSecretAuth{ |
| 50 |
ClientSecret: "test_client_secret", |
| 51 |
}, |
| 52 |
}, |
| 53 |
AuthorizationCodeFetcher: func(ctx context.Context, args *AuthorizationArgs) (*AuthorizationResult, error) { |
| 54 |
|
| 55 |
client := &http.Client{ |
| 56 |
CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 57 |
return http.ErrUseLastResponse |
| 58 |
}, |
| 59 |
} |
| 60 |
resp, err := client.Get(args.URL) |
| 61 |
if err != nil { |
| 62 |
return nil, fmt.Errorf("failed to visit auth URL: %v", err) |
| 63 |
} |
| 64 |
defer resp.Body.Close() |
| 65 |
dump, err := httputil.DumpResponse(resp, true) |
| 66 |
if err != nil { |
| 67 |
t.Fatalf("failed to dump response: %v", err) |
| 68 |
} |
| 69 |
t.Log(string(dump)) |
| 70 |
|
| 71 |
location, err := resp.Location() |
| 72 |
if err != nil { |
| 73 |
return nil, fmt.Errorf("failed to get location header: %v", err) |
| 74 |
} |
| 75 |
return &AuthorizationResult{ |
| 76 |
Code: location.Query().Get("code"), |
| 77 |
State: location.Query().Get("state"), |
| 78 |
}, nil |
| 79 |
}, |
| 80 |
}) |
| 81 |
if err != nil { |
| 82 |
t.Fatalf("NewAuthorizationCodeHandler failed: %v", err) |
| 83 |
} |
| 84 |
|
| 85 |
req := httptest.NewRequest(http.MethodGet, resourceURL, nil) |
| 86 |
resp := &http.Response{ |
| 87 |
StatusCode: http.StatusUnauthorized, |
| 88 |
Header: make(http.Header), |
| 89 |
Body: http.NoBody, |
| 90 |
Request: req, |
| 91 |
} |
| 92 |
resp.Header.Set( |
| 93 |
"WWW-Authenticate", |
| 94 |
"Bearer resource_metadata="+resourceServer.URL+"/.well-known/oauth-protected-resource/resource", |
| 95 |
) |
| 96 |
|
| 97 |
if err := handler.Authorize(context.Background(), req, resp); err != nil { |
| 98 |
t.Fatalf("Authorize failed: %v", err) |
| 99 |
} |
| 100 |
|
| 101 |
tokenSource, err := handler.TokenSource(t.Context()) |
| 102 |
if err != nil { |
| 103 |
t.Fatalf("Failed to get token source: %v", err) |
| 104 |
} |
| 105 |
token, err := tokenSource.Token() |
| 106 |
if err != nil { |
| 107 |
t.Fatalf("Failed to get token: %v", err) |
| 108 |
} |
| 109 |
if token.AccessToken != "test_access_token" { |
| 110 |
t.Errorf("Expected access token 'test_access_token', got '%s'", token.AccessToken) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
func TestAuthorize_ForbiddenUnhandledError(t *testing.T) { |
| 115 |
req := httptest.NewRequest(http.MethodGet, "http://example.com/resource", nil) |
| 116 |
resp := &http.Response{ |
| 117 |
StatusCode: http.StatusForbidden, |
| 118 |
Header: make(http.Header), |
| 119 |
Body: http.NoBody, |
| 120 |
Request: req, |
| 121 |
} |
| 122 |
resp.Header.Set( |
| 123 |
"WWW-Authenticate", |
| 124 |
"Bearer error=invalid_token", |
| 125 |
) |
| 126 |
handler, err := NewAuthorizationCodeHandler(validConfig()) |
| 127 |
if err != nil { |
| 128 |
t.Fatalf("NewAuthorizationCodeHandler failed: %v", err) |
| 129 |
} |
| 130 |
err = handler.Authorize(t.Context(), req, resp) |
| 131 |
if err != nil { |
| 132 |
t.Fatalf("Authorize() failed: %v", err) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func TestNewAuthorizationCodeHandler_Success(t *testing.T) { |
| 137 |
simpleHandler := func(ctx context.Context, args *AuthorizationArgs) (*AuthorizationResult, error) { |
| 138 |
return nil, nil |
| 139 |
} |
| 140 |
tests := []struct { |
| 141 |
name string |
| 142 |
config *AuthorizationCodeHandlerConfig |
| 143 |
}{ |
| 144 |
{ |
| 145 |
name: "ClientIDMetadataDocumentConfig", |
| 146 |
config: &AuthorizationCodeHandlerConfig{ |
| 147 |
ClientIDMetadataDocumentConfig: &ClientIDMetadataDocumentConfig{URL: "https://example.com/client"}, |
| 148 |
RedirectURL: "https://example.com/callback", |
| 149 |
AuthorizationCodeFetcher: simpleHandler, |
| 150 |
}, |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "PreregisteredClientConfig", |
| 154 |
config: &AuthorizationCodeHandlerConfig{ |
| 155 |
PreregisteredClient: &oauthex.ClientCredentials{ |
| 156 |
ClientID: "test_client_id", |
| 157 |
ClientSecretAuth: &oauthex.ClientSecretAuth{ |
| 158 |
ClientSecret: "test_client_secret", |
| 159 |
}, |
| 160 |
}, |
| 161 |
RedirectURL: "https://example.com/callback", |
| 162 |
AuthorizationCodeFetcher: simpleHandler, |
| 163 |
}, |
| 164 |
}, |
| 165 |
{ |
| 166 |
name: "DynamicClientRegistrationConfig_NoRedirectURL", |
| 167 |
config: &AuthorizationCodeHandlerConfig{ |
| 168 |
DynamicClientRegistrationConfig: &DynamicClientRegistrationConfig{ |
| 169 |
Metadata: &oauthex.ClientRegistrationMetadata{ |
| 170 |
RedirectURIs: []string{ |
| 171 |
"https://example.com/callback", |
| 172 |
}, |
| 173 |
}, |
| 174 |
}, |
| 175 |
AuthorizationCodeFetcher: simpleHandler, |
| 176 |
}, |
| 177 |
}, |
| 178 |
{ |
| 179 |
name: "DynamicClientRegistrationConfig_WithRedirectURL", |
| 180 |
config: &AuthorizationCodeHandlerConfig{ |
| 181 |
DynamicClientRegistrationConfig: &DynamicClientRegistrationConfig{ |
| 182 |
Metadata: &oauthex.ClientRegistrationMetadata{ |
| 183 |
RedirectURIs: []string{ |
| 184 |
"https://example.com/callback", |
| 185 |
}, |
| 186 |
}, |
| 187 |
}, |
| 188 |
RedirectURL: "https://example.com/callback", |
| 189 |
AuthorizationCodeFetcher: simpleHandler, |
| 190 |
}, |
| 191 |
}, |
| 192 |
} |
| 193 |
|
| 194 |
for _, tt := range tests { |
| 195 |
t.Run(tt.name, func(t *testing.T) { |
| 196 |
if _, err := NewAuthorizationCodeHandler(tt.config); err != nil { |
| 197 |
t.Fatalf("NewAuthorizationCodeHandler failed: %v", err) |
| 198 |
} |
| 199 |
}) |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
func TestNewAuthorizationCodeHandler_Error(t *testing.T) { |
| 204 |
|
| 205 |
if _, err := NewAuthorizationCodeHandler(validConfig()); err != nil { |
| 206 |
t.Fatalf("NewAuthorizationCodeHandler failed: %v", err) |
| 207 |
} |
| 208 |
|
| 209 |
tests := []struct { |
| 210 |
name string |
| 211 |
config func() *AuthorizationCodeHandlerConfig |
| 212 |
}{ |
| 213 |
{ |
| 214 |
name: "NilConfig", |
| 215 |
config: func() *AuthorizationCodeHandlerConfig { |
| 216 |
return nil |
| 217 |
}, |
| 218 |
}, |
| 219 |
{ |
| 220 |
name: "NoRegistrationConfig", |
| 221 |
config: func() *AuthorizationCodeHandlerConfig { |
| 222 |
cfg := validConfig() |
| 223 |
cfg.ClientIDMetadataDocumentConfig = nil |
| 224 |
cfg.PreregisteredClient = nil |
| 225 |
cfg.DynamicClientRegistrationConfig = nil |
| 226 |
return cfg |
| 227 |
}, |
| 228 |
}, |
| 229 |
{ |
| 230 |
name: "MissingRedirectURL", |
| 231 |
config: func() *AuthorizationCodeHandlerConfig { |
| 232 |
cfg := validConfig() |
| 233 |
cfg.RedirectURL = "" |
| 234 |
return cfg |
| 235 |
}, |
| 236 |
}, |
| 237 |
{ |
| 238 |
name: "MissingAuthorizationCodeFetcher", |
| 239 |
config: func() *AuthorizationCodeHandlerConfig { |
| 240 |
cfg := validConfig() |
| 241 |
cfg.AuthorizationCodeFetcher = nil |
| 242 |
return cfg |
| 243 |
}, |
| 244 |
}, |
| 245 |
{ |
| 246 |
name: "InvalidMetadataURL", |
| 247 |
config: func() *AuthorizationCodeHandlerConfig { |
| 248 |
cfg := validConfig() |
| 249 |
cfg.ClientIDMetadataDocumentConfig.URL = "https://example.com" |
| 250 |
return cfg |
| 251 |
}, |
| 252 |
}, |
| 253 |
{ |
| 254 |
name: "InvalidPreregistered_MissingSecretConfig", |
| 255 |
config: func() *AuthorizationCodeHandlerConfig { |
| 256 |
cfg := validConfig() |
| 257 |
cfg.PreregisteredClient = &oauthex.ClientCredentials{} |
| 258 |
return cfg |
| 259 |
}, |
| 260 |
}, |
| 261 |
{ |
| 262 |
name: "InvalidPreregistered_EmptyID", |
| 263 |
config: func() *AuthorizationCodeHandlerConfig { |
| 264 |
cfg := validConfig() |
| 265 |
cfg.PreregisteredClient = &oauthex.ClientCredentials{ |
| 266 |
ClientID: "", |
| 267 |
ClientSecretAuth: &oauthex.ClientSecretAuth{ |
| 268 |
ClientSecret: "secret", |
| 269 |
}, |
| 270 |
} |
| 271 |
return cfg |
| 272 |
}, |
| 273 |
}, |
| 274 |
{ |
| 275 |
name: "InvalidPreregistered_EmptySecret", |
| 276 |
config: func() *AuthorizationCodeHandlerConfig { |
| 277 |
cfg := validConfig() |
| 278 |
cfg.PreregisteredClient = &oauthex.ClientCredentials{ |
| 279 |
ClientID: "test_client_id", |
| 280 |
ClientSecretAuth: &oauthex.ClientSecretAuth{ |
| 281 |
ClientSecret: "", |
| 282 |
}, |
| 283 |
} |
| 284 |
return cfg |
| 285 |
}, |
| 286 |
}, |
| 287 |
{ |
| 288 |
name: "InvalidDynamic_MissingMetadata", |
| 289 |
config: func() *AuthorizationCodeHandlerConfig { |
| 290 |
cfg := validConfig() |
| 291 |
cfg.DynamicClientRegistrationConfig = &DynamicClientRegistrationConfig{} |
| 292 |
return cfg |
| 293 |
}, |
| 294 |
}, |
| 295 |
{ |
| 296 |
name: "InvalidDynamic_InconsistentRedirectURI", |
| 297 |
config: func() *AuthorizationCodeHandlerConfig { |
| 298 |
cfg := validConfig() |
| 299 |
cfg.DynamicClientRegistrationConfig = &DynamicClientRegistrationConfig{ |
| 300 |
Metadata: &oauthex.ClientRegistrationMetadata{ |
| 301 |
RedirectURIs: []string{"https://example.com/callback1"}, |
| 302 |
}, |
| 303 |
} |
| 304 |
cfg.RedirectURL = "https://example.com/callback2" |
| 305 |
return cfg |
| 306 |
}, |
| 307 |
}, |
| 308 |
} |
| 309 |
|
| 310 |
for _, tt := range tests { |
| 311 |
t.Run(tt.name, func(t *testing.T) { |
| 312 |
_, err := NewAuthorizationCodeHandler(tt.config()) |
| 313 |
if err == nil { |
| 314 |
t.Errorf("NewAuthorizationCodeHandler() = nil, want error") |
| 315 |
} |
| 316 |
}) |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
func TestGetProtectedResourceMetadata_Success(t *testing.T) { |
| 321 |
handler, err := NewAuthorizationCodeHandler(validConfig()) |
| 322 |
if err != nil { |
| 323 |
t.Fatalf("NewAuthorizationCodeHandler() error = %v", err) |
| 324 |
} |
| 325 |
|
| 326 |
pathForChallenge := "/protected-resource" |
| 327 |
|
| 328 |
tests := []struct { |
| 329 |
name string |
| 330 |
challengesProvided bool |
| 331 |
|
| 332 |
prmPath string |
| 333 |
|
| 334 |
mcpServerPath string |
| 335 |
|
| 336 |
resourcePath string |
| 337 |
}{ |
| 338 |
{ |
| 339 |
name: "FromChallenges", |
| 340 |
challengesProvided: true, |
| 341 |
prmPath: pathForChallenge, |
| 342 |
mcpServerPath: "/resource", |
| 343 |
resourcePath: "/resource", |
| 344 |
}, |
| 345 |
{ |
| 346 |
name: "FallbackToEndpoint", |
| 347 |
challengesProvided: false, |
| 348 |
prmPath: "/.well-known/oauth-protected-resource/resource", |
| 349 |
mcpServerPath: "/resource", |
| 350 |
resourcePath: "/resource", |
| 351 |
}, |
| 352 |
{ |
| 353 |
name: "FallbackToRoot", |
| 354 |
challengesProvided: false, |
| 355 |
prmPath: "/.well-known/oauth-protected-resource", |
| 356 |
mcpServerPath: "/resource", |
| 357 |
resourcePath: "", |
| 358 |
}, |
| 359 |
} |
| 360 |
|
| 361 |
for _, tt := range tests { |
| 362 |
t.Run(tt.name, func(t *testing.T) { |
| 363 |
mux := http.NewServeMux() |
| 364 |
server := httptest.NewServer(mux) |
| 365 |
t.Cleanup(server.Close) |
| 366 |
metadata := &oauthex.ProtectedResourceMetadata{ |
| 367 |
Resource: server.URL + tt.resourcePath, |
| 368 |
AuthorizationServers: []string{"https://oauth.example.com"}, |
| 369 |
ScopesSupported: []string{"read", "write"}, |
| 370 |
} |
| 371 |
mux.Handle(tt.prmPath, ProtectedResourceMetadataHandler(metadata)) |
| 372 |
var challenges []oauthex.Challenge |
| 373 |
if tt.challengesProvided { |
| 374 |
challenges = []oauthex.Challenge{ |
| 375 |
{ |
| 376 |
Scheme: "Bearer", |
| 377 |
Params: map[string]string{ |
| 378 |
"resource_metadata": server.URL + pathForChallenge, |
| 379 |
}, |
| 380 |
}, |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
got, err := handler.getProtectedResourceMetadata(t.Context(), challenges, server.URL+tt.mcpServerPath) |
| 385 |
if err != nil { |
| 386 |
t.Fatalf("getProtectedResourceMetadata() error = %v", err) |
| 387 |
} |
| 388 |
if got == nil { |
| 389 |
t.Fatal("getProtectedResourceMetadata() got nil, want metadata") |
| 390 |
} |
| 391 |
if diff := cmp.Diff(metadata, got); diff != "" { |
| 392 |
t.Errorf("getProtectedResourceMetadata() metadata mismatch (-want +got):\n%s", diff) |
| 393 |
} |
| 394 |
}) |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
func TestGetProtectedResourceMetadata_Backcompat(t *testing.T) { |
| 399 |
handler, err := NewAuthorizationCodeHandler(validConfig()) |
| 400 |
if err != nil { |
| 401 |
t.Fatalf("NewAuthorizationCodeHandler() error = %v", err) |
| 402 |
} |
| 403 |
var challenges []oauthex.Challenge |
| 404 |
got, err := handler.getProtectedResourceMetadata(t.Context(), challenges, "http://localhost:1234/resource") |
| 405 |
if err != nil { |
| 406 |
t.Fatalf("getProtectedResourceMetadata() error = %v", err) |
| 407 |
} |
| 408 |
wantPRM := &oauthex.ProtectedResourceMetadata{ |
| 409 |
Resource: "http://localhost:1234/resource", |
| 410 |
AuthorizationServers: []string{"http://localhost:1234"}, |
| 411 |
} |
| 412 |
if diff := cmp.Diff(wantPRM, got); diff != "" { |
| 413 |
t.Errorf("getProtectedResourceMetadata() metadata mismatch (-want +got):\n%s", diff) |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
func TestGetProtectedResourceMetadata_Error(t *testing.T) { |
| 418 |
mux := http.NewServeMux() |
| 419 |
server := httptest.NewServer(mux) |
| 420 |
t.Cleanup(server.Close) |
| 421 |
metadata := &oauthex.ProtectedResourceMetadata{ |
| 422 |
Resource: server.URL + "/resource", |
| 423 |
AuthorizationServers: nil, |
| 424 |
ScopesSupported: []string{"read", "write"}, |
| 425 |
} |
| 426 |
mux.Handle("/.well-known/oauth-protected-resource/resource", ProtectedResourceMetadataHandler(metadata)) |
| 427 |
handler, err := NewAuthorizationCodeHandler(validConfig()) |
| 428 |
if err != nil { |
| 429 |
t.Fatalf("NewAuthorizationCodeHandler() error = %v", err) |
| 430 |
} |
| 431 |
var challenges []oauthex.Challenge |
| 432 |
got, err := handler.getProtectedResourceMetadata(t.Context(), challenges, server.URL+"/resource") |
| 433 |
if err == nil || !strings.Contains(err.Error(), "authorization servers") { |
| 434 |
t.Errorf("getProtectedResourceMetadata() = %v, want error containing \"authorization servers\"", err) |
| 435 |
} |
| 436 |
if got != nil { |
| 437 |
t.Errorf("getProtectedResourceMetadata() = %+v, want nil", got) |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
func TestSelectTokenAuthMethod(t *testing.T) { |
| 442 |
tests := []struct { |
| 443 |
name string |
| 444 |
supported []string |
| 445 |
want oauth2.AuthStyle |
| 446 |
}{ |
| 447 |
{ |
| 448 |
name: "PostPreferredOverBasic", |
| 449 |
supported: []string{"client_secret_basic", "client_secret_post"}, |
| 450 |
want: oauth2.AuthStyleInParams, |
| 451 |
}, |
| 452 |
{ |
| 453 |
name: "BasicChosenIfPostNotSupported", |
| 454 |
supported: []string{"private_key_jwt", "client_secret_basic"}, |
| 455 |
want: oauth2.AuthStyleInHeader, |
| 456 |
}, |
| 457 |
{ |
| 458 |
name: "NoneSupported", |
| 459 |
supported: []string{"private_key_jwt"}, |
| 460 |
want: oauth2.AuthStyleAutoDetect, |
| 461 |
}, |
| 462 |
} |
| 463 |
|
| 464 |
for _, tt := range tests { |
| 465 |
t.Run(tt.name, func(t *testing.T) { |
| 466 |
got := selectTokenAuthMethod(tt.supported) |
| 467 |
if got != tt.want { |
| 468 |
t.Errorf("selectTokenAuthMethod() = %v, want %v", got, tt.want) |
| 469 |
} |
| 470 |
}) |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
func TestHandleRegistration(t *testing.T) { |
| 475 |
tests := []struct { |
| 476 |
name string |
| 477 |
serverConfig *oauthtest.RegistrationConfig |
| 478 |
handlerConfig *AuthorizationCodeHandlerConfig |
| 479 |
asm *oauthex.AuthServerMeta |
| 480 |
want *resolvedClientConfig |
| 481 |
wantError bool |
| 482 |
}{ |
| 483 |
{ |
| 484 |
name: "ClientIDMetadataDocument", |
| 485 |
serverConfig: &oauthtest.RegistrationConfig{ |
| 486 |
ClientIDMetadataDocumentSupported: true, |
| 487 |
}, |
| 488 |
handlerConfig: &AuthorizationCodeHandlerConfig{ |
| 489 |
ClientIDMetadataDocumentConfig: &ClientIDMetadataDocumentConfig{URL: "https://client.example.com/metadata.json"}, |
| 490 |
}, |
| 491 |
want: &resolvedClientConfig{ |
| 492 |
registrationType: registrationTypeClientIDMetadataDocument, |
| 493 |
clientID: "https://client.example.com/metadata.json", |
| 494 |
}, |
| 495 |
}, |
| 496 |
{ |
| 497 |
name: "Preregistered", |
| 498 |
serverConfig: &oauthtest.RegistrationConfig{ |
| 499 |
PreregisteredClients: map[string]oauthtest.ClientInfo{ |
| 500 |
"pre_client_id": { |
| 501 |
Secret: "pre_client_secret", |
| 502 |
}, |
| 503 |
}, |
| 504 |
}, |
| 505 |
handlerConfig: &AuthorizationCodeHandlerConfig{ |
| 506 |
PreregisteredClient: &oauthex.ClientCredentials{ |
| 507 |
ClientID: "pre_client_id", |
| 508 |
ClientSecretAuth: &oauthex.ClientSecretAuth{ |
| 509 |
ClientSecret: "pre_client_secret", |
| 510 |
}, |
| 511 |
}, |
| 512 |
}, |
| 513 |
want: &resolvedClientConfig{ |
| 514 |
registrationType: registrationTypePreregistered, |
| 515 |
clientID: "pre_client_id", |
| 516 |
clientSecret: "pre_client_secret", |
| 517 |
authStyle: oauth2.AuthStyleInParams, |
| 518 |
}, |
| 519 |
}, |
| 520 |
{ |
| 521 |
name: "NoneSupported", |
| 522 |
handlerConfig: &AuthorizationCodeHandlerConfig{ |
| 523 |
ClientIDMetadataDocumentConfig: &ClientIDMetadataDocumentConfig{URL: "https://client.example.com/metadata.json"}, |
| 524 |
}, |
| 525 |
wantError: true, |
| 526 |
}, |
| 527 |
} |
| 528 |
|
| 529 |
for _, tt := range tests { |
| 530 |
t.Run(tt.name, func(t *testing.T) { |
| 531 |
s := oauthtest.NewFakeAuthorizationServer(oauthtest.Config{RegistrationConfig: tt.serverConfig}) |
| 532 |
s.Start(t) |
| 533 |
tt.handlerConfig.AuthorizationCodeFetcher = func(ctx context.Context, args *AuthorizationArgs) (*AuthorizationResult, error) { |
| 534 |
return nil, nil |
| 535 |
} |
| 536 |
tt.handlerConfig.RedirectURL = "https://example.com/callback" |
| 537 |
handler, err := NewAuthorizationCodeHandler(tt.handlerConfig) |
| 538 |
if err != nil { |
| 539 |
t.Fatalf("NewAuthorizationCodeHandler() error = %v, want nil", err) |
| 540 |
} |
| 541 |
asm, err := GetAuthServerMetadata(t.Context(), s.URL(), http.DefaultClient) |
| 542 |
if err != nil { |
| 543 |
t.Fatalf("GetAuthServerMetadata() unexpected error = %v", err) |
| 544 |
} |
| 545 |
got, err := handler.handleRegistration(t.Context(), asm) |
| 546 |
if err != nil { |
| 547 |
if !tt.wantError { |
| 548 |
t.Fatalf("handleRegistration() unexpected error = %v", err) |
| 549 |
} |
| 550 |
return |
| 551 |
} |
| 552 |
if got.registrationType != tt.want.registrationType { |
| 553 |
t.Errorf("handleRegistration() registrationType = %v, want %v", got.registrationType, tt.want.registrationType) |
| 554 |
} |
| 555 |
if got.clientID != tt.want.clientID { |
| 556 |
t.Errorf("handleRegistration() clientID = %q, want %q", got.clientID, tt.want.clientID) |
| 557 |
} |
| 558 |
if got.clientSecret != tt.want.clientSecret { |
| 559 |
t.Errorf("handleRegistration() clientSecret = %q, want %q", got.clientSecret, tt.want.clientSecret) |
| 560 |
} |
| 561 |
if got.authStyle != tt.want.authStyle { |
| 562 |
t.Errorf("handleRegistration() authStyle = %v, want %v", got.authStyle, tt.want.authStyle) |
| 563 |
} |
| 564 |
}) |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
func TestDynamicRegistration(t *testing.T) { |
| 569 |
s := oauthtest.NewFakeAuthorizationServer(oauthtest.Config{ |
| 570 |
RegistrationConfig: &oauthtest.RegistrationConfig{ |
| 571 |
DynamicClientRegistrationEnabled: true, |
| 572 |
}, |
| 573 |
}) |
| 574 |
s.Start(t) |
| 575 |
handler, err := NewAuthorizationCodeHandler(&AuthorizationCodeHandlerConfig{ |
| 576 |
DynamicClientRegistrationConfig: &DynamicClientRegistrationConfig{ |
| 577 |
Metadata: &oauthex.ClientRegistrationMetadata{ |
| 578 |
RedirectURIs: []string{"https://example.com/callback"}, |
| 579 |
}, |
| 580 |
}, |
| 581 |
RedirectURL: "https://example.com/callback", |
| 582 |
AuthorizationCodeFetcher: func(ctx context.Context, args *AuthorizationArgs) (*AuthorizationResult, error) { |
| 583 |
return nil, nil |
| 584 |
}, |
| 585 |
}) |
| 586 |
if err != nil { |
| 587 |
t.Fatalf("NewAuthorizationCodeHandler() error = %v", err) |
| 588 |
} |
| 589 |
asm, err := GetAuthServerMetadata(t.Context(), s.URL(), http.DefaultClient) |
| 590 |
if err != nil { |
| 591 |
t.Fatalf("GetAuthServerMetadata() unexpected error = %v", err) |
| 592 |
} |
| 593 |
got, err := handler.handleRegistration(t.Context(), asm) |
| 594 |
if err != nil { |
| 595 |
t.Fatalf("handleRegistration() error = %v, want nil", err) |
| 596 |
} |
| 597 |
if got.registrationType != registrationTypeDynamic { |
| 598 |
t.Errorf("handleRegistration() registrationType = %v, want %v", got.registrationType, registrationTypeDynamic) |
| 599 |
} |
| 600 |
if got.clientID == "" { |
| 601 |
t.Errorf("handleRegistration() clientID = %q, want non-empty", got.clientID) |
| 602 |
} |
| 603 |
if got.clientSecret == "" { |
| 604 |
t.Errorf("handleRegistration() clientSecret = %q, want non-empty", got.clientSecret) |
| 605 |
} |
| 606 |
if got.authStyle != oauth2.AuthStyleInHeader { |
| 607 |
t.Errorf("handleRegistration() authStyle = %v, want %v", got.authStyle, oauth2.AuthStyleInHeader) |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
func TestInferApplicationType(t *testing.T) { |
| 612 |
tests := []struct { |
| 613 |
name string |
| 614 |
redirectURIs []string |
| 615 |
want string |
| 616 |
}{ |
| 617 |
{ |
| 618 |
name: "localhost", |
| 619 |
redirectURIs: []string{"http://localhost:8085/callback"}, |
| 620 |
want: "native", |
| 621 |
}, |
| 622 |
{ |
| 623 |
name: "127.0.0.1", |
| 624 |
redirectURIs: []string{"http://127.0.0.1:8085/callback"}, |
| 625 |
want: "native", |
| 626 |
}, |
| 627 |
{ |
| 628 |
name: "IPv6 loopback", |
| 629 |
redirectURIs: []string{"http://[::1]:8085/callback"}, |
| 630 |
want: "native", |
| 631 |
}, |
| 632 |
{ |
| 633 |
name: "custom scheme", |
| 634 |
redirectURIs: []string{"myapp://callback"}, |
| 635 |
want: "native", |
| 636 |
}, |
| 637 |
{ |
| 638 |
name: "HTTPS remote", |
| 639 |
redirectURIs: []string{"https://myapp.example.com/callback"}, |
| 640 |
want: "web", |
| 641 |
}, |
| 642 |
{ |
| 643 |
name: "mixed native and web", |
| 644 |
redirectURIs: []string{"https://myapp.example.com/callback", "http://localhost:8085/callback"}, |
| 645 |
want: "", |
| 646 |
}, |
| 647 |
{ |
| 648 |
name: "multiple remote", |
| 649 |
redirectURIs: []string{"https://app1.example.com/cb", "https://app2.example.com/cb"}, |
| 650 |
want: "web", |
| 651 |
}, |
| 652 |
} |
| 653 |
for _, tt := range tests { |
| 654 |
t.Run(tt.name, func(t *testing.T) { |
| 655 |
got := inferApplicationType(tt.redirectURIs) |
| 656 |
if got != tt.want { |
| 657 |
t.Errorf("inferApplicationType() = %q, want %q", got, tt.want) |
| 658 |
} |
| 659 |
}) |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
func TestApplicationTypeInference(t *testing.T) { |
| 664 |
fetcher := func(ctx context.Context, args *AuthorizationArgs) (*AuthorizationResult, error) { |
| 665 |
return nil, nil |
| 666 |
} |
| 667 |
|
| 668 |
tests := []struct { |
| 669 |
name string |
| 670 |
redirectURIs []string |
| 671 |
initialAppType string |
| 672 |
wantAppType string |
| 673 |
wantErr bool |
| 674 |
}{ |
| 675 |
{ |
| 676 |
name: "inferred as native for localhost", |
| 677 |
redirectURIs: []string{"http://localhost:8085/callback"}, |
| 678 |
wantAppType: "native", |
| 679 |
}, |
| 680 |
{ |
| 681 |
name: "inferred as web for remote", |
| 682 |
redirectURIs: []string{"https://example.com/callback"}, |
| 683 |
wantAppType: "web", |
| 684 |
}, |
| 685 |
{ |
| 686 |
name: "mixed native and web URIs sets empty application type", |
| 687 |
redirectURIs: []string{"https://example.com/callback", "http://localhost:8085/callback"}, |
| 688 |
wantAppType: "", |
| 689 |
}, |
| 690 |
{ |
| 691 |
name: "explicit value matching inference is preserved", |
| 692 |
redirectURIs: []string{"http://localhost:8085/callback"}, |
| 693 |
initialAppType: "native", |
| 694 |
wantAppType: "native", |
| 695 |
}, |
| 696 |
{ |
| 697 |
name: "explicit value conflicts with inference returns error", |
| 698 |
redirectURIs: []string{"http://localhost:8085/callback"}, |
| 699 |
initialAppType: "web", |
| 700 |
wantErr: true, |
| 701 |
}, |
| 702 |
{ |
| 703 |
name: "explicit value when inference is ambiguous returns error", |
| 704 |
redirectURIs: []string{"https://example.com/callback", "http://localhost:8085/callback"}, |
| 705 |
initialAppType: "web", |
| 706 |
wantErr: true, |
| 707 |
}, |
| 708 |
{ |
| 709 |
name: "invalid URI returns empty application type", |
| 710 |
redirectURIs: []string{"http://%/"}, |
| 711 |
wantAppType: "", |
| 712 |
}, |
| 713 |
} |
| 714 |
|
| 715 |
for _, tt := range tests { |
| 716 |
t.Run(tt.name, func(t *testing.T) { |
| 717 |
cfg := &AuthorizationCodeHandlerConfig{ |
| 718 |
DynamicClientRegistrationConfig: &DynamicClientRegistrationConfig{ |
| 719 |
Metadata: &oauthex.ClientRegistrationMetadata{ |
| 720 |
RedirectURIs: tt.redirectURIs, |
| 721 |
ApplicationType: tt.initialAppType, |
| 722 |
}, |
| 723 |
}, |
| 724 |
AuthorizationCodeFetcher: fetcher, |
| 725 |
} |
| 726 |
_, err := NewAuthorizationCodeHandler(cfg) |
| 727 |
if (err != nil) != tt.wantErr { |
| 728 |
t.Fatalf("NewAuthorizationCodeHandler() error = %v, wantErr %v", err, tt.wantErr) |
| 729 |
} |
| 730 |
if tt.wantErr { |
| 731 |
return |
| 732 |
} |
| 733 |
got := cfg.DynamicClientRegistrationConfig.Metadata.ApplicationType |
| 734 |
if got != tt.wantAppType { |
| 735 |
t.Errorf("ApplicationType = %q, want %q", got, tt.wantAppType) |
| 736 |
} |
| 737 |
}) |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
func validConfig() *AuthorizationCodeHandlerConfig { |
| 744 |
return &AuthorizationCodeHandlerConfig{ |
| 745 |
ClientIDMetadataDocumentConfig: &ClientIDMetadataDocumentConfig{URL: "https://example.com/client"}, |
| 746 |
RedirectURL: "https://example.com/callback", |
| 747 |
AuthorizationCodeFetcher: func(ctx context.Context, args *AuthorizationArgs) (*AuthorizationResult, error) { |
| 748 |
return nil, nil |
| 749 |
}, |
| 750 |
} |
| 751 |
} |