Skip to content

Commit fa7ee5a

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 69700f9 commit fa7ee5a

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
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: 30 additions & 1 deletion
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

@@ -16,7 +18,34 @@ func TestSignOut(t *testing.T) {
1618
session := loginUser(t, "user2")
1719

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

2150
// try to view a private repo, should fail
2251
req = NewRequest(t, "GET", "/user2/repo2")

0 commit comments

Comments
 (0)