Skip to content

Commit d093cd4

Browse files
committed
Enable logout redirection for reverse proxy setups
When authentication is handled externally by a reverse proxy or SSO provider, users can be redirected to an external logout URL or relative path defined on the reverse proxy.
1 parent b49dd8e commit d093cd4

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

custom/conf/app.example.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,11 @@ INTERNAL_TOKEN =
463463
;; Name of cookie used to store authentication information.
464464
;COOKIE_REMEMBER_NAME = gitea_incredible
465465
;;
466+
;; URL or path that Gitea should redirect users to *after* performing its own logout.
467+
;; Use this, if needed, when authentication is handled by a reverse proxy or SSO.
468+
;; Mellon example: REVERSE_PROXY_LOGOUT_REDIRECT = /mellon/logout?ReturnTo=/
469+
;REVERSE_PROXY_LOGOUT_REDIRECT =
470+
;;
466471
;; Reverse proxy authentication header name of user name, email, and full name
467472
;REVERSE_PROXY_AUTHENTICATION_USER = X-WEBAUTH-USER
468473
;REVERSE_PROXY_AUTHENTICATION_EMAIL = X-WEBAUTH-EMAIL

modules/setting/security.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var (
2525
ReverseProxyAuthEmail string
2626
ReverseProxyAuthFullName string
2727
ReverseProxyLimit int
28+
ReverseProxyLogoutRedirect string
2829
ReverseProxyTrustedProxies []string
2930
MinPasswordLength int
3031
ImportLocalPaths bool
@@ -121,6 +122,7 @@ func loadSecurityFrom(rootCfg ConfigProvider) {
121122
ReverseProxyAuthFullName = sec.Key("REVERSE_PROXY_AUTHENTICATION_FULL_NAME").MustString("X-WEBAUTH-FULLNAME")
122123

123124
ReverseProxyLimit = sec.Key("REVERSE_PROXY_LIMIT").MustInt(1)
125+
ReverseProxyLogoutRedirect = sec.Key("REVERSE_PROXY_LOGOUT_REDIRECT").MustString("")
124126
ReverseProxyTrustedProxies = sec.Key("REVERSE_PROXY_TRUSTED_PROXIES").Strings(",")
125127
if len(ReverseProxyTrustedProxies) == 0 {
126128
ReverseProxyTrustedProxies = []string{"127.0.0.0/8", "::1/128"}

routers/web/auth/auth.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,11 @@ func SignOut(ctx *context.Context) {
416416
})
417417
}
418418
HandleSignOut(ctx)
419-
ctx.JSONRedirect(setting.AppSubURL + "/")
419+
if setting.ReverseProxyLogoutRedirect != "" {
420+
ctx.Redirect(setting.ReverseProxyLogoutRedirect)
421+
return
422+
}
423+
ctx.Redirect(setting.AppSubURL + "/")
420424
}
421425

422426
// SignUp render the register page

templates/base/head_navbar.tmpl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
</div>
5656

5757
<div class="divider"></div>
58-
<a class="item link-action" href data-url="{{AppSubUrl}}/user/logout">
58+
<form id="logout-form" method="post" action="{{AppSubUrl}}/user/logout"></form>
59+
<a class="item" onclick="document.getElementById('logout-form').submit();">
5960
{{svg "octicon-sign-out"}}
6061
{{ctx.Locale.Tr "sign_out"}}
6162
</a>
@@ -128,7 +129,8 @@
128129
</a>
129130
{{end}}
130131
<div class="divider"></div>
131-
<a class="item link-action" href data-url="{{AppSubUrl}}/user/logout">
132+
<form id="logout-form" method="post" action="{{AppSubUrl}}/user/logout"></form>
133+
<a class="item" onclick="document.getElementById('logout-form').submit();">
132134
{{svg "octicon-sign-out"}}
133135
{{ctx.Locale.Tr "sign_out"}}
134136
</a>

tests/integration/signout_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"net/http"
88
"testing"
99

10+
"code.gitea.io/gitea/modules/setting"
11+
"code.gitea.io/gitea/modules/test"
1012
"code.gitea.io/gitea/tests"
1113
)
1214

@@ -22,3 +24,24 @@ func TestSignOut(t *testing.T) {
2224
req = NewRequest(t, "GET", "/user2/repo2")
2325
session.MakeRequest(t, req, http.StatusNotFound)
2426
}
27+
28+
func TestSignOut_ReverseProxyLogoutRedirect(t *testing.T) {
29+
defer tests.PrepareTestEnv(t)()
30+
31+
defer test.MockVariableValue(&setting.ReverseProxyLogoutRedirect, "/mellon/logout?ReturnTo=/")()
32+
33+
session := loginUser(t, "user2")
34+
35+
req := NewRequest(t, "POST", "/user/logout")
36+
resp := session.MakeRequest(t, req, http.StatusSeeOther)
37+
38+
expected := "/mellon/logout?ReturnTo=/"
39+
loc := resp.Header().Get("Location")
40+
if loc != expected {
41+
t.Fatalf("expected redirect to %q, got %q", expected, loc)
42+
}
43+
44+
// try to view a private repo, should fail
45+
req = NewRequest(t, "GET", "/user2/repo2")
46+
session.MakeRequest(t, req, http.StatusNotFound)
47+
}

0 commit comments

Comments
 (0)