fix(auth): drop the fragment from the OIDC logout redirect

The frontend routes are hash routes, so frontendUrl("/login") builds
https://host/app/#/login. That value went into post_logout_redirect_uri, which
OIDC RP-Initiated Logout does not allow to carry a fragment; Keycloak rejects
the request, so logging out with logout_idp_session: true failed, and turning it
off left the IdP session open.

Strip the fragment before the value is put on the end session URL. The browser
lands on the app root instead, which the client already handles.
This commit is contained in:
compiler-segfault
2026-11-25 20:57:58 +08:00
parent 61c8ceaab8
commit 8773731682
2 changed files with 62 additions and 1 deletions
+16 -1
View File
@@ -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 {
+46
View File
@@ -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)
}
}