Skip to content

Commit f1e50aa

Browse files
committed
✨ Logic to correctly use multiple authHosts
1 parent 6f3967c commit f1e50aa

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

internal/auth.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,28 +135,43 @@ func returnUrl(r *http.Request) string {
135135

136136
// Get oauth redirect uri
137137
func redirectUri(r *http.Request) string {
138-
if use, _ := useAuthDomain(r); use {
138+
if use, authHost, _ := useAuthDomain(r); use {
139139
p := r.Header.Get("X-Forwarded-Proto")
140-
return fmt.Sprintf("%s://%s%s", p, config.AuthHost, config.Path)
140+
return fmt.Sprintf("%s://%s%s", p, authHost, config.Path)
141141
}
142142

143143
return fmt.Sprintf("%s%s", redirectBase(r), config.Path)
144144
}
145145

146146
// Should we use auth host + what it is
147-
func useAuthDomain(r *http.Request) (bool, string) {
148-
if config.AuthHost == "" {
149-
return false, ""
147+
func useAuthDomain(r *http.Request) (bool, string, string) {
148+
if len(config.AuthHosts) == 0 {
149+
return false, "", ""
150150
}
151151

152152
// Does the request match a given cookie domain?
153153
reqMatch, reqHost := matchCookieDomains(r.Host)
154154

155155
// Do any of the auth hosts match a cookie domain?
156-
authMatch, authHost := matchCookieDomains(config.AuthHost)
156+
authMatch, authHost := matchAuthHosts(reqHost)
157157

158158
// We need both to match the same domain
159-
return reqMatch && authMatch && reqHost == authHost, reqHost
159+
return reqMatch && authMatch, authHost, reqHost
160+
}
161+
162+
// Return matching auth host domain if exists
163+
func matchAuthHosts(domain string) (bool, string) {
164+
// Remove port
165+
p := strings.Split(domain, ":")
166+
167+
for _, d := range config.AuthHosts {
168+
// Subdomain match?
169+
if len(d) >= len(domain) && d[len(d)-len(domain):] == domain {
170+
return true, d
171+
}
172+
}
173+
174+
return false, p[0]
160175
}
161176

162177
// Cookie methods
@@ -287,7 +302,7 @@ func cookieDomain(r *http.Request) string {
287302
// Cookie domain
288303
func csrfCookieDomain(r *http.Request) string {
289304
var host string
290-
if use, domain := useAuthDomain(r); use {
305+
if use, _, domain := useAuthDomain(r); use {
291306
host = domain
292307
} else {
293308
host = r.Host

0 commit comments

Comments
 (0)