diff --git a/helpers.go b/helpers.go index 8f135b7..0e9c6a9 100644 --- a/helpers.go +++ b/helpers.go @@ -385,14 +385,22 @@ func (t *TraefikOidc) handleLogout(rw http.ResponseWriter, req *http.Request) { return } - // Build post logout redirect URI if not set + // Build post logout redirect URI with FQDN if not set if t.postLogoutRedirectURI == "" { - t.postLogoutRedirectURI = buildFullURL(t.scheme, t.determineHost(req), "/") + // Get the host from request headers or direct host + host := t.determineHost(req) + scheme := t.determineScheme(req) + + // Build the full URL with scheme and host + t.postLogoutRedirectURI = fmt.Sprintf("%s://%s%s", + scheme, + host, + t.logoutURLPath) } // If we have an end session endpoint and an ID token, use OIDC end session if t.endSessionURL != "" && idToken != "" { - logoutURL, err := BuildLogoutURL(t.endSessionURL, idToken, t.logoutURLPath) + logoutURL, err := BuildLogoutURL(t.endSessionURL, idToken, t.postLogoutRedirectURI) if err != nil { handleError(rw, fmt.Sprintf("Failed to build logout URL: %v", err), http.StatusInternalServerError, t.logger) return @@ -414,6 +422,10 @@ func BuildLogoutURL(endSessionURL, idToken, postLogoutRedirectURI string) (strin return "", fmt.Errorf("failed to parse end session URL: %w", err) } + if postLogoutRedirectURI == "" { + postLogoutRedirectURI = "/" + } + q := u.Query() q.Set("id_token_hint", idToken) if postLogoutRedirectURI != "" { diff --git a/main.go b/main.go index 1662642..e618ae2 100644 --- a/main.go +++ b/main.go @@ -772,8 +772,15 @@ func (t *TraefikOidc) extractGroupsAndRoles(idToken string) ([]string, []string, // buildFullURL constructs a full URL from scheme, host and path func buildFullURL(scheme, host, path string) string { + // If the path is already a full URL, return it as-is if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") { return path } + + // Ensure the path starts with a forward slash + if !strings.HasPrefix(path, "/") { + path = "/" + path + } + return fmt.Sprintf("%s://%s%s", scheme, host, path) } diff --git a/main_test.go b/main_test.go index e91728a..0e85458 100644 --- a/main_test.go +++ b/main_test.go @@ -863,7 +863,7 @@ func TestHandleLogout(t *testing.T) { }, endSessionURL: "https://provider/end-session", expectedStatus: http.StatusFound, - expectedURL: "https://provider/end-session?id_token_hint=test.id.token&post_logout_redirect_uri=http%3A%2F%2Fexample.com%2F", + expectedURL: "https://provider/end-session?id_token_hint=test.id.token&post_logout_redirect_uri=http%3A%2F%2Fexample.com", host: "test-host", }, { @@ -876,14 +876,14 @@ func TestHandleLogout(t *testing.T) { }, endSessionURL: "", expectedStatus: http.StatusFound, - expectedURL: "http://example.com/", + expectedURL: "http://example.com", host: "test-host", }, { name: "Logout with empty session", setupSession: func(session *sessions.Session) {}, expectedStatus: http.StatusFound, - expectedURL: "http://example.com/", + expectedURL: "http://example.com", host: "test-host", }, {