From 0066510419d6d4212cacb2208b7205bfb7d393b7 Mon Sep 17 00:00:00 2001 From: Daniel Pernold <34278179+dandjo@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:28:21 +0100 Subject: [PATCH 1/2] fix: return to the app after signing in as another user - pass the original url as redirect_uri to the unauthorized page when a signed-in user is not allowed to access an app or lacks a required group - on the unauthorized page, sign out and open the login with that redirect_uri, so another user can sign in and gets back to the app --- frontend/src/pages/unauthorized-page.tsx | 18 +++++++++++++++- internal/controller/controller.go | 9 ++++---- internal/controller/proxy_controller.go | 8 ++++--- internal/controller/proxy_controller_test.go | 22 ++++++++++++++++++++ 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/frontend/src/pages/unauthorized-page.tsx b/frontend/src/pages/unauthorized-page.tsx index e5bce8b52..fd87cc930 100644 --- a/frontend/src/pages/unauthorized-page.tsx +++ b/frontend/src/pages/unauthorized-page.tsx @@ -6,6 +6,8 @@ import { CardHeader, CardTitle, } from "@/components/ui/card"; +import { searchParamsFromObject } from "@/lib/hooks/screen-params"; +import axios from "axios"; import { useState } from "react"; import { Trans, useTranslation } from "react-i18next"; import { Navigate, useLocation, useNavigate } from "react-router"; @@ -22,9 +24,23 @@ export const UnauthorizedPage = () => { const resource = searchParams.get("resource"); const groupErr = searchParams.get("groupErr"); const ip = searchParams.get("ip"); + const redirectUri = searchParams.get("redirect_uri"); - const handleRedirect = () => { + const handleRedirect = async () => { setLoading(true); + + // The user is signed in but not allowed to access the app. Sign out first + // so that another user can sign in, then return to the app after login. + if (username && redirectUri) { + await axios.post("/api/user/logout").catch(() => undefined); + const params = searchParamsFromObject({ + redirect_uri: redirectUri, + login_for: "app", + }); + window.location.replace(`/login?${params}`); + return; + } + navigate("/login"); }; diff --git a/internal/controller/controller.go b/internal/controller/controller.go index d711efdd7..3f6fe7d51 100644 --- a/internal/controller/controller.go +++ b/internal/controller/controller.go @@ -8,10 +8,11 @@ const ( ) type UnauthorizedQuery struct { - Username string `url:"username"` - Resource string `url:"resource"` - GroupErr bool `url:"groupErr"` - IP string `url:"ip"` + Username string `url:"username"` + Resource string `url:"resource"` + GroupErr bool `url:"groupErr"` + IP string `url:"ip"` + RedirectURI string `url:"redirect_uri,omitempty"` } type RedirectQuery struct { diff --git a/internal/controller/proxy_controller.go b/internal/controller/proxy_controller.go index 4be21258d..1f34f3a4d 100644 --- a/internal/controller/proxy_controller.go +++ b/internal/controller/proxy_controller.go @@ -190,7 +190,8 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) { controller.log.App.Warn().Str("user", userContext.GetUsername()).Str("resource", strings.Split(proxyCtx.Host, ".")[0]).Msg("User is not allowed to access resource") queries, err := query.Values(UnauthorizedQuery{ - Resource: strings.Split(proxyCtx.Host, ".")[0], + Resource: strings.Split(proxyCtx.Host, ".")[0], + RedirectURI: fmt.Sprintf("%s://%s%s", proxyCtx.Proto, proxyCtx.Host, proxyCtx.PathRaw), }) if err != nil { @@ -233,8 +234,9 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) { controller.log.App.Warn().Str("user", userContext.GetUsername()).Str("resource", strings.Split(proxyCtx.Host, ".")[0]).Msg("User is not in the required group to access resource") queries, err := query.Values(UnauthorizedQuery{ - Resource: strings.Split(proxyCtx.Host, ".")[0], - GroupErr: true, + Resource: strings.Split(proxyCtx.Host, ".")[0], + GroupErr: true, + RedirectURI: fmt.Sprintf("%s://%s%s", proxyCtx.Proto, proxyCtx.Host, proxyCtx.PathRaw), }) if err != nil { diff --git a/internal/controller/proxy_controller_test.go b/internal/controller/proxy_controller_test.go index df606e2d8..acef4e23f 100644 --- a/internal/controller/proxy_controller_test.go +++ b/internal/controller/proxy_controller_test.go @@ -666,6 +666,25 @@ func TestProxyController(t *testing.T) { assert.Equal(t, "", recorder.Header().Get("remote-email")) }, }, + { + description: "Ensure user allow ACL redirects incorrect user with browser to unauthorized page with redirect uri", + middlewares: []gin.HandlerFunc{ + simpleCtxTotp, + }, + run: func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder) { + req := httptest.NewRequest("GET", "/api/auth/traefik", nil) + req.Header.Set("x-forwarded-host", "user-allow.example.com") + req.Header.Set("x-forwarded-proto", "https") + req.Header.Set("x-forwarded-uri", "/some/path") + req.Header.Set("user-agent", browserUserAgent) + router.ServeHTTP(recorder, req) + assert.Equal(t, http.StatusFound, recorder.Code) + location := recorder.Header().Get("Location") + assert.Contains(t, location, runtime.AppURL+"/unauthorized?") + assert.Contains(t, location, "username=totpuser") + assert.Contains(t, location, "redirect_uri="+url.QueryEscape("https://user-allow.example.com/some/path")) + }, + }, { description: "Test IP block rule, with non browser user agent", middlewares: []gin.HandlerFunc{}, @@ -699,6 +718,7 @@ func TestProxyController(t *testing.T) { assert.Contains(t, location, url.QueryEscape("10.10.10.10")) assert.Contains(t, location, url.QueryEscape("ip-block")) assert.Contains(t, location, runtime.AppURL) + assert.NotContains(t, location, "redirect_uri") }, }, { @@ -799,6 +819,7 @@ func TestProxyController(t *testing.T) { assert.Contains(t, location, "groupErr=true") assert.Contains(t, location, "oauth-group") assert.Contains(t, location, runtime.AppURL) + assert.Contains(t, location, "redirect_uri="+url.QueryEscape("https://oauth-group.example.com/")) }, }, { @@ -899,6 +920,7 @@ func TestProxyController(t *testing.T) { assert.Contains(t, location, "groupErr=true") assert.Contains(t, location, "ldap-group") assert.Contains(t, location, runtime.AppURL) + assert.Contains(t, location, "redirect_uri="+url.QueryEscape("https://ldap-group.example.com/")) }, }, { From 9e67ea730795fe87dd5ff09a646ca7b99894c67c Mon Sep 17 00:00:00 2001 From: Daniel Pernold <34278179+dandjo@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:41:50 +0100 Subject: [PATCH 2/2] fix: keep the unauthorized page on logout failure and skip OAuth auto redirect - if the logout request fails, stay on the page, reset the button and show the logout error instead of opening the login page with the old session - the account switch passes skip_auto_redirect=true, so the login page shows the sign-in choice instead of redirecting to the auto-redirect OAuth provider, which could sign the same user in again --- frontend/src/lib/hooks/screen-params.ts | 2 ++ frontend/src/pages/login-page.tsx | 10 ++++++++-- frontend/src/pages/unauthorized-page.tsx | 14 +++++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/frontend/src/lib/hooks/screen-params.ts b/frontend/src/lib/hooks/screen-params.ts index 6a459733e..44291ea64 100644 --- a/frontend/src/lib/hooks/screen-params.ts +++ b/frontend/src/lib/hooks/screen-params.ts @@ -7,6 +7,7 @@ type ScreenParams = { oidc_scope?: string; oidc_name?: string; oidc_prompt?: "none" | "login"; + skip_auto_redirect?: "true"; }; const zodScreenParams = z.object({ @@ -16,6 +17,7 @@ const zodScreenParams = z.object({ oidc_scope: z.string().optional(), oidc_name: z.string().optional(), oidc_prompt: z.enum(["none", "login"]).optional(), + skip_auto_redirect: z.enum(["true"]).optional(), }); export function useScreenParams(params: URLSearchParams): ScreenParams { diff --git a/frontend/src/pages/login-page.tsx b/frontend/src/pages/login-page.tsx index 91478e6c1..4fa4e7617 100644 --- a/frontend/src/pages/login-page.tsx +++ b/frontend/src/pages/login-page.tsx @@ -72,12 +72,18 @@ export const LoginPage = () => { })(); const loginForUrl = useLoginFor({ login_for: screenParams.login_for, - params: searchParamsFromObject({ ...screenParams, oidc_prompt: undefined}), + params: searchParamsFromObject({ + ...screenParams, + oidc_prompt: undefined, + skip_auto_redirect: undefined, + }), }); const [isOauthAutoRedirect, setIsOauthAutoRedirect] = useState( providers.find((provider) => provider.id === oauth.autoRedirect) !== - undefined && (screenParams.redirect_uri || screenParams.oidc_ticket), + undefined && + screenParams.skip_auto_redirect !== "true" && + (screenParams.redirect_uri || screenParams.oidc_ticket), ); const oauthProviders = providers.filter( diff --git a/frontend/src/pages/unauthorized-page.tsx b/frontend/src/pages/unauthorized-page.tsx index fd87cc930..6e57f244f 100644 --- a/frontend/src/pages/unauthorized-page.tsx +++ b/frontend/src/pages/unauthorized-page.tsx @@ -11,6 +11,7 @@ import axios from "axios"; import { useState } from "react"; import { Trans, useTranslation } from "react-i18next"; import { Navigate, useLocation, useNavigate } from "react-router"; +import { toast } from "sonner"; export const UnauthorizedPage = () => { const { search } = useLocation(); @@ -31,11 +32,22 @@ export const UnauthorizedPage = () => { // The user is signed in but not allowed to access the app. Sign out first // so that another user can sign in, then return to the app after login. + // Skip the automatic OAuth redirect, which could sign the same user in + // again, so that the user can choose how to sign in. if (username && redirectUri) { - await axios.post("/api/user/logout").catch(() => undefined); + try { + await axios.post("/api/user/logout"); + } catch { + setLoading(false); + toast.error(t("logoutFailTitle"), { + description: t("logoutFailSubtitle"), + }); + return; + } const params = searchParamsFromObject({ redirect_uri: redirectUri, login_for: "app", + skip_auto_redirect: "true", }); window.location.replace(`/login?${params}`); return;