diff --git a/internal/app/auth/auth_oidc.go b/internal/app/auth/auth_oidc.go index 2dc01378..c250fef3 100644 --- a/internal/app/auth/auth_oidc.go +++ b/internal/app/auth/auth_oidc.go @@ -124,13 +124,28 @@ func (o OidcAuthenticator) GetLogoutUrl(idTokenHint, postLogoutRedirectUri strin params.Set("id_token_hint", idTokenHint) } if postLogoutRedirectUri != "" { - params.Set("post_logout_redirect_uri", postLogoutRedirectUri) + // A post_logout_redirect_uri must not contain a fragment (OIDC RP-Initiated + // Logout 1.0, section 2), and providers such as Keycloak reject the logout + // request when it does. The frontend routes are hash routes, so drop the + // fragment instead of failing the whole logout. + params.Set("post_logout_redirect_uri", withoutFragment(postLogoutRedirectUri)) } logoutUrl.RawQuery = params.Encode() return logoutUrl.String(), true } +// withoutFragment returns uri without its fragment, so it can be used as an OIDC +// redirect URI. An unparsable uri is returned unchanged. +func withoutFragment(uri string) string { + parsed, err := url.Parse(uri) + if err != nil { + return uri + } + parsed.Fragment = "" + return parsed.String() +} + // PKCEAuthCodeOptions returns PKCE options for the authorization request and the verifier for the token exchange. func (o OidcAuthenticator) PKCEAuthCodeOptions() ([]oauth2.AuthCodeOption, string) { if !o.usePKCE { diff --git a/internal/app/auth/auth_oidc_test.go b/internal/app/auth/auth_oidc_test.go index d57de35a..7cac9057 100644 --- a/internal/app/auth/auth_oidc_test.go +++ b/internal/app/auth/auth_oidc_test.go @@ -77,3 +77,49 @@ func TestOidcAuthenticatorPKCEDisabled(t *testing.T) { t.Fatalf("expected no token options, got %d", len(tokenOptions)) } } + +func TestOidcAuthenticatorGetLogoutUrl(t *testing.T) { + provider := OidcAuthenticator{ + name: "test", + endSessionEndpoint: "https://idp.example.com/realms/test/protocol/openid-connect/logout", + logoutIdpSession: true, + } + + logoutUrl, ok := provider.GetLogoutUrl("the-token", "https://wg.example.com/app/#/login") + if !ok { + t.Fatal("expected a logout url") + } + + parsed, err := url.Parse(logoutUrl) + if err != nil { + t.Fatalf("failed to parse logout url: %v", err) + } + + query := parsed.Query() + if got := query.Get("id_token_hint"); got != "the-token" { + t.Fatalf("expected id_token_hint to be kept, got %q", got) + } + if got := query.Get("post_logout_redirect_uri"); got != "https://wg.example.com/app/" { + t.Fatalf("expected the fragment to be dropped, got %q", got) + } + + // a redirect that has no fragment is passed on unchanged + logoutUrl, _ = provider.GetLogoutUrl("", "https://wg.example.com/app/login") + parsed, err = url.Parse(logoutUrl) + if err != nil { + t.Fatalf("failed to parse logout url: %v", err) + } + if got := parsed.Query().Get("post_logout_redirect_uri"); got != "https://wg.example.com/app/login" { + t.Fatalf("expected the redirect to be kept, got %q", got) + } + + // without a redirect there is nothing to normalise + logoutUrl, _ = provider.GetLogoutUrl("the-token", "") + parsed, err = url.Parse(logoutUrl) + if err != nil { + t.Fatalf("failed to parse logout url: %v", err) + } + if got := parsed.Query().Get("post_logout_redirect_uri"); got != "" { + t.Fatalf("expected no post_logout_redirect_uri, got %q", got) + } +}