Verifies OpenID Connect callback URL in redirect link is returned in the Location header for both oidc and _oidc_challenge requests.
(t *testing.T)
| 383 | // Verifies OpenID Connect callback URL in redirect link is returned in the Location |
| 384 | // header for both oidc and _oidc_challenge requests. |
| 385 | func TestGetOIDCCallbackURL(t *testing.T) { |
| 386 | type test struct { |
| 387 | name string |
| 388 | authURL string |
| 389 | issuer string |
| 390 | wantProvider string |
| 391 | } |
| 392 | tests := []test{ |
| 393 | { |
| 394 | // Provider should be included in callback URL when the requested provider is not default. |
| 395 | name: "requested provider is not default", |
| 396 | authURL: "/db/${path}?provider=bar&offline=true", |
| 397 | wantProvider: "bar", |
| 398 | issuer: "${path}/bar", |
| 399 | }, { |
| 400 | // Provider should NOT be included in callback URL when the requested provider is not default. |
| 401 | name: "requested provider is default", |
| 402 | authURL: "/db/${path}?provider=foo&offline=true", |
| 403 | issuer: "${path}/foo", |
| 404 | }, { |
| 405 | // Provider should NOT be included in callback URL when no provider is requested. |
| 406 | name: "no provider is requested", |
| 407 | authURL: "/db/${path}?offline=true", |
| 408 | issuer: "${path}/foo", |
| 409 | }, |
| 410 | } |
| 411 | |
| 412 | for _, tc := range tests { |
| 413 | t.Run(tc.name, func(t *testing.T) { |
| 414 | providers := auth.OIDCProviderMap{"foo": mockProvider("foo"), "bar": mockProvider("bar")} |
| 415 | openIDConnectOptions := auth.OIDCOptions{Providers: providers, DefaultProvider: base.Ptr("foo")} |
| 416 | rtConfig := RestTesterConfig{DatabaseConfig: &DatabaseConfig{DbConfig: DbConfig{OIDCConfig: &openIDConnectOptions}}} |
| 417 | rt := NewRestTester(t, &rtConfig) |
| 418 | defer rt.Close() |
| 419 | |
| 420 | mockAuthServer, err := newMockAuthServer() |
| 421 | require.NoError(t, err, "Error creating mock oauth2 server") |
| 422 | mockAuthServer.Start() |
| 423 | defer mockAuthServer.Shutdown() |
| 424 | refreshProviderConfig(providers, mockAuthServer.URL) |
| 425 | tc.issuer = strings.ReplaceAll(tc.issuer, "${path}", mockAuthServer.URL) |
| 426 | mockAuthServer.options.issuer = tc.issuer |
| 427 | |
| 428 | // Check _oidc_challenge behavior |
| 429 | authURL := strings.ReplaceAll(tc.authURL, "${path}", "_oidc_challenge") |
| 430 | resp := rt.SendAdminRequest(http.MethodGet, authURL, "") |
| 431 | require.Equal(t, http.StatusUnauthorized, resp.Code) |
| 432 | wwwAuthHeader := resp.Header().Get("Www-Authenticate") |
| 433 | location := regexp.MustCompile(`login="(?P<login>.*?)"`).FindStringSubmatch(wwwAuthHeader)[1] |
| 434 | |
| 435 | require.NotEmpty(t, location, "error extracting location from header") |
| 436 | locationURL, err := url.Parse(location) |
| 437 | require.NoError(t, err, "error parsing location URL") |
| 438 | redirectURI := locationURL.Query().Get(requestParamRedirectURI) |
| 439 | redirectURL, err := url.Parse(redirectURI) |
| 440 | require.NoError(t, err, "error parsing redirect_uri URL") |
| 441 | assert.Equal(t, tc.wantProvider, redirectURL.Query().Get(auth.OIDCAuthProvider)) |
| 442 |
nothing calls this directly
no test coverage detected