mirror of
https://github.com/h44z/wg-portal.git
synced 2026-12-05 11:46:09 +01:00
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:
@@ -124,13 +124,28 @@ func (o OidcAuthenticator) GetLogoutUrl(idTokenHint, postLogoutRedirectUri strin
|
|||||||
params.Set("id_token_hint", idTokenHint)
|
params.Set("id_token_hint", idTokenHint)
|
||||||
}
|
}
|
||||||
if postLogoutRedirectUri != "" {
|
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()
|
logoutUrl.RawQuery = params.Encode()
|
||||||
|
|
||||||
return logoutUrl.String(), true
|
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.
|
// PKCEAuthCodeOptions returns PKCE options for the authorization request and the verifier for the token exchange.
|
||||||
func (o OidcAuthenticator) PKCEAuthCodeOptions() ([]oauth2.AuthCodeOption, string) {
|
func (o OidcAuthenticator) PKCEAuthCodeOptions() ([]oauth2.AuthCodeOption, string) {
|
||||||
if !o.usePKCE {
|
if !o.usePKCE {
|
||||||
|
|||||||
@@ -77,3 +77,49 @@ func TestOidcAuthenticatorPKCEDisabled(t *testing.T) {
|
|||||||
t.Fatalf("expected no token options, got %d", len(tokenOptions))
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user